github.com/ava-labs/avalanchego@v1.11.11/vms/platformvm/block/executor/rejector_test.go (about) 1 // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved. 2 // See the file LICENSE for licensing terms. 3 4 package executor 5 6 import ( 7 "testing" 8 "time" 9 10 "github.com/stretchr/testify/require" 11 "go.uber.org/mock/gomock" 12 13 "github.com/ava-labs/avalanchego/ids" 14 "github.com/ava-labs/avalanchego/snow" 15 "github.com/ava-labs/avalanchego/utils/logging" 16 "github.com/ava-labs/avalanchego/vms/components/verify" 17 "github.com/ava-labs/avalanchego/vms/platformvm/block" 18 "github.com/ava-labs/avalanchego/vms/platformvm/state" 19 "github.com/ava-labs/avalanchego/vms/platformvm/txs" 20 "github.com/ava-labs/avalanchego/vms/platformvm/txs/mempool/mempoolmock" 21 "github.com/ava-labs/avalanchego/vms/secp256k1fx" 22 ) 23 24 func TestRejectBlock(t *testing.T) { 25 type test struct { 26 name string 27 newBlockFunc func() (block.Block, error) 28 rejectFunc func(*rejector, block.Block) error 29 } 30 31 tests := []test{ 32 { 33 name: "proposal block", 34 newBlockFunc: func() (block.Block, error) { 35 return block.NewBanffProposalBlock( 36 time.Now(), 37 ids.GenerateTestID(), 38 1, 39 &txs.Tx{ 40 Unsigned: &txs.AddDelegatorTx{ 41 // Without the line below, this function will error. 42 DelegationRewardsOwner: &secp256k1fx.OutputOwners{}, 43 }, 44 Creds: []verify.Verifiable{}, 45 }, 46 []*txs.Tx{}, 47 ) 48 }, 49 rejectFunc: func(r *rejector, b block.Block) error { 50 return r.BanffProposalBlock(b.(*block.BanffProposalBlock)) 51 }, 52 }, 53 { 54 name: "atomic block", 55 newBlockFunc: func() (block.Block, error) { 56 return block.NewApricotAtomicBlock( 57 ids.GenerateTestID(), 58 1, 59 &txs.Tx{ 60 Unsigned: &txs.AddDelegatorTx{ 61 // Without the line below, this function will error. 62 DelegationRewardsOwner: &secp256k1fx.OutputOwners{}, 63 }, 64 Creds: []verify.Verifiable{}, 65 }, 66 ) 67 }, 68 rejectFunc: func(r *rejector, b block.Block) error { 69 return r.ApricotAtomicBlock(b.(*block.ApricotAtomicBlock)) 70 }, 71 }, 72 { 73 name: "standard block", 74 newBlockFunc: func() (block.Block, error) { 75 return block.NewBanffStandardBlock( 76 time.Now(), 77 ids.GenerateTestID(), 78 1, 79 []*txs.Tx{ 80 { 81 Unsigned: &txs.AddDelegatorTx{ 82 // Without the line below, this function will error. 83 DelegationRewardsOwner: &secp256k1fx.OutputOwners{}, 84 }, 85 Creds: []verify.Verifiable{}, 86 }, 87 }, 88 ) 89 }, 90 rejectFunc: func(r *rejector, b block.Block) error { 91 return r.BanffStandardBlock(b.(*block.BanffStandardBlock)) 92 }, 93 }, 94 { 95 name: "commit", 96 newBlockFunc: func() (block.Block, error) { 97 return block.NewBanffCommitBlock(time.Now(), ids.GenerateTestID() /*parent*/, 1 /*height*/) 98 }, 99 rejectFunc: func(r *rejector, blk block.Block) error { 100 return r.BanffCommitBlock(blk.(*block.BanffCommitBlock)) 101 }, 102 }, 103 { 104 name: "abort", 105 newBlockFunc: func() (block.Block, error) { 106 return block.NewBanffAbortBlock(time.Now(), ids.GenerateTestID() /*parent*/, 1 /*height*/) 107 }, 108 rejectFunc: func(r *rejector, blk block.Block) error { 109 return r.BanffAbortBlock(blk.(*block.BanffAbortBlock)) 110 }, 111 }, 112 } 113 114 for _, tt := range tests { 115 t.Run(tt.name, func(t *testing.T) { 116 require := require.New(t) 117 ctrl := gomock.NewController(t) 118 119 blk, err := tt.newBlockFunc() 120 require.NoError(err) 121 122 mempool := mempoolmock.NewMempool(ctrl) 123 state := state.NewMockState(ctrl) 124 blkIDToState := map[ids.ID]*blockState{ 125 blk.Parent(): nil, 126 blk.ID(): nil, 127 } 128 rejector := &rejector{ 129 backend: &backend{ 130 ctx: &snow.Context{ 131 Log: logging.NoLog{}, 132 }, 133 blkIDToState: blkIDToState, 134 Mempool: mempool, 135 state: state, 136 }, 137 addTxsToMempool: true, 138 } 139 140 // Set expected calls on dependencies. 141 for _, tx := range blk.Txs() { 142 mempool.EXPECT().Add(tx).Return(nil).Times(1) 143 } 144 145 mempool.EXPECT().RequestBuildBlock(false).Times(1) 146 147 require.NoError(tt.rejectFunc(rejector, blk)) 148 // Make sure block and its parent are removed from the state map. 149 require.NotContains(rejector.blkIDToState, blk.ID()) 150 }) 151 } 152 }