Skip to content

Commit

Permalink
Migration to fix trace_filter (#2095)
Browse files Browse the repository at this point in the history
* Migration to fix trace_filter

* Fix to db/tx

* Fix to db/tx

* Error fixes

* Bump KV version

Co-authored-by: Alex Sharp <[email protected]>
  • Loading branch information
AlexeyAkhunov and Alex Sharp authored Jun 4, 2021
1 parent 801784f commit 43915a7
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 3 deletions.
5 changes: 5 additions & 0 deletions cmd/rpcdaemon/commands/trace_filtering.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ func (api *TraceAPIImpl) Block(ctx context.Context, blockNr rpc.BlockNumber) (Pa
func (api *TraceAPIImpl) Filter(ctx context.Context, req TraceFilterRequest, stream *jsoniter.Stream) error {
dbtx, err1 := api.kv.BeginRo(ctx)
if err1 != nil {
stream.WriteNil()
return fmt.Errorf("traceFilter cannot open tx: %v", err1)
}
defer dbtx.Rollback()
Expand All @@ -212,6 +213,7 @@ func (api *TraceAPIImpl) Filter(ctx context.Context, req TraceFilterRequest, str
}

if fromBlock > toBlock {
stream.WriteNil()
return fmt.Errorf("invalid parameters: fromBlock cannot be greater than toBlock")
}

Expand Down Expand Up @@ -265,6 +267,7 @@ func (api *TraceAPIImpl) Filter(ctx context.Context, req TraceFilterRequest, str
// Extract transactions from block
hash, hashErr := rawdb.ReadCanonicalHash(dbtx, b)
if hashErr != nil {
stream.WriteNil()
return hashErr
}

Expand All @@ -274,6 +277,7 @@ func (api *TraceAPIImpl) Filter(ctx context.Context, req TraceFilterRequest, str
return bErr
}
if block == nil {
stream.WriteNil()
return fmt.Errorf("could not find block %x %d", hash, b)
}

Expand All @@ -282,6 +286,7 @@ func (api *TraceAPIImpl) Filter(ctx context.Context, req TraceFilterRequest, str
txs := block.Transactions()
t, tErr := api.callManyTransactions(ctx, dbtx, txs, block.ParentHash(), rpc.BlockNumber(block.NumberU64()-1), block.Header())
if tErr != nil {
stream.WriteNil()
return tErr
}
includeAll := len(fromAddresses) == 0 && len(toAddresses) == 0
Expand Down
4 changes: 2 additions & 2 deletions common/dbutils/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
)

// DBSchemaVersion
var DBSchemaVersionLMDB = types.VersionReply{Major: 1, Minor: 0, Patch: 0}
var DBSchemaVersionMDBX = types.VersionReply{Major: 2, Minor: 0, Patch: 0}
var DBSchemaVersionLMDB = types.VersionReply{Major: 1, Minor: 1, Patch: 0}
var DBSchemaVersionMDBX = types.VersionReply{Major: 2, Minor: 1, Patch: 0}

// Buckets

Expand Down
2 changes: 1 addition & 1 deletion ethdb/remote/remotedbserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const MaxTxTTL = 30 * time.Second
// 1.1.0 - added pending transactions, add methods eth_getRawTransactionByHash, eth_retRawTransactionByBlockHashAndIndex, eth_retRawTransactionByBlockNumberAndIndex| Yes | |
// 1.2.0 - Added separated services for mining and txpool methods
// 2.0.0 - Rename all buckets
var KvServiceAPIVersion = &types.VersionReply{Major: 2, Minor: 0, Patch: 0}
var KvServiceAPIVersion = &types.VersionReply{Major: 2, Minor: 1, Patch: 0}

type KvServer struct {
remote.UnimplementedKVServer // must be embedded to have forward compatible implementations.
Expand Down
51 changes: 51 additions & 0 deletions migrations/call_trace_index.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package migrations

import (
"encoding/binary"

"github.com/ledgerwatch/erigon/common/dbutils"
"github.com/ledgerwatch/erigon/common/etl"
"github.com/ledgerwatch/erigon/eth/stagedsync/stages"
"github.com/ledgerwatch/erigon/ethdb"
"github.com/ledgerwatch/erigon/log"
)

var rebuilCallTraceIndex = Migration{
Name: "rebuild_call_trace_index",
Up: func(db ethdb.Database, tmpdir string, progress []byte, CommitProgress etl.LoadCommitHandler) (err error) {
sm, err := ethdb.GetStorageModeFromDB(db)
if err != nil {
return err
}
if !sm.CallTraces {
// Call traces are not on, nothing to migrate
return nil
}
// Find the lowest key in the TraceCallSet table
tx := db.(ethdb.HasTx).Tx()
c, err := tx.CursorDupSort(dbutils.CallTraceSet)
if err != nil {
return err
}
defer c.Close()
var k []byte
k, _, err = c.First()
if err != nil {
return err
}
if k == nil {
log.Warn("Nothing to rebuild, CallTraceSet table is empty")
return nil
}
blockNum := binary.BigEndian.Uint64((k))
if blockNum == 0 {
log.Warn("Nothing to rebuild, CallTraceSet's first record", "number", blockNum)
return nil
}
log.Info("First record in CallTraceTable", "number", blockNum)
if err = stages.SaveStageUnwind(db, stages.CallTraces, blockNum-1); err != nil {
return err
}
return CommitProgress(db, nil, true)
},
}
1 change: 1 addition & 0 deletions migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ var migrations = []Migration{
headerPrefixToSeparateBuckets,
removeCliqueBucket,
dbSchemaVersion,
rebuilCallTraceIndex,
}

type Migration struct {
Expand Down

0 comments on commit 43915a7

Please sign in to comment.