Skip to content

Commit

Permalink
wip(etrog): add l1inforoot from seq, fix db tests
Browse files Browse the repository at this point in the history
  • Loading branch information
revitteth committed Jan 19, 2024
1 parent 63e8198 commit 1496ac9
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 48 deletions.
1 change: 1 addition & 0 deletions core/state/intra_block_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (
type ReadOnlyHermezDb interface {
GetEffectiveGasPricePercentage(txHash libcommon.Hash) (uint8, error)
GetStateRoot(l2BlockNo uint64) (libcommon.Hash, error)
GetL1InfoRoot(l2BlockNo uint64) (libcommon.Hash, error)
}

type revision struct {
Expand Down
57 changes: 37 additions & 20 deletions zk/hermez_db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,18 +216,23 @@ func (db *HermezDbReader) getByL1Block(table string, l1BlockNo uint64) (*types.L
}

if l1Block == l1BlockNo {
if len(v) != 64 {
if len(v) != 96 && len(v) != 64 {
return nil, fmt.Errorf("invalid hash length")
}

l1TxHash := common.BytesToHash(v[:32])
stateRoot := common.BytesToHash(v[32:])
stateRoot := common.BytesToHash(v[32:64])
var l1InfoRoot common.Hash
if len(v) > 64 {
l1InfoRoot = common.BytesToHash(v[64:])
}

return &types.L1BatchInfo{
BatchNo: batchNo,
L1BlockNo: l1Block,
StateRoot: stateRoot,
L1TxHash: l1TxHash,
BatchNo: batchNo,
L1BlockNo: l1Block,
StateRoot: stateRoot,
L1TxHash: l1TxHash,
L1InfoRoot: l1InfoRoot,
}, nil
}
}
Expand All @@ -254,18 +259,23 @@ func (db *HermezDbReader) getByBatchNo(table string, batchNo uint64) (*types.L1B
}

if batch == batchNo {
if len(v) != 64 {
if len(v) != 96 && len(v) != 64 {
return nil, fmt.Errorf("invalid hash length")
}

l1TxHash := common.BytesToHash(v[:32])
stateRoot := common.BytesToHash(v[32:])
stateRoot := common.BytesToHash(v[32:64])
var l1InfoRoot common.Hash
if len(v) > 64 {
l1InfoRoot = common.BytesToHash(v[64:])
}

return &types.L1BatchInfo{
BatchNo: batchNo,
L1BlockNo: l1Block,
StateRoot: stateRoot,
L1TxHash: l1TxHash,
BatchNo: batchNo,
L1BlockNo: l1Block,
StateRoot: stateRoot,
L1TxHash: l1TxHash,
L1InfoRoot: l1InfoRoot,
}, nil
}
}
Expand Down Expand Up @@ -298,23 +308,30 @@ func (db *HermezDbReader) getLatest(table string) (*types.L1BatchInfo, error) {
return nil, err
}

if len(v) != 64 {
if len(v) != 96 && len(v) != 64 {
return nil, fmt.Errorf("invalid hash length")
}

l1TxHash := common.BytesToHash(v[:32])
stateRoot := common.BytesToHash(v[32:])
stateRoot := common.BytesToHash(v[32:64])
var l1InfoRoot common.Hash
if len(v) > 64 {
l1InfoRoot = common.BytesToHash(v[64:])
}

return &types.L1BatchInfo{
BatchNo: batchNo,
L1BlockNo: l1BlockNo,
L1TxHash: l1TxHash,
StateRoot: stateRoot,
BatchNo: batchNo,
L1BlockNo: l1BlockNo,
L1TxHash: l1TxHash,
StateRoot: stateRoot,
L1InfoRoot: l1InfoRoot,
}, nil
}

func (db *HermezDb) WriteSequence(l1BlockNo, batchNo uint64, l1TxHash common.Hash, stateRoot common.Hash) error {
return db.tx.Put(L1SEQUENCES, ConcatKey(l1BlockNo, batchNo), append(l1TxHash.Bytes(), stateRoot.Bytes()...))
func (db *HermezDb) WriteSequence(l1BlockNo, batchNo uint64, l1TxHash, stateRoot, l1BlockInfo common.Hash) error {
val := append(l1TxHash.Bytes(), stateRoot.Bytes()...)
val = append(val, l1BlockInfo.Bytes()...)
return db.tx.Put(L1SEQUENCES, ConcatKey(l1BlockNo, batchNo), val)
}

func (db *HermezDb) WriteVerification(l1BlockNo, batchNo uint64, l1TxHash common.Hash, stateRoot common.Hash) error {
Expand Down
51 changes: 31 additions & 20 deletions zk/hermez_db/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

type IHermezDb interface {
WriteSequence(uint64, uint64, common.Hash, common.Hash) error
WriteSequence(uint64, uint64, common.Hash, common.Hash, common.Hash) error
WriteVerification(uint64, uint64, common.Hash, common.Hash) error
}

Expand All @@ -26,6 +26,11 @@ func GetDbTx() (tx kv.RwTx, cleanup func()) {
panic(err)
}

err = CreateHermezBuckets(tx)
if err != nil {
panic(err)
}

return tx, func() {
tx.Rollback()
dbi.Close()
Expand All @@ -46,8 +51,8 @@ func TestGetSequenceByL1Block(t *testing.T) {
db, err := NewHermezDb(tx)
require.NoError(t, err)

require.NoError(t, db.WriteSequence(1, 1001, common.HexToHash("0xabc"), common.HexToHash("0xabc")))
require.NoError(t, db.WriteSequence(2, 1002, common.HexToHash("0xdef"), common.HexToHash("0xdef")))
require.NoError(t, db.WriteSequence(1, 1001, common.HexToHash("0xabc"), common.HexToHash("0xabc"), common.HexToHash("0xabc")))
require.NoError(t, db.WriteSequence(2, 1002, common.HexToHash("0xdef"), common.HexToHash("0xdef"), common.HexToHash("0xdef")))

info, err := db.GetSequenceByL1Block(1)
require.NoError(t, err)
Expand All @@ -68,8 +73,8 @@ func TestGetSequenceByBatchNo(t *testing.T) {
db, err := NewHermezDb(tx)
require.NoError(t, err)

require.NoError(t, db.WriteSequence(1, 1001, common.HexToHash("0xabc"), common.HexToHash("0xabcd")))
require.NoError(t, db.WriteSequence(2, 1002, common.HexToHash("0xdef"), common.HexToHash("0xdefg")))
require.NoError(t, db.WriteSequence(1, 1001, common.HexToHash("0xabc"), common.HexToHash("0xabcd"), common.HexToHash("0xabcde")))
require.NoError(t, db.WriteSequence(2, 1002, common.HexToHash("0xdef"), common.HexToHash("0xdefg"), common.HexToHash("0xdefgh")))

info, err := db.GetSequenceByBatchNo(1001)
require.NoError(t, err)
Expand Down Expand Up @@ -113,26 +118,32 @@ func TestGetVerificationByL1BlockAndBatchNo(t *testing.T) {
func TestGetAndSetLatest(t *testing.T) {

testCases := []struct {
desc string
table string
writeMethod func(IHermezDb, uint64, uint64, common.Hash, common.Hash) error
l1BlockNo uint64
batchNo uint64
l1TxHashBytes common.Hash
stateRoot common.Hash
desc string
table string
writeSequenceMethod func(IHermezDb, uint64, uint64, common.Hash, common.Hash, common.Hash) error
writeVerificationMethod func(IHermezDb, uint64, uint64, common.Hash, common.Hash) error
l1BlockNo uint64
batchNo uint64
l1TxHashBytes common.Hash
stateRoot common.Hash
l1InfoRoot common.Hash
}{
{"sequence 1", L1SEQUENCES, IHermezDb.WriteSequence, 1, 1001, common.HexToHash("0xabc"), common.HexToHash("0xabc")},
{"sequence 2", L1SEQUENCES, IHermezDb.WriteSequence, 2, 1002, common.HexToHash("0xdef"), common.HexToHash("0xdef")},
{"verification 1", L1VERIFICATIONS, IHermezDb.WriteVerification, 3, 1003, common.HexToHash("0xghi"), common.HexToHash("0xghi")},
{"verification 2", L1VERIFICATIONS, IHermezDb.WriteVerification, 4, 1004, common.HexToHash("0xjkl"), common.HexToHash("0xjkl")},
{"sequence 1", L1SEQUENCES, IHermezDb.WriteSequence, IHermezDb.WriteVerification, 1, 1001, common.HexToHash("0xabc"), common.HexToHash("0xabc"), common.HexToHash("0xabc")},
{"sequence 2", L1SEQUENCES, IHermezDb.WriteSequence, IHermezDb.WriteVerification, 2, 1002, common.HexToHash("0xdef"), common.HexToHash("0xdef"), common.HexToHash("0xdef")},
{"verification 1", L1VERIFICATIONS, IHermezDb.WriteSequence, IHermezDb.WriteVerification, 3, 1003, common.HexToHash("0xghi"), common.HexToHash("0xghi"), common.HexToHash("0xghi")},
{"verification 2", L1VERIFICATIONS, IHermezDb.WriteSequence, IHermezDb.WriteVerification, 4, 1004, common.HexToHash("0xjkl"), common.HexToHash("0xjkl"), common.HexToHash("0xjkl")},
}

for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
tx, cleanup := GetDbTx()
db, err := NewHermezDb(tx)
require.NoError(t, err)
err = tc.writeMethod(db, tc.l1BlockNo, tc.batchNo, tc.l1TxHashBytes, tc.stateRoot)
if tc.table == L1SEQUENCES {
err = tc.writeSequenceMethod(db, tc.l1BlockNo, tc.batchNo, tc.l1TxHashBytes, tc.stateRoot, tc.l1InfoRoot)
} else {
err = tc.writeVerificationMethod(db, tc.l1BlockNo, tc.batchNo, tc.l1TxHashBytes, tc.stateRoot)
}
assert.Nil(t, err)

info, err := db.getLatest(tc.table)
Expand Down Expand Up @@ -298,7 +309,7 @@ func BenchmarkWriteSequence(b *testing.B) {
b.ResetTimer()

for i := 0; i < b.N; i++ {
err := db.WriteSequence(uint64(i), uint64(i+1000), common.HexToHash("0xabc"), common.HexToHash("0xabc"))
err := db.WriteSequence(uint64(i), uint64(i+1000), common.HexToHash("0xabc"), common.HexToHash("0xabc"), common.HexToHash("0xabc"))
if err != nil {
b.Fatal(err)
}
Expand Down Expand Up @@ -328,7 +339,7 @@ func BenchmarkGetSequenceByL1Block(b *testing.B) {
require.NoError(b, err)

for i := 0; i < 1000; i++ {
err := db.WriteSequence(uint64(i), uint64(i+1000), common.HexToHash("0xabc"), common.HexToHash("0xabc"))
err := db.WriteSequence(uint64(i), uint64(i+1000), common.HexToHash("0xabc"), common.HexToHash("0xabc"), common.HexToHash("0xabc"))
if err != nil {
b.Fatal(err)
}
Expand Down Expand Up @@ -374,7 +385,7 @@ func BenchmarkGetSequenceByBatchNo(b *testing.B) {
require.NoError(b, err)

for i := 0; i < 1000; i++ {
err := db.WriteSequence(uint64(i), uint64(i+1000), common.HexToHash("0xabc"), common.HexToHash("0xabc"))
err := db.WriteSequence(uint64(i), uint64(i+1000), common.HexToHash("0xabc"), common.HexToHash("0xabc"), common.HexToHash("0xabc"))
if err != nil {
b.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion zk/stages/stage_l1syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ Loop:
}
newVerificationsCount++
case sequence := <-sequencesChan:
err = hermezDb.WriteSequence(sequence.L1BlockNo, sequence.BatchNo, sequence.L1TxHash, sequence.StateRoot)
err = hermezDb.WriteSequence(sequence.L1BlockNo, sequence.BatchNo, sequence.L1TxHash, sequence.StateRoot, sequence.L1InfoRoot)
if err != nil {
return fmt.Errorf("failed to write batch info, %w", err)
}
Expand Down
8 changes: 5 additions & 3 deletions zk/syncer/l1_syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,11 +229,13 @@ loop:
func convertResultToBatchInfo(log *ethTypes.Log) types.L1BatchInfo {
batchNumber := new(big.Int).SetBytes(log.Topics[1].Bytes())
l1TxHash := common.BytesToHash(log.TxHash.Bytes())
l1InfoRoot := common.BytesToHash(log.Data)
blockNumber := log.BlockNumber
return types.L1BatchInfo{
BatchNo: batchNumber.Uint64(),
L1BlockNo: blockNumber,
L1TxHash: l1TxHash,
BatchNo: batchNumber.Uint64(),
L1BlockNo: blockNumber,
L1TxHash: l1TxHash,
L1InfoRoot: l1InfoRoot,
}
}

Expand Down
9 changes: 5 additions & 4 deletions zk/types/zk_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ const EFFECTIVE_GAS_PRICE_PERCENTAGE_DISABLED = 0
var EFFECTIVE_GAS_PRICE_MAX_VAL = new(uint256.Int).SetUint64(256)

type L1BatchInfo struct {
BatchNo uint64
L1BlockNo uint64
L1TxHash common.Hash
StateRoot common.Hash
BatchNo uint64
L1BlockNo uint64
L1TxHash common.Hash
StateRoot common.Hash
L1InfoRoot common.Hash
}

// Batch struct
Expand Down

0 comments on commit 1496ac9

Please sign in to comment.