-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathengine_server_test.go
More file actions
427 lines (370 loc) · 16.4 KB
/
engine_server_test.go
File metadata and controls
427 lines (370 loc) · 16.4 KB
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
// Copyright 2025 The Erigon Authors
// This file is part of Erigon.
//
// Erigon is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Erigon is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with Erigon. If not, see <http://www.gnu.org/licenses/>.
package engineapi
import (
"bytes"
"context"
"math/big"
"testing"
"time"
"github.com/holiman/uint256"
"github.com/jinzhu/copier"
"github.com/stretchr/testify/require"
"golang.org/x/sync/errgroup"
"github.com/erigontech/erigon/cmd/rpcdaemon/cli/httpcfg"
"github.com/erigontech/erigon/cmd/rpcdaemon/rpcdaemontest"
"github.com/erigontech/erigon/cmd/rpcdaemon/rpcservices"
"github.com/erigontech/erigon/common"
"github.com/erigontech/erigon/common/crypto"
"github.com/erigontech/erigon/common/hexutil"
"github.com/erigontech/erigon/common/log/v3"
"github.com/erigontech/erigon/db/kv"
"github.com/erigontech/erigon/db/kv/kvcache"
"github.com/erigontech/erigon/db/rawdb"
"github.com/erigontech/erigon/execution/chain"
"github.com/erigontech/erigon/execution/execmodule/execmoduletester"
"github.com/erigontech/erigon/execution/tests/blockgen"
"github.com/erigontech/erigon/execution/types"
"github.com/erigontech/erigon/node/direct"
"github.com/erigontech/erigon/node/ethconfig"
"github.com/erigontech/erigon/node/gointerfaces/txpoolproto"
"github.com/erigontech/erigon/rpc/jsonrpc"
"github.com/erigontech/erigon/rpc/rpccfg"
"github.com/erigontech/erigon/rpc/rpchelper"
)
// Do 1 step to start txPool
func oneBlockSteps(m *execmoduletester.ExecModuleTester, require *require.Assertions, blocks int) {
chain, err := blockgen.GenerateChain(m.ChainConfig, m.Genesis, m.Engine, m.DB, blocks, func(i int, b *blockgen.BlockGen) {
b.SetCoinbase(common.Address{1})
})
require.NoError(err)
err = m.InsertChain(chain)
require.NoError(err)
}
// Do 1 step to start txPool
func oneBlockStep(mockSentry *execmoduletester.ExecModuleTester, require *require.Assertions) {
oneBlockSteps(mockSentry, require, 1)
}
func newBaseApiForTest(m *execmoduletester.ExecModuleTester) *jsonrpc.BaseAPI {
stateCache := kvcache.New(kvcache.DefaultCoherentConfig)
return jsonrpc.NewBaseApi(nil, stateCache, m.BlockReader, false, rpccfg.DefaultEvmCallTimeout, m.Engine, m.Dirs, nil, 0)
}
func newEthApiForTest(base *jsonrpc.BaseAPI, db kv.TemporalRoDB, txPool txpoolproto.TxpoolClient) *jsonrpc.APIImpl {
cfg := &jsonrpc.EthApiConfig{
GasCap: 5000000,
FeeCap: ethconfig.Defaults.RPCTxFeeCap,
ReturnDataLimit: 100_000,
AllowUnprotectedTxs: false,
MaxGetProofRewindBlockCount: 100_000,
SubscribeLogsChannelSize: 128,
RpcTxSyncDefaultTimeout: 20 * time.Second,
RpcTxSyncMaxTimeout: 1 * time.Minute,
}
return jsonrpc.NewEthAPI(base, db, nil, txPool, nil, cfg, log.New())
}
func TestGetBlobsV1(t *testing.T) {
if testing.Short() {
t.Skip("slow test")
}
buf := bytes.NewBuffer(nil)
funds := big.NewInt(1 * common.Ether)
key, _ := crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
address := crypto.PubkeyToAddress(key.PublicKey)
var chainConfig chain.Config
err := copier.CopyWithOption(&chainConfig, chain.AllProtocolChanges, copier.Option{DeepCopy: true})
require.NoError(t, err)
chainConfig.PragueTime = nil
chainConfig.OsakaTime = nil
chainConfig.AmsterdamTime = nil
gspec := &types.Genesis{
Config: &chainConfig,
Alloc: types.GenesisAlloc{
address: {Balance: funds},
},
}
mockSentry := execmoduletester.New(
t,
execmoduletester.WithGenesisSpec(gspec),
execmoduletester.WithKey(key),
execmoduletester.WithTxPool(),
)
require := require.New(t)
oneBlockStep(mockSentry, require)
wrappedTxn := types.MakeWrappedBlobTxn(uint256.MustFromBig(mockSentry.ChainConfig.ChainID))
txn, err := types.SignTx(wrappedTxn, *types.LatestSignerForChainID(mockSentry.ChainConfig.ChainID), mockSentry.Key)
require.NoError(err)
dt := &wrappedTxn.Tx.DynamicFeeTransaction
v, r, s := txn.RawSignatureValues()
dt.V.Set(v)
dt.R.Set(r)
dt.S.Set(s)
ctx, conn := rpcdaemontest.CreateTestGrpcConn(t, mockSentry)
txPool := direct.NewTxPoolClient(mockSentry.TxPoolGrpcServer)
ff := rpchelper.New(ctx, rpchelper.DefaultFiltersConfig, nil, txPool, txpoolproto.NewMiningClient(conn), func() {}, mockSentry.Log)
api := newEthApiForTest(newBaseApiForTest(mockSentry), mockSentry.DB, txPool)
executionRpc := direct.NewExecutionClientDirect(mockSentry.ExecModule)
eth := rpcservices.NewRemoteBackend(nil, mockSentry.DB, mockSentry.BlockReader)
fcuTimeout := ethconfig.Defaults.FcuTimeout
maxReorgDepth := ethconfig.Defaults.MaxReorgDepth
engineServer := NewEngineServer(mockSentry.Log, mockSentry.ChainConfig, executionRpc, nil, false, false, true, txPool, fcuTimeout, maxReorgDepth)
ctx, cancel := context.WithCancel(ctx)
var eg errgroup.Group
t.Cleanup(func() {
err := eg.Wait() // wait for clean exit
require.ErrorIs(err, context.Canceled)
})
t.Cleanup(cancel)
eg.Go(func() error {
return engineServer.Start(ctx, &httpcfg.HttpCfg{}, mockSentry.DB, mockSentry.BlockReader, ff, nil, mockSentry.Engine, eth, nil)
})
err = wrappedTxn.MarshalBinaryWrapped(buf)
require.NoError(err)
_, err = api.SendRawTransaction(ctx, buf.Bytes())
require.NoError(err)
blobHashes := append([]common.Hash{{}}, wrappedTxn.Tx.BlobVersionedHashes...)
blobsResp, err := engineServer.GetBlobsV1(ctx, blobHashes)
require.NoError(err)
require.True(blobsResp[0] == nil)
require.Equal(blobsResp[1].Blob, hexutil.Bytes(wrappedTxn.Blobs[0][:]))
require.Equal(blobsResp[2].Blob, hexutil.Bytes(wrappedTxn.Blobs[1][:]))
require.Equal(blobsResp[1].Proof, hexutil.Bytes(wrappedTxn.Proofs[0][:]))
require.Equal(blobsResp[2].Proof, hexutil.Bytes(wrappedTxn.Proofs[1][:]))
}
func TestGetBlobsV2(t *testing.T) {
if testing.Short() {
t.Skip("slow test")
}
buf := bytes.NewBuffer(nil)
mockSentry := execmoduletester.New(t, execmoduletester.WithTxPool(), execmoduletester.WithChainConfig(chain.AllProtocolChanges))
require := require.New(t)
oneBlockStep(mockSentry, require)
wrappedTxn := types.MakeV1WrappedBlobTxn(uint256.MustFromBig(mockSentry.ChainConfig.ChainID))
txn, err := types.SignTx(wrappedTxn, *types.LatestSignerForChainID(mockSentry.ChainConfig.ChainID), mockSentry.Key)
require.NoError(err)
dt := &wrappedTxn.Tx.DynamicFeeTransaction
v, r, s := txn.RawSignatureValues()
dt.V.Set(v)
dt.R.Set(r)
dt.S.Set(s)
ctx, conn := rpcdaemontest.CreateTestGrpcConn(t, mockSentry)
txPool := direct.NewTxPoolClient(mockSentry.TxPoolGrpcServer)
ff := rpchelper.New(ctx, rpchelper.DefaultFiltersConfig, nil, txPool, txpoolproto.NewMiningClient(conn), func() {}, mockSentry.Log)
api := newEthApiForTest(newBaseApiForTest(mockSentry), mockSentry.DB, txPool)
executionRpc := direct.NewExecutionClientDirect(mockSentry.ExecModule)
eth := rpcservices.NewRemoteBackend(nil, mockSentry.DB, mockSentry.BlockReader)
fcuTimeout := ethconfig.Defaults.FcuTimeout
maxReorgDepth := ethconfig.Defaults.MaxReorgDepth
engineServer := NewEngineServer(mockSentry.Log, mockSentry.ChainConfig, executionRpc, nil, false, false, true, txPool, fcuTimeout, maxReorgDepth)
ctx, cancel := context.WithCancel(ctx)
var eg errgroup.Group
t.Cleanup(func() {
err := eg.Wait() // wait for clean exit
require.ErrorIs(err, context.Canceled)
})
t.Cleanup(cancel)
eg.Go(func() error {
return engineServer.Start(ctx, &httpcfg.HttpCfg{}, mockSentry.DB, mockSentry.BlockReader, ff, nil, mockSentry.Engine, eth, nil)
})
err = wrappedTxn.MarshalBinaryWrapped(buf)
require.NoError(err)
hh, err := api.SendRawTransaction(ctx, buf.Bytes())
require.NoError(err)
require.NotEmpty(hh)
blobHashes := append([]common.Hash{{}}, wrappedTxn.Tx.BlobVersionedHashes...)
blobsResp, err := engineServer.GetBlobsV2(ctx, blobHashes)
require.NoError(err)
require.Nil(blobsResp) // Any one blob not found makes the whole response nil
blobHashes = blobHashes[1:]
blobsResp, err = engineServer.GetBlobsV2(ctx, blobHashes)
require.NoError(err)
require.Len(blobsResp, 2)
require.Equal(blobsResp[0].Blob, hexutil.Bytes(wrappedTxn.Blobs[0][:]))
require.Equal(blobsResp[1].Blob, hexutil.Bytes(wrappedTxn.Blobs[1][:]))
for i := range 128 {
require.Equal(blobsResp[0].CellProofs[i], hexutil.Bytes(wrappedTxn.Proofs[i][:]))
require.Equal(blobsResp[1].CellProofs[i], hexutil.Bytes(wrappedTxn.Proofs[i+128][:]))
}
}
func TestGetBlobsV3(t *testing.T) {
if testing.Short() {
t.Skip("slow test")
}
buf := bytes.NewBuffer(nil)
mockSentry := execmoduletester.New(t, execmoduletester.WithTxPool(), execmoduletester.WithChainConfig(chain.AllProtocolChanges))
require := require.New(t)
oneBlockStep(mockSentry, require)
wrappedTxn := types.MakeV1WrappedBlobTxn(uint256.MustFromBig(mockSentry.ChainConfig.ChainID))
txn, err := types.SignTx(wrappedTxn, *types.LatestSignerForChainID(mockSentry.ChainConfig.ChainID), mockSentry.Key)
require.NoError(err)
dt := &wrappedTxn.Tx.DynamicFeeTransaction
v, r, s := txn.RawSignatureValues()
dt.V.Set(v)
dt.R.Set(r)
dt.S.Set(s)
ctx, conn := rpcdaemontest.CreateTestGrpcConn(t, mockSentry)
txPool := direct.NewTxPoolClient(mockSentry.TxPoolGrpcServer)
ff := rpchelper.New(ctx, rpchelper.DefaultFiltersConfig, nil, txPool, txpoolproto.NewMiningClient(conn), func() {}, mockSentry.Log)
api := newEthApiForTest(newBaseApiForTest(mockSentry), mockSentry.DB, txPool)
executionRpc := direct.NewExecutionClientDirect(mockSentry.ExecModule)
eth := rpcservices.NewRemoteBackend(nil, mockSentry.DB, mockSentry.BlockReader)
fcuTimeout := ethconfig.Defaults.FcuTimeout
maxReorgDepth := ethconfig.Defaults.MaxReorgDepth
engineServer := NewEngineServer(mockSentry.Log, mockSentry.ChainConfig, executionRpc, nil, false, false, true, txPool, fcuTimeout, maxReorgDepth)
ctx, cancel := context.WithCancel(ctx)
var eg errgroup.Group
t.Cleanup(func() {
err := eg.Wait() // wait for clean exit
require.ErrorIs(err, context.Canceled)
})
t.Cleanup(cancel)
eg.Go(func() error {
return engineServer.Start(ctx, &httpcfg.HttpCfg{}, mockSentry.DB, mockSentry.BlockReader, ff, nil, mockSentry.Engine, eth, nil)
})
err = wrappedTxn.MarshalBinaryWrapped(buf)
require.NoError(err)
hh, err := api.SendRawTransaction(ctx, buf.Bytes())
require.NoError(err)
require.NotEmpty(hh)
blobHashes := append([]common.Hash{{}}, wrappedTxn.Tx.BlobVersionedHashes...)
blobsResp, err := engineServer.GetBlobsV3(ctx, blobHashes)
require.NoError(err)
require.Len(blobsResp, 3) // Unlike GetBlobsV2, only the missing blob should be nil
require.Nil(blobsResp[0])
require.Equal(blobsResp[1].Blob, hexutil.Bytes(wrappedTxn.Blobs[0][:]))
require.Equal(blobsResp[2].Blob, hexutil.Bytes(wrappedTxn.Blobs[1][:]))
blobHashes = blobHashes[1:]
blobsResp, err = engineServer.GetBlobsV3(ctx, blobHashes)
require.NoError(err)
require.Len(blobsResp, 2)
require.Equal(blobsResp[0].Blob, hexutil.Bytes(wrappedTxn.Blobs[0][:]))
require.Equal(blobsResp[1].Blob, hexutil.Bytes(wrappedTxn.Blobs[1][:]))
for i := range 128 {
require.Equal(blobsResp[0].CellProofs[i], hexutil.Bytes(wrappedTxn.Proofs[i][:]))
require.Equal(blobsResp[1].CellProofs[i], hexutil.Bytes(wrappedTxn.Proofs[i+128][:]))
}
}
func canonicalHashAt(t *testing.T, db kv.TemporalRoDB, blockNum uint64) common.Hash {
t.Helper()
var hash common.Hash
err := db.View(context.Background(), func(tx kv.Tx) error {
var err error
hash, err = rawdb.ReadCanonicalHash(tx, blockNum)
return err
})
require.NoError(t, err)
return hash
}
func writeBlockAccessListBytes(t *testing.T, db kv.TemporalRwDB, blockHash common.Hash, blockNum uint64, balBytes []byte) {
t.Helper()
err := db.Update(context.Background(), func(tx kv.RwTx) error {
return rawdb.WriteBlockAccessListBytes(tx, blockHash, blockNum, balBytes)
})
require.NoError(t, err)
}
func TestGetPayloadBodiesByHashV2(t *testing.T) {
mockSentry := execmoduletester.New(t, execmoduletester.WithTxPool(), execmoduletester.WithChainConfig(chain.AllProtocolChanges))
req := require.New(t)
oneBlockStep(mockSentry, req)
executionRpc := direct.NewExecutionClientDirect(mockSentry.ExecModule)
maxReorgDepth := ethconfig.Defaults.MaxReorgDepth
engineServer := NewEngineServer(mockSentry.Log, mockSentry.ChainConfig, executionRpc, nil, false, false, true, nil, ethconfig.Defaults.FcuTimeout, maxReorgDepth)
const blockNum = 1
blockHash := canonicalHashAt(t, mockSentry.DB, blockNum)
req.NotEqual(common.Hash{}, blockHash)
ctx := context.Background()
// Amsterdam-enabled chains always have a BAL (even if empty) written by GenerateChain
emptyBAL, err := types.EncodeBlockAccessListBytes(nil)
req.NoError(err)
bodies, err := engineServer.GetPayloadBodiesByHashV2(ctx, []common.Hash{blockHash})
req.NoError(err)
req.Len(bodies, 1)
req.NotNil(bodies[0])
req.Equal(hexutil.Bytes(emptyBAL), bodies[0].BlockAccessList)
// Overwrite with a non-empty BAL and verify it's returned
balBytes := []byte{0x01, 0x02, 0x03}
writeBlockAccessListBytes(t, mockSentry.DB, blockHash, blockNum, balBytes)
bodies, err = engineServer.GetPayloadBodiesByHashV2(ctx, []common.Hash{blockHash})
req.NoError(err)
req.Len(bodies, 1)
req.NotNil(bodies[0])
req.NotNil(bodies[0].BlockAccessList)
req.Equal(hexutil.Bytes(balBytes), bodies[0].BlockAccessList)
}
func TestGetClientCommunicationChannelsV1(t *testing.T) {
mockSentry := execmoduletester.New(t, execmoduletester.WithTxPool(), execmoduletester.WithChainConfig(chain.AllProtocolChanges))
req := require.New(t)
executionRpc := direct.NewExecutionClientDirect(mockSentry.ExecModule)
maxReorgDepth := ethconfig.Defaults.MaxReorgDepth
engineServer := NewEngineServer(mockSentry.Log, mockSentry.ChainConfig, executionRpc, nil, false, false, true, nil, ethconfig.Defaults.FcuTimeout, maxReorgDepth)
ctx := context.Background()
// Before Start (no httpConfig set) — should return defaults
channels, err := engineServer.GetClientCommunicationChannelsV1(ctx)
req.NoError(err)
req.Len(channels, 1)
req.Equal("json_rpc", channels[0].Protocol)
req.Equal("localhost:8551", channels[0].URL)
// After setting httpConfig via Start-like initialization
engineServer.httpConfig = &httpcfg.HttpCfg{
AuthRpcHTTPListenAddress: "0.0.0.0",
AuthRpcPort: 9551,
}
channels, err = engineServer.GetClientCommunicationChannelsV1(ctx)
req.NoError(err)
req.Len(channels, 1)
req.Equal("json_rpc", channels[0].Protocol)
req.Equal("0.0.0.0:9551", channels[0].URL)
}
func TestGetPayloadBodiesByRangeV2(t *testing.T) {
mockSentry := execmoduletester.New(t, execmoduletester.WithTxPool(), execmoduletester.WithChainConfig(chain.AllProtocolChanges))
req := require.New(t)
oneBlockSteps(mockSentry, req, 2)
executionRpc := direct.NewExecutionClientDirect(mockSentry.ExecModule)
maxReorgDepth := ethconfig.Defaults.MaxReorgDepth
engineServer := NewEngineServer(mockSentry.Log, mockSentry.ChainConfig, executionRpc, nil, false, false, true, nil, ethconfig.Defaults.FcuTimeout, maxReorgDepth)
const (
start = 1
count = 2
)
blockHash1 := canonicalHashAt(t, mockSentry.DB, start)
blockHash2 := canonicalHashAt(t, mockSentry.DB, start+1)
req.NotEqual(common.Hash{}, blockHash1)
req.NotEqual(common.Hash{}, blockHash2)
ctx := context.Background()
// Amsterdam-enabled chains always have a BAL (even if empty) written by GenerateChain
emptyBAL, err := types.EncodeBlockAccessListBytes(nil)
req.NoError(err)
bodies, err := engineServer.GetPayloadBodiesByRangeV2(ctx, start, count)
req.NoError(err)
req.Len(bodies, 2)
req.NotNil(bodies[0])
req.NotNil(bodies[1])
req.Equal(hexutil.Bytes(emptyBAL), bodies[0].BlockAccessList)
req.Equal(hexutil.Bytes(emptyBAL), bodies[1].BlockAccessList)
// Overwrite with non-empty BALs and verify they're returned
balBytes1 := []byte{0x01, 0x02, 0x03}
balBytes2 := []byte{0x04, 0x05, 0x06}
writeBlockAccessListBytes(t, mockSentry.DB, blockHash1, start, balBytes1)
writeBlockAccessListBytes(t, mockSentry.DB, blockHash2, start+1, balBytes2)
bodies, err = engineServer.GetPayloadBodiesByRangeV2(ctx, start, count)
req.NoError(err)
req.Len(bodies, 2)
req.NotNil(bodies[0])
req.NotNil(bodies[1])
req.NotNil(bodies[0].BlockAccessList)
req.NotNil(bodies[1].BlockAccessList)
req.Equal(hexutil.Bytes(balBytes1), bodies[0].BlockAccessList)
req.Equal(hexutil.Bytes(balBytes2), bodies[1].BlockAccessList)
}