Skip to content

Commit

Permalink
abstract tree
Browse files Browse the repository at this point in the history
  • Loading branch information
arnaubennassar committed Jul 31, 2024
1 parent c90f2d0 commit 4869094
Show file tree
Hide file tree
Showing 21 changed files with 719 additions and 693 deletions.
2 changes: 1 addition & 1 deletion bridgesync/bridgesync.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func new(
return nil, err
}
if lastProcessedBlock < initialBlock {
err = processor.ProcessBlock(sync.Block{
err = processor.ProcessBlock(ctx, sync.Block{
Num: initialBlock,
})
if err != nil {
Expand Down
55 changes: 20 additions & 35 deletions bridgesync/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import (
"encoding/binary"
"encoding/json"
"errors"
"log"
"math/big"
"path"

dbCommon "github.com/0xPolygon/cdk/common"
"github.com/0xPolygon/cdk/sync"
"github.com/0xPolygon/cdk/tree"
"github.com/ethereum/go-ethereum/common"
"github.com/iden3/go-iden3-crypto/keccak256"
"github.com/ledgerwatch/erigon-lib/kv"
Expand All @@ -19,8 +20,6 @@ import (
const (
eventsTableSufix = "-events"
lastBlockTableSufix = "-lastBlock"
rootTableSufix = "-root"
rhtTableSufix = "-rht"
)

var (
Expand Down Expand Up @@ -84,38 +83,34 @@ type processor struct {
db kv.RwDB
eventsTable string
lastBlockTable string
tree *tree
exitTree *tree.AppendOnlyTree
}

func newProcessor(ctx context.Context, dbPath, dbPrefix string) (*processor, error) {
eventsTable := dbPrefix + eventsTableSufix
lastBlockTable := dbPrefix + lastBlockTableSufix
rootTable := dbPrefix + rootTableSufix
rhtTable := dbPrefix + rhtTableSufix
db, err := mdbx.NewMDBX(nil).
Path(dbPath).
WithTableCfg(func(defaultBuckets kv.TableCfg) kv.TableCfg {
return kv.TableCfg{
eventsTable: {},
lastBlockTable: {},
rootTable: {},
rhtTable: {},
}
}).
Open()
if err != nil {
return nil, err
}

tree, err := newTree(ctx, rhtTable, rootTable, db)
exitTreeDBPath := path.Join(dbPath, "exittree")
exitTree, err := tree.NewAppendOnly(ctx, exitTreeDBPath, dbPrefix)
if err != nil {
return nil, err
}
return &processor{
db: db,
eventsTable: eventsTable,
lastBlockTable: lastBlockTable,
tree: tree,
exitTree: exitTree,
}, nil
}

Expand Down Expand Up @@ -181,8 +176,8 @@ func (p *processor) getLastProcessedBlockWithTx(tx kv.Tx) (uint64, error) {
}
}

func (p *processor) Reorg(firstReorgedBlock uint64) error {
tx, err := p.db.BeginRw(context.Background())
func (p *processor) Reorg(ctx context.Context, firstReorgedBlock uint64) error {
tx, err := p.db.BeginRw(ctx)
if err != nil {
return err
}
Expand Down Expand Up @@ -221,22 +216,15 @@ func (p *processor) Reorg(firstReorgedBlock uint64) error {
return err
}
if firstDepositCountReorged != -1 {
var lastValidDepositCount uint32
if firstDepositCountReorged == 0 {
lastValidDepositCount = 0
} else {
lastValidDepositCount = uint32(firstDepositCountReorged) - 1
}
if err := p.tree.reorg(tx, lastValidDepositCount); err != nil {
if err := p.exitTree.Reorg(ctx, uint32(firstDepositCountReorged)); err != nil {
tx.Rollback()
return err
}
}
return tx.Commit()
}

func (p *processor) ProcessBlock(block sync.Block) error {
ctx := context.Background()
func (p *processor) ProcessBlock(ctx context.Context, block sync.Block) error {
tx, err := p.db.BeginRw(ctx)
if err != nil {
return err
Expand Down Expand Up @@ -267,19 +255,16 @@ func (p *processor) ProcessBlock(block sync.Block) error {
return err
}

for i, bridge := range bridges {
if err := p.tree.addLeaf(tx, bridge.DepositCount, bridge.Hash()); err != nil {
if i != 0 {
tx.Rollback()
if err2 := p.tree.initLastLeftCacheAndLastDepositCount(ctx); err2 != nil {
log.Fatalf(
"after failing to add a leaf to the tree with error: %v, error initializing the cache with error: %v",
err, err2,
)
}
return err
}
}
leaves := []tree.Leaf{}
for _, bridge := range bridges {
leaves = append(leaves, tree.Leaf{
Index: bridge.DepositCount,
Hash: bridge.Hash(),
})
}
if err := p.exitTree.AddLeaves(ctx, leaves); err != nil {
tx.Rollback()
return err
}
return tx.Commit()
}
Expand Down
21 changes: 5 additions & 16 deletions bridgesync/processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"testing"

"github.com/0xPolygon/cdk/sync"
"github.com/0xPolygon/cdk/tree/testvectors"
"github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -391,7 +392,7 @@ func (a *reorgAction) desc() string {
}

func (a *reorgAction) execute(t *testing.T) {
actualErr := a.p.Reorg(a.firstReorgedBlock)
actualErr := a.p.Reorg(context.Background(), a.firstReorgedBlock)
require.Equal(t, a.expectedErr, actualErr)
}

Expand All @@ -413,7 +414,7 @@ func (a *processBlockAction) desc() string {
}

func (a *processBlockAction) execute(t *testing.T) {
actualErr := a.p.ProcessBlock(a.block)
actualErr := a.p.ProcessBlock(context.Background(), a.block)
require.Equal(t, a.expectedErr, actualErr)
}

Expand All @@ -425,23 +426,11 @@ func eventsToBridgeEvents(events []interface{}) []Event {
return bridgeEvents
}

// DepositVectorRaw represents the deposit vector
type DepositVectorRaw struct {
OriginalNetwork uint32 `json:"originNetwork"`
TokenAddress string `json:"tokenAddress"`
Amount string `json:"amount"`
DestinationNetwork uint32 `json:"destinationNetwork"`
DestinationAddress string `json:"destinationAddress"`
ExpectedHash string `json:"leafValue"`
CurrentHash string `json:"currentLeafValue"`
Metadata string `json:"metadata"`
}

func TestHashBridge(t *testing.T) {
data, err := os.ReadFile("testvectors/leaf-vectors.json")
data, err := os.ReadFile("../tree/testvectors/leaf-vectors.json")
require.NoError(t, err)

var leafVectors []DepositVectorRaw
var leafVectors []testvectors.DepositVectorRaw
err = json.Unmarshal(data, &leafVectors)
require.NoError(t, err)

Expand Down
Loading

0 comments on commit 4869094

Please sign in to comment.