Skip to content

Commit a590ca3

Browse files
committed
fix: handle chain ID panic
1 parent 9244feb commit a590ca3

File tree

3 files changed

+26
-5
lines changed

3 files changed

+26
-5
lines changed

cmd/p2p/sensor/sensor.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ var SensorCmd = &cobra.Command{
169169
ProjectID: inputSensorParams.ProjectID,
170170
DatabaseID: inputSensorParams.DatabaseID,
171171
SensorID: inputSensorParams.SensorID,
172+
ChainID: inputSensorParams.NetworkID,
172173
MaxConcurrency: inputSensorParams.MaxDatabaseConcurrency,
173174
ShouldWriteBlocks: inputSensorParams.ShouldWriteBlocks,
174175
ShouldWriteBlockEvents: inputSensorParams.ShouldWriteBlockEvents,
@@ -180,6 +181,7 @@ var SensorCmd = &cobra.Command{
180181
case "json":
181182
db = database.NewJSONDatabase(database.JSONDatabaseOptions{
182183
SensorID: inputSensorParams.SensorID,
184+
ChainID: inputSensorParams.NetworkID,
183185
MaxConcurrency: inputSensorParams.MaxDatabaseConcurrency,
184186
ShouldWriteBlocks: inputSensorParams.ShouldWriteBlocks,
185187
ShouldWriteBlockEvents: inputSensorParams.ShouldWriteBlockEvents,

p2p/database/datastore.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const (
3131
type Datastore struct {
3232
client *datastore.Client
3333
sensorID string
34+
chainID *big.Int
3435
maxConcurrency int
3536
shouldWriteBlocks bool
3637
shouldWriteBlockEvents bool
@@ -118,6 +119,7 @@ type DatastoreOptions struct {
118119
ProjectID string
119120
DatabaseID string
120121
SensorID string
122+
ChainID uint64
121123
MaxConcurrency int
122124
ShouldWriteBlocks bool
123125
ShouldWriteBlockEvents bool
@@ -138,6 +140,7 @@ func NewDatastore(ctx context.Context, opts DatastoreOptions) Database {
138140
return &Datastore{
139141
client: client,
140142
sensorID: opts.SensorID,
143+
chainID: new(big.Int).SetUint64(opts.ChainID),
141144
maxConcurrency: opts.MaxConcurrency,
142145
shouldWriteBlocks: opts.ShouldWriteBlocks,
143146
shouldWriteBlockEvents: opts.ShouldWriteBlockEvents,
@@ -348,9 +351,14 @@ func (d *Datastore) newDatastoreTransaction(tx *types.Transaction, tfs time.Time
348351
v, r, s := tx.RawSignatureValues()
349352
var from, to string
350353

351-
address, err := types.Sender(types.LatestSignerForChainID(tx.ChainId()), tx)
354+
chainID := tx.ChainId()
355+
if tx.ChainId() == nil || tx.ChainId().Sign() <= 0 {
356+
chainID = d.chainID
357+
}
358+
359+
address, err := types.Sender(types.LatestSignerForChainID(chainID), tx)
352360
if err == nil {
353-
from = address.String()
361+
from = address.Hex()
354362
}
355363

356364
if tx.To() != nil {

p2p/database/json.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
// Each record is output as a single line of JSON (newline-delimited JSON).
2121
type JSONDatabase struct {
2222
sensorID string
23+
chainID *big.Int
2324
shouldWriteBlocks bool
2425
shouldWriteBlockEvents bool
2526
shouldWriteTransactions bool
@@ -30,6 +31,7 @@ type JSONDatabase struct {
3031
// JSONDatabaseOptions is used when creating a NewJSONDatabase.
3132
type JSONDatabaseOptions struct {
3233
SensorID string
34+
ChainID uint64
3335
MaxConcurrency int
3436
ShouldWriteBlocks bool
3537
ShouldWriteBlockEvents bool
@@ -42,6 +44,7 @@ type JSONDatabaseOptions struct {
4244
func NewJSONDatabase(opts JSONDatabaseOptions) Database {
4345
return &JSONDatabase{
4446
sensorID: opts.SensorID,
47+
chainID: new(big.Int).SetUint64(opts.ChainID),
4548
shouldWriteBlocks: opts.ShouldWriteBlocks,
4649
shouldWriteBlockEvents: opts.ShouldWriteBlockEvents,
4750
shouldWriteTransactions: opts.ShouldWriteTransactions,
@@ -252,14 +255,22 @@ func (j *JSONDatabase) writeTxs(txs []*types.Transaction, tfs time.Time) {
252255
}
253256

254257
for _, tx := range txs {
255-
var from common.Address
256-
from, _ = types.Sender(types.LatestSignerForChainID(tx.ChainId()), tx)
258+
chainID := tx.ChainId()
259+
if tx.ChainId() == nil || tx.ChainId().Sign() <= 0 {
260+
chainID = j.chainID
261+
}
262+
263+
var from string
264+
addr, err := types.Sender(types.LatestSignerForChainID(chainID), tx)
265+
if err == nil {
266+
from = addr.Hex()
267+
}
257268

258269
jsonTx := JSONTransaction{
259270
Type: "transaction",
260271
SensorID: j.sensorID,
261272
Hash: tx.Hash().Hex(),
262-
From: from.Hex(),
273+
From: from,
263274
Value: tx.Value().String(),
264275
Gas: tx.Gas(),
265276
GasPrice: tx.GasPrice().String(),

0 commit comments

Comments
 (0)