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

feat(rollup-verifier): codecv4 #991

Merged
merged 20 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
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
41 changes: 38 additions & 3 deletions core/rawdb/accessors_rollup_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,19 @@ import (
)

// ChunkBlockRange represents the range of blocks within a chunk.
// for backward compatibility, new info is also stored in CommittedBatchMeta.
type ChunkBlockRange struct {
StartBlockNumber uint64
EndBlockNumber uint64
}

// CommittedBatchMeta holds metadata for committed batches.
type CommittedBatchMeta struct {
Version uint8
BlobVersionedHashes []common.Hash
ChunkBlockRanges []*ChunkBlockRange
}

// FinalizedBatchMeta holds metadata for finalized batches.
type FinalizedBatchMeta struct {
BatchHash common.Hash
Expand Down Expand Up @@ -91,13 +99,12 @@ func ReadBatchChunkRanges(db ethdb.Reader, batchIndex uint64) []*ChunkBlockRange

// WriteFinalizedBatchMeta stores the metadata of a finalized batch in the database.
func WriteFinalizedBatchMeta(db ethdb.KeyValueWriter, batchIndex uint64, finalizedBatchMeta *FinalizedBatchMeta) {
var err error
value, err := rlp.EncodeToBytes(finalizedBatchMeta)
if err != nil {
log.Crit("failed to RLP encode batch metadata", "batch index", batchIndex, "finalized batch meta", finalizedBatchMeta, "err", err)
log.Crit("failed to RLP encode finalized batch metadata", "batch index", batchIndex, "finalized batch meta", finalizedBatchMeta, "err", err)
}
if err := db.Put(batchMetaKey(batchIndex), value); err != nil {
log.Crit("failed to store batch metadata", "batch index", batchIndex, "value", value, "err", err)
log.Crit("failed to store finalized batch metadata", "batch index", batchIndex, "value", value, "err", err)
}
}

Expand Down Expand Up @@ -171,3 +178,31 @@ func ReadLastFinalizedBatchIndex(db ethdb.Reader) *uint64 {
lastFinalizedBatchIndex := number.Uint64()
return &lastFinalizedBatchIndex
}

// WriteCommittedBatchMeta stores the CommittedBatchMeta for a specific batch in the database.
func WriteCommittedBatchMeta(db ethdb.KeyValueWriter, batchIndex uint64, committedBatchMeta *CommittedBatchMeta) {
value, err := rlp.EncodeToBytes(committedBatchMeta)
if err != nil {
log.Crit("failed to RLP encode committed batch metadata", "batch index", batchIndex, "committed batch meta", committedBatchMeta, "err", err)
}
if err := db.Put(committedBatchMetaKey(batchIndex), value); err != nil {
log.Crit("failed to store committed batch metadata", "batch index", batchIndex, "value", value, "err", err)
}
}

// ReadCommittedBatchMeta fetches the CommittedBatchMeta for a specific batch from the database.
func ReadCommittedBatchMeta(db ethdb.Reader, batchIndex uint64) *CommittedBatchMeta {
data, err := db.Get(committedBatchMetaKey(batchIndex))
if err != nil && isNotFoundErr(err) {
return nil
}
if err != nil {
log.Crit("failed to read committed batch metadata from database", "batch index", batchIndex, "err", err)
}

cbm := new(CommittedBatchMeta)
if err := rlp.Decode(bytes.NewReader(data), cbm); err != nil {
log.Crit("Invalid CommittedBatchMeta RLP", "batch index", batchIndex, "data", data, "err", err)
}
return cbm
}
115 changes: 115 additions & 0 deletions core/rawdb/accessors_rollup_event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,3 +210,118 @@ func TestBatchChunkRanges(t *testing.T) {
// delete non-existing value: ensure the delete operation handles non-existing values without errors.
DeleteBatchChunkRanges(db, uint64(len(chunks)+1))
}

func TestWriteReadCommittedBatchMeta(t *testing.T) {
db := NewMemoryDatabase()

testCases := []struct {
batchIndex uint64
meta *CommittedBatchMeta
}{
{
batchIndex: 0,
meta: &CommittedBatchMeta{
Version: 0,
BlobVersionedHashes: []common.Hash{},
ChunkBlockRanges: []*ChunkBlockRange{},
},
},
{
batchIndex: 1,
meta: &CommittedBatchMeta{
Version: 1,
BlobVersionedHashes: []common.Hash{common.HexToHash("0x1234")},
ChunkBlockRanges: []*ChunkBlockRange{{StartBlockNumber: 0, EndBlockNumber: 10}},
},
},
{
batchIndex: 255,
meta: &CommittedBatchMeta{
Version: 255,
BlobVersionedHashes: []common.Hash{common.HexToHash("0xabcd"), common.HexToHash("0xef01")},
ChunkBlockRanges: []*ChunkBlockRange{{StartBlockNumber: 0, EndBlockNumber: 10}, {StartBlockNumber: 11, EndBlockNumber: 20}},
},
},
}

for _, tc := range testCases {
WriteCommittedBatchMeta(db, tc.batchIndex, tc.meta)
got := ReadCommittedBatchMeta(db, tc.batchIndex)

if got == nil {
t.Fatalf("Expected non-nil value for batch index %d", tc.batchIndex)
}

if !compareCommittedBatchMeta(tc.meta, got) {
t.Fatalf("CommittedBatchMeta mismatch for batch index %d, expected %+v, got %+v", tc.batchIndex, tc.meta, got)
}
}

// reading a non-existing value
if got := ReadCommittedBatchMeta(db, 256); got != nil {
t.Fatalf("Expected nil for non-existing value, got %+v", got)
}
}

func TestOverwriteCommittedBatchMeta(t *testing.T) {
db := NewMemoryDatabase()

batchIndex := uint64(42)
initialMeta := &CommittedBatchMeta{
Version: 1,
BlobVersionedHashes: []common.Hash{common.HexToHash("0x1234")},
ChunkBlockRanges: []*ChunkBlockRange{{StartBlockNumber: 0, EndBlockNumber: 10}},
}
newMeta := &CommittedBatchMeta{
Version: 2,
BlobVersionedHashes: []common.Hash{common.HexToHash("0x5678"), common.HexToHash("0x9abc")},
ChunkBlockRanges: []*ChunkBlockRange{{StartBlockNumber: 0, EndBlockNumber: 20}, {StartBlockNumber: 21, EndBlockNumber: 30}},
}

// write initial meta
WriteCommittedBatchMeta(db, batchIndex, initialMeta)
got := ReadCommittedBatchMeta(db, batchIndex)

if !compareCommittedBatchMeta(initialMeta, got) {
t.Fatalf("Initial write failed, expected %+v, got %+v", initialMeta, got)
}

// overwrite with new meta
WriteCommittedBatchMeta(db, batchIndex, newMeta)
got = ReadCommittedBatchMeta(db, batchIndex)

if !compareCommittedBatchMeta(newMeta, got) {
t.Fatalf("Overwrite failed, expected %+v, got %+v", newMeta, got)
}

// read non-existing batch index
nonExistingIndex := uint64(999)
got = ReadCommittedBatchMeta(db, nonExistingIndex)

if got != nil {
t.Fatalf("Expected nil for non-existing batch index, got %+v", got)
}
}

func compareCommittedBatchMeta(a, b *CommittedBatchMeta) bool {
if a.Version != b.Version {
return false
}
if len(a.BlobVersionedHashes) != len(b.BlobVersionedHashes) {
return false
}
for i := range a.BlobVersionedHashes {
if a.BlobVersionedHashes[i] != b.BlobVersionedHashes[i] {
return false
}
}
if len(a.ChunkBlockRanges) != len(b.ChunkBlockRanges) {
return false
}
for i := range a.ChunkBlockRanges {
if a.ChunkBlockRanges[i].StartBlockNumber != b.ChunkBlockRanges[i].StartBlockNumber || a.ChunkBlockRanges[i].EndBlockNumber != b.ChunkBlockRanges[i].EndBlockNumber {
return false
}
}
return true
}
6 changes: 6 additions & 0 deletions core/rawdb/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ var (
batchMetaPrefix = []byte("R-bm")
finalizedL2BlockNumberKey = []byte("R-finalized")
lastFinalizedBatchIndexKey = []byte("R-finalizedBatchIndex")
committedBatchMetaPrefix = []byte("R-cbm")

// Row consumption
rowConsumptionPrefix = []byte("rc") // rowConsumptionPrefix + hash -> row consumption by block
Expand Down Expand Up @@ -309,3 +310,8 @@ func batchChunkRangesKey(batchIndex uint64) []byte {
func batchMetaKey(batchIndex uint64) []byte {
return append(batchMetaPrefix, encodeBigEndian(batchIndex)...)
}

// committedBatchMetaKey = committedBatchMetaPrefix + batch index (uint64 big endian)
func committedBatchMetaKey(batchIndex uint64) []byte {
return append(committedBatchMetaPrefix, encodeBigEndian(batchIndex)...)
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ require (
github.com/prometheus/tsdb v0.7.1
github.com/rjeczalik/notify v0.9.1
github.com/rs/cors v1.7.0
github.com/scroll-tech/da-codec v0.1.1-0.20240718144756-1875fd490923
github.com/scroll-tech/da-codec v0.1.1-0.20240819100936-c6af3bbe7068
Thegaram marked this conversation as resolved.
Show resolved Hide resolved
github.com/scroll-tech/zktrie v0.8.4
github.com/shirou/gopsutil v3.21.11+incompatible
github.com/status-im/keycard-go v0.0.0-20190316090335-8537d3370df4
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -392,8 +392,8 @@ github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncj
github.com/rs/cors v1.7.0 h1:+88SsELBHx5r+hZ8TCkggzSstaWNbDvThkVK8H6f9ik=
github.com/rs/cors v1.7.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/scroll-tech/da-codec v0.1.1-0.20240718144756-1875fd490923 h1:A1ItzpnFDCHMh4g6cpeBZf7/fPf2lfwHbhjr/FSpk2w=
github.com/scroll-tech/da-codec v0.1.1-0.20240718144756-1875fd490923/go.mod h1:D6XEESeNVJkQJlv3eK+FyR+ufPkgVQbJzERylQi53Bs=
github.com/scroll-tech/da-codec v0.1.1-0.20240819100936-c6af3bbe7068 h1:KyTp4aedcpjr/rbntrmlhUxjrDYu1Q02QDLaF5vqpxs=
github.com/scroll-tech/da-codec v0.1.1-0.20240819100936-c6af3bbe7068/go.mod h1:D6XEESeNVJkQJlv3eK+FyR+ufPkgVQbJzERylQi53Bs=
github.com/scroll-tech/zktrie v0.8.4 h1:UagmnZ4Z3ITCk+aUq9NQZJNAwnWl4gSxsLb2Nl7IgRE=
github.com/scroll-tech/zktrie v0.8.4/go.mod h1:XvNo7vAk8yxNyTjBDj5WIiFzYW4bx/gJ78+NK6Zn6Uk=
github.com/segmentio/kafka-go v0.1.0/go.mod h1:X6itGqS9L4jDletMsxZ7Dz+JFWxM6JHfPOCvTvk+EJo=
Expand Down
Loading
Loading