Skip to content

Commit a273520

Browse files
authored
E3: Prune TotalDifficulty canonical markers (#11809)
Implements ethereum/execution-apis#570
1 parent b346ece commit a273520

File tree

18 files changed

+10
-159
lines changed

18 files changed

+10
-159
lines changed

Diff for: cmd/rpcdaemon/graphql/eip-1767.graphqls.ref

-3
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,6 @@ type Block {
154154
mixHash: Bytes32!
155155
# Difficulty is a measure of the difficulty of mining this block.
156156
difficulty: BigInt!
157-
# TotalDifficulty is the sum of all difficulty values up to and including
158-
# this block.
159-
totalDifficulty: BigInt!
160157
# OmmerCount is the number of ommers (AKA uncles) associated with this
161158
# block. If ommers are unavailable, this field will be null.
162159
ommerCount: Int

Diff for: cmd/rpcdaemon/graphql/geth-schema.graphqls.ref

-3
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,6 @@
190190
mixHash: Bytes32!
191191
# Difficulty is a measure of the difficulty of mining this block.
192192
difficulty: BigInt!
193-
# TotalDifficulty is the sum of all difficulty values up to and including
194-
# this block.
195-
totalDifficulty: BigInt!
196193
# OmmerCount is the number of ommers (AKA uncles) associated with this
197194
# block. If ommers are unavailable, this field will be null.
198195
ommerCount: Int

Diff for: cmd/rpcdaemon/graphql/graph/generated.go

-69
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: cmd/rpcdaemon/graphql/graph/model/models_gen.go

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: cmd/rpcdaemon/graphql/graph/schema.graphqls

-3
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,6 @@ type Block {
192192
mixHash: Bytes32!
193193
# Difficulty is a measure of the difficulty of mining this block.
194194
difficulty: BigInt!
195-
# TotalDifficulty is the sum of all difficulty values up to and including
196-
# this block.
197-
totalDifficulty: BigInt!
198195
# OmmerCount is the number of ommers (AKA uncles) associated with this
199196
# block. If ommers are unavailable, this field will be null.
200197
ommerCount: Int

Diff for: cmd/rpcdaemon/graphql/graph/schema.resolvers.go

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: cmd/rpcdaemon/graphql/query_block.graphql

-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
stateRoot
2626
timestamp
2727
transactionsRoot
28-
totalDifficulty
2928
transactions {
3029
createdContract {
3130
address

Diff for: cmd/rpcdaemon/postman/RPC_Testing.json

+1-5
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,6 @@
290290
" \"size\": \"0x3e1\",",
291291
" \"stateRoot\": \"0xb3f9408d80048b6f206951c4e387f8da37fb8510eccc18527865fa746c47bbc5\",",
292292
" \"timestamp\": \"0x56bff9bb\",",
293-
" \"totalDifficulty\": \"0x6332227c16fd7c67\",",
294293
" \"transactions\": [",
295294
" \"0x730724cb08a6eb17bf6b3296359d261570d343ea7944a17a9d7287d77900db08\",",
296295
" \"0xef2ea39c20ba09553b2f3cf02380406ac766039ca56612937eed5e7f3503fb3a\",",
@@ -336,7 +335,7 @@
336335
"raw": "{{HOST}}",
337336
"host": ["{{HOST}}"]
338337
},
339-
"description": "Returns information about a block given the block's number.\r\n\r\n**Parameters**\r\n\r\nTAG - Integer block number or one of \"earliest\", \"latest\" or \"pending\"\r\n\r\nBoolean - If true it returns the full transaction objects, if false only the hashes of the transactions\r\n\r\n**Returns**\r\n\r\nObject - An object of type Block defined as:\r\n\r\nnumber: QUANTITY - The block number or null when pending\r\n\r\nhash: DATA, 32 Bytes - Hash of the block or null when pending\r\n\r\nparentHash: DATA, 32 Bytes - Hash of the parent block\r\n\r\nnonce: DATA, 8 bytes - Hash of the proof of work or null when pending\r\n\r\nsha3Uncles: DATA, 32 Bytes - SHA3 of the uncles data in the block\r\n\r\nlogsBloom: DATA, 256 Bytes - The bloom filter for the block's logs or null when pending\r\n\r\ntransactionsRoot: DATA, 32 Bytes - The root of the transaction trie of the block\r\n\r\nstateRoot: DATA, 32 Bytes - The root of the final state trie of the block\r\n\r\nreceiptsRoot: DATA, 32 Bytes - The root of the receipts trie of the block\r\n\r\nminer: DATA, 20 Bytes - The address of the beneficiary to whom the mining rewards were given\r\n\r\ndifficulty: QUANTITY - Integer of the difficulty for this block\r\n\r\ntotalDifficulty: QUANTITY - Integer of the total difficulty of the chain until this block\r\n\r\nextraData: DATA - The extra data field of this block\r\n\r\nsize: QUANTITY - Integer the size of this block in bytes\r\n\r\ngasLimit: QUANTITY - The maximum gas allowed in this block\r\n\r\ngasUsed: QUANTITY - The total used gas by all transactions in this block\r\n\r\ntimestamp: QUANTITY - The unix timestamp for when the block was collated\r\n\r\ntransactions: ARRAY - Array of transaction objects, or 32 Bytes transaction hashes depending on the last given parameter\r\n\r\nuncles: ARRAY - Array of uncle hashes\r\n\r\n"
338+
"description": "Returns information about a block given the block's number.\r\n\r\n**Parameters**\r\n\r\nTAG - Integer block number or one of \"earliest\", \"latest\" or \"pending\"\r\n\r\nBoolean - If true it returns the full transaction objects, if false only the hashes of the transactions\r\n\r\n**Returns**\r\n\r\nObject - An object of type Block defined as:\r\n\r\nnumber: QUANTITY - The block number or null when pending\r\n\r\nhash: DATA, 32 Bytes - Hash of the block or null when pending\r\n\r\nparentHash: DATA, 32 Bytes - Hash of the parent block\r\n\r\nnonce: DATA, 8 bytes - Hash of the proof of work or null when pending\r\n\r\nsha3Uncles: DATA, 32 Bytes - SHA3 of the uncles data in the block\r\n\r\nlogsBloom: DATA, 256 Bytes - The bloom filter for the block's logs or null when pending\r\n\r\ntransactionsRoot: DATA, 32 Bytes - The root of the transaction trie of the block\r\n\r\nstateRoot: DATA, 32 Bytes - The root of the final state trie of the block\r\n\r\nreceiptsRoot: DATA, 32 Bytes - The root of the receipts trie of the block\r\n\r\nminer: DATA, 20 Bytes - The address of the beneficiary to whom the mining rewards were given\r\n\r\ndifficulty: QUANTITY - Integer of the difficulty for this block\r\n\r\nextraData: DATA - The extra data field of this block\r\n\r\nsize: QUANTITY - Integer the size of this block in bytes\r\n\r\ngasLimit: QUANTITY - The maximum gas allowed in this block\r\n\r\ngasUsed: QUANTITY - The total used gas by all transactions in this block\r\n\r\ntimestamp: QUANTITY - The unix timestamp for when the block was collated\r\n\r\ntransactions: ARRAY - Array of transaction objects, or 32 Bytes transaction hashes depending on the last given parameter\r\n\r\nuncles: ARRAY - Array of uncle hashes\r\n\r\n"
340339
},
341340
"response": []
342341
},
@@ -368,7 +367,6 @@
368367
" \"size\": \"0x3e1\",",
369368
" \"stateRoot\": \"0xb3f9408d80048b6f206951c4e387f8da37fb8510eccc18527865fa746c47bbc5\",",
370369
" \"timestamp\": \"0x56bff9bb\",",
371-
" \"totalDifficulty\": \"0x6332227c16fd7c67\",",
372370
" \"transactions\": [",
373371
" \"0x730724cb08a6eb17bf6b3296359d261570d343ea7944a17a9d7287d77900db08\",",
374372
" \"0xef2ea39c20ba09553b2f3cf02380406ac766039ca56612937eed5e7f3503fb3a\",",
@@ -870,7 +868,6 @@
870868
" \"size\": \"0x21a\",",
871869
" \"stateRoot\": \"0x1e6e030581fd1873b4784280859cd3b3c04aa85520f08c304cf5ee63d3935add\",",
872870
" \"timestamp\": \"0x55ba4242\",",
873-
" \"totalDifficulty\": \"0xffd003ffe\",",
874871
" \"transactionsRoot\": \"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421\",",
875872
" \"uncles\": []",
876873
" }",
@@ -949,7 +946,6 @@
949946
" \"size\": \"0x21a\",",
950947
" \"stateRoot\": \"0x1e6e030581fd1873b4784280859cd3b3c04aa85520f08c304cf5ee63d3935add\",",
951948
" \"timestamp\": \"0x55ba4242\",",
952-
" \"totalDifficulty\": \"0xffd003ffe\",",
953949
" \"transactionsRoot\": \"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421\",",
954950
" \"uncles\": []",
955951
" }",

Diff for: cmd/rpctest/rpctest/type.go

-1
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,6 @@ type OtsFullBlock struct {
326326
StateRoot string `json:"stateRoot"`
327327
Timestamp string `json:"timestamp"`
328328

329-
TotalDifficulty hexutil.Big `json:"totalDifficulty"`
330329
TransactionCount uint64 `json:"transactionCount"`
331330
Transactions []OtsTransaction `json:"transactions"`
332331
TxRoot libcommon.Hash `json:"transactionsRoot"`

Diff for: docs/readthedocs/source/rpc/index.rst

-3
Original file line numberDiff line numberDiff line change
@@ -265,9 +265,6 @@ Object - An object of type Block defined as:
265265
* - ``QUANTITY``
266266
- ``difficulty``
267267
- Integer of the difficulty for this block
268-
* - ``QUANTITY``
269-
- ``totalDifficulty``
270-
- Integer of the total difficulty of the chain until this block
271268
* - ``DATA``
272269
- ``extraData``
273270
- The extra data field of this block

Diff for: erigon-lib/common/dbg/experiments.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ var (
4949
noMerge = EnvBool("NO_MERGE", false)
5050
discardHistory = EnvBool("DISCARD_HISTORY", false)
5151
discardCommitment = EnvBool("DISCARD_COMMITMENT", false)
52-
pruneTotalDifficulty = EnvBool("PRUNE_TOTAL_DIFFICULTY", false)
52+
pruneTotalDifficulty = EnvBool("PRUNE_TOTAL_DIFFICULTY", true)
5353

5454
// force skipping of any non-Erigon2 .torrent files
5555
DownloaderOnlyBlocks = EnvBool("DOWNLOADER_ONLY_BLOCKS", false)

Diff for: eth/stagedsync/stage_snapshots.go

+8
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,7 @@ func pruneCanonicalMarkers(ctx context.Context, tx kv.RwTx, blockReader services
541541
return err
542542
}
543543
defer c.Close()
544+
var tdKey [40]byte
544545
for k, v, err := c.First(); k != nil && err == nil; k, v, err = c.Next() {
545546
blockNum := binary.BigEndian.Uint64(k)
546547
if blockNum == 0 { // Do not prune genesis marker
@@ -552,6 +553,13 @@ func pruneCanonicalMarkers(ctx context.Context, tx kv.RwTx, blockReader services
552553
if err := tx.Delete(kv.HeaderNumber, v); err != nil {
553554
return err
554555
}
556+
if dbg.PruneTotalDifficulty() {
557+
copy(tdKey[:], k)
558+
copy(tdKey[8:], v)
559+
if err := tx.Delete(kv.HeaderTD, tdKey[:]); err != nil {
560+
return err
561+
}
562+
}
555563
if err := c.DeleteCurrent(); err != nil {
556564
return err
557565
}

Diff for: turbo/adapter/ethapi/api.go

-24
Original file line numberDiff line numberDiff line change
@@ -374,30 +374,6 @@ func RPCMarshalBlockExDeprecated(block *types.Block, inclTx bool, fullTx bool, b
374374
return fields, nil
375375
}
376376

377-
/*
378-
379-
// rpcMarshalHeader uses the generalized output filler, then adds the total difficulty field, which requires
380-
// a `PublicBlockchainAPI`.
381-
func (s *PublicBlockChainAPI) rpcMarshalHeader(ctx context.Context, header *types.Header) map[string]interface{} {
382-
fields := RPCMarshalHeader(header)
383-
fields["totalDifficulty"] = (*hexutil.Big)(s.b.GetTd(ctx, header.Hash()))
384-
return fields
385-
}
386-
387-
// rpcMarshalBlock uses the generalized output filler, then adds the total difficulty field, which requires
388-
// a `PublicBlockchainAPI`.
389-
func (s *PublicBlockChainAPI) rpcMarshalBlock(ctx context.Context, b *types.Block, inclTx bool, fullTx bool) (map[string]interface{}, error) {
390-
fields, err := RPCMarshalBlock(b, inclTx, fullTx)
391-
if err != nil {
392-
return nil, err
393-
}
394-
if inclTx {
395-
fields["totalDifficulty"] = (*hexutil.Big)(s.b.GetTd(ctx, b.Hash()))
396-
}
397-
return fields, err
398-
}
399-
*/
400-
401377
// RPCTransaction represents a transaction that will serialize to the RPC representation of a transaction
402378
type RPCTransaction struct {
403379
BlockHash *libcommon.Hash `json:"blockHash"`

Diff for: turbo/jsonrpc/erigon_block.go

-7
Original file line numberDiff line numberDiff line change
@@ -197,13 +197,6 @@ func buildBlockResponse(ctx context.Context, br services.FullBlockReader, db kv.
197197
}
198198

199199
additionalFields := make(map[string]interface{})
200-
td, err := rawdb.ReadTd(db, header.Hash(), header.Number.Uint64())
201-
if err != nil {
202-
return nil, err
203-
}
204-
if td != nil {
205-
additionalFields["totalDifficulty"] = (*hexutil.Big)(td)
206-
}
207200

208201
response, err := ethapi.RPCMarshalBlockEx(block, true, fullTx, nil, common.Hash{}, additionalFields)
209202

Diff for: turbo/jsonrpc/eth_block.go

-13
Original file line numberDiff line numberDiff line change
@@ -228,13 +228,6 @@ func (api *APIImpl) GetBlockByNumber(ctx context.Context, number rpc.BlockNumber
228228
return nil, nil
229229
}
230230
additionalFields := make(map[string]interface{})
231-
td, err := rawdb.ReadTd(tx, b.Hash(), b.NumberU64())
232-
if err != nil {
233-
return nil, err
234-
}
235-
if td != nil {
236-
additionalFields["totalDifficulty"] = (*hexutil.Big)(td)
237-
}
238231

239232
chainConfig, err := api.chainConfig(ctx, tx)
240233
if err != nil {
@@ -289,12 +282,6 @@ func (api *APIImpl) GetBlockByHash(ctx context.Context, numberOrHash rpc.BlockNu
289282
}
290283
number := block.NumberU64()
291284

292-
td, err := rawdb.ReadTd(tx, hash, number)
293-
if err != nil {
294-
return nil, err
295-
}
296-
additionalFields["totalDifficulty"] = (*hexutil.Big)(td)
297-
298285
chainConfig, err := api.chainConfig(ctx, tx)
299286
if err != nil {
300287
return nil, err

Diff for: turbo/jsonrpc/eth_uncles.go

-12
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
"github.com/erigontech/erigon-lib/common"
2525
"github.com/erigontech/erigon-lib/log/v3"
2626

27-
"github.com/erigontech/erigon/core/rawdb"
2827
"github.com/erigontech/erigon/core/types"
2928
"github.com/erigontech/erigon/rpc"
3029
"github.com/erigontech/erigon/turbo/adapter/ethapi"
@@ -51,11 +50,6 @@ func (api *APIImpl) GetUncleByBlockNumberAndIndex(ctx context.Context, number rp
5150
return nil, nil // not error, see https://github.com/erigontech/erigon/issues/1645
5251
}
5352
additionalFields := make(map[string]interface{})
54-
td, err := rawdb.ReadTd(tx, block.Hash(), blockNum)
55-
if err != nil {
56-
return nil, err
57-
}
58-
additionalFields["totalDifficulty"] = (*hexutil.Big)(td)
5953

6054
uncles := block.Uncles()
6155
if index >= hexutil.Uint(len(uncles)) {
@@ -81,13 +75,7 @@ func (api *APIImpl) GetUncleByBlockHashAndIndex(ctx context.Context, hash common
8175
if block == nil {
8276
return nil, nil // not error, see https://github.com/erigontech/erigon/issues/1645
8377
}
84-
number := block.NumberU64()
8578
additionalFields := make(map[string]interface{})
86-
td, err := rawdb.ReadTd(tx, hash, number)
87-
if err != nil {
88-
return nil, err
89-
}
90-
additionalFields["totalDifficulty"] = (*hexutil.Big)(td)
9179

9280
uncles := block.Uncles()
9381
if index >= hexutil.Uint(len(uncles)) {

0 commit comments

Comments
 (0)