Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added functional beacon snapshots reader and generator to Caplin #8570

Merged
merged 63 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
63 commits
Select commit Hold shift + click to select a range
98ec9c1
save
Giulio2002 Oct 23, 2023
92735f3
save
Giulio2002 Oct 23, 2023
624410c
save
Giulio2002 Oct 23, 2023
9e1f734
save
Giulio2002 Oct 23, 2023
aaefb41
ops
Giulio2002 Oct 23, 2023
c91f9fc
save
Giulio2002 Oct 23, 2023
bbbf0e1
save
Giulio2002 Oct 23, 2023
1589693
save
Giulio2002 Oct 23, 2023
84f49dc
save
Giulio2002 Oct 23, 2023
f2e3555
save
Giulio2002 Oct 23, 2023
d142f5b
save
Giulio2002 Oct 23, 2023
bbc33be
save
Giulio2002 Oct 23, 2023
6909830
save
Giulio2002 Oct 23, 2023
fc4a31c
save
Giulio2002 Oct 23, 2023
f9f62d1
save
Giulio2002 Oct 23, 2023
bd43167
save
Giulio2002 Oct 23, 2023
a6fbe3a
save
Giulio2002 Oct 23, 2023
87136e1
save
Giulio2002 Oct 23, 2023
6226245
save
Giulio2002 Oct 23, 2023
fc21ce9
better
Giulio2002 Oct 23, 2023
5c1fe96
save
Giulio2002 Oct 23, 2023
f42b7ee
save
Giulio2002 Oct 23, 2023
76791d2
save
Giulio2002 Oct 23, 2023
c39b2a2
save
Giulio2002 Oct 23, 2023
9a7e94d
save
Giulio2002 Oct 23, 2023
00e295c
save
Giulio2002 Oct 23, 2023
47f51c2
fixed?
Giulio2002 Oct 23, 2023
b8ea117
whoops
Giulio2002 Oct 23, 2023
d7a9fb6
log
Giulio2002 Oct 23, 2023
0c62756
save
Giulio2002 Oct 24, 2023
f24e736
save
Giulio2002 Oct 24, 2023
bdfb1fb
save
Giulio2002 Oct 24, 2023
d4630ca
save
Giulio2002 Oct 24, 2023
1733224
save
Giulio2002 Oct 24, 2023
03866ad
save
Giulio2002 Oct 24, 2023
611ab67
save
Giulio2002 Oct 24, 2023
709d065
save
Giulio2002 Oct 24, 2023
f622c52
save
Giulio2002 Oct 24, 2023
a8446a4
save
Giulio2002 Oct 24, 2023
06ac326
save
Giulio2002 Oct 24, 2023
9b3c73c
save
Giulio2002 Oct 24, 2023
a770ed5
save
Giulio2002 Oct 24, 2023
c001617
save
Giulio2002 Oct 24, 2023
177a4a4
save
Giulio2002 Oct 24, 2023
fa288d6
save
Giulio2002 Oct 24, 2023
d27dded
save
Giulio2002 Oct 24, 2023
b039ec2
save
Giulio2002 Oct 24, 2023
d956d67
save
Giulio2002 Oct 24, 2023
dbe7a08
no prints
Giulio2002 Oct 24, 2023
68d2a76
save
Giulio2002 Oct 24, 2023
0f649e2
save
Giulio2002 Oct 24, 2023
522a382
save
Giulio2002 Oct 24, 2023
92779a8
save
Giulio2002 Oct 24, 2023
ced23d7
save
Giulio2002 Oct 24, 2023
575cef3
save
Giulio2002 Oct 24, 2023
09a9395
save
Giulio2002 Oct 24, 2023
49a03ec
svae
Giulio2002 Oct 24, 2023
5b82911
save
Giulio2002 Oct 24, 2023
7d3d7b1
save
Giulio2002 Oct 24, 2023
f68c38b
save
Giulio2002 Oct 24, 2023
33f6dd2
save
Giulio2002 Oct 24, 2023
8f1ab65
save
Giulio2002 Oct 24, 2023
ba5ac0b
save
Giulio2002 Oct 24, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion cl/cltypes/beacon_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@ func NewBeaconBlock(beaconCfg *clparams.BeaconChainConfig) *BeaconBlock {
}

func NewBeaconBody(beaconCfg *clparams.BeaconChainConfig) *BeaconBody {
return &BeaconBody{beaconCfg: beaconCfg}
return &BeaconBody{
beaconCfg: beaconCfg,
}
}

// Version returns beacon block version.
Expand Down Expand Up @@ -172,6 +174,8 @@ func (b *BeaconBody) DecodeSSZ(buf []byte, version int) error {
return fmt.Errorf("[BeaconBody] err: %s", ssz.ErrLowBufferSize)
}

b.ExecutionPayload = NewEth1Block(b.Version, b.beaconCfg)

err := ssz2.UnmarshalSSZ(buf, version, b.getSchema(false)...)
return err
}
Expand Down
5 changes: 5 additions & 0 deletions cl/persistence/format/chunk_encoding/chunks.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ func ReadChunk(r io.Reader) (buf []byte, t DataType, err error) {
}
t = DataType(prefix[0])
prefix[0] = 0

bufLen := binary.BigEndian.Uint64(prefix)
if bufLen == 0 {
return
}
buf = make([]byte, binary.BigEndian.Uint64(prefix))
if _, err = r.Read(buf); err != nil {
return
Expand Down
79 changes: 59 additions & 20 deletions cl/persistence/format/snapshot_format/blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,25 +86,10 @@ func WriteBlockForSnapshot(block *cltypes.SignedBeaconBlock, w io.Writer) error
// count in body for phase0 fields
currentChunkLength += uint64(body.ProposerSlashings.EncodingSizeSSZ())
currentChunkLength += uint64(body.AttesterSlashings.EncodingSizeSSZ())

// Write the chunk and chunk attestations
if err := chunk_encoding.WriteChunk(w, encoded[:currentChunkLength], chunk_encoding.ChunkDataType); err != nil {
return err
}
encoded = encoded[currentChunkLength:]
snappyWriter := snappy.NewBufferedWriter(w)
if err := chunk_encoding.WriteChunk(snappyWriter, encoded[:uint64(body.Attestations.EncodingSizeSSZ())], chunk_encoding.ChunkDataType); err != nil {
return err
}
if err := snappyWriter.Close(); err != nil {
return err
}
encoded = encoded[body.Attestations.EncodingSizeSSZ():]
currentChunkLength = 0

currentChunkLength += uint64(body.Attestations.EncodingSizeSSZ())
currentChunkLength += uint64(body.Deposits.EncodingSizeSSZ())
currentChunkLength += uint64(body.VoluntaryExits.EncodingSizeSSZ())

// Write the chunk and chunk attestations
if err := chunk_encoding.WriteChunk(w, encoded[:currentChunkLength], chunk_encoding.ChunkDataType); err != nil {
return err
}
Expand Down Expand Up @@ -140,6 +125,60 @@ func ReadBlockFromSnapshot(r io.Reader, executionReader ExecutionBlockReaderByNu
return nil, err
}

// Read the first chunk
chunk1, dT1, err := chunk_encoding.ReadChunk(r)
if err != nil {
return nil, err
}
if dT1 != chunk_encoding.ChunkDataType {
return nil, fmt.Errorf("malformed beacon block, invalid chunk 1 type %d, expected: %d", dT1, chunk_encoding.ChunkDataType)
}
plainSSZ = append(plainSSZ, chunk1...)

if v <= clparams.AltairVersion {
return block, block.DecodeSSZ(plainSSZ, int(v))
}
// Read the block pointer and retrieve chunk4 from the execution reader
blockPointer, err := readExecutionBlockPtr(r)
if err != nil {
return nil, err
}
executionBlock, err := executionReader.BlockByNumber(blockPointer)
if err != nil {
return nil, err
}
// Read the 4th chunk
chunk2, err := executionBlock.EncodeSSZ(nil)
if err != nil {
return nil, err
}
plainSSZ = append(plainSSZ, chunk2...)
if v <= clparams.BellatrixVersion {
return block, block.DecodeSSZ(plainSSZ, int(v))
}

// Read the 5h chunk
chunk3, dT5, err := chunk_encoding.ReadChunk(r)
if err != nil {
return nil, err
}
if dT5 != chunk_encoding.ChunkDataType {
return nil, fmt.Errorf("malformed beacon block, invalid chunk 5 type %d, expected: %d", dT5, chunk_encoding.ChunkDataType)
}
plainSSZ = append(plainSSZ, chunk3...)

return block, block.DecodeSSZ(plainSSZ, int(v))
}

func ReadRawBlockFromSnapshot(r io.Reader, executionReader ExecutionBlockReaderByNumber, cfg *clparams.BeaconChainConfig) ([]byte, error) {
plainSSZ := []byte{}

// Metadata section is just the current hardfork of the block. TODO(give it a useful purpose)
v, err := readMetadataForBlock(r)
if err != nil {
return nil, err
}

// Read the first chunk
chunk1, dT1, err := chunk_encoding.ReadChunk(r)
if err != nil {
Expand Down Expand Up @@ -168,7 +207,7 @@ func ReadBlockFromSnapshot(r io.Reader, executionReader ExecutionBlockReaderByNu
}
plainSSZ = append(plainSSZ, chunk3...)
if v <= clparams.AltairVersion {
return block, block.DecodeSSZ(plainSSZ, int(v))
return plainSSZ, nil
}
// Read the block pointer and retrieve chunk4 from the execution reader
blockPointer, err := readExecutionBlockPtr(r)
Expand All @@ -186,7 +225,7 @@ func ReadBlockFromSnapshot(r io.Reader, executionReader ExecutionBlockReaderByNu
}
plainSSZ = append(plainSSZ, chunk4...)
if v <= clparams.BellatrixVersion {
return block, block.DecodeSSZ(plainSSZ, int(v))
return plainSSZ, nil
}

// Read the 5h chunk
Expand All @@ -199,5 +238,5 @@ func ReadBlockFromSnapshot(r io.Reader, executionReader ExecutionBlockReaderByNu
}
plainSSZ = append(plainSSZ, chunk5...)

return block, block.DecodeSSZ(plainSSZ, int(v))
return plainSSZ, nil
}
21 changes: 10 additions & 11 deletions cl/persistence/format/snapshot_format/blocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,15 @@ var capellaBlockSSZSnappy []byte
//go:embed test_data/deneb.ssz_snappy
var denebBlockSSZSnappy []byte

var emptyBlock = cltypes.NewSignedBeaconBlock(&clparams.MainnetBeaconConfig)

// obtain the test blocks
func getTestBlocks(t *testing.T) []*cltypes.SignedBeaconBlock {
var emptyBlockCapella = cltypes.NewSignedBeaconBlock(&clparams.MainnetBeaconConfig)
emptyBlockCapella.Block.Slot = clparams.MainnetBeaconConfig.CapellaForkEpoch * 32

emptyBlock.EncodingSizeSSZ()
emptyBlockCapella.EncodingSizeSSZ()
denebBlock := cltypes.NewSignedBeaconBlock(&clparams.MainnetBeaconConfig)
capellaBlock := cltypes.NewSignedBeaconBlock(&clparams.MainnetBeaconConfig)
bellatrixBlock := cltypes.NewSignedBeaconBlock(&clparams.MainnetBeaconConfig)
Expand All @@ -40,22 +47,14 @@ func getTestBlocks(t *testing.T) []*cltypes.SignedBeaconBlock {
require.NoError(t, utils.DecodeSSZSnappy(bellatrixBlock, bellatrixBlockSSZSnappy, int(clparams.BellatrixVersion)))
require.NoError(t, utils.DecodeSSZSnappy(altairBlock, altairBlockSSZSnappy, int(clparams.AltairVersion)))
require.NoError(t, utils.DecodeSSZSnappy(phase0Block, phase0BlockSSZSnappy, int(clparams.Phase0Version)))
return []*cltypes.SignedBeaconBlock{phase0Block, altairBlock, bellatrixBlock, capellaBlock, denebBlock}
}

type TestBlockReader struct {
Block *cltypes.Eth1Block
}

func (t *TestBlockReader) BlockByNumber(number uint64) (*cltypes.Eth1Block, error) {
return t.Block, nil
return []*cltypes.SignedBeaconBlock{phase0Block, altairBlock, bellatrixBlock, capellaBlock, denebBlock, emptyBlock, emptyBlockCapella}
}

func TestBlockSnapshotEncoding(t *testing.T) {
for _, blk := range getTestBlocks(t) {
var br TestBlockReader
var br snapshot_format.MockBlockReader
if blk.Version() >= clparams.BellatrixVersion {
br = TestBlockReader{Block: blk.Block.Body.ExecutionPayload}
br = snapshot_format.MockBlockReader{Block: blk.Block.Body.ExecutionPayload}
}
var b bytes.Buffer
require.NoError(t, snapshot_format.WriteBlockForSnapshot(blk, &b))
Expand Down
87 changes: 0 additions & 87 deletions cl/persistence/format/snapshot_format/snapshots.go

This file was deleted.

11 changes: 11 additions & 0 deletions cl/persistence/format/snapshot_format/test_util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package snapshot_format

import "github.com/ledgerwatch/erigon/cl/cltypes"

type MockBlockReader struct {
Block *cltypes.Eth1Block
}

func (t *MockBlockReader) BlockByNumber(number uint64) (*cltypes.Eth1Block, error) {
return t.Block, nil
}
25 changes: 24 additions & 1 deletion cl/spectest/consensus_tests/ssz_static.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package consensus_tests

import (
"github.com/ledgerwatch/erigon/spectest"
"bytes"
"io/fs"
"testing"

"github.com/ledgerwatch/erigon/spectest"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use _test.go for test files - then they will not compiled in binary


"github.com/ledgerwatch/erigon/cl/clparams"
"github.com/ledgerwatch/erigon/cl/cltypes"
"github.com/ledgerwatch/erigon/cl/persistence/format/snapshot_format"
"github.com/ledgerwatch/erigon/cl/phase1/core/state"

libcommon "github.com/ledgerwatch/erigon-lib/common"
Expand Down Expand Up @@ -59,6 +64,24 @@ func getSSZStaticConsensusTest[T unmarshalerMarshalerHashable](ref T) spectest.H
haveEncoded, err := object.EncodeSSZ(nil)
require.NoError(t, err)
require.EqualValues(t, haveEncoded, encoded)
// Now let it do the encoding in snapshot format
if blk, ok := object.(*cltypes.SignedBeaconBlock); ok {
var b bytes.Buffer
require.NoError(t, snapshot_format.WriteBlockForSnapshot(blk, &b))
var br snapshot_format.MockBlockReader
if blk.Version() >= clparams.BellatrixVersion {
br = snapshot_format.MockBlockReader{Block: blk.Block.Body.ExecutionPayload}

}

blk2, err := snapshot_format.ReadBlockFromSnapshot(&b, &br, &clparams.MainnetBeaconConfig)
require.NoError(t, err)

haveRoot, err := blk2.HashSSZ()
require.NoError(t, err)
require.EqualValues(t, expectedRoot, haveRoot)
}

return nil
})
}
Loading
Loading