github.com/iotexproject/iotex-core@v1.14.1-rc1/blockchain/block/validator_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 block 7 8 import ( 9 "context" 10 "math/big" 11 "strings" 12 "testing" 13 14 "github.com/pkg/errors" 15 "github.com/stretchr/testify/require" 16 17 "github.com/iotexproject/iotex-address/address" 18 19 "github.com/iotexproject/iotex-core/action" 20 "github.com/iotexproject/iotex-core/action/protocol" 21 "github.com/iotexproject/iotex-core/state" 22 "github.com/iotexproject/iotex-core/test/identityset" 23 "github.com/iotexproject/iotex-core/testutil" 24 ) 25 26 func TestValidator(t *testing.T) { 27 require := require.New(t) 28 29 ctx := context.Background() 30 blk := &Block{} 31 32 v := NewValidator(nil) 33 require.NoError(v.Validate(ctx, blk)) 34 35 valid := protocol.NewGenericValidator(nil, func(ctx context.Context, sr protocol.StateReader, addr address.Address) (*state.Account, error) { 36 pk := identityset.PrivateKey(27).PublicKey() 37 eAddr := pk.Address() 38 if strings.EqualFold(eAddr.String(), addr.String()) { 39 return nil, errors.New("MockChainManager nonce error") 40 } 41 account, err := state.NewAccount() 42 require.NoError(err) 43 require.NoError(account.SetPendingNonce(3)) 44 return account, nil 45 }) 46 47 tsf1, err := action.SignedTransfer(identityset.Address(28).String(), identityset.PrivateKey(27), 1, big.NewInt(20), []byte{}, 100000, big.NewInt(10)) 48 require.NoError(err) 49 50 tsf2, err := action.SignedTransfer(identityset.Address(29).String(), identityset.PrivateKey(27), 1, big.NewInt(30), []byte{}, 100000, big.NewInt(10)) 51 require.NoError(err) 52 53 blkhash, err := tsf1.Hash() 54 require.NoError(err) 55 nblk, err := NewTestingBuilder(). 56 SetHeight(1). 57 SetPrevBlockHash(blkhash). 58 SetTimeStamp(testutil.TimestampNow()). 59 AddActions(tsf1, tsf2). 60 SignAndBuild(identityset.PrivateKey(27)) 61 require.NoError(err) 62 63 v = NewValidator(nil, valid) 64 require.Contains(v.Validate(ctx, &nblk).Error(), "MockChainManager nonce error") 65 66 }