-
Notifications
You must be signed in to change notification settings - Fork 232
/
consensus_test.go
142 lines (136 loc) · 4.36 KB
/
consensus_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// (c) 2019-2020, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package dummy
import (
"math"
"math/big"
"testing"
"github.com/ava-labs/subnet-evm/core/types"
"github.com/ava-labs/subnet-evm/params"
"github.com/ethereum/go-ethereum/common"
)
var testBlockGasCostStep = big.NewInt(50_000)
func TestVerifyBlockFee(t *testing.T) {
tests := map[string]struct {
baseFee *big.Int
parentBlockGasCost *big.Int
parentTime, currentTime uint64
txs []*types.Transaction
receipts []*types.Receipt
shouldErr bool
}{
"tx only base fee": {
baseFee: big.NewInt(100),
parentBlockGasCost: big.NewInt(0),
parentTime: 10,
currentTime: 10,
txs: []*types.Transaction{
types.NewTransaction(0, common.HexToAddress("7ef5a6135f1fd6a02593eedc869c6d41d934aef8"), big.NewInt(0), 100, big.NewInt(100), nil),
},
receipts: []*types.Receipt{
{GasUsed: 1000},
},
shouldErr: true,
},
"tx covers exactly block fee": {
baseFee: big.NewInt(100),
parentBlockGasCost: big.NewInt(0),
parentTime: 10,
currentTime: 10,
txs: []*types.Transaction{
types.NewTransaction(0, common.HexToAddress("7ef5a6135f1fd6a02593eedc869c6d41d934aef8"), big.NewInt(0), 100_000, big.NewInt(200), nil),
},
receipts: []*types.Receipt{
{GasUsed: 100_000},
},
shouldErr: false,
},
"txs share block fee": {
baseFee: big.NewInt(100),
parentBlockGasCost: big.NewInt(0),
parentTime: 10,
currentTime: 10,
txs: []*types.Transaction{
types.NewTransaction(0, common.HexToAddress("7ef5a6135f1fd6a02593eedc869c6d41d934aef8"), big.NewInt(0), 100_000, big.NewInt(200), nil),
types.NewTransaction(1, common.HexToAddress("7ef5a6135f1fd6a02593eedc869c6d41d934aef8"), big.NewInt(0), 100_000, big.NewInt(100), nil),
},
receipts: []*types.Receipt{
{GasUsed: 100_000},
{GasUsed: 100_000},
},
shouldErr: false,
},
"txs split block fee": {
baseFee: big.NewInt(100),
parentBlockGasCost: big.NewInt(0),
parentTime: 10,
currentTime: 10,
txs: []*types.Transaction{
types.NewTransaction(0, common.HexToAddress("7ef5a6135f1fd6a02593eedc869c6d41d934aef8"), big.NewInt(0), 100_000, big.NewInt(150), nil),
types.NewTransaction(1, common.HexToAddress("7ef5a6135f1fd6a02593eedc869c6d41d934aef8"), big.NewInt(0), 100_000, big.NewInt(150), nil),
},
receipts: []*types.Receipt{
{GasUsed: 100_000},
{GasUsed: 100_000},
},
shouldErr: false,
},
"tx only base fee after full time window": {
baseFee: big.NewInt(100),
parentBlockGasCost: big.NewInt(500_000),
parentTime: 10,
currentTime: 22, // 2s target + 10
txs: []*types.Transaction{
types.NewTransaction(0, common.HexToAddress("7ef5a6135f1fd6a02593eedc869c6d41d934aef8"), big.NewInt(0), 100, big.NewInt(100), nil),
},
receipts: []*types.Receipt{
{GasUsed: 1000},
},
shouldErr: false,
},
"tx only base fee after large time window": {
baseFee: big.NewInt(100),
parentBlockGasCost: big.NewInt(100_000),
parentTime: 0,
currentTime: math.MaxUint64,
txs: []*types.Transaction{
types.NewTransaction(0, common.HexToAddress("7ef5a6135f1fd6a02593eedc869c6d41d934aef8"), big.NewInt(0), 100, big.NewInt(100), nil),
},
receipts: []*types.Receipt{
{GasUsed: 1000},
},
shouldErr: false,
},
"parent time > current time": {
baseFee: big.NewInt(100),
parentBlockGasCost: big.NewInt(0),
parentTime: 11,
currentTime: 10,
txs: nil,
receipts: nil,
shouldErr: true,
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
blockGasCost := calcBlockGasCost(
params.DefaultFeeConfig.TargetBlockRate,
params.DefaultFeeConfig.MinBlockGasCost,
params.DefaultFeeConfig.MaxBlockGasCost,
testBlockGasCostStep,
test.parentBlockGasCost,
test.parentTime, test.currentTime,
)
engine := NewFaker()
if err := engine.verifyBlockFee(test.baseFee, blockGasCost, test.txs, test.receipts); err != nil {
if !test.shouldErr {
t.Fatalf("Unexpected error: %s", err)
}
} else {
if test.shouldErr {
t.Fatal("Should have failed verification")
}
}
})
}
}