Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

e3: getLogs on iterators #6683

Merged
merged 28 commits into from
Jan 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/erigon-el/backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -888,7 +888,7 @@ func (s *Ethereum) setUpBlockReader(ctx context.Context, dirs datadir.Dirs, snCo
}

dir.MustExist(dirs.SnapHistory)
agg, err := libstate.NewAggregator22(ctx, dirs.SnapHistory, dirs.Tmp, ethconfig.HistoryV3AggregationStep, s.chainDB)
agg, err := libstate.NewAggregatorV3(ctx, dirs.SnapHistory, dirs.Tmp, ethconfig.HistoryV3AggregationStep, s.chainDB)
if err != nil {
return nil, nil, nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/integration/commands/reset_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (
"text/tabwriter"

"github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon-lib/common/rawdbv3"
"github.com/ledgerwatch/erigon-lib/kv"
"github.com/ledgerwatch/erigon-lib/kv/kvcfg"
"github.com/ledgerwatch/erigon-lib/kv/rawdbv3"
"github.com/ledgerwatch/erigon-lib/state"
"github.com/ledgerwatch/erigon/core/rawdb/rawdbhelpers"
reset2 "github.com/ledgerwatch/erigon/core/rawdb/rawdbreset"
Expand Down
6 changes: 3 additions & 3 deletions cmd/integration/commands/stages.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import (
"github.com/ledgerwatch/erigon-lib/common/cmp"
"github.com/ledgerwatch/erigon-lib/common/datadir"
"github.com/ledgerwatch/erigon-lib/common/dir"
"github.com/ledgerwatch/erigon-lib/common/rawdbv3"
"github.com/ledgerwatch/erigon-lib/kv"
"github.com/ledgerwatch/erigon-lib/kv/kvcfg"
"github.com/ledgerwatch/erigon-lib/kv/rawdbv3"
libstate "github.com/ledgerwatch/erigon-lib/state"
"github.com/ledgerwatch/erigon/cmd/hack/tool/fromdb"
"github.com/ledgerwatch/erigon/cmd/sentry/sentry"
Expand Down Expand Up @@ -487,7 +487,7 @@ func stageHeaders(db kv.RwDB, ctx context.Context) error {

if reset {
dirs := datadir.New(datadirCli)
if err := reset2.ResetBlocks(tx, db, sn, br, dirs, *chainConfig, engine); err != nil {
if err := reset2.ResetBlocks(tx, db, sn, agg, br, dirs, *chainConfig, engine); err != nil {
return err
}
return nil
Expand Down Expand Up @@ -1149,7 +1149,7 @@ func allSnapshots(ctx context.Context, db kv.RoDB) (*snapshotsync.RoSnapshots, *
_allSnapshotsSingleton = snapshotsync.NewRoSnapshots(snapCfg, dirs.Snap)

var err error
_aggSingleton, err = libstate.NewAggregator22(ctx, dirs.SnapHistory, dirs.Tmp, ethconfig.HistoryV3AggregationStep, db)
_aggSingleton, err = libstate.NewAggregatorV3(ctx, dirs.SnapHistory, dirs.Tmp, ethconfig.HistoryV3AggregationStep, db)
if err != nil {
panic(err)
}
Expand Down
7 changes: 3 additions & 4 deletions cmd/rpcdaemon/cli/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@ import (
"github.com/ledgerwatch/erigon-lib/common/datadir"
"github.com/ledgerwatch/erigon-lib/common/dir"
"github.com/ledgerwatch/erigon-lib/common/hexutility"
"github.com/ledgerwatch/erigon-lib/common/rawdbv3"
"github.com/ledgerwatch/erigon-lib/kv/kvcfg"
"github.com/ledgerwatch/erigon-lib/kv/rawdbv3"
libstate "github.com/ledgerwatch/erigon-lib/state"
"github.com/ledgerwatch/erigon/core/systemcontracts"

"github.com/ledgerwatch/erigon/core/state/historyv2read"
"github.com/ledgerwatch/erigon/core/state/temporal"
"github.com/ledgerwatch/erigon/core/systemcontracts"
"github.com/ledgerwatch/erigon/core/types/accounts"
"github.com/ledgerwatch/erigon/eth/ethconfig"
"github.com/ledgerwatch/erigon/rpc/rpccfg"
Expand Down Expand Up @@ -350,7 +349,7 @@ func RemoteServices(ctx context.Context, cfg httpcfg.HttpCfg, logger log.Logger,
allSnapshots.OptimisticReopenWithDB(db)
allSnapshots.LogStat()

if agg, err = libstate.NewAggregator22(ctx, cfg.Dirs.SnapHistory, cfg.Dirs.Tmp, ethconfig.HistoryV3AggregationStep, db); err != nil {
if agg, err = libstate.NewAggregatorV3(ctx, cfg.Dirs.SnapHistory, cfg.Dirs.Tmp, ethconfig.HistoryV3AggregationStep, db); err != nil {
return nil, nil, nil, nil, nil, nil, nil, ff, nil, fmt.Errorf("create aggregator: %w", err)
}
_ = agg.ReopenFiles()
Expand Down
2 changes: 1 addition & 1 deletion cmd/rpcdaemon/commands/debug_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (

jsoniter "github.com/json-iterator/go"
libcommon "github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon-lib/common/rawdbv3"
"github.com/ledgerwatch/erigon-lib/kv"
"github.com/ledgerwatch/erigon-lib/kv/rawdbv3"

"github.com/ledgerwatch/erigon/common/changeset"
"github.com/ledgerwatch/erigon/common/hexutil"
Expand Down
2 changes: 1 addition & 1 deletion cmd/rpcdaemon/commands/erigon_receipts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func TestGetLogs(t *testing.T) {
//
//api := NewErigonAPI(baseApi, m.DB, nil)
//logs, err := api.GetLogs(m.Ctx, filters.FilterCriteria{FromBlock: big.NewInt(0), ToBlock: big.NewInt(10)})
//assert.NoError(err)
//assert.NoError(err)s
//assert.Equal(uint64(10), logs[0].BlockNumber)
//
////filer by wrong address
Expand Down
115 changes: 97 additions & 18 deletions cmd/rpcdaemon/commands/eth_receipts.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,30 @@ import (
"math/big"

"github.com/RoaringBitmap/roaring"
"github.com/RoaringBitmap/roaring/roaring64"
"github.com/holiman/uint256"
"github.com/ledgerwatch/erigon-lib/chain"
libcommon "github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon-lib/common/hexutility"
"github.com/ledgerwatch/erigon-lib/common/rawdbv3"
"github.com/ledgerwatch/erigon-lib/kv"
"github.com/ledgerwatch/erigon-lib/kv/bitmapdb"
"github.com/ledgerwatch/erigon-lib/kv/iter"
"github.com/ledgerwatch/erigon-lib/kv/rawdbv3"
"github.com/ledgerwatch/log/v3"

"github.com/ledgerwatch/erigon/consensus"
"github.com/ledgerwatch/erigon/core/vm/evmtypes"
"github.com/ledgerwatch/erigon/turbo/services"

"github.com/ledgerwatch/erigon/core/state/temporal"

"github.com/ledgerwatch/erigon/common/hexutil"
"github.com/ledgerwatch/erigon/consensus"
"github.com/ledgerwatch/erigon/core"
"github.com/ledgerwatch/erigon/core/rawdb"
"github.com/ledgerwatch/erigon/core/state"
"github.com/ledgerwatch/erigon/core/state/temporal"
"github.com/ledgerwatch/erigon/core/types"
"github.com/ledgerwatch/erigon/core/vm"
"github.com/ledgerwatch/erigon/core/vm/evmtypes"
"github.com/ledgerwatch/erigon/eth/filters"
"github.com/ledgerwatch/erigon/ethdb/cbor"
"github.com/ledgerwatch/erigon/rpc"
"github.com/ledgerwatch/erigon/turbo/rpchelper"
"github.com/ledgerwatch/erigon/turbo/services"
"github.com/ledgerwatch/erigon/turbo/transactions"
)

Expand Down Expand Up @@ -301,6 +298,8 @@ func applyFilters(out *roaring.Bitmap, tx kv.Tx, begin, end uint64, crit filters
return nil
}

/*

func applyFiltersV3(out *roaring64.Bitmap, tx kv.TemporalTx, begin, end uint64, crit filters.FilterCriteria) error {
//[from,to)
var fromTxNum, toTxNum uint64
Expand Down Expand Up @@ -334,18 +333,54 @@ func applyFiltersV3(out *roaring64.Bitmap, tx kv.TemporalTx, begin, end uint64,
}
return nil
}
*/

func applyFiltersV3(tx kv.TemporalTx, begin, end uint64, crit filters.FilterCriteria) (out iter.U64, err error) {
//[from,to)
var fromTxNum, toTxNum uint64
if begin > 0 {
fromTxNum, err = rawdbv3.TxNums.Min(tx, begin)
if err != nil {
return out, err
}
}
toTxNum, err = rawdbv3.TxNums.Max(tx, end)
if err != nil {
return out, err
}
toTxNum++

topicsBitmap, err := getTopicsBitmapV3(tx, crit.Topics, fromTxNum, toTxNum)
if err != nil {
return out, err
}
if topicsBitmap != nil {
out = topicsBitmap
}
addrBitmap, err := getAddrsBitmapV3(tx, crit.Addresses, fromTxNum, toTxNum)
if err != nil {
return out, err
}
if addrBitmap != nil {
if out == nil {
out = addrBitmap
} else {
out = iter.Intersect[uint64](out, addrBitmap)
}
}
if out == nil {
out = iter.Range[uint64](fromTxNum, toTxNum)
}
return out, nil
}

func (api *APIImpl) getLogsV3(ctx context.Context, tx kv.TemporalTx, begin, end uint64, crit filters.FilterCriteria) ([]*types.Log, error) {
logs := []*types.Log{}

txNumbers := bitmapdb.NewBitmap64()
defer bitmapdb.ReturnToPool64(txNumbers)
if err := applyFiltersV3(txNumbers, tx, begin, end, crit); err != nil {
txNumbers, err := applyFiltersV3(tx, begin, end, crit)
if err != nil {
return logs, err
}
if txNumbers.IsEmpty() {
return logs, nil
}

addrMap := make(map[libcommon.Address]struct{}, len(crit.Addresses))
for _, v := range crit.Addresses {
Expand All @@ -356,12 +391,12 @@ func (api *APIImpl) getLogsV3(ctx context.Context, tx kv.TemporalTx, begin, end
if err != nil {
return nil, err
}
exec := newIntraBlockExec(tx, chainConfig, api.engine())
exec := newIntraBlockExec(tx, chainConfig, api.engine(), api._blockReader)

var blockHash libcommon.Hash
var header *types.Header

iter := MapTxNum2BlockNum(tx, bitmapdb.ToIter(txNumbers.Iterator()))
iter := MapTxNum2BlockNum(tx, txNumbers)
for iter.HasNext() {
if err = ctx.Err(); err != nil {
return nil, err
Expand All @@ -380,7 +415,7 @@ func (api *APIImpl) getLogsV3(ctx context.Context, tx kv.TemporalTx, begin, end
return nil, err
}
if header == nil {
log.Warn("header is nil", "blockNum", blockNum)
log.Warn("[rpc] header is nil", "blockNum", blockNum)
continue
}
blockHash = header.Hash()
Expand Down Expand Up @@ -439,12 +474,13 @@ type intraBlockExec struct {
vmConfig *vm.Config
}

func newIntraBlockExec(tx kv.TemporalTx, chainConfig *chain.Config, engine consensus.EngineReader) *intraBlockExec {
func newIntraBlockExec(tx kv.TemporalTx, chainConfig *chain.Config, engine consensus.EngineReader, br services.FullBlockReader) *intraBlockExec {
stateReader := state.NewHistoryReaderV3()
stateReader.SetTx(tx)
return &intraBlockExec{
engine: engine,
chainConfig: chainConfig,
br: br,
stateReader: stateReader,
evm: vm.NewEVM(evmtypes.BlockContext{}, evmtypes.TxContext{}, nil, chainConfig, vm.Config{}),
vmConfig: &vm.Config{},
Expand Down Expand Up @@ -492,6 +528,48 @@ func (e *intraBlockExec) execTx(txNum uint64, txIndex int, txn types.Transaction
// {{}, {B}} matches any topic in first position AND B in second position
// {{A}, {B}} matches topic A in first position AND B in second position
// {{A, B}, {C, D}} matches topic (A OR B) in first position AND (C OR D) in second position
func getTopicsBitmapV3(tx kv.TemporalTx, topics [][]libcommon.Hash, from, to uint64) (iter.U64, error) {
var result iter.U64
for _, sub := range topics {
var bitmapForORing iter.U64 = iter.Array[uint64]([]uint64{})

for _, topic := range sub {
it, err := tx.IndexRange(temporal.LogTopicIdx, topic.Bytes(), from, to, true, -1)
if err != nil {
return nil, err
}
bitmapForORing = iter.Union[uint64](bitmapForORing, it)
}

if result == nil {
result = bitmapForORing
continue
}
result = iter.Intersect[uint64](result, bitmapForORing)
}
return result, nil
}

func getAddrsBitmapV3(tx kv.TemporalTx, addrs []libcommon.Address, from, to uint64) (iter.U64, error) {
if len(addrs) == 0 {
return nil, nil
}
var rx iter.U64
for _, addr := range addrs {
it, err := tx.IndexRange(temporal.LogAddrIdx, addr[:], from, to, true, -1)
if err != nil {
return nil, err
}
if rx == nil {
rx = it
} else {
rx = iter.Union[uint64](rx, it)
}
}
return rx, nil
}

/*
func getTopicsBitmapV3(tx kv.TemporalTx, topics [][]libcommon.Hash, from, to uint64) (*roaring64.Bitmap, error) {
var result *roaring64.Bitmap
for _, sub := range topics {
Expand Down Expand Up @@ -544,6 +622,7 @@ func getAddrsBitmapV3(tx kv.TemporalTx, addrs []libcommon.Address, from, to uint
}
return roaring64.FastOr(rx...), nil
}
*/

// GetTransactionReceipt implements eth_getTransactionReceipt. Returns the receipt of a transaction given the transaction's hash.
func (api *APIImpl) GetTransactionReceipt(ctx context.Context, txnHash libcommon.Hash) (map[string]interface{}, error) {
Expand Down
6 changes: 3 additions & 3 deletions cmd/rpcdaemon/commands/otterscan_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (
"github.com/holiman/uint256"
"github.com/ledgerwatch/erigon-lib/chain"
libcommon "github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon-lib/common/rawdbv3"
"github.com/ledgerwatch/erigon-lib/kv"
"github.com/ledgerwatch/erigon-lib/kv/iter"
"github.com/ledgerwatch/erigon-lib/kv/rawdbv3"
"github.com/ledgerwatch/erigon/core/state/temporal"
"github.com/ledgerwatch/log/v3"

Expand Down Expand Up @@ -277,7 +277,7 @@ func (api *OtterscanAPIImpl) searchTransactionsBeforeV3(tx kv.TemporalTx, ctx co
txNums := iter.Union[uint64](itFrom, itTo)
txNumsIter := MapDescendTxNum2BlockNum(tx, txNums)

exec := newIntraBlockExec(tx, chainConfig, api.engine())
exec := newIntraBlockExec(tx, chainConfig, api.engine(), api._blockReader)
var blockHash libcommon.Hash
var header *types.Header
txs := make([]*RPCTransaction, 0, pageSize)
Expand All @@ -298,7 +298,7 @@ func (api *OtterscanAPIImpl) searchTransactionsBeforeV3(tx kv.TemporalTx, ctx co
return nil, err
}
if header == nil {
log.Warn("header is nil", "blockNum", blockNum)
log.Warn("[rpc] header is nil", "blockNum", blockNum)
continue
}
blockHash = header.Hash()
Expand Down
2 changes: 1 addition & 1 deletion cmd/rpcdaemon/commands/trace_filtering.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (
jsoniter "github.com/json-iterator/go"
"github.com/ledgerwatch/erigon-lib/chain"
libcommon "github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon-lib/common/rawdbv3"
"github.com/ledgerwatch/erigon-lib/kv"
"github.com/ledgerwatch/erigon-lib/kv/bitmapdb"
"github.com/ledgerwatch/erigon-lib/kv/rawdbv3"
"github.com/ledgerwatch/erigon/common"
"github.com/ledgerwatch/erigon/common/hexutil"
"github.com/ledgerwatch/erigon/consensus/ethash"
Expand Down
4 changes: 2 additions & 2 deletions cmd/state/exec3/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ type Worker struct {
background bool // if true - worker does manage RoTx (begin/rollback) in .ResetTx()
blockReader services.FullBlockReader
rs *state.StateV3
stateWriter *state.StateWriter22
stateReader *state.StateReader22
stateWriter *state.StateWriterV3
stateReader *state.StateReaderV3
chainConfig *chain.Config
getHeader func(hash libcommon.Hash, number uint64) *types.Header

Expand Down
4 changes: 2 additions & 2 deletions core/chain_makers.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ func GenerateChain(config *chain.Config, parent *types.Block, engine consensus.E
c.Close()
if GenerateTrace {
fmt.Printf("State after %d================\n", b.header.Number)
it, err := tx.Stream(kv.HashedAccounts, nil, nil)
it, err := tx.Range(kv.HashedAccounts, nil, nil)
if err != nil {
return nil, nil, err
}
Expand All @@ -406,7 +406,7 @@ func GenerateChain(config *chain.Config, parent *types.Block, engine consensus.E
fmt.Printf("%x: %x\n", k, v)
}
fmt.Printf("..................\n")
it, err = tx.Stream(kv.HashedStorage, nil, nil)
it, err = tx.Range(kv.HashedStorage, nil, nil)
if err != nil {
return nil, nil, err
}
Expand Down
7 changes: 3 additions & 4 deletions core/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,9 @@ import (
"github.com/holiman/uint256"
"github.com/ledgerwatch/erigon-lib/chain"
libcommon "github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon-lib/common/rawdbv3"
"github.com/ledgerwatch/erigon-lib/kv"
"github.com/ledgerwatch/erigon-lib/kv/mdbx"
"github.com/ledgerwatch/log/v3"
"golang.org/x/exp/slices"

"github.com/ledgerwatch/erigon-lib/kv/rawdbv3"
"github.com/ledgerwatch/erigon/common"
"github.com/ledgerwatch/erigon/common/hexutil"
"github.com/ledgerwatch/erigon/common/math"
Expand All @@ -50,6 +47,8 @@ import (
"github.com/ledgerwatch/erigon/params"
"github.com/ledgerwatch/erigon/params/networkname"
"github.com/ledgerwatch/erigon/turbo/trie"
"github.com/ledgerwatch/log/v3"
"golang.org/x/exp/slices"
)

//go:generate gencodec -type Genesis -field-override genesisSpecMarshaling -out gen_genesis.go
Expand Down
Loading