From d56025226d9f414dcada17abcf8be19f6494719f Mon Sep 17 00:00:00 2001 From: Diego C Date: Tue, 27 Aug 2024 15:26:56 +0200 Subject: [PATCH] fix reputer window + strict reputer window tests + lint (#550) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Purpose of Changes and their Description * Fix - close reputer window on the upper part * Add 4 tests to strictly define reputer window * Add basic validation on appending reputer losses ## Link(s) to Ticket(s) or Issue(s) resolved by this PR PROTO-2246 ## Are these changes tested and documented? - [X] If tested, please describe how. If not, why tests are not needed. -- Adds new tests - [X] If documented, please describe where. If not, describe why docs are not needed. -- not needed: no new behaviour added, just fix existing one - [x] Added to `Unreleased` section of `CHANGELOG.md`? --- CHANGELOG.md | 2 + x/emissions/keeper/keeper.go | 9 ++++ .../msgserver/msg_server_reputer_payload.go | 3 +- .../msg_server_reputer_payload_test.go | 44 ++++++++++++------- 4 files changed, 42 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2977bc028..9549c17f6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/x/emissions/keeper/keeper.go b/x/emissions/keeper/keeper.go index d4bd4afa3..d8a8209d7 100644 --- a/x/emissions/keeper/keeper.go +++ b/x/emissions/keeper/keeper.go @@ -933,6 +933,15 @@ func (k *Keeper) DeleteTopicRewardNonce(ctx context.Context, topicId TopicId) er // Append loss bundle for a topoic and blockheight func (k *Keeper) AppendReputerLoss(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") + } moduleParams, err := k.GetParams(ctx) if err != nil { return err diff --git a/x/emissions/keeper/msgserver/msg_server_reputer_payload.go b/x/emissions/keeper/msgserver/msg_server_reputer_payload.go index 2b51471ad..70166d229 100644 --- a/x/emissions/keeper/msgserver/msg_server_reputer_payload.go +++ b/x/emissions/keeper/msgserver/msg_server_reputer_payload.go @@ -62,7 +62,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 } diff --git a/x/emissions/keeper/msgserver/msg_server_reputer_payload_test.go b/x/emissions/keeper/msgserver/msg_server_reputer_payload_test.go index 2b6b95238..1699c73c0 100644 --- a/x/emissions/keeper/msgserver/msg_server_reputer_payload_test.go +++ b/x/emissions/keeper/msgserver/msg_server_reputer_payload_test.go @@ -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() @@ -43,10 +42,7 @@ 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, } @@ -54,7 +50,7 @@ func (s *MsgServerTestSuite) setUpMsgReputerPayload( 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 @@ -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 { @@ -149,13 +145,11 @@ 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()) @@ -163,7 +157,7 @@ func (s *MsgServerTestSuite) TestMsgInsertReputerPayload() { 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) @@ -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) }