Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/dev' into tyler/proto-2204-imple…
Browse files Browse the repository at this point in the history
…ment-merit-based-sortition
  • Loading branch information
relyt29 committed Aug 27, 2024
2 parents b96cf85 + d560252 commit f54411a
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 16 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ Implements merit based sortition for top N inferers, forecasters, and reputers.

* [#544](https://github.com/allora-network/allora-chain/pull/544) Added check against zero-rewards after conversion to cosmosInt
* [#547](https://github.com/allora-network/allora-chain/pull/547) Improve error handling on InsertPayload, fixed/added tests err handling
* [#550](https://github.com/allora-network/allora-chain/pull/550) Fix reputer window upper limit


### Security

Expand Down
9 changes: 9 additions & 0 deletions x/emissions/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -894,6 +894,15 @@ func (k *Keeper) DeleteTopicRewardNonce(ctx context.Context, topicId TopicId) er

// Append loss bundle for a topoic and blockheight
func (k *Keeper) UpsertReputerLoss(ctx context.Context, topicId TopicId, block BlockHeight, reputerLoss *types.ReputerValueBundle) error {
if reputerLoss == nil {
return errors.New("invalid reputerLoss bundle: inferer is empty or nil")
}
if reputerLoss.ValueBundle == nil {
return errors.New("reputerLoss bundle is nil")
}
if reputerLoss.ValueBundle.Reputer == "" {
return errors.New("invalid reputerLoss bundle: reputer is empty")
}
key := collections.Join(topicId, block)
reputerLossBundles, err := k.allLossBundles.Get(ctx, key)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion x/emissions/keeper/msgserver/msg_server_reputer_payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ func (ms msgServer) InsertReputerPayload(ctx context.Context, msg *types.MsgInse
}

// Check if the ground truth lag has passed: if blockheight > nonce.BlockHeight + topic.GroundTruthLag
if blockHeight < nonce.ReputerNonce.BlockHeight+topic.GroundTruthLag {
if blockHeight < nonce.ReputerNonce.BlockHeight+topic.GroundTruthLag ||
blockHeight > nonce.ReputerNonce.BlockHeight+topic.GroundTruthLag*2 {
return nil, types.ErrReputerNonceWindowNotAvailable
}

Expand Down
44 changes: 29 additions & 15 deletions x/emissions/keeper/msgserver/msg_server_reputer_payload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,16 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
)

const block = types.BlockHeight(1)

func (s *MsgServerTestSuite) setUpMsgReputerPayload(
reputerAddr sdk.AccAddress,
workerAddr sdk.AccAddress,
block types.BlockHeight,
) (
reputerValueBundle types.ValueBundle,
expectedInferences types.Inferences,
expectedForecasts types.Forecasts,
topicId uint64,
reputerNonce types.Nonce,
workerNonce types.Nonce,
) {
ctx, msgServer := s.ctx, s.msgServer
require := s.Require()
Expand All @@ -43,18 +42,15 @@ func (s *MsgServerTestSuite) setUpMsgReputerPayload(
_, err = msgServer.AddStake(ctx, addStakeMsg)
s.Require().NoError(err)

reputerNonce = types.Nonce{
BlockHeight: block,
}
workerNonce = types.Nonce{
workerNonce := types.Nonce{
BlockHeight: block,
}

err = keeper.AddWorkerNonce(ctx, topicId, &workerNonce)
require.NoError(err)
_, err = keeper.FulfillWorkerNonce(ctx, topicId, &workerNonce)
require.NoError(err)
err = keeper.AddReputerNonce(ctx, topicId, &reputerNonce)
err = keeper.AddReputerNonce(ctx, topicId, &workerNonce)
require.NoError(err)

// add in inference and forecast data
Expand Down Expand Up @@ -107,11 +103,11 @@ func (s *MsgServerTestSuite) setUpMsgReputerPayload(
},
},
ReputerRequestNonce: &types.ReputerRequestNonce{
ReputerNonce: &reputerNonce,
ReputerNonce: &workerNonce,
},
}

return reputerValueBundle, expectedInferences, expectedForecasts, topicId, reputerNonce, workerNonce
return reputerValueBundle, expectedInferences, expectedForecasts, topicId
}

func (s *MsgServerTestSuite) signValueBundle(reputerValueBundle *types.ValueBundle, privateKey secp256k1.PrivKey) []byte {
Expand Down Expand Up @@ -149,21 +145,19 @@ func (s *MsgServerTestSuite) constructAndInsertReputerPayload(
return err
}

func (s *MsgServerTestSuite) TestMsgInsertReputerPayload() {
func (s *MsgServerTestSuite) TestMsgInsertReputerPayloadFailsEarlyWindow() {
ctx := s.ctx
require := s.Require()
keeper := s.emissionsKeeper

block := types.BlockHeight(1)

reputerPrivateKey := secp256k1.GenPrivKey()
reputerPublicKeyBytes := reputerPrivateKey.PubKey().Bytes()
reputerAddr := sdk.AccAddress(reputerPrivateKey.PubKey().Address())

workerPrivateKey := secp256k1.GenPrivKey()
workerAddr := sdk.AccAddress(workerPrivateKey.PubKey().Address())

reputerValueBundle, expectedInferences, expectedForecasts, topicId, _, _ := s.setUpMsgReputerPayload(reputerAddr, workerAddr, block)
reputerValueBundle, expectedInferences, expectedForecasts, topicId := s.setUpMsgReputerPayload(reputerAddr, workerAddr)

err := keeper.InsertForecasts(ctx, topicId, types.Nonce{BlockHeight: block}, expectedForecasts)
require.NoError(err)
Expand All @@ -174,9 +168,29 @@ func (s *MsgServerTestSuite) TestMsgInsertReputerPayload() {
topic, err := s.emissionsKeeper.GetTopic(s.ctx, topicId)
s.Require().NoError(err)

newBlockheight := block + topic.GroundTruthLag
// Prior to the ground truth lag, should not allow reputer payload
newBlockheight := block + topic.GroundTruthLag - 1
s.ctx = sdk.UnwrapSDKContext(s.ctx).WithBlockHeight(newBlockheight)

err = s.constructAndInsertReputerPayload(reputerAddr, reputerPrivateKey, reputerPublicKeyBytes, &reputerValueBundle)
require.ErrorIs(err, types.ErrReputerNonceWindowNotAvailable)

// Valid reputer nonce window, start
newBlockheight = block + topic.GroundTruthLag
s.ctx = sdk.UnwrapSDKContext(s.ctx).WithBlockHeight(newBlockheight)

err = s.constructAndInsertReputerPayload(reputerAddr, reputerPrivateKey, reputerPublicKeyBytes, &reputerValueBundle)
require.NoError(err)

// Valid reputer nonce window, end
newBlockheight = block + topic.GroundTruthLag*2
s.ctx = sdk.UnwrapSDKContext(s.ctx).WithBlockHeight(newBlockheight)
err = s.constructAndInsertReputerPayload(reputerAddr, reputerPrivateKey, reputerPublicKeyBytes, &reputerValueBundle)
require.NoError(err)

// Valid reputer nonce window, end
newBlockheight = block + topic.GroundTruthLag*2 + 1
s.ctx = sdk.UnwrapSDKContext(s.ctx).WithBlockHeight(newBlockheight)
err = s.constructAndInsertReputerPayload(reputerAddr, reputerPrivateKey, reputerPublicKeyBytes, &reputerValueBundle)
require.ErrorIs(err, types.ErrReputerNonceWindowNotAvailable)
}

0 comments on commit f54411a

Please sign in to comment.