Skip to content

Commit

Permalink
[test] adds a test for block proposal revert
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanlott committed Jul 25, 2023
1 parent 6362930 commit d430f23
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 1 deletion.
7 changes: 6 additions & 1 deletion utility/unit_of_work/uow_leader.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package unit_of_work

import (
"encoding/hex"
"fmt"

"github.com/pokt-network/pocket/logger"
coreTypes "github.com/pokt-network/pocket/shared/core/types"
Expand Down Expand Up @@ -58,7 +59,11 @@ func (uow *leaderUtilityUnitOfWork) CreateProposalBlock(proposer []byte, maxTxBy
// Compute & return the new state hash
stateHash, err = uow.persistenceRWContext.ComputeStateHash()
if err != nil {
log.Fatal().Err(err).Bool("TODO", true).Msg("Updating the app hash failed. TODO: Look into roll-backing the entire commit...")
if err := uow.persistenceRWContext.RollbackToSavePoint(); err != nil {
log.Error().Msgf("failed to recover from rollback at height %+v: %+v", uow.height, err)
return "", nil, err
}
return "", nil, fmt.Errorf("rollback at height %d: failed to compute state hash: %w", uow.height, err)
}
log.Info().Str("state_hash", stateHash).Msg("Finished successfully")

Expand Down
102 changes: 102 additions & 0 deletions utility/unit_of_work/uow_leader_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package unit_of_work

import (
"fmt"
"math/big"
"reflect"
"testing"

"github.com/golang/mock/gomock"
"github.com/pokt-network/pocket/shared/modules"
mockModules "github.com/pokt-network/pocket/shared/modules/mocks"
"github.com/pokt-network/pocket/shared/utils"
)

var DefaultStakeBig = big.NewInt(1000000000000000)

func Test_leaderUtilityUnitOfWork_CreateProposalBlock(t *testing.T) {
t.Helper()

type fields struct {
leaderUOW func(t *testing.T) *leaderUtilityUnitOfWork
}
type args struct {
proposer []byte
maxTxBytes uint64
}
tests := []struct {
name string
args args
fields fields
wantStateHash string
wantTxs [][]byte
wantErr bool
}{
{
name: "should revert a failed block proposal",
args: args{},
fields: fields{
leaderUOW: func(t *testing.T) *leaderUtilityUnitOfWork {
ctrl := gomock.NewController(t)

mockUtilityMod := newDefaultMockUtilityModule(t, ctrl)
mockrwcontext := newDefaultMockRWContext(t, ctrl)

mockrwcontext.EXPECT().ComputeStateHash().Return("", fmt.Errorf("rollback error"))
mockbus := mockModules.NewMockBus(ctrl)
mockbus.EXPECT().GetUtilityModule().Return(mockUtilityMod).AnyTimes()

luow := NewLeaderUOW(0, mockrwcontext, mockrwcontext)
luow.SetBus(mockbus)

return luow
},
},
wantErr: true,
wantTxs: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
luow := tt.fields.leaderUOW(t)
_, gotTxs, err := luow.CreateProposalBlock(tt.args.proposer, tt.args.maxTxBytes)
if (err != nil) != tt.wantErr {
t.Errorf("leaderUtilityUnitOfWork.CreateProposalBlock() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(gotTxs, tt.wantTxs) {
t.Errorf("leaderUtilityUnitOfWork.CreateProposalBlock() gotTxs = %v, want %v", gotTxs, tt.wantTxs)
}
})
}
}

func newDefaultMockRWContext(t *testing.T, ctrl *gomock.Controller) *mockModules.MockPersistenceRWContext {

Check failure on line 74 in utility/unit_of_work/uow_leader_test.go

View workflow job for this annotation

GitHub Actions / lint

`newDefaultMockRWContext` - `t` is unused (unparam)
mockrwcontext := mockModules.NewMockPersistenceRWContext(ctrl)

mockrwcontext.EXPECT().SetPoolAmount(gomock.Any(), gomock.Any()).AnyTimes()
mockrwcontext.EXPECT().RollbackToSavePoint().Times(1)
mockrwcontext.EXPECT().GetIntParam(gomock.Any(), gomock.Any()).Return(0, nil).AnyTimes()
mockrwcontext.EXPECT().GetPoolAmount(gomock.Any(), gomock.Any()).Return(utils.BigIntToString(DefaultStakeBig), nil).Times(1)
mockrwcontext.EXPECT().AddAccountAmount(gomock.Any(), gomock.Any()).Return(nil).AnyTimes()
mockrwcontext.EXPECT().AddPoolAmount(gomock.Any(), gomock.Any()).Return(nil).AnyTimes()
mockrwcontext.EXPECT().GetAppsReadyToUnstake(gomock.Any(), gomock.Any()).Return(nil, nil).AnyTimes()
mockrwcontext.EXPECT().GetServicersReadyToUnstake(gomock.Any(), gomock.Any()).Return(nil, nil).AnyTimes()
mockrwcontext.EXPECT().GetValidatorsReadyToUnstake(gomock.Any(), gomock.Any()).Return(nil, nil).AnyTimes()
mockrwcontext.EXPECT().GetFishermenReadyToUnstake(gomock.Any(), gomock.Any()).Return(nil, nil).AnyTimes()
mockrwcontext.EXPECT().SetServicerStatusAndUnstakingHeightIfPausedBefore(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).AnyTimes()
mockrwcontext.EXPECT().SetAppStatusAndUnstakingHeightIfPausedBefore(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).AnyTimes()
mockrwcontext.EXPECT().SetValidatorsStatusAndUnstakingHeightIfPausedBefore(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).AnyTimes()
mockrwcontext.EXPECT().SetFishermanStatusAndUnstakingHeightIfPausedBefore(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).AnyTimes()

return mockrwcontext
}

func newDefaultMockUtilityModule(t *testing.T, ctrl *gomock.Controller) *mockModules.MockUtilityModule {
mockUtilityMod := mockModules.NewMockUtilityModule(ctrl)
testmempool := NewTestingMempool(t)
mockUtilityMod.EXPECT().GetModuleName().Return(modules.UtilityModuleName).AnyTimes()
mockUtilityMod.EXPECT().SetBus(gomock.Any()).Return().AnyTimes()
mockUtilityMod.EXPECT().GetMempool().Return(testmempool).AnyTimes()
return mockUtilityMod
}

0 comments on commit d430f23

Please sign in to comment.