diff --git a/cl/persistence/beacon_indicies/indicies.go b/cl/persistence/beacon_indicies/indicies.go index da08b445b..f9cd4e2fb 100644 --- a/cl/persistence/beacon_indicies/indicies.go +++ b/cl/persistence/beacon_indicies/indicies.go @@ -94,6 +94,38 @@ func MarkRootCanonical(ctx context.Context, tx kv.RwTx, slot uint64, blockRoot l return tx.Put(kv.CanonicalBlockRoots, base_encoding.Encode64(slot), blockRoot[:]) } +func WriteExecutionBlockNumber(tx kv.RwTx, blockRoot libcommon.Hash, blockNumber uint64) error { + return tx.Put(kv.BlockRootToBlockNumber, blockRoot[:], base_encoding.Encode64(blockNumber)) +} + +func WriteExecutionBlockHash(tx kv.RwTx, blockRoot, blockHash libcommon.Hash) error { + return tx.Put(kv.BlockRootToBlockHash, blockRoot[:], blockHash[:]) +} + +func ReadExecutionBlockNumber(tx kv.Tx, blockRoot libcommon.Hash) (*uint64, error) { + val, err := tx.GetOne(kv.BlockRootToBlockNumber, blockRoot[:]) + if err != nil { + return nil, err + } + if len(val) == 0 { + return nil, nil + } + ret := new(uint64) + *ret = base_encoding.Decode64(val) + return ret, nil +} + +func ReadExecutionBlockHash(tx kv.Tx, blockRoot libcommon.Hash) (libcommon.Hash, error) { + val, err := tx.GetOne(kv.BlockRootToBlockHash, blockRoot[:]) + if err != nil { + return libcommon.Hash{}, err + } + if len(val) == 0 { + return libcommon.Hash{}, nil + } + return libcommon.BytesToHash(val), nil +} + func WriteBeaconBlockHeader(ctx context.Context, tx kv.RwTx, signedHeader *cltypes.SignedBeaconBlockHeader) error { headersBytes, err := signedHeader.EncodeSSZ(nil) if err != nil { diff --git a/cl/persistence/beacon_indicies/indicies_test.go b/cl/persistence/beacon_indicies/indicies_test.go index 7451327b8..3db10d48e 100644 --- a/cl/persistence/beacon_indicies/indicies_test.go +++ b/cl/persistence/beacon_indicies/indicies_test.go @@ -140,3 +140,35 @@ func TestReadBeaconBlockHeader(t *testing.T) { require.Equal(t, headerRoot, blockRoot) } + +func TestWriteExecutionBlockNumber(t *testing.T) { + db := setupTestDB(t) + defer db.Close() + tx, _ := db.BeginRw(context.Background()) + defer tx.Rollback() + + tHash := libcommon.HexToHash("0x2") + require.NoError(t, WriteExecutionBlockNumber(tx, tHash, 1)) + require.NoError(t, WriteExecutionBlockNumber(tx, tHash, 2)) + require.NoError(t, WriteExecutionBlockNumber(tx, tHash, 3)) + + // Try to retrieve the block's slot by its blockRoot and verify + blockNumber, err := ReadExecutionBlockNumber(tx, tHash) + require.NoError(t, err) + require.Equal(t, uint64(3), *blockNumber) +} + +func TestWriteExecutionBlockHash(t *testing.T) { + db := setupTestDB(t) + defer db.Close() + tx, _ := db.BeginRw(context.Background()) + defer tx.Rollback() + + tHash := libcommon.HexToHash("0x2") + tHash2 := libcommon.HexToHash("0x3") + require.NoError(t, WriteExecutionBlockHash(tx, tHash, tHash2)) + // Try to retrieve the block's slot by its blockRoot and verify + tHash3, err := ReadExecutionBlockHash(tx, tHash) + require.NoError(t, err) + require.Equal(t, tHash2, tHash3) +} diff --git a/cl/persistence/block_saver.go b/cl/persistence/block_saver.go index bbbaecdaf..15a02986a 100644 --- a/cl/persistence/block_saver.go +++ b/cl/persistence/block_saver.go @@ -145,6 +145,15 @@ func (b beaconChainDatabaseFilesystem) WriteBlock(ctx context.Context, tx kv.RwT if err != nil { return err } + if block.Version() >= clparams.BellatrixVersion { + if err := beacon_indicies.WriteExecutionBlockNumber(tx, blockRoot, block.Block.Body.ExecutionPayload.BlockNumber); err != nil { + return err + } + if err := beacon_indicies.WriteExecutionBlockHash(tx, blockRoot, block.Block.Body.ExecutionPayload.BlockHash); err != nil { + return err + } + } + if err := beacon_indicies.WriteBeaconBlockHeaderAndIndicies(ctx, tx, &cltypes.SignedBeaconBlockHeader{ Signature: block.Signature, Header: &cltypes.BeaconBlockHeader{ diff --git a/erigon-lib/go.mod b/erigon-lib/go.mod index 9f0c6da0c..2df132aea 100644 --- a/erigon-lib/go.mod +++ b/erigon-lib/go.mod @@ -4,7 +4,7 @@ go 1.20 require ( github.com/erigontech/mdbx-go v0.35.2-0.20231101074031-9f999220e9ed - github.com/ledgerwatch/erigon-snapshot v1.3.1-0.20231106151827-2653fe9782b2 + github.com/ledgerwatch/erigon-snapshot v1.3.1-0.20231106204511-f1e556dd5c50 github.com/ledgerwatch/interfaces v0.0.0-20231031050643-c86352e41520 github.com/ledgerwatch/log/v3 v3.9.0 github.com/ledgerwatch/secp256k1 v1.0.0 diff --git a/erigon-lib/go.sum b/erigon-lib/go.sum index ebe0a6632..f1b7cdf2f 100644 --- a/erigon-lib/go.sum +++ b/erigon-lib/go.sum @@ -291,8 +291,8 @@ github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/leanovate/gopter v0.2.9 h1:fQjYxZaynp97ozCzfOyOuAGOU4aU/z37zf/tOujFk7c= -github.com/ledgerwatch/erigon-snapshot v1.3.1-0.20231106151827-2653fe9782b2 h1:2av0zql2zwx8PL/mZWhjsuWOpHbDMK2tsiH003KriXs= -github.com/ledgerwatch/erigon-snapshot v1.3.1-0.20231106151827-2653fe9782b2/go.mod h1:3AuPxZc85jkehh/HA9h8gabv5MSi3kb/ddtzBsTVJFo= +github.com/ledgerwatch/erigon-snapshot v1.3.1-0.20231106204511-f1e556dd5c50 h1:RECb+fAC9doD1EhVxK2/b20JeLCAumLDjnysSQ3kWfs= +github.com/ledgerwatch/erigon-snapshot v1.3.1-0.20231106204511-f1e556dd5c50/go.mod h1:3AuPxZc85jkehh/HA9h8gabv5MSi3kb/ddtzBsTVJFo= github.com/ledgerwatch/interfaces v0.0.0-20231031050643-c86352e41520 h1:j/PRJWbPrbk8wpVjU77SWS8xJ/N+dcxPs1relNSolUs= github.com/ledgerwatch/interfaces v0.0.0-20231031050643-c86352e41520/go.mod h1:ugQv1QllJzBny3cKZKxUrSnykkjkBgm27eQM6dnGAcc= github.com/ledgerwatch/log/v3 v3.9.0 h1:iDwrXe0PVwBC68Dd94YSsHbMgQ3ufsgjzXtFNFVZFRk= diff --git a/erigon-lib/kv/tables.go b/erigon-lib/kv/tables.go index 995603bfa..d67d43db6 100644 --- a/erigon-lib/kv/tables.go +++ b/erigon-lib/kv/tables.go @@ -438,6 +438,10 @@ const ( // [Block Root] => [State Root] BlockRootToStateRoot = "BlockRootToStateRoot" StateRootToBlockRoot = "StateRootToBlockRoot" + + BlockRootToBlockNumber = "BlockRootToBlockNumber" + BlockRootToBlockHash = "BlockRootToBlockHash" + // [Block Root] => [Parent Root] BlockRootToParentRoot = "BlockRootToParentRoot" @@ -608,6 +612,8 @@ var ChaindataTables = []string{ Attestetations, LightClient, LightClientUpdates, + BlockRootToBlockHash, + BlockRootToBlockNumber, } const ( diff --git a/go.mod b/go.mod index 934bb0dd8..5b32e8f30 100644 --- a/go.mod +++ b/go.mod @@ -186,7 +186,7 @@ require ( github.com/koron/go-ssdp v0.0.4 // indirect github.com/kr/pretty v0.3.1 // indirect github.com/kr/text v0.2.0 // indirect - github.com/ledgerwatch/erigon-snapshot v1.3.1-0.20231106151827-2653fe9782b2 // indirect + github.com/ledgerwatch/erigon-snapshot v1.3.1-0.20231106204511-f1e556dd5c50 // indirect github.com/libp2p/go-buffer-pool v0.1.0 // indirect github.com/libp2p/go-cidranger v1.1.0 // indirect github.com/libp2p/go-flow-metrics v0.1.0 // indirect diff --git a/go.sum b/go.sum index 26825776a..f781d165a 100644 --- a/go.sum +++ b/go.sum @@ -539,8 +539,8 @@ github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/kylelemons/godebug v0.0.0-20170224010052-a616ab194758 h1:0D5M2HQSGD3PYPwICLl+/9oulQauOuETfgFvhBDffs0= github.com/leanovate/gopter v0.2.9 h1:fQjYxZaynp97ozCzfOyOuAGOU4aU/z37zf/tOujFk7c= github.com/leanovate/gopter v0.2.9/go.mod h1:U2L/78B+KVFIx2VmW6onHJQzXtFb+p5y3y2Sh+Jxxv8= -github.com/ledgerwatch/erigon-snapshot v1.3.1-0.20231106151827-2653fe9782b2 h1:2av0zql2zwx8PL/mZWhjsuWOpHbDMK2tsiH003KriXs= -github.com/ledgerwatch/erigon-snapshot v1.3.1-0.20231106151827-2653fe9782b2/go.mod h1:3AuPxZc85jkehh/HA9h8gabv5MSi3kb/ddtzBsTVJFo= +github.com/ledgerwatch/erigon-snapshot v1.3.1-0.20231106204511-f1e556dd5c50 h1:RECb+fAC9doD1EhVxK2/b20JeLCAumLDjnysSQ3kWfs= +github.com/ledgerwatch/erigon-snapshot v1.3.1-0.20231106204511-f1e556dd5c50/go.mod h1:3AuPxZc85jkehh/HA9h8gabv5MSi3kb/ddtzBsTVJFo= github.com/ledgerwatch/log/v3 v3.9.0 h1:iDwrXe0PVwBC68Dd94YSsHbMgQ3ufsgjzXtFNFVZFRk= github.com/ledgerwatch/log/v3 v3.9.0/go.mod h1:EiAY6upmI/6LkNhOVxb4eVsmsP11HZCnZ3PlJMjYiqE= github.com/ledgerwatch/secp256k1 v1.0.0 h1:Usvz87YoTG0uePIV8woOof5cQnLXGYa162rFf3YnwaQ=