github.com/iotexproject/iotex-core@v1.14.1-rc1/action/protocol/poll/lifelong_protocol_test.go (about) 1 // Copyright (c) 2019 IoTeX Foundation 2 // This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability 3 // or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed. 4 // This source code is governed by Apache License 2.0 that can be found in the LICENSE file. 5 6 package poll 7 8 import ( 9 "context" 10 "testing" 11 12 "github.com/golang/mock/gomock" 13 "github.com/stretchr/testify/require" 14 15 "github.com/iotexproject/iotex-core/action/protocol" 16 "github.com/iotexproject/iotex-core/action/protocol/rolldpos" 17 "github.com/iotexproject/iotex-core/blockchain/genesis" 18 "github.com/iotexproject/iotex-core/db/batch" 19 "github.com/iotexproject/iotex-core/state" 20 "github.com/iotexproject/iotex-core/test/mock/mock_chainmanager" 21 ) 22 23 func initLifeLongDelegateProtocol(ctrl *gomock.Controller) (Protocol, context.Context, protocol.StateManager, error) { 24 genesisConfig := genesis.Default 25 delegates := genesisConfig.Delegates 26 p := NewLifeLongDelegatesProtocol(delegates) 27 registry := protocol.NewRegistry() 28 err := registry.Register("rolldpos", rolldpos.NewProtocol(36, 36, 20)) 29 if err != nil { 30 return nil, nil, nil, err 31 } 32 ctx := genesis.WithGenesisContext( 33 protocol.WithRegistry(context.Background(), registry), 34 genesis.Default, 35 ) 36 ctx = protocol.WithActionCtx( 37 ctx, 38 protocol.ActionCtx{}, 39 ) 40 ctx = protocol.WithBlockCtx( 41 ctx, 42 protocol.BlockCtx{}, 43 ) 44 45 sm := mock_chainmanager.NewMockStateManager(ctrl) 46 cb := batch.NewCachedBatch() 47 sm.EXPECT().State(gomock.Any(), gomock.Any()).DoAndReturn( 48 func(account interface{}, opts ...protocol.StateOption) (uint64, error) { 49 cfg, err := protocol.CreateStateConfig(opts...) 50 if err != nil { 51 return 0, err 52 } 53 val, err := cb.Get("state", cfg.Key) 54 if err != nil { 55 return 0, state.ErrStateNotExist 56 } 57 return 0, state.Deserialize(account, val) 58 }).AnyTimes() 59 sm.EXPECT().PutState(gomock.Any(), gomock.Any()).DoAndReturn( 60 func(account interface{}, opts ...protocol.StateOption) (uint64, error) { 61 cfg, err := protocol.CreateStateConfig(opts...) 62 if err != nil { 63 return 0, err 64 } 65 ss, err := state.Serialize(account) 66 if err != nil { 67 return 0, err 68 } 69 cb.Put("state", cfg.Key, ss, "failed to put state") 70 return 0, nil 71 }).AnyTimes() 72 return p, ctx, sm, nil 73 } 74 75 func TestCreateGenesisStates_WithLifeLong(t *testing.T) { 76 require := require.New(t) 77 ctrl := gomock.NewController(t) 78 79 p, ctx, sm, err := initLifeLongDelegateProtocol(ctrl) 80 require.NoError(err) 81 82 ctx = protocol.WithFeatureWithHeightCtx(ctx) 83 ctx = protocol.WithFeatureCtx(ctx) 84 require.NoError(p.CreateGenesisStates(ctx, sm)) 85 } 86 87 func TestProtocol_Handle_WithLifeLong(t *testing.T) { 88 require := require.New(t) 89 ctrl := gomock.NewController(t) 90 91 p, ctx, sm, err := initLifeLongDelegateProtocol(ctrl) 92 require.NoError(err) 93 94 ctx = protocol.WithFeatureWithHeightCtx(ctx) 95 receipt, error := p.Handle(ctx, nil, sm) 96 require.Nil(receipt) 97 require.NoError(error) 98 }