Skip to content

Commit

Permalink
Implement base addLeaf
Browse files Browse the repository at this point in the history
  • Loading branch information
arnaubennassar committed Jul 29, 2024
1 parent d88238b commit fc3df5e
Show file tree
Hide file tree
Showing 6 changed files with 169 additions and 31 deletions.
66 changes: 59 additions & 7 deletions localbridgesync/localbridgesync.go → bridgesync/bridgesync.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package localbridgesync
package bridgesync

import (
"context"
Expand All @@ -10,7 +10,10 @@ import (
)

const (
reorgDetectorID = "localbridgesync"
reorgDetectorIDL1 = "bridgesyncl1"
reorgDetectorIDL2 = "bridgesyncl2"
dbPrefixL1 = "bridgesyncl1"
dbPrefixL2 = "bridgesyncl2"
downloadBufferSize = 1000
)

Expand All @@ -24,17 +27,66 @@ type LocalBridgeSync struct {
driver *sync.EVMDriver
}

func New(
func NewL1(
ctx context.Context,
dbPath string,
bridge common.Address,
syncBlockChunkSize uint64,
blockFinalityType etherman.BlockNumberFinality,
rd sync.ReorgDetector,
l2Client EthClienter,
ethClient EthClienter,
initialBlock uint64,
) (*LocalBridgeSync, error) {
processor, err := newProcessor(dbPath)
return new(
ctx,
dbPath,
bridge,
syncBlockChunkSize,
blockFinalityType,
rd,
ethClient,
initialBlock,
dbPrefixL1,
reorgDetectorIDL1,
)
}

func NewL2(
ctx context.Context,
dbPath string,
bridge common.Address,
syncBlockChunkSize uint64,
blockFinalityType etherman.BlockNumberFinality,
rd sync.ReorgDetector,
ethClient EthClienter,
initialBlock uint64,
) (*LocalBridgeSync, error) {
return new(
ctx,
dbPath,
bridge,
syncBlockChunkSize,
blockFinalityType,
rd,
ethClient,
initialBlock,
dbPrefixL1,
reorgDetectorIDL1,
)
}

func new(
ctx context.Context,
dbPath string,
bridge common.Address,
syncBlockChunkSize uint64,
blockFinalityType etherman.BlockNumberFinality,
rd sync.ReorgDetector,
ethClient EthClienter,
initialBlock uint64,
dbPrefix, reorgDetectorID string,
) (*LocalBridgeSync, error) {
processor, err := newProcessor(dbPath, dbPrefix)
if err != nil {
return nil, err
}
Expand All @@ -51,12 +103,12 @@ func New(
}
}

appender, err := buildAppender(l2Client, bridge)
appender, err := buildAppender(ethClient, bridge)
if err != nil {
return nil, err
}
downloader, err := sync.NewEVMDownloader(
l2Client,
ethClient,
syncBlockChunkSize,
blockFinalityType,
waitForNewBlocksPeriod,
Expand Down
2 changes: 1 addition & 1 deletion localbridgesync/downloader.go → bridgesync/downloader.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package localbridgesync
package bridgesync

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion localbridgesync/e2e_test.go → bridgesync/e2e_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package localbridgesync
package bridgesync

// TODO: add E2E test, prolly need a mock contract
44 changes: 24 additions & 20 deletions localbridgesync/processor.go → bridgesync/processor.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package localbridgesync
package bridgesync

import (
"context"
Expand All @@ -14,8 +14,8 @@ import (
)

const (
eventsTable = "localbridgesync-events"
lastBlockTable = "localbridgesync-lastBlock"
eventsTableSufix = "-events"
lastBlockTableSufix = "-lastBlock"
)

var (
Expand Down Expand Up @@ -48,26 +48,30 @@ type Event struct {
}

type processor struct {
db kv.RwDB
db kv.RwDB
eventsTable string
lastBlockTable string
}

func tableCfgFunc(defaultBuckets kv.TableCfg) kv.TableCfg {
return kv.TableCfg{
eventsTable: {},
lastBlockTable: {},
}
}

func newProcessor(dbPath string) (*processor, error) {
func newProcessor(dbPath, dbPrefix string) (*processor, error) {
eventsTable := dbPrefix + eventsTableSufix
lastBlockTable := dbPrefix + lastBlockTableSufix
db, err := mdbx.NewMDBX(nil).
Path(dbPath).
WithTableCfg(tableCfgFunc).
WithTableCfg(func(defaultBuckets kv.TableCfg) kv.TableCfg {
return kv.TableCfg{
eventsTable: {},
lastBlockTable: {},
}
}).
Open()
if err != nil {
return nil, err
}
return &processor{
db: db,
db: db,
eventsTable: eventsTable,
lastBlockTable: lastBlockTable,
}, nil
}

Expand All @@ -90,7 +94,7 @@ func (p *processor) GetClaimsAndBridges(
if lpb < toBlock {
return nil, ErrBlockNotProcessed
}
c, err := tx.Cursor(eventsTable)
c, err := tx.Cursor(p.eventsTable)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -124,7 +128,7 @@ func (p *processor) GetLastProcessedBlock(ctx context.Context) (uint64, error) {
}

func (p *processor) getLastProcessedBlockWithTx(tx kv.Tx) (uint64, error) {
if blockNumBytes, err := tx.GetOne(lastBlockTable, lastBlokcKey); err != nil {
if blockNumBytes, err := tx.GetOne(p.lastBlockTable, lastBlokcKey); err != nil {
return 0, err
} else if blockNumBytes == nil {
return 0, nil
Expand All @@ -138,7 +142,7 @@ func (p *processor) Reorg(firstReorgedBlock uint64) error {
if err != nil {
return err
}
c, err := tx.Cursor(eventsTable)
c, err := tx.Cursor(p.eventsTable)
if err != nil {
return err
}
Expand All @@ -149,7 +153,7 @@ func (p *processor) Reorg(firstReorgedBlock uint64) error {
tx.Rollback()
return err
}
if err := tx.Delete(eventsTable, k); err != nil {
if err := tx.Delete(p.eventsTable, k); err != nil {
tx.Rollback()
return err
}
Expand All @@ -176,7 +180,7 @@ func (p *processor) ProcessBlock(block sync.Block) error {
tx.Rollback()
return err
}
if err := tx.Put(eventsTable, common.Uint64To2Bytes(block.Num), value); err != nil {
if err := tx.Put(p.eventsTable, common.Uint64To2Bytes(block.Num), value); err != nil {
tx.Rollback()
return err
}
Expand All @@ -190,5 +194,5 @@ func (p *processor) ProcessBlock(block sync.Block) error {

func (p *processor) updateLastProcessedBlock(tx kv.RwTx, blockNum uint64) error {
blockNumBytes := common.Uint64To2Bytes(blockNum)
return tx.Put(lastBlockTable, lastBlokcKey, blockNumBytes)
return tx.Put(p.lastBlockTable, lastBlokcKey, blockNumBytes)
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package localbridgesync
package bridgesync

import (
"context"
Expand All @@ -14,7 +14,7 @@ import (

func TestProceessor(t *testing.T) {
path := t.TempDir()
p, err := newProcessor(path)
p, err := newProcessor(path, "foo")
require.NoError(t, err)
actions := []processAction{
// processed: ~
Expand Down
82 changes: 82 additions & 0 deletions bridgesync/tree.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package bridgesync

import (
"errors"
"fmt"

"github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon-lib/kv"
"golang.org/x/crypto/sha3"
)

type tree struct {
db kv.RwDB
lastIndex int64
height uint8
rightPathCache []common.Hash
}

type treeNode struct {
left common.Hash
right common.Hash
}

func (n *treeNode) hash() common.Hash {
var hash common.Hash
hasher := sha3.NewLegacyKeccak256()
hasher.Write(n.left[:])
hasher.Write(n.right[:])
copy(hash[:], hasher.Sum(nil))
return hash
}

func newTree() (*tree, error) {
// TODO: init lastIndex & rightPathCache
return &tree{}, errors.New("not implemented")
}

func (t *tree) addLeaf(index uint, hash common.Hash) error {
if int64(index) != t.lastIndex+1 {
return fmt.Errorf("mismatched index. Expected: %d, actual: %d", t.lastIndex+1, index)
}

currentChildHash := hash
leftIsFilled := true
newNodes := []treeNode{}
for h := uint8(0); h < t.height; h++ {
var parent treeNode
if index&(1<<h) > 0 {
// Add child to the right
var child common.Hash
copy(child[:], currentChildHash[:])
parent = treeNode{
left: t.rightPathCache[h],
right: child,
}
} else {
// Add child to the left
if leftIsFilled {
// if at this level the left is filled, it means that the new node will be in the right path
copy(t.rightPathCache[h][:], currentChildHash[:])
leftIsFilled = false
}
var child common.Hash
copy(child[:], currentChildHash[:])
parent = treeNode{
left: child,
right: common.Hash{},
}
}
currentChildHash = parent.hash()
newNodes = append(newNodes, parent)
}

// store root
root := currentChildHash

Check failure on line 75 in bridgesync/tree.go

View workflow job for this annotation

GitHub Actions / test-unittest

root declared and not used

Check failure on line 75 in bridgesync/tree.go

View workflow job for this annotation

GitHub Actions / test-unittest

root declared and not used
// store nodes

t.lastIndex++
return nil
}

// TODO: handle rerog: lastIndex & rightPathCache

0 comments on commit fc3df5e

Please sign in to comment.