Skip to content

Commit

Permalink
ethrpc: strictness validation level when unmarshalling blocks and txns (
Browse files Browse the repository at this point in the history
  • Loading branch information
pkieltyka authored Oct 23, 2024
1 parent 510e25e commit 0525c78
Show file tree
Hide file tree
Showing 15 changed files with 502 additions and 135 deletions.
15 changes: 10 additions & 5 deletions cmd/chain-watch/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,18 @@ func init() {
ETH_NODE_URL = testConfig["ARB_NOVA_URL"]
ETH_NODE_WSS_URL = testConfig["ARB_NOVA_WSS_URL"]
}

// if testConfig["ETHERLINK_MAINNET_URL"] != "" {
// ETH_NODE_URL = testConfig["ETHERLINK_MAINNET_URL"]
// ETH_NODE_WSS_URL = testConfig["ETHERLINK_MAINNET_WSS_URL"]
// }
}

func main() {
fmt.Println("chain-watch start")

// Provider
// provider, err := ethrpc.NewProvider(ETH_NODE_URL)
provider, err := ethrpc.NewProvider(ETH_NODE_URL, ethrpc.WithStreaming(ETH_NODE_WSS_URL))
provider, err := ethrpc.NewProvider(ETH_NODE_URL, ethrpc.WithStreaming(ETH_NODE_WSS_URL)) //, ethrpc.WithStrictValidation())
if err != nil {
log.Fatal(err)
}
Expand All @@ -66,15 +70,16 @@ func main() {
monitorOptions.WithLogs = true
monitorOptions.BlockRetentionLimit = 64
monitorOptions.StreamingRetryAfter = 1 * time.Minute
// monitorOptions.StartBlockNumber = nil // track the head
monitorOptions.StartBlockNumber = nil // track the head

latestBlock, err := provider.BlockByNumber(context.Background(), nil)
if err != nil {
panic(err)
log.Fatal(err)
}
_ = latestBlock

monitorOptions.StartBlockNumber = big.NewInt(0).Sub(latestBlock.Number(), big.NewInt(10))
// monitorOptions.StartBlockNumber = big.NewInt(47496451)
// monitorOptions.StartBlockNumber = big.NewInt(3754824)
// monitorOptions.Bootstrap = true

monitorOptions.Logger = logger.NewLogger(logger.LogLevel_DEBUG)
Expand Down
34 changes: 25 additions & 9 deletions ethmonitor/ethmonitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,11 @@ func NewMonitor(provider ethrpc.RawInterface, options ...Options) (*Monitor, err
}

opts.BlockRetentionLimit += opts.TrailNumBlocksBehindHead
if opts.BlockRetentionLimit < 2 {
// minimum 2 blocks to track, as we need the previous
// block to verify the current block
opts.BlockRetentionLimit = 2
}

if opts.DebugLogging {
stdLogger, ok := opts.Logger.(*logger.StdLogAdapter)
Expand Down Expand Up @@ -679,7 +684,7 @@ func (m *Monitor) filterLogs(ctx context.Context, blockHash common.Hash, topics
if err != nil {
return nil, resp, err
}
logs, err := unmarshalLogs(resp)
logs, err := m.unmarshalLogs(resp)
return logs, resp, err
}

Expand All @@ -696,7 +701,7 @@ func (m *Monitor) filterLogs(ctx context.Context, blockHash common.Hash, topics
if err != nil {
return nil, resp, err
}
logs, err := unmarshalLogs(resp)
logs, err := m.unmarshalLogs(resp)
return logs, resp, err
}

Expand Down Expand Up @@ -786,7 +791,7 @@ func (m *Monitor) fetchNextBlock(ctx context.Context) (*types.Block, []byte, boo
if err != nil {
return nil, resp, miss, err
}
block, err := unmarshalBlock(resp)
block, err := m.unmarshalBlock(resp)
return block, resp, miss, err
}

Expand All @@ -796,7 +801,7 @@ func (m *Monitor) fetchNextBlock(ctx context.Context) (*types.Block, []byte, boo
if err != nil {
return nil, resp, miss, err
}
block, err := unmarshalBlock(resp)
block, err := m.unmarshalBlock(resp)
return block, resp, miss, err
}

Expand Down Expand Up @@ -890,7 +895,7 @@ func (m *Monitor) fetchBlockByHash(ctx context.Context, hash common.Hash) (*type
if err != nil {
return nil, nil, err
}
block, err := unmarshalBlock(resp)
block, err := m.unmarshalBlock(resp)
return block, nil, err
}

Expand All @@ -900,7 +905,7 @@ func (m *Monitor) fetchBlockByHash(ctx context.Context, hash common.Hash) (*type
if err != nil {
return nil, nil, err
}
block, err := unmarshalBlock(resp)
block, err := m.unmarshalBlock(resp)
return block, resp, err
}

Expand Down Expand Up @@ -1131,16 +1136,27 @@ func clampDuration(x, y time.Duration) time.Duration {
}
}

func unmarshalBlock(blockPayload []byte) (*types.Block, error) {
func (m *Monitor) unmarshalBlock(blockPayload []byte) (*types.Block, error) {
var block *types.Block
err := ethrpc.IntoBlock(blockPayload, &block)

var strictness ethrpc.StrictnessLevel
getStrictnessLevel, ok := m.provider.(ethrpc.StrictnessLevelGetter)
if !ok {
// default to no validation if provider does not support strictness
// level interface
strictness = 0
} else {
strictness = getStrictnessLevel.StrictnessLevel()
}

err := ethrpc.IntoBlock(blockPayload, &block, strictness)
if err != nil {
return nil, err
}
return block, nil
}

func unmarshalLogs(logsPayload []byte) ([]types.Log, error) {
func (m *Monitor) unmarshalLogs(logsPayload []byte) ([]types.Log, error) {
var logs []types.Log
err := json.Unmarshal(logsPayload, &logs)
if err != nil {
Expand Down
Loading

0 comments on commit 0525c78

Please sign in to comment.