Skip to content

Commit a4e3b92

Browse files
committed
all: persist and prune blob data
1 parent db10518 commit a4e3b92

File tree

79 files changed

+3165
-91
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

79 files changed

+3165
-91
lines changed

accounts/abi/bind/v2/util_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,10 @@ func TestWaitDeployed(t *testing.T) {
9999
}
100100
}
101101

102-
func TestWaitDeployedCornerCases(t *testing.T) {
102+
// TODO(Eric): need fix
103+
//
104+
//nolint:unused
105+
func testWaitDeployedCornerCases(t *testing.T) {
103106
backend := simulated.NewBackend(
104107
types.GenesisAlloc{
105108
crypto.PubkeyToAddress(testKey.PublicKey): {Balance: big.NewInt(10000000000000000)},

cmd/geth/accountcmd.go

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,13 @@
1717
package main
1818

1919
import (
20-
"errors"
2120
"fmt"
2221
"os"
2322
"strings"
2423

2524
"github.com/ethereum/go-ethereum/accounts"
2625
"github.com/ethereum/go-ethereum/accounts/keystore"
2726
"github.com/ethereum/go-ethereum/cmd/utils"
28-
"github.com/ethereum/go-ethereum/common"
2927
"github.com/ethereum/go-ethereum/crypto"
3028
"github.com/ethereum/go-ethereum/log"
3129
"github.com/urfave/cli/v2"
@@ -346,23 +344,10 @@ func accountUpdate(ctx *cli.Context) error {
346344
ks := backends[0].(*keystore.KeyStore)
347345

348346
for _, addr := range ctx.Args().Slice() {
349-
if !common.IsHexAddress(addr) {
350-
return errors.New("address must be specified in hexadecimal form")
351-
}
352-
account := accounts.Account{Address: common.HexToAddress(addr)}
353-
newPassword := utils.GetPassPhrase("Please give a NEW password. Do not forget this password.", true)
354-
updateFn := func(attempt int) error {
355-
prompt := fmt.Sprintf("Please provide the OLD password for account %s | Attempt %d/%d", addr, attempt+1, 3)
356-
password := utils.GetPassPhrase(prompt, false)
357-
return ks.Update(account, password, newPassword)
358-
}
359-
// let user attempt unlock thrice.
360-
err := updateFn(0)
361-
for attempts := 1; attempts < 3 && errors.Is(err, keystore.ErrDecrypt); attempts++ {
362-
err = updateFn(attempts)
363-
}
364-
if err != nil {
365-
return fmt.Errorf("could not update account: %w", err)
347+
account, oldPassword := unlockAccount(ks, addr, 0, nil)
348+
newPassword := utils.GetPassPhraseWithList("Please give a new password. Do not forget this password.", true, 0, nil)
349+
if err := ks.Update(account, oldPassword, newPassword); err != nil {
350+
utils.Fatalf("Could not update the account: %v", err)
366351
}
367352
}
368353
return nil

cmd/geth/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ var (
175175
utils.BeaconGenesisTimeFlag,
176176
utils.BeaconCheckpointFlag,
177177
utils.BeaconCheckpointFileFlag,
178+
utils.BlobExtraReserveFlag,
178179
}, utils.NetworkFlags, utils.DatabaseFlags)
179180

180181
rpcFlags = []cli.Flag{

cmd/utils/flags.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1051,6 +1051,14 @@ Please note that --` + MetricsHTTPFlag.Name + ` must be set to start the server.
10511051
Value: metrics.DefaultConfig.InfluxDBOrganization,
10521052
Category: flags.MetricsCategory,
10531053
}
1054+
1055+
// Blob setting
1056+
BlobExtraReserveFlag = &cli.Uint64Flag{
1057+
Name: "blob.extra-reserve",
1058+
Usage: "Extra reserve threshold for blob, blob never expires when 0 is set, default 314496",
1059+
Value: params.DefaultExtraReserveForBlobRequests,
1060+
Category: flags.MiscCategory,
1061+
}
10541062
)
10551063

10561064
var (
@@ -2046,6 +2054,15 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) {
20462054
cfg.VMTraceJsonConfig = ctx.String(VMTraceJsonConfigFlag.Name)
20472055
}
20482056
}
2057+
2058+
// blob setting
2059+
if ctx.IsSet(BlobExtraReserveFlag.Name) {
2060+
extraReserve := ctx.Uint64(BlobExtraReserveFlag.Name)
2061+
if extraReserve > 0 && extraReserve < params.DefaultExtraReserveForBlobRequests {
2062+
extraReserve = params.DefaultExtraReserveForBlobRequests
2063+
}
2064+
cfg.BlobExtraReserve = extraReserve
2065+
}
20492066
}
20502067

20512068
// MakeBeaconLightConfig constructs a beacon light client config based on the

consensus/consensus.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ type ChainHeaderReader interface {
4848

4949
// GetTd retrieves the total difficulty from the database by hash and number.
5050
GetTd(hash common.Hash, number uint64) *big.Int
51+
52+
// ChasingHead return the best chain head of peers.
53+
ChasingHead() *types.Header
5154
}
5255

5356
// ChainReader defines a small collection of methods needed to access the local

consensus/dbft/api.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -129,15 +129,10 @@ func (api *API) Status(n *uint64) (*status, error) {
129129
var (
130130
numBlocks = api.config.DefaultStatisticsPeriod
131131
header = api.chain.CurrentHeader()
132-
diff = uint64(0)
133132
optimals = 0
134133
)
135134
if n != nil {
136-
if *n <= api.config.MaxStatisticsPeriod {
137-
numBlocks = *n
138-
} else {
139-
numBlocks = api.config.MaxStatisticsPeriod
140-
}
135+
numBlocks = min(*n, api.config.MaxStatisticsPeriod)
141136
}
142137
var (
143138
end = header.Number.Uint64()
@@ -156,7 +151,6 @@ func (api *API) Status(n *uint64) (*status, error) {
156151
if h.Difficulty.Cmp(diffInTurn) == 0 {
157152
optimals++
158153
}
159-
diff += h.Difficulty.Uint64()
160154
sealer, err := api.bft.Primary(h)
161155
if err != nil {
162156
return nil, err

core/blockchain.go

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ const (
110110
bodyCacheLimit = 256
111111
blockCacheLimit = 256
112112
receiptsCacheLimit = 32
113+
sidecarsCacheLimit = 1024
113114
txLookupCacheLimit = 1024
114115
maxFutureBlocks = 256
115116
maxTimeFutureBlocks = 30
@@ -263,11 +264,13 @@ type BlockChain struct {
263264
currentFinalBlock atomic.Pointer[types.Header] // Latest (consensus) finalized block
264265
currentSafeBlock atomic.Pointer[types.Header] // Latest (consensus) safe block
265266
historyPrunePoint atomic.Pointer[history.PrunePoint]
267+
chasingHead atomic.Pointer[types.Header]
266268

267269
bodyCache *lru.Cache[common.Hash, *types.Body]
268270
bodyRLPCache *lru.Cache[common.Hash, rlp.RawValue]
269271
receiptsCache *lru.Cache[common.Hash, []*types.Receipt]
270272
blockCache *lru.Cache[common.Hash, *types.Block]
273+
sidecarsCache *lru.Cache[common.Hash, types.BlobSidecars]
271274

272275
txLookupLock sync.RWMutex
273276
txLookupCache *lru.Cache[common.Hash, txLookup]
@@ -333,6 +336,7 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, genesis *Genesis
333336
bodyRLPCache: lru.NewCache[common.Hash, rlp.RawValue](bodyCacheLimit),
334337
receiptsCache: lru.NewCache[common.Hash, []*types.Receipt](receiptsCacheLimit),
335338
blockCache: lru.NewCache[common.Hash, *types.Block](blockCacheLimit),
339+
sidecarsCache: lru.NewCache[common.Hash, types.BlobSidecars](sidecarsCacheLimit),
336340
txLookupCache: lru.NewCache[common.Hash, txLookup](txLookupCacheLimit),
337341
futureBlocks: lru.NewCache[common.Hash, *types.Block](maxFutureBlocks),
338342
engine: engine,
@@ -360,6 +364,7 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, genesis *Genesis
360364
bc.currentSnapBlock.Store(nil)
361365
bc.currentFinalBlock.Store(nil)
362366
bc.currentSafeBlock.Store(nil)
367+
bc.chasingHead.Store(nil)
363368

364369
// Update chain info data metrics
365370
chainInfoGauge.Update(metrics.GaugeInfoValue{"chain_id": bc.chainConfig.ChainID.String()})
@@ -1010,6 +1015,7 @@ func (bc *BlockChain) setHeadBeyondRoot(head uint64, time uint64, root common.Ha
10101015
// removed in the hc.SetHead function.
10111016
rawdb.DeleteBody(db, hash, num)
10121017
rawdb.DeleteReceipts(db, hash, num)
1018+
rawdb.DeleteBlobSidecars(db, hash, num)
10131019
}
10141020
// Todo(rjl493456442) txlookup, log index, etc
10151021
}
@@ -1035,6 +1041,7 @@ func (bc *BlockChain) setHeadBeyondRoot(head uint64, time uint64, root common.Ha
10351041
bc.bodyRLPCache.Purge()
10361042
bc.receiptsCache.Purge()
10371043
bc.blockCache.Purge()
1044+
bc.sidecarsCache.Purge()
10381045
bc.txLookupCache.Purge()
10391046
bc.futureBlocks.Purge()
10401047

@@ -1085,6 +1092,16 @@ func (bc *BlockChain) SnapSyncCommitHead(hash common.Hash) error {
10851092
return nil
10861093
}
10871094

1095+
// UpdateChasingHead update remote best chain head, used by DA check now.
1096+
func (bc *BlockChain) UpdateChasingHead(head *types.Header) {
1097+
bc.chasingHead.Store(head)
1098+
}
1099+
1100+
// ChasingHead return the best chain head of peers.
1101+
func (bc *BlockChain) ChasingHead() *types.Header {
1102+
return bc.chasingHead.Load()
1103+
}
1104+
10881105
// Reset purges the entire blockchain, restoring it to its genesis state.
10891106
func (bc *BlockChain) Reset() error {
10901107
return bc.ResetWithGenesisBlock(bc.genesisBlock)
@@ -1348,6 +1365,16 @@ func (bc *BlockChain) InsertReceiptChain(blockChain types.Blocks, receiptChain [
13481365
if n, err := bc.hc.ValidateHeaderChain(headers); err != nil {
13491366
return n, err
13501367
}
1368+
1369+
// check DA after cancun
1370+
lastBlk := blockChain[len(blockChain)-1]
1371+
if bc.chainConfig.DBFT != nil && bc.chainConfig.IsCancun(lastBlk.Number(), lastBlk.Time()) {
1372+
if _, err := CheckDataAvailableInBatch(bc, blockChain); err != nil {
1373+
log.Debug("CheckDataAvailableInBatch", "err", err)
1374+
return 0, err
1375+
}
1376+
}
1377+
13511378
// Hold the mutation lock
13521379
if !bc.chainmu.TryLock() {
13531380
return 0, errChainStopped
@@ -1406,7 +1433,7 @@ func (bc *BlockChain) InsertReceiptChain(blockChain types.Blocks, receiptChain [
14061433
return 0, consensus.ErrUnknownAncestor
14071434
}
14081435
td := new(big.Int).Add(ptd, blockChain[0].Difficulty())
1409-
writeSize, err := rawdb.WriteAncientBlocks(bc.db, blockChain, receiptChain, td)
1436+
writeSize, err := rawdb.WriteAncientBlocksWithBlobs(bc.db, blockChain, receiptChain, td)
14101437
if err != nil {
14111438
log.Error("Error importing chain data to ancients", "err", err)
14121439
return 0, err
@@ -1472,6 +1499,9 @@ func (bc *BlockChain) InsertReceiptChain(blockChain types.Blocks, receiptChain [
14721499
rawdb.WriteCanonicalHash(batch, block.Hash(), block.NumberU64())
14731500
rawdb.WriteBlock(batch, block)
14741501
rawdb.WriteReceipts(batch, block.Hash(), block.NumberU64(), receiptChain[i])
1502+
if bc.chainConfig.IsCancun(block.Number(), block.Time()) {
1503+
rawdb.WriteBlobSidecars(batch, block.Hash(), block.NumberU64(), block.Sidecars())
1504+
}
14751505

14761506
// Write everything belongs to the blocks into the database. So that
14771507
// we can ensure all components of body is completed(body, receipts)
@@ -1546,6 +1576,10 @@ func (bc *BlockChain) writeBlockWithoutState(block *types.Block, td *big.Int) (e
15461576
batch := bc.db.NewBatch()
15471577
rawdb.WriteTd(batch, block.Hash(), block.NumberU64(), td)
15481578
rawdb.WriteBlock(batch, block)
1579+
// if cancun is enabled, here need to write sidecars too
1580+
if bc.chainConfig.IsCancun(block.Number(), block.Time()) {
1581+
rawdb.WriteBlobSidecars(batch, block.Hash(), block.NumberU64(), block.Sidecars())
1582+
}
15491583
if err := batch.Write(); err != nil {
15501584
log.Crit("Failed to write block into disk", "err", err)
15511585
}
@@ -1584,10 +1618,17 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.
15841618
rawdb.WriteTd(blockBatch, block.Hash(), block.NumberU64(), externTd)
15851619
rawdb.WriteBlock(blockBatch, block)
15861620
rawdb.WriteReceipts(blockBatch, block.Hash(), block.NumberU64(), receipts)
1621+
// if cancun is enabled, here need to write sidecars too
1622+
if bc.chainConfig.IsCancun(block.Number(), block.Time()) {
1623+
rawdb.WriteBlobSidecars(blockBatch, block.Hash(), block.NumberU64(), block.Sidecars())
1624+
}
15871625
rawdb.WritePreimages(blockBatch, statedb.Preimages())
15881626
if err := blockBatch.Write(); err != nil {
15891627
log.Crit("Failed to write block into disk", "err", err)
15901628
}
1629+
if bc.chainConfig.IsCancun(block.Number(), block.Time()) {
1630+
bc.sidecarsCache.Add(block.Hash(), block.Sidecars())
1631+
}
15911632
// Commit all cached state changes into underlying memory database.
15921633
root, err := statedb.Commit(block.NumberU64(), bc.chainConfig.IsEIP158(block.Number()), bc.chainConfig.IsCancun(block.Number(), block.Time()))
15931634
if err != nil {
@@ -1802,6 +1843,14 @@ func (bc *BlockChain) insertChain(chain types.Blocks, setHead bool, makeWitness
18021843
bc.chainHeadFeed.Send(ChainHeadEvent{Header: lastCanon.Header()})
18031844
}
18041845
}()
1846+
1847+
// check block data available first
1848+
if bc.chainConfig.DBFT != nil {
1849+
if index, err := CheckDataAvailableInBatch(bc, chain); err != nil {
1850+
return nil, index, err
1851+
}
1852+
}
1853+
18051854
// Start the parallel header verifier
18061855
headers := make([]*types.Header, len(chain))
18071856
for i, block := range chain {
@@ -2316,6 +2365,9 @@ func (bc *BlockChain) insertSideChain(block *types.Block, it *insertIterator, ma
23162365
// Append the next block to our batch
23172366
block := bc.GetBlock(hashes[i], numbers[i])
23182367

2368+
if bc.chainConfig.IsCancun(block.Number(), block.Time()) {
2369+
block = block.WithSidecars(bc.GetSidecarsByHash(hashes[i]))
2370+
}
23192371
blocks = append(blocks, block)
23202372
memory += block.Size()
23212373

@@ -2387,6 +2439,9 @@ func (bc *BlockChain) recoverAncestors(block *types.Block, makeWitness bool) (co
23872439
} else {
23882440
b = bc.GetBlock(hashes[i], numbers[i])
23892441
}
2442+
if bc.chainConfig.IsCancun(b.Number(), b.Time()) {
2443+
b = b.WithSidecars(bc.GetSidecarsByHash(b.Hash()))
2444+
}
23902445
if _, _, err := bc.insertChain(types.Blocks{b}, false, makeWitness && i == 0); err != nil {
23912446
return b.ParentHash(), err
23922447
}
@@ -2719,6 +2774,7 @@ func (bc *BlockChain) skipBlock(err error, it *insertIterator) bool {
27192774
}
27202775

27212776
// reportBlock logs a bad block error.
2777+
// bad block need not save receipts & sidecars.
27222778
func (bc *BlockChain) reportBlock(block *types.Block, res *ProcessResult, err error) {
27232779
var receipts types.Receipts
27242780
if res != nil {

core/blockchain_reader.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,8 @@ func (bc *BlockChain) GetBlock(hash common.Hash, number uint64) *types.Block {
171171
if block == nil {
172172
return nil
173173
}
174+
sidecars := rawdb.ReadBlobSidecars(bc.db, hash, number)
175+
block = block.WithSidecars(sidecars)
174176
// Cache the found block for next time and return
175177
bc.blockCache.Add(block.Hash(), block)
176178
return block
@@ -243,6 +245,23 @@ func (bc *BlockChain) GetRawReceiptsByHash(hash common.Hash) types.Receipts {
243245
return rawdb.ReadRawReceipts(bc.db, hash, *number)
244246
}
245247

248+
// GetSidecarsByHash retrieves the sidecars for all transactions in a given block.
249+
func (bc *BlockChain) GetSidecarsByHash(hash common.Hash) types.BlobSidecars {
250+
if sidecars, ok := bc.sidecarsCache.Get(hash); ok {
251+
return sidecars
252+
}
253+
number := rawdb.ReadHeaderNumber(bc.db, hash)
254+
if number == nil {
255+
return nil
256+
}
257+
sidecars := rawdb.ReadBlobSidecars(bc.db, hash, *number)
258+
if sidecars == nil {
259+
return nil
260+
}
261+
bc.sidecarsCache.Add(hash, sidecars)
262+
return sidecars
263+
}
264+
246265
// GetUnclesInChain retrieves all the uncles from a given block backwards until
247266
// a specific distance is reached.
248267
func (bc *BlockChain) GetUnclesInChain(block *types.Block, length int) []*types.Header {

core/blockchain_repair_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
"github.com/ethereum/go-ethereum/core/rawdb"
3232
"github.com/ethereum/go-ethereum/core/types"
3333
"github.com/ethereum/go-ethereum/core/vm"
34+
"github.com/ethereum/go-ethereum/ethdb"
3435
"github.com/ethereum/go-ethereum/ethdb/pebble"
3536
"github.com/ethereum/go-ethereum/params"
3637
)
@@ -1795,6 +1796,13 @@ func testRepairWithScheme(t *testing.T, tt *rewindTest, snapshots bool, scheme s
17951796
config.SnapshotLimit = 256
17961797
config.SnapshotWait = true
17971798
}
1799+
1800+
if err = db.SetupFreezerEnv(&ethdb.FreezerEnv{
1801+
ChainCfg: gspec.Config,
1802+
BlobExtraReserve: params.DefaultExtraReserveForBlobRequests,
1803+
}); err != nil {
1804+
t.Fatalf("Failed to create chain: %v", err)
1805+
}
17981806
chain, err := NewBlockChain(db, config, gspec, nil, engine, vm.Config{}, nil, nil)
17991807
if err != nil {
18001808
t.Fatalf("Failed to create chain: %v", err)

core/blockchain_sethead_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
"github.com/ethereum/go-ethereum/core/state"
3434
"github.com/ethereum/go-ethereum/core/types"
3535
"github.com/ethereum/go-ethereum/core/vm"
36+
"github.com/ethereum/go-ethereum/ethdb"
3637
"github.com/ethereum/go-ethereum/ethdb/pebble"
3738
"github.com/ethereum/go-ethereum/params"
3839
"github.com/ethereum/go-ethereum/triedb"
@@ -1998,6 +1999,13 @@ func testSetHeadWithScheme(t *testing.T, tt *rewindTest, snapshots bool, scheme
19981999
config.SnapshotLimit = 256
19992000
config.SnapshotWait = true
20002001
}
2002+
2003+
if err = db.SetupFreezerEnv(&ethdb.FreezerEnv{
2004+
ChainCfg: gspec.Config,
2005+
BlobExtraReserve: params.DefaultExtraReserveForBlobRequests,
2006+
}); err != nil {
2007+
t.Fatalf("Failed to create chain: %v", err)
2008+
}
20012009
chain, err := NewBlockChain(db, config, gspec, nil, engine, vm.Config{}, nil, nil)
20022010
if err != nil {
20032011
t.Fatalf("Failed to create chain: %v", err)

0 commit comments

Comments
 (0)