github.com/dim4egster/coreth@v0.10.2/consensus/dummy/consensus_test.go (about) 1 // (c) 2019-2020, Ava Labs, Inc. All rights reserved. 2 // See the file LICENSE for licensing terms. 3 4 package dummy 5 6 import ( 7 "math" 8 "math/big" 9 "testing" 10 11 "github.com/dim4egster/coreth/core/types" 12 "github.com/ethereum/go-ethereum/common" 13 ) 14 15 func TestVerifyBlockFee(t *testing.T) { 16 tests := map[string]struct { 17 baseFee *big.Int 18 parentBlockGasCost *big.Int 19 parentTime, currentTime uint64 20 txs []*types.Transaction 21 receipts []*types.Receipt 22 extraStateContribution *big.Int 23 shouldErr bool 24 }{ 25 "tx only base fee": { 26 baseFee: big.NewInt(100), 27 parentBlockGasCost: big.NewInt(0), 28 parentTime: 10, 29 currentTime: 10, 30 txs: []*types.Transaction{ 31 types.NewTransaction(0, common.HexToAddress("7ef5a6135f1fd6a02593eedc869c6d41d934aef8"), big.NewInt(0), 100, big.NewInt(100), nil), 32 }, 33 receipts: []*types.Receipt{ 34 {GasUsed: 1000}, 35 }, 36 extraStateContribution: nil, 37 shouldErr: true, 38 }, 39 "tx covers exactly block fee": { 40 baseFee: big.NewInt(100), 41 parentBlockGasCost: big.NewInt(0), 42 parentTime: 10, 43 currentTime: 10, 44 txs: []*types.Transaction{ 45 types.NewTransaction(0, common.HexToAddress("7ef5a6135f1fd6a02593eedc869c6d41d934aef8"), big.NewInt(0), 100_000, big.NewInt(200), nil), 46 }, 47 receipts: []*types.Receipt{ 48 {GasUsed: 100_000}, 49 }, 50 extraStateContribution: nil, 51 shouldErr: false, 52 }, 53 "txs share block fee": { 54 baseFee: big.NewInt(100), 55 parentBlockGasCost: big.NewInt(0), 56 parentTime: 10, 57 currentTime: 10, 58 txs: []*types.Transaction{ 59 types.NewTransaction(0, common.HexToAddress("7ef5a6135f1fd6a02593eedc869c6d41d934aef8"), big.NewInt(0), 100_000, big.NewInt(200), nil), 60 types.NewTransaction(1, common.HexToAddress("7ef5a6135f1fd6a02593eedc869c6d41d934aef8"), big.NewInt(0), 100_000, big.NewInt(100), nil), 61 }, 62 receipts: []*types.Receipt{ 63 {GasUsed: 100_000}, 64 {GasUsed: 100_000}, 65 }, 66 extraStateContribution: nil, 67 shouldErr: false, 68 }, 69 "txs split block fee": { 70 baseFee: big.NewInt(100), 71 parentBlockGasCost: big.NewInt(0), 72 parentTime: 10, 73 currentTime: 10, 74 txs: []*types.Transaction{ 75 types.NewTransaction(0, common.HexToAddress("7ef5a6135f1fd6a02593eedc869c6d41d934aef8"), big.NewInt(0), 100_000, big.NewInt(150), nil), 76 types.NewTransaction(1, common.HexToAddress("7ef5a6135f1fd6a02593eedc869c6d41d934aef8"), big.NewInt(0), 100_000, big.NewInt(150), nil), 77 }, 78 receipts: []*types.Receipt{ 79 {GasUsed: 100_000}, 80 {GasUsed: 100_000}, 81 }, 82 extraStateContribution: nil, 83 shouldErr: false, 84 }, 85 "split block fee with extra state contribution": { 86 baseFee: big.NewInt(100), 87 parentBlockGasCost: big.NewInt(0), 88 parentTime: 10, 89 currentTime: 10, 90 txs: []*types.Transaction{ 91 types.NewTransaction(0, common.HexToAddress("7ef5a6135f1fd6a02593eedc869c6d41d934aef8"), big.NewInt(0), 100_000, big.NewInt(150), nil), 92 }, 93 receipts: []*types.Receipt{ 94 {GasUsed: 100_000}, 95 }, 96 extraStateContribution: big.NewInt(5_000_000), 97 shouldErr: false, 98 }, 99 "extra state contribution insufficient": { 100 baseFee: big.NewInt(100), 101 parentBlockGasCost: big.NewInt(0), 102 parentTime: 10, 103 currentTime: 10, 104 txs: nil, 105 receipts: nil, 106 extraStateContribution: big.NewInt(9_999_999), 107 shouldErr: true, 108 }, 109 "negative extra state contribution": { 110 baseFee: big.NewInt(100), 111 parentBlockGasCost: big.NewInt(0), 112 parentTime: 10, 113 currentTime: 10, 114 txs: nil, 115 receipts: nil, 116 extraStateContribution: big.NewInt(-1), 117 shouldErr: true, 118 }, 119 "extra state contribution covers block fee": { 120 baseFee: big.NewInt(100), 121 parentBlockGasCost: big.NewInt(0), 122 parentTime: 10, 123 currentTime: 10, 124 txs: nil, 125 receipts: nil, 126 extraStateContribution: big.NewInt(10_000_000), 127 shouldErr: false, 128 }, 129 "extra state contribution covers more than block fee": { 130 baseFee: big.NewInt(100), 131 parentBlockGasCost: big.NewInt(0), 132 parentTime: 10, 133 currentTime: 10, 134 txs: nil, 135 receipts: nil, 136 extraStateContribution: big.NewInt(10_000_001), 137 shouldErr: false, 138 }, 139 "tx only base fee after full time window": { 140 baseFee: big.NewInt(100), 141 parentBlockGasCost: big.NewInt(500_000), 142 parentTime: 10, 143 currentTime: 22, // 2s target + 10 144 txs: []*types.Transaction{ 145 types.NewTransaction(0, common.HexToAddress("7ef5a6135f1fd6a02593eedc869c6d41d934aef8"), big.NewInt(0), 100, big.NewInt(100), nil), 146 }, 147 receipts: []*types.Receipt{ 148 {GasUsed: 1000}, 149 }, 150 extraStateContribution: nil, 151 shouldErr: false, 152 }, 153 "tx only base fee after large time window": { 154 baseFee: big.NewInt(100), 155 parentBlockGasCost: big.NewInt(100_000), 156 parentTime: 0, 157 currentTime: math.MaxUint64, 158 txs: []*types.Transaction{ 159 types.NewTransaction(0, common.HexToAddress("7ef5a6135f1fd6a02593eedc869c6d41d934aef8"), big.NewInt(0), 100, big.NewInt(100), nil), 160 }, 161 receipts: []*types.Receipt{ 162 {GasUsed: 1000}, 163 }, 164 extraStateContribution: nil, 165 shouldErr: false, 166 }, 167 "parent time > current time": { 168 baseFee: big.NewInt(100), 169 parentBlockGasCost: big.NewInt(0), 170 parentTime: 11, 171 currentTime: 10, 172 txs: nil, 173 receipts: nil, 174 extraStateContribution: big.NewInt(10_000_000), 175 shouldErr: false, 176 }, 177 } 178 179 for name, test := range tests { 180 t.Run(name, func(t *testing.T) { 181 blockGasCost := calcBlockGasCost( 182 ApricotPhase4TargetBlockRate, 183 ApricotPhase4MinBlockGasCost, 184 ApricotPhase4MaxBlockGasCost, 185 ApricotPhase4BlockGasCostStep, 186 test.parentBlockGasCost, 187 test.parentTime, test.currentTime, 188 ) 189 engine := NewFaker() 190 if err := engine.verifyBlockFee(test.baseFee, blockGasCost, test.txs, test.receipts, test.extraStateContribution); err != nil { 191 if !test.shouldErr { 192 t.Fatalf("Unexpected error: %s", err) 193 } 194 } else { 195 if test.shouldErr { 196 t.Fatal("Should have failed verification") 197 } 198 } 199 }) 200 } 201 }