Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/bugfix/polygon-snapshot-input' i…
Browse files Browse the repository at this point in the history
…nto feature/archive
  • Loading branch information
philip-morlier committed Apr 17, 2024
2 parents d9e4b61 + b038f0a commit b248290
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 75 deletions.
6 changes: 3 additions & 3 deletions plugins/packages/polygon/eth_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ func (service *PolygonEthService) GetTransactionReceiptsByBlock(ctx context.Cont
gtrbbMissMeter.Mark(1)
responseShell, err := heavy.CallHeavy[[]map[string]interface{}](ctx, service.cfg.HeavyServer, "eth_getTransactionReceiptsByBlock", number)
if err != nil {
return nil, err
return nil, errBlockNotFound
}
return *responseShell, nil
}
Expand All @@ -326,7 +326,7 @@ func (service *PolygonEthService) GetTransactionReceiptsByBlock(ctx context.Cont
gtrbbMissMeter.Mark(1)
responseShell, err := heavy.CallHeavy[[]map[string]interface{}](ctx, service.cfg.HeavyServer, "eth_getTransactionReceiptsByBlock", hash)
if err != nil {
return nil, err
return nil, errBlockHashNotFound
}
return *responseShell, nil
}
Expand All @@ -350,7 +350,7 @@ func (service *PolygonEthService) GetTransactionReceiptsByBlock(ctx context.Cont
var err error
receipts, err = plugins.GetTransactionReceiptsBlock(context.Background(), service.db, 0, 100000, service.cfg.Chainid, whereClause, column)
if err != nil {
return nil, err
return nil, errBlockNotFound
}

if borTxHashBytes != nil {
Expand Down
21 changes: 16 additions & 5 deletions plugins/packages/polygon/main.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
package main

import (
"database/sql"
"io"
"golang.org/x/crypto/sha3"
"database/sql"
"regexp"
"errors"

"golang.org/x/crypto/sha3"
log "github.com/inconshreveable/log15"

"github.com/openrelayxyz/cardinal-evm/crypto"
"github.com/openrelayxyz/cardinal-evm/rlp"
"github.com/openrelayxyz/cardinal-evm/common"
evm "github.com/openrelayxyz/cardinal-evm/types"
"github.com/openrelayxyz/cardinal-types/metrics"
"github.com/openrelayxyz/cardinal-types"
"github.com/openrelayxyz/cardinal-rpc"
evm "github.com/openrelayxyz/cardinal-evm/types"
log "github.com/inconshreveable/log15"
rpcTransports "github.com/openrelayxyz/cardinal-rpc/transports"

"github.com/openrelayxyz/cardinal-flume/config"
"github.com/openrelayxyz/cardinal-flume/indexer"
"github.com/openrelayxyz/cardinal-flume/plugins"
rpcTransports "github.com/openrelayxyz/cardinal-rpc/transports"
)

var TrackedPrefixes = []*regexp.Regexp{
Expand All @@ -25,6 +29,13 @@ var TrackedPrefixes = []*regexp.Regexp{
regexp.MustCompile("c/[0-9a-z]+/b/[0-9a-z]+/bs"),
}

var (
errBlockNotFound = errors.New("block not found")
errBlockHashNotFound = errors.New("blockHash not found")
errHeaderNotFound = errors.New("header for hash not found")
errInvalidInput = errors.New("Invalid input")
)

func Initialize(cfg *config.Config, pl *plugins.PluginLoader) {
log.Info("Polygon plugin loaded")
}
Expand Down
117 changes: 50 additions & 67 deletions plugins/packages/polygon/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import (
"errors"

log "github.com/inconshreveable/log15"

"github.com/openrelayxyz/cardinal-evm/common"
"github.com/openrelayxyz/cardinal-flume/heavy"
"github.com/openrelayxyz/cardinal-flume/plugins"
"github.com/openrelayxyz/cardinal-types"

"github.com/openrelayxyz/cardinal-types/hexutil"
"github.com/openrelayxyz/cardinal-types/metrics"

"github.com/openrelayxyz/cardinal-flume/heavy"
"github.com/openrelayxyz/cardinal-flume/plugins"

)

type Snapshot struct {
Expand Down Expand Up @@ -40,13 +42,13 @@ func (service *PolygonBorService) fetchSnapshot(ctx context.Context, blockNumber
var snapshotBytes []byte

if err := service.db.QueryRowContext(context.Background(), "SELECT snapshot FROM bor.bor_snapshots WHERE block = ?;", blockNumber).Scan(&snapshotBytes); err != nil {
log.Error("sql snapshot fetch error Snapshot()", "err", err.Error())
log.Error("sql snapshot fetch error Snapshot()", "err", err)
return nil, err
}

ssb, err := plugins.Decompress(snapshotBytes)
if err != nil {
log.Error("sql snapshot decompress error Snapshot()", "err", err.Error())
log.Error("sql snapshot decompress error Snapshot()", "err", err)
return nil, err
}

Expand All @@ -72,7 +74,7 @@ func (service *PolygonBorService) getRecents(blockNumber uint64) (map[uint64]com
var signerBytes []byte
err := rows.Scan(&signerBytes)
if err != nil {
log.Error("getRecents scan error", "err", err.Error())
log.Error("getRecents scan error", "err", err)
return nil, err
}
recents[index] = plugins.BytesToAddress(signerBytes)
Expand All @@ -97,75 +99,56 @@ func (service *PolygonBorService) GetSnapshot(ctx context.Context, blockNrOrHash
var blockNumber uint64

switch {
case numOk:

blockNumber = uint64(number)
requiredSnapshot := blockNumber - (blockNumber % 64)

if len(service.cfg.HeavyServer) > 0 && requiredSnapshot < service.cfg.EarliestBlock {
log.Debug("bor_getSnapshot sent to flume heavy")
polygonMissMeter.Mark(1)
bgssMissMeter.Mark(1)
response, err := heavy.CallHeavy[*Snapshot](ctx, service.cfg.HeavyServer, "bor_getSnapshot", hexutil.Uint64(blockNumber))
if err != nil {
log.Error("Error calling to heavy server, getSnapshot()", "blockNumber", blockNumber, "err", err.Error())
return nil, nil
}
return *response, nil
}
case numOk:

var hashBytes []byte
blockNumber = uint64(number)
var hashBytes []byte

if err := service.db.QueryRow("SELECT hash FROM blocks WHERE number = ?", blockNumber).Scan(&hashBytes); err != nil {
log.Error("Error deriving blockHashash from blockNumber, getSnapshot()", "number", blockNumber, "err", err.Error())
return nil, nil
}
if err := service.db.QueryRow("SELECT hash FROM blocks WHERE number = ?", blockNumber).Scan(&hashBytes); err != nil {
log.Error("Error deriving blockHashash from blockNumber, getSnapshot()", "number", blockNumber, "err", err)
return nil, errBlockNotFound
}

blockHash = plugins.BytesToHash(hashBytes)
blockHash = plugins.BytesToHash(hashBytes)

if len(service.cfg.HeavyServer) > 0 {
log.Debug("bor_getSnapshot served from flume light")
polygonHitMeter.Mark(1)
bgssHitMeter.Mark(1)
}
case hshOk:

case hshOk:
var present int
service.db.QueryRow("SELECT 1 FROM blocks WHERE hash = ?;", plugins.TrimPrefix(blockHash.Bytes())).Scan(&present)

if len(service.cfg.HeavyServer) > 0 && present == 0 {
log.Debug("bor_getSnapshot sent to flume heavy")
polygonMissMeter.Mark(1)
bgssMissMeter.Mark(1)
response, err := heavy.CallHeavy[*Snapshot](ctx, service.cfg.HeavyServer, "bor_getSnapshot", blockHash)
if err != nil {
log.Error("Error calling to heavy server, getSnapshot()", "blockHash", blockHash, "err", err.Error())
return nil, err
if err := service.db.QueryRow("SELECT number FROM blocks WHERE hash = ?;", plugins.TrimPrefix(blockHash.Bytes())).Scan(&blockNumber); err != nil {
log.Error("Error deriving blockNumber from blockHash, getSnapshot()", "hash", blockHash, "err", err)
return nil, errBlockHashNotFound
}
return *response, nil
}

if err := service.db.QueryRow("SELECT number FROM blocks WHERE hash = ?;", plugins.TrimPrefix(blockHash.Bytes())).Scan(&blockNumber); err != nil {
log.Error("Error deriving blockNumber from blockHash, getSnapshot()", "hash", blockHash, "err", err.Error())
return nil, nil
}
default:
log.Error("Error deriving input, getSnapshot", "input", blockNrOrHash)
return nil, errInvalidInput
}

requiredSnapshot := blockNumber - (blockNumber % 64)

if len(service.cfg.HeavyServer) > 0 {
log.Debug("bor_getSnapshot served from flume light")
polygonHitMeter.Mark(1)
bgssHitMeter.Mark(1)
if len(service.cfg.HeavyServer) > 0 && requiredSnapshot < service.cfg.EarliestBlock {
log.Debug("bor_getSnapshot sent to flume heavy")
polygonMissMeter.Mark(1)
bgssMissMeter.Mark(1)
response, err := heavy.CallHeavy[*Snapshot](ctx, service.cfg.HeavyServer, "bor_getSnapshot", hexutil.Uint64(blockNumber))
if err != nil {
log.Error("Error calling to heavy server, getSnapshot()", "blockNumber", blockNumber, "err", err)
return nil, errBlockNotFound
}
return *response, nil
}

default:
log.Error("Error deriving input, getSnapshot")
return nil, nil
if len(service.cfg.HeavyServer) > 0 {
log.Debug("bor_getSnapshot served from flume light")
polygonHitMeter.Mark(1)
bgssHitMeter.Mark(1)
}

log.Debug("getSnapshot() intial block value", "blockNumber", blockNumber)

recents, err := service.getRecents(blockNumber)
if err != nil {
log.Error("Error getting recents get_snapshot()", "err", err.Error())
log.Error("Error getting recents get_snapshot()", "err", err)
return nil, errBlockNotFound
}

var sprint uint64
Expand All @@ -183,8 +166,8 @@ func (service *PolygonBorService) GetSnapshot(ctx context.Context, blockNrOrHash
snap := &Snapshot{}
snap, err = service.fetchSnapshot(ctx, blockNumber)
if err != nil {
log.Error("Error fetching snapshot get_snapshot(), mod 64 == 0 case", "err", err.Error())
return nil, err
log.Error("Error fetching snapshot get_snapshot(), mod 64 == 0 case", "err", err)
return nil, errBlockNotFound
}
return snap, nil

Expand All @@ -193,20 +176,20 @@ func (service *PolygonBorService) GetSnapshot(ctx context.Context, blockNrOrHash
subsequentSnapshot := blockNumber + 1
snap, _ = service.fetchSnapshot(ctx, subsequentSnapshot)
if err != nil {
log.Error("Error fetching snapshot get_snapshot() mod 64 == 0 63 case", "err", err.Error())
return nil, err
log.Error("Error fetching snapshot get_snapshot() mod 64 == 0 63 case", "err", err)
return nil, errBlockNotFound
}
snap.Number = blockNumber
snap.Hash = blockHash
snap.Recents = recents
return snap, nil
default:
snap := &Snapshot{}
previousSnapshot := blockNumber - (blockNumber % 64)
previousSnapshot := blockNumber - (blockNumber % sprint)
snap, _ = service.fetchSnapshot(ctx, previousSnapshot)
if err != nil {
log.Error("Error fetching snapshot get_snapshot() default condition", "err", err.Error())
return nil, err
log.Error("Error fetching snapshot get_snapshot() default condition", "err", err)
return nil, errBlockNotFound
}
snap.Number = blockNumber
snap.Hash = blockHash
Expand All @@ -216,5 +199,5 @@ func (service *PolygonBorService) GetSnapshot(ctx context.Context, blockNrOrHash
log.Error("outside of case switch for input")
err = errors.New("unknown block")
log.Error("unkown block error", "block number", blockNumber)
return nil, err
return nil, errBlockNotFound
}

0 comments on commit b248290

Please sign in to comment.