Skip to content

Commit

Permalink
Erigon 3: Prune canonical markers - CanonicalHash, HeaderNumber f…
Browse files Browse the repository at this point in the history
…rom chaindata (#11634)

We prune `CanonicalHash` and `HeaderNumber ` in `chaindata`. this leads
to a -99% in these 2 tables and reduce their total size to 40 pages in
TOTAL (from >500k pages).

Benchmarks:

```
Total time taken to query blocks from 1000000 to 3000000: 1720.1410 seconds (OLD)
Total time taken to query blocks from 1000000 to 3000000: 1747.4729 seconds (New)

Amortized delta cost: 0.1ms/req
```

Is the RPC working for those ranges? Yes.

## How does it work?

Query canonical hash from snapshots by: `viewHeaderByNumber` ->
`header.Hash()` if db hit is missed
Query header number from snapshots by: `viewHeaderByHash` ->
`header.Num64` if db hit is missed
  • Loading branch information
Giulio2002 authored Aug 18, 2024
1 parent 6c387bf commit c78450c
Show file tree
Hide file tree
Showing 48 changed files with 1,050 additions and 613 deletions.
6 changes: 6 additions & 0 deletions cmd/rpcdaemon/rpcservices/eth_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,12 @@ func (back *RemoteBackend) HeaderByHash(ctx context.Context, tx kv.Getter, hash
func (back *RemoteBackend) CanonicalHash(ctx context.Context, tx kv.Getter, blockNum uint64) (common.Hash, error) {
return back.blockReader.CanonicalHash(ctx, tx, blockNum)
}
func (back *RemoteBackend) HeaderNumber(ctx context.Context, tx kv.Getter, hash common.Hash) (*uint64, error) {
return back.blockReader.HeaderNumber(ctx, tx, hash)
}
func (back *RemoteBackend) IsCanonical(ctx context.Context, tx kv.Getter, hash common.Hash, blockNum uint64) (bool, error) {
return back.blockReader.IsCanonical(ctx, tx, hash, blockNum)
}
func (back *RemoteBackend) TxnByIdxInBlock(ctx context.Context, tx kv.Getter, blockNum uint64, i int) (types.Transaction, error) {
return back.blockReader.TxnByIdxInBlock(ctx, tx, blockNum, i)
}
Expand Down
14 changes: 0 additions & 14 deletions core/rawdb/accessors_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -483,20 +483,6 @@ func WriteBodyForStorage(db kv.Putter, hash common.Hash, number uint64, body *ty
return db.Put(kv.BlockBody, dbutils.BlockBodyKey(number, hash), data)
}

// ReadBodyByNumber - returns canonical block body
func ReadBodyByNumber(db kv.Tx, number uint64) (*types.Body, uint64, uint32, error) {
hash, err := ReadCanonicalHash(db, number)
if err != nil {
return nil, 0, 0, fmt.Errorf("failed ReadCanonicalHash: %w", err)
}
if hash == (common.Hash{}) {
return nil, 0, 0, nil
}
body, baseTxnID, txCount := ReadBody(db, hash, number)
// TODO baseTxnID from ReadBody is baseTxnID+1, but we could expect original baseTxnID here
return body, baseTxnID, txCount, nil
}

func ReadBodyWithTransactions(db kv.Getter, hash common.Hash, number uint64) (*types.Body, error) {
body, firstTxId, txCount := ReadBody(db, hash, number)
if body == nil {
Expand Down
18 changes: 10 additions & 8 deletions erigon-lib/common/dbg/experiments.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,11 @@ var (
mergeTr = EnvInt("MERGE_THRESHOLD", -1)

//state v3
noPrune = EnvBool("NO_PRUNE", false)
noMerge = EnvBool("NO_MERGE", false)
discardHistory = EnvBool("DISCARD_HISTORY", false)
discardCommitment = EnvBool("DISCARD_COMMITMENT", false)
noPrune = EnvBool("NO_PRUNE", false)
noMerge = EnvBool("NO_MERGE", false)
discardHistory = EnvBool("DISCARD_HISTORY", false)
discardCommitment = EnvBool("DISCARD_COMMITMENT", false)
pruneTotalDifficulty = EnvBool("PRUNE_TOTAL_DIFFICULTY", false)

// force skipping of any non-Erigon2 .torrent files
DownloaderOnlyBlocks = EnvBool("DOWNLOADER_ONLY_BLOCKS", false)
Expand Down Expand Up @@ -82,10 +83,11 @@ func NoSync() bool { return noSync }
func MdbxReadAhead() bool { return mdbxReadahead }
func MdbxLockInRam() bool { return mdbxLockInRam }

func DiscardHistory() bool { return discardHistory }
func DiscardCommitment() bool { return discardCommitment }
func NoPrune() bool { return noPrune }
func NoMerge() bool { return noMerge }
func DiscardHistory() bool { return discardHistory }
func DiscardCommitment() bool { return discardCommitment }
func NoPrune() bool { return noPrune }
func NoMerge() bool { return noMerge }
func PruneTotalDifficulty() bool { return pruneTotalDifficulty }

var (
dirtySace uint64
Expand Down
8 changes: 8 additions & 0 deletions erigon-lib/direct/eth_backend_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,14 @@ func (c *SubscribeLogsStreamC) Recv() (*remote.SubscribeLogsReply, error) {

// -- end SubscribeLogs

func (s *EthBackendClientDirect) CanonicalHash(ctx context.Context, in *remote.CanonicalHashRequest, opts ...grpc.CallOption) (*remote.CanonicalHashReply, error) {
return s.server.CanonicalHash(ctx, in)
}

func (s *EthBackendClientDirect) HeaderNumber(ctx context.Context, in *remote.HeaderNumberRequest, opts ...grpc.CallOption) (*remote.HeaderNumberReply, error) {
return s.server.HeaderNumber(ctx, in)
}

func (s *EthBackendClientDirect) Block(ctx context.Context, in *remote.BlockRequest, opts ...grpc.CallOption) (*remote.BlockReply, error) {
return s.server.Block(ctx, in)
}
Expand Down
2 changes: 1 addition & 1 deletion erigon-lib/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.21.5

require (
github.com/erigontech/erigon-snapshot v1.3.1-0.20240814160410-2ce37904b978
github.com/erigontech/interfaces v0.0.0-20240723225543-c6b574058f8c
github.com/erigontech/interfaces v0.0.0-20240816222220-760b11456e56
github.com/erigontech/mdbx-go v0.38.4
github.com/erigontech/secp256k1 v1.1.0
github.com/rs/dnscache v0.0.0-20211102005908-e0241e321417
Expand Down
4 changes: 2 additions & 2 deletions erigon-lib/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1m
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/erigontech/erigon-snapshot v1.3.1-0.20240814160410-2ce37904b978 h1:7ECOf7Us3+/706WGZXIX84qQc6zmxQby8fGbFLiqFlU=
github.com/erigontech/erigon-snapshot v1.3.1-0.20240814160410-2ce37904b978/go.mod h1:ooHlCl+eEYzebiPu+FP6Q6SpPUeMADn8Jxabv3IKb9M=
github.com/erigontech/interfaces v0.0.0-20240723225543-c6b574058f8c h1:KbcMdRKYRL+A4bJGK+fNOITk95wJzh9rUXFJ17wCyUY=
github.com/erigontech/interfaces v0.0.0-20240723225543-c6b574058f8c/go.mod h1:N7OUkhkcagp9+7yb4ycHsG2VWCOmuJ1ONBecJshxtLE=
github.com/erigontech/interfaces v0.0.0-20240816222220-760b11456e56 h1:RFLd10LsbUdcXS7zntLuVj+mZZJFDD6E7y183kYC+SM=
github.com/erigontech/interfaces v0.0.0-20240816222220-760b11456e56/go.mod h1:N7OUkhkcagp9+7yb4ycHsG2VWCOmuJ1ONBecJshxtLE=
github.com/erigontech/mdbx-go v0.38.4 h1:S9T7mTe9KPcFe4dOoOtVdI6gPzht9y7wMnYfUBgrQLo=
github.com/erigontech/mdbx-go v0.38.4/go.mod h1:IcOLQDPw3VM/asP6T5JVPPN4FHHgJtY16XfYjzWKVNI=
github.com/erigontech/secp256k1 v1.1.0 h1:mO3YJMUSoASE15Ya//SoHiisptUhdXExuMUN1M0X9qY=
Expand Down
Loading

0 comments on commit c78450c

Please sign in to comment.