forked from ethereum/go-ethereum
-
Notifications
You must be signed in to change notification settings - Fork 142
/
consensus_test.go
212 lines (182 loc) 路 7.11 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package taiko_test
import (
"bytes"
"math/big"
"testing"
"time"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/consensus"
"github.com/ethereum/go-ethereum/consensus/taiko"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/eth"
"github.com/ethereum/go-ethereum/eth/ethconfig"
"github.com/ethereum/go-ethereum/node"
"github.com/ethereum/go-ethereum/params"
"github.com/stretchr/testify/assert"
)
var (
testL2RollupAddress = common.HexToAddress("0x79fcdef22feed20eddacbb2587640e45491b757f")
testKey, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
testAddr = crypto.PubkeyToAddress(testKey.PublicKey)
genesis *core.Genesis
txs []*types.Transaction
testEngine *taiko.Taiko
)
func init() {
config := params.TestChainConfig
config.GrayGlacierBlock = nil
config.ArrowGlacierBlock = nil
config.Ethash = nil
config.Taiko = true
testEngine = taiko.New()
genesis = &core.Genesis{
Config: config,
Alloc: core.GenesisAlloc{testAddr: {Balance: big.NewInt(2e15)}},
ExtraData: []byte("test genesis"),
Timestamp: 9000,
Difficulty: common.Big0,
BaseFee: big.NewInt(params.InitialBaseFee),
}
txs = []*types.Transaction{
types.MustSignNewTx(testKey, types.LatestSigner(genesis.Config), &types.LegacyTx{
Nonce: 0,
Value: big.NewInt(12),
GasPrice: big.NewInt(params.InitialBaseFee),
Gas: params.TxGas,
To: &common.Address{2},
}),
types.MustSignNewTx(testKey, types.LatestSigner(genesis.Config), &types.LegacyTx{
Nonce: 1,
Value: big.NewInt(8),
GasPrice: big.NewInt(params.InitialBaseFee),
Gas: params.TxGas,
To: &common.Address{2},
}),
// prepareBlockTx
types.MustSignNewTx(testKey, types.LatestSigner(genesis.Config), &types.LegacyTx{
Nonce: 2,
Value: big.NewInt(8),
GasPrice: big.NewInt(params.InitialBaseFee),
Gas: params.TxGas,
To: &testL2RollupAddress,
}),
}
}
func newTestBackend(t *testing.T) (*eth.Ethereum, []*types.Block) {
// Generate test chain.
blocks := generateTestChain()
// Create node
n, err := node.New(&node.Config{})
if err != nil {
t.Fatalf("can't create new node: %v", err)
}
// Create Ethereum Service
config := ðconfig.Config{
Genesis: genesis,
}
ethservice, err := eth.New(n, config)
if err != nil {
t.Fatalf("can't create new ethereum service: %v", err)
}
// Import the test chain.
if err := n.Start(); err != nil {
t.Fatalf("can't start test node: %v", err)
}
if _, err := ethservice.BlockChain().InsertChain(blocks[1:]); err != nil {
t.Fatalf("can't import test blocks: %v", err)
}
if _, ok := ethservice.Engine().(*taiko.Taiko); !ok {
t.Fatalf("not use taiko engine")
}
return ethservice, blocks
}
func generateTestChain() []*types.Block {
db := rawdb.NewMemoryDatabase()
generate := func(i int, g *core.BlockGen) {
g.OffsetTime(5)
g.SetExtra([]byte("test_taiko"))
g.SetDifficulty(common.Big0)
for _, tx := range txs {
g.AddTx(tx)
}
}
gblock := genesis.MustCommit(db)
blocks, _ := core.GenerateChain(genesis.Config, gblock, testEngine, db, 1, generate)
blocks = append([]*types.Block{gblock}, blocks...)
return blocks
}
func TestVerifyHeader(t *testing.T) {
ethService, blocks := newTestBackend(t)
for _, b := range blocks {
err := testEngine.VerifyHeader(ethService.BlockChain(), b.Header(), true)
assert.NoErrorf(t, err, "VerifyHeader error: %s", err)
}
err := testEngine.VerifyHeader(ethService.BlockChain(), &types.Header{
Number: common.Big1,
Time: uint64(time.Now().Unix()),
BaseFee: big.NewInt(params.InitialBaseFee),
WithdrawalsHash: &types.EmptyWithdrawalsHash,
UncleHash: types.EmptyUncleHash,
}, true)
assert.ErrorIs(t, err, consensus.ErrUnknownAncestor, "VerifyHeader should throw ErrUnknownAncestor when parentHash is unknown")
err = testEngine.VerifyHeader(ethService.BlockChain(), &types.Header{
ParentHash: blocks[len(blocks)-1].Hash(),
Number: common.Big0,
Time: uint64(time.Now().Unix()),
BaseFee: big.NewInt(params.InitialBaseFee),
WithdrawalsHash: &types.EmptyWithdrawalsHash,
UncleHash: types.EmptyUncleHash,
}, true)
assert.ErrorIs(t, err, consensus.ErrInvalidNumber, "VerifyHeader should throw ErrInvalidNumber when the block number is wrong")
err = testEngine.VerifyHeader(ethService.BlockChain(), &types.Header{
ParentHash: blocks[len(blocks)-1].Hash(),
Number: new(big.Int).SetInt64(int64(len(blocks))),
Time: uint64(time.Now().Unix()),
Extra: bytes.Repeat([]byte{1}, int(params.MaximumExtraDataSize+1)),
BaseFee: big.NewInt(params.InitialBaseFee),
WithdrawalsHash: &types.EmptyWithdrawalsHash,
UncleHash: types.EmptyUncleHash,
}, true)
assert.ErrorContains(t, err, "extra-data too long", "VerifyHeader should throw ErrExtraDataTooLong when the block has too much extra data")
err = testEngine.VerifyHeader(ethService.BlockChain(), &types.Header{
ParentHash: blocks[len(blocks)-1].Hash(),
Number: new(big.Int).SetInt64(int64(len(blocks))),
Time: uint64(time.Now().Unix()),
Difficulty: common.Big1,
BaseFee: big.NewInt(params.InitialBaseFee),
WithdrawalsHash: &types.EmptyWithdrawalsHash,
UncleHash: types.EmptyUncleHash,
}, true)
assert.ErrorContains(t, err, "invalid difficulty", "VerifyHeader should throw ErrInvalidDifficulty when difficulty is not 0")
err = testEngine.VerifyHeader(ethService.BlockChain(), &types.Header{
ParentHash: blocks[len(blocks)-1].Hash(),
Number: new(big.Int).SetInt64(int64(len(blocks))),
Time: uint64(time.Now().Unix()),
GasLimit: params.MaxGasLimit + 1,
BaseFee: big.NewInt(params.InitialBaseFee),
WithdrawalsHash: &types.EmptyWithdrawalsHash,
UncleHash: types.EmptyUncleHash,
}, true)
assert.ErrorContains(t, err, "invalid gasLimit", "VerifyHeader should throw ErrInvalidGasLimit when gasLimit is higher than the limit")
err = testEngine.VerifyHeader(ethService.BlockChain(), &types.Header{
ParentHash: blocks[len(blocks)-1].Hash(),
Number: new(big.Int).SetInt64(int64(len(blocks))),
Time: uint64(time.Now().Unix()),
GasLimit: params.MaxGasLimit,
BaseFee: big.NewInt(params.InitialBaseFee),
UncleHash: types.EmptyUncleHash,
}, true)
assert.ErrorContains(t, err, "withdrawals hash missing", "VerifyHeader should throw ErrWithdrawalsHashMissing withdrawalshash is nil")
err = testEngine.VerifyHeader(ethService.BlockChain(), &types.Header{
ParentHash: blocks[len(blocks)-1].Hash(),
Number: new(big.Int).SetInt64(int64(len(blocks))),
Time: uint64(time.Now().Unix()),
GasLimit: params.MaxGasLimit,
BaseFee: big.NewInt(params.InitialBaseFee),
WithdrawalsHash: &types.EmptyWithdrawalsHash,
}, true)
assert.ErrorContains(t, err, "uncles not empty", "VerifyHeader should throw ErrUnclesNotEmpty if uncles is not the empty hash")
}