Skip to content

Commit

Permalink
Merge pull request #17 from 0xPolygon/feat/reorg-detector
Browse files Browse the repository at this point in the history
feat: Reorg detector
  • Loading branch information
goran-ethernal committed Jul 24, 2024
2 parents cae791d + b8e0804 commit a4bf5f8
Show file tree
Hide file tree
Showing 7 changed files with 1,040 additions and 156 deletions.
16 changes: 16 additions & 0 deletions common/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package common

import "encoding/binary"

// BlockNum2Bytes converts a block number to a byte slice
func BlockNum2Bytes(blockNum uint64) []byte {
key := make([]byte, 8)
binary.LittleEndian.PutUint64(key, blockNum)

return key
}

// Bytes2BlockNum converts a byte slice to a block number
func Bytes2BlockNum(key []byte) uint64 {
return binary.LittleEndian.Uint64(key)
}
5 changes: 3 additions & 2 deletions localbridgesync/downloader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/0xPolygon/cdk-contracts-tooling/contracts/etrog/polygonzkevmbridge"
"github.com/0xPolygon/cdk-contracts-tooling/contracts/etrog/polygonzkevmbridgev2"
cdkcommon "github.com/0xPolygon/cdk/common"
"github.com/0xPolygon/cdk/etherman"
"github.com/0xPolygon/cdk/log"
"github.com/ethereum/go-ethereum"
Expand Down Expand Up @@ -203,7 +204,7 @@ func generateBridge(t *testing.T, blockNum uint32) (*types.Log, Bridge) {
log := &types.Log{
Address: contractAddr,
BlockNumber: uint64(blockNum),
BlockHash: common.BytesToHash(blockNum2Bytes(uint64(blockNum))),
BlockHash: common.BytesToHash(cdkcommon.BlockNum2Bytes(uint64(blockNum))),
Topics: []common.Hash{bridgeEventSignature},
Data: data,
}
Expand Down Expand Up @@ -262,7 +263,7 @@ func generateClaim(t *testing.T, blockNum uint32, event *abi.Event, isV1 bool) (
log := &types.Log{
Address: contractAddr,
BlockNumber: uint64(blockNum),
BlockHash: common.BytesToHash(blockNum2Bytes(uint64(blockNum))),
BlockHash: common.BytesToHash(cdkcommon.BlockNum2Bytes(uint64(blockNum))),
Topics: []common.Hash{signature},
Data: data,
}
Expand Down
27 changes: 10 additions & 17 deletions localbridgesync/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package localbridgesync

import (
"context"
"encoding/binary"
"encoding/json"
"errors"

"github.com/0xPolygon/cdk/common"
"github.com/ledgerwatch/erigon-lib/kv"
"github.com/ledgerwatch/erigon-lib/kv/mdbx"
)
Expand Down Expand Up @@ -57,6 +57,9 @@ func (p *processor) GetClaimsAndBridges(
}
defer tx.Rollback()
lpb, err := p.getLastProcessedBlockWithTx(tx)
if err != nil {
return nil, err
}
if lpb < toBlock {
return nil, ErrBlockNotProcessed
}
Expand All @@ -66,11 +69,11 @@ func (p *processor) GetClaimsAndBridges(
}
defer c.Close()

for k, v, err := c.Seek(blockNum2Bytes(fromBlock)); k != nil; k, v, err = c.Next() {
for k, v, err := c.Seek(common.BlockNum2Bytes(fromBlock)); k != nil; k, v, err = c.Next() {
if err != nil {
return nil, err
}
if bytes2BlockNum(k) > toBlock {
if common.Bytes2BlockNum(k) > toBlock {
break
}
blockEvents := []BridgeEvent{}
Expand Down Expand Up @@ -99,7 +102,7 @@ func (p *processor) getLastProcessedBlockWithTx(tx kv.Tx) (uint64, error) {
} else if blockNumBytes == nil {
return 0, nil
} else {
return bytes2BlockNum(blockNumBytes), nil
return common.Bytes2BlockNum(blockNumBytes), nil
}
}

Expand All @@ -113,7 +116,7 @@ func (p *processor) reorg(firstReorgedBlock uint64) error {
return err
}
defer c.Close()
firstKey := blockNum2Bytes(firstReorgedBlock)
firstKey := common.BlockNum2Bytes(firstReorgedBlock)
for k, _, err := c.Seek(firstKey); k != nil; k, _, err = c.Next() {
if err != nil {
tx.Rollback()
Expand Down Expand Up @@ -142,7 +145,7 @@ func (p *processor) storeBridgeEvents(blockNum uint64, events []BridgeEvent) err
tx.Rollback()
return err
}
if err := tx.Put(eventsTable, blockNum2Bytes(blockNum), value); err != nil {
if err := tx.Put(eventsTable, common.BlockNum2Bytes(blockNum), value); err != nil {
tx.Rollback()
return err
}
Expand All @@ -155,16 +158,6 @@ func (p *processor) storeBridgeEvents(blockNum uint64, events []BridgeEvent) err
}

func (p *processor) updateLastProcessedBlock(tx kv.RwTx, blockNum uint64) error {
blockNumBytes := blockNum2Bytes(blockNum)
blockNumBytes := common.BlockNum2Bytes(blockNum)
return tx.Put(lastBlockTable, lastBlokcKey, blockNumBytes)
}

func blockNum2Bytes(blockNum uint64) []byte {
key := make([]byte, 8)
binary.LittleEndian.PutUint64(key, blockNum)
return key
}

func bytes2BlockNum(key []byte) uint64 {
return binary.LittleEndian.Uint64(key)
}
89 changes: 89 additions & 0 deletions reorgdetector/mock_eth_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit a4bf5f8

Please sign in to comment.