2021-11-26 10:59:05 +00:00
|
|
|
package privateapi
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-12-22 16:12:35 +00:00
|
|
|
"sync/atomic"
|
2021-11-26 10:59:05 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/ledgerwatch/erigon-lib/gointerfaces"
|
|
|
|
"github.com/ledgerwatch/erigon-lib/gointerfaces/remote"
|
|
|
|
types2 "github.com/ledgerwatch/erigon-lib/gointerfaces/types"
|
2021-11-27 15:29:46 +00:00
|
|
|
"github.com/ledgerwatch/erigon-lib/kv"
|
2021-11-26 10:59:05 +00:00
|
|
|
"github.com/ledgerwatch/erigon-lib/kv/memdb"
|
|
|
|
"github.com/ledgerwatch/erigon/common"
|
|
|
|
"github.com/ledgerwatch/erigon/core/rawdb"
|
|
|
|
"github.com/ledgerwatch/erigon/params"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2021-11-26 13:51:32 +00:00
|
|
|
// Hashes
|
|
|
|
var (
|
|
|
|
startingHeadHash = common.HexToHash("0x1")
|
2021-12-29 16:25:13 +00:00
|
|
|
payload1Hash = common.HexToHash("2afc6f4132be8d1fded51aa7f914fd831d2939a100f61322842ab41d7898255b")
|
2021-12-01 12:41:31 +00:00
|
|
|
payload2Hash = common.HexToHash("a19013b8b5f95ffaa008942fd2f04f72a3ae91f54eb64a3f6f8c6630db742aef")
|
2021-12-29 16:25:13 +00:00
|
|
|
payload3Hash = common.HexToHash("a2dd4fc599747c1ce2176a4abae13afbc7ccb4a240f8f4cf22252767bab52f12")
|
2021-11-26 13:51:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Payloads
|
|
|
|
var (
|
|
|
|
mockPayload1 *types2.ExecutionPayload = &types2.ExecutionPayload{
|
|
|
|
ParentHash: gointerfaces.ConvertHashToH256(common.HexToHash("0x2")),
|
|
|
|
BlockHash: gointerfaces.ConvertHashToH256(payload1Hash),
|
|
|
|
ReceiptRoot: gointerfaces.ConvertHashToH256(common.HexToHash("0x3")),
|
|
|
|
StateRoot: gointerfaces.ConvertHashToH256(common.HexToHash("0x4")),
|
|
|
|
Random: gointerfaces.ConvertHashToH256(common.HexToHash("0x0b3")),
|
|
|
|
LogsBloom: gointerfaces.ConvertBytesToH2048(make([]byte, 256)),
|
2021-12-29 16:25:13 +00:00
|
|
|
ExtraData: make([]byte, 0),
|
2021-11-26 13:51:32 +00:00
|
|
|
BaseFeePerGas: gointerfaces.ConvertHashToH256(common.HexToHash("0x0b3")),
|
|
|
|
BlockNumber: 100,
|
|
|
|
GasLimit: 52,
|
|
|
|
GasUsed: 4,
|
|
|
|
Timestamp: 4,
|
|
|
|
Coinbase: gointerfaces.ConvertAddressToH160(common.HexToAddress("0x1")),
|
|
|
|
Transactions: make([][]byte, 0),
|
|
|
|
}
|
|
|
|
mockPayload2 *types2.ExecutionPayload = &types2.ExecutionPayload{
|
|
|
|
ParentHash: gointerfaces.ConvertHashToH256(payload1Hash),
|
|
|
|
BlockHash: gointerfaces.ConvertHashToH256(payload2Hash),
|
|
|
|
ReceiptRoot: gointerfaces.ConvertHashToH256(common.HexToHash("0x3")),
|
|
|
|
StateRoot: gointerfaces.ConvertHashToH256(common.HexToHash("0x4")),
|
|
|
|
Random: gointerfaces.ConvertHashToH256(common.HexToHash("0x0b3")),
|
|
|
|
LogsBloom: gointerfaces.ConvertBytesToH2048(make([]byte, 256)),
|
2021-12-29 16:25:13 +00:00
|
|
|
ExtraData: make([]byte, 0),
|
2021-11-26 13:51:32 +00:00
|
|
|
BaseFeePerGas: gointerfaces.ConvertHashToH256(common.HexToHash("0x0b3")),
|
|
|
|
BlockNumber: 101,
|
|
|
|
GasLimit: 52,
|
|
|
|
GasUsed: 4,
|
|
|
|
Timestamp: 4,
|
|
|
|
Coinbase: gointerfaces.ConvertAddressToH160(common.HexToAddress("0x1")),
|
|
|
|
Transactions: make([][]byte, 0),
|
|
|
|
}
|
|
|
|
mockPayload3 = &types2.ExecutionPayload{
|
|
|
|
ParentHash: gointerfaces.ConvertHashToH256(startingHeadHash),
|
|
|
|
BlockHash: gointerfaces.ConvertHashToH256(payload3Hash),
|
|
|
|
ReceiptRoot: gointerfaces.ConvertHashToH256(common.HexToHash("0x3")),
|
|
|
|
StateRoot: gointerfaces.ConvertHashToH256(common.HexToHash("0x4")),
|
|
|
|
Random: gointerfaces.ConvertHashToH256(common.HexToHash("0x0b3")),
|
|
|
|
LogsBloom: gointerfaces.ConvertBytesToH2048(make([]byte, 256)),
|
2021-12-29 16:25:13 +00:00
|
|
|
ExtraData: make([]byte, 0),
|
2021-11-26 13:51:32 +00:00
|
|
|
BaseFeePerGas: gointerfaces.ConvertHashToH256(common.HexToHash("0x0b3")),
|
|
|
|
BlockNumber: 51,
|
|
|
|
GasLimit: 52,
|
|
|
|
GasUsed: 4,
|
|
|
|
Timestamp: 4,
|
|
|
|
Coinbase: gointerfaces.ConvertAddressToH160(common.HexToAddress("0x1")),
|
|
|
|
Transactions: make([][]byte, 0),
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2021-11-27 15:29:46 +00:00
|
|
|
func makeTestDb(ctx context.Context, db kv.RwDB) {
|
|
|
|
tx, _ := db.BeginRw(ctx)
|
|
|
|
rawdb.WriteHeadBlockHash(tx, startingHeadHash)
|
|
|
|
rawdb.WriteHeaderNumber(tx, startingHeadHash, 50)
|
|
|
|
_ = tx.Commit()
|
|
|
|
}
|
|
|
|
|
2021-11-26 13:51:32 +00:00
|
|
|
func TestMockDownloadRequest(t *testing.T) {
|
|
|
|
db := memdb.New()
|
|
|
|
ctx := context.Background()
|
|
|
|
require := require.New(t)
|
|
|
|
|
2021-11-27 15:29:46 +00:00
|
|
|
makeTestDb(ctx, db)
|
2021-12-29 16:25:13 +00:00
|
|
|
reverseDownloadCh := make(chan PayloadMessage)
|
2021-11-29 14:47:08 +00:00
|
|
|
statusCh := make(chan ExecutionStatus)
|
2021-12-22 16:12:35 +00:00
|
|
|
waitingForHeaders := uint32(1)
|
2021-11-26 13:51:32 +00:00
|
|
|
|
2021-11-29 14:47:08 +00:00
|
|
|
backend := NewEthBackendServer(ctx, nil, db, nil, nil, ¶ms.ChainConfig{TerminalTotalDifficulty: common.Big1}, reverseDownloadCh, statusCh, &waitingForHeaders)
|
2021-11-26 13:51:32 +00:00
|
|
|
|
|
|
|
var err error
|
|
|
|
var reply *remote.EngineExecutePayloadReply
|
|
|
|
done := make(chan bool)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
reply, err = backend.EngineExecutePayloadV1(ctx, mockPayload1)
|
|
|
|
done <- true
|
|
|
|
}()
|
|
|
|
|
|
|
|
<-reverseDownloadCh
|
2021-12-23 14:06:10 +00:00
|
|
|
statusCh <- ExecutionStatus{Status: Syncing}
|
2021-12-22 16:12:35 +00:00
|
|
|
atomic.StoreUint32(&waitingForHeaders, 0)
|
2021-11-26 13:51:32 +00:00
|
|
|
<-done
|
|
|
|
require.NoError(err)
|
2021-11-30 17:43:46 +00:00
|
|
|
require.Equal(reply.Status, string(Syncing))
|
2021-12-23 14:06:10 +00:00
|
|
|
require.Nil(reply.LatestValidHash)
|
2021-11-26 13:51:32 +00:00
|
|
|
|
2021-12-23 14:06:10 +00:00
|
|
|
// If we get another request we don't need to process it with processDownloadCh and ignore it and return Syncing status
|
2021-11-26 13:51:32 +00:00
|
|
|
go func() {
|
|
|
|
reply, err = backend.EngineExecutePayloadV1(ctx, mockPayload2)
|
|
|
|
done <- true
|
|
|
|
}()
|
|
|
|
|
|
|
|
<-done
|
|
|
|
// Same result as before
|
|
|
|
require.NoError(err)
|
2021-11-30 17:43:46 +00:00
|
|
|
require.Equal(reply.Status, string(Syncing))
|
2021-12-23 14:06:10 +00:00
|
|
|
require.Nil(reply.LatestValidHash)
|
2021-11-26 13:51:32 +00:00
|
|
|
|
|
|
|
// However if we simulate that we finish reverse downloading the chain by updating the head, we just execute 1:1
|
2021-11-27 15:29:46 +00:00
|
|
|
tx, _ := db.BeginRw(ctx)
|
2021-11-26 13:51:32 +00:00
|
|
|
rawdb.WriteHeadBlockHash(tx, payload1Hash)
|
|
|
|
rawdb.WriteHeaderNumber(tx, payload1Hash, 100)
|
|
|
|
_ = tx.Commit()
|
|
|
|
// Now we try to sync the next payload again
|
|
|
|
go func() {
|
|
|
|
reply, err = backend.EngineExecutePayloadV1(ctx, mockPayload2)
|
|
|
|
done <- true
|
|
|
|
}()
|
|
|
|
|
|
|
|
<-done
|
|
|
|
|
|
|
|
require.NoError(err)
|
2021-11-30 17:43:46 +00:00
|
|
|
require.Equal(reply.Status, string(Syncing))
|
2021-12-23 14:06:10 +00:00
|
|
|
require.Nil(reply.LatestValidHash)
|
2021-11-26 13:51:32 +00:00
|
|
|
}
|
|
|
|
|
2021-11-26 10:59:05 +00:00
|
|
|
func TestMockValidExecution(t *testing.T) {
|
|
|
|
db := memdb.New()
|
|
|
|
ctx := context.Background()
|
|
|
|
require := require.New(t)
|
2021-11-27 15:29:46 +00:00
|
|
|
|
|
|
|
makeTestDb(ctx, db)
|
2021-11-26 10:59:05 +00:00
|
|
|
|
2021-12-29 16:25:13 +00:00
|
|
|
reverseDownloadCh := make(chan PayloadMessage)
|
2021-11-29 14:47:08 +00:00
|
|
|
statusCh := make(chan ExecutionStatus)
|
2021-12-22 16:12:35 +00:00
|
|
|
waitingForHeaders := uint32(1)
|
2021-11-26 10:59:05 +00:00
|
|
|
|
2021-11-29 14:47:08 +00:00
|
|
|
backend := NewEthBackendServer(ctx, nil, db, nil, nil, ¶ms.ChainConfig{TerminalTotalDifficulty: common.Big1}, reverseDownloadCh, statusCh, &waitingForHeaders)
|
2021-11-26 10:59:05 +00:00
|
|
|
|
|
|
|
var err error
|
|
|
|
var reply *remote.EngineExecutePayloadReply
|
|
|
|
done := make(chan bool)
|
|
|
|
|
|
|
|
go func() {
|
2021-11-26 13:51:32 +00:00
|
|
|
reply, err = backend.EngineExecutePayloadV1(ctx, mockPayload3)
|
2021-11-26 10:59:05 +00:00
|
|
|
done <- true
|
|
|
|
}()
|
|
|
|
|
|
|
|
<-reverseDownloadCh
|
|
|
|
|
2021-11-29 14:47:08 +00:00
|
|
|
statusCh <- ExecutionStatus{
|
2021-12-23 14:06:10 +00:00
|
|
|
Status: Valid,
|
|
|
|
LatestValidHash: payload3Hash,
|
2021-11-26 10:59:05 +00:00
|
|
|
}
|
|
|
|
<-done
|
|
|
|
|
|
|
|
require.NoError(err)
|
2021-11-30 17:43:46 +00:00
|
|
|
require.Equal(reply.Status, string(Valid))
|
2021-11-26 10:59:05 +00:00
|
|
|
replyHash := gointerfaces.ConvertH256ToHash(reply.LatestValidHash)
|
2021-11-26 13:51:32 +00:00
|
|
|
require.Equal(replyHash[:], payload3Hash[:])
|
2021-11-26 10:59:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMockInvalidExecution(t *testing.T) {
|
|
|
|
db := memdb.New()
|
|
|
|
ctx := context.Background()
|
|
|
|
require := require.New(t)
|
2021-11-27 15:29:46 +00:00
|
|
|
|
|
|
|
makeTestDb(ctx, db)
|
2021-11-26 10:59:05 +00:00
|
|
|
|
2021-12-29 16:25:13 +00:00
|
|
|
reverseDownloadCh := make(chan PayloadMessage)
|
2021-11-29 14:47:08 +00:00
|
|
|
statusCh := make(chan ExecutionStatus)
|
2021-11-26 10:59:05 +00:00
|
|
|
|
2021-12-22 16:12:35 +00:00
|
|
|
waitingForHeaders := uint32(1)
|
2021-11-29 14:47:08 +00:00
|
|
|
backend := NewEthBackendServer(ctx, nil, db, nil, nil, ¶ms.ChainConfig{TerminalTotalDifficulty: common.Big1}, reverseDownloadCh, statusCh, &waitingForHeaders)
|
2021-11-26 10:59:05 +00:00
|
|
|
|
|
|
|
var err error
|
|
|
|
var reply *remote.EngineExecutePayloadReply
|
|
|
|
done := make(chan bool)
|
|
|
|
|
|
|
|
go func() {
|
2021-11-26 13:51:32 +00:00
|
|
|
reply, err = backend.EngineExecutePayloadV1(ctx, mockPayload3)
|
2021-11-26 10:59:05 +00:00
|
|
|
done <- true
|
|
|
|
}()
|
|
|
|
|
|
|
|
<-reverseDownloadCh
|
2021-11-30 17:14:34 +00:00
|
|
|
// Simulate invalid status
|
2021-11-29 14:47:08 +00:00
|
|
|
statusCh <- ExecutionStatus{
|
2021-12-23 14:06:10 +00:00
|
|
|
Status: Invalid,
|
|
|
|
LatestValidHash: startingHeadHash,
|
2021-11-26 10:59:05 +00:00
|
|
|
}
|
|
|
|
<-done
|
|
|
|
|
|
|
|
require.NoError(err)
|
2021-11-30 17:43:46 +00:00
|
|
|
require.Equal(reply.Status, string(Invalid))
|
2021-11-26 10:59:05 +00:00
|
|
|
replyHash := gointerfaces.ConvertH256ToHash(reply.LatestValidHash)
|
2021-11-26 13:51:32 +00:00
|
|
|
require.Equal(replyHash[:], startingHeadHash[:])
|
2021-11-26 10:59:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNoTTD(t *testing.T) {
|
|
|
|
db := memdb.New()
|
|
|
|
ctx := context.Background()
|
|
|
|
require := require.New(t)
|
2021-11-27 15:29:46 +00:00
|
|
|
|
|
|
|
makeTestDb(ctx, db)
|
2021-11-26 10:59:05 +00:00
|
|
|
|
2021-12-29 16:25:13 +00:00
|
|
|
reverseDownloadCh := make(chan PayloadMessage)
|
2021-11-29 14:47:08 +00:00
|
|
|
statusCh := make(chan ExecutionStatus)
|
2021-12-22 16:12:35 +00:00
|
|
|
waitingForHeaders := uint32(1)
|
2021-11-26 10:59:05 +00:00
|
|
|
|
2021-11-29 14:47:08 +00:00
|
|
|
backend := NewEthBackendServer(ctx, nil, db, nil, nil, ¶ms.ChainConfig{}, reverseDownloadCh, statusCh, &waitingForHeaders)
|
2021-11-26 10:59:05 +00:00
|
|
|
|
|
|
|
var err error
|
|
|
|
|
|
|
|
done := make(chan bool)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
_, err = backend.EngineExecutePayloadV1(ctx, &types2.ExecutionPayload{
|
|
|
|
ParentHash: gointerfaces.ConvertHashToH256(common.HexToHash("0x2")),
|
|
|
|
BlockHash: gointerfaces.ConvertHashToH256(common.HexToHash("0x3")),
|
|
|
|
ReceiptRoot: gointerfaces.ConvertHashToH256(common.HexToHash("0x4")),
|
|
|
|
StateRoot: gointerfaces.ConvertHashToH256(common.HexToHash("0x4")),
|
|
|
|
Random: gointerfaces.ConvertHashToH256(common.HexToHash("0x0b3")),
|
|
|
|
LogsBloom: gointerfaces.ConvertBytesToH2048(make([]byte, 256)),
|
2021-12-29 16:25:13 +00:00
|
|
|
ExtraData: make([]byte, 0),
|
2021-11-26 10:59:05 +00:00
|
|
|
BaseFeePerGas: gointerfaces.ConvertHashToH256(common.HexToHash("0x0b3")),
|
2021-11-26 13:51:32 +00:00
|
|
|
BlockNumber: 51,
|
2021-11-26 10:59:05 +00:00
|
|
|
GasLimit: 52,
|
|
|
|
GasUsed: 4,
|
|
|
|
Timestamp: 4,
|
|
|
|
Coinbase: gointerfaces.ConvertAddressToH160(common.HexToAddress("0x1")),
|
|
|
|
Transactions: make([][]byte, 0),
|
|
|
|
})
|
|
|
|
done <- true
|
|
|
|
}()
|
|
|
|
|
|
|
|
<-done
|
|
|
|
|
|
|
|
require.Equal(err.Error(), "not a proof-of-stake chain")
|
|
|
|
}
|