Skip to content

Commit 393cf17

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

File tree

81 files changed

+3183
-92
lines changed

Some content is hidden

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

81 files changed

+3183
-92
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

consensus/dbft/block.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ type Block struct {
2525
header *types.Header
2626
withdrawals []*types.Withdrawal
2727
transactions []*types.Transaction
28+
sidecars types.BlobSidecars
2829
localSignatureBytes []byte
2930

3031
// Local data got after [dbft.Block] construction. Always non-nil in a properly

consensus/dbft/dbft.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ type DBFT struct {
204204
// mutex since every access point is controlled by eventLoop, thus, not concurrent.
205205
sealingProposal *types.Header
206206
sealingTransactions types.Transactions
207+
sealingSidecars types.BlobSidecars
207208

208209
// sealingState, sealingBlock and sealingReceipts are Primary-only set of fields got
209210
// after sealingProposal construction and processing. These fields are not protected
@@ -512,6 +513,10 @@ func (c *DBFT) processBlockCb(b dbft.Block[common.Hash]) error {
512513
state := dbftBlock.state.Copy()
513514
res := types.NewBlockWithHeader(dbftBlock.header).WithBody(types.Body{Transactions: dbftBlock.transactions, Uncles: nil, Withdrawals: dbftBlock.withdrawals})
514515

516+
if c.chain.Config().IsCancun(dbftBlock.header.Number, dbftBlock.header.Time) {
517+
res = res.WithSidecars(dbftBlock.sidecars).WithSeal(dbftBlock.header)
518+
}
519+
515520
// Firstly, notify chain about new block.
516521
if err := c.blockQueue.PutBlock(res, dbftBlock.state, dbftBlock.receipts); err != nil {
517522
// The block might already be added via the regular network
@@ -545,6 +550,7 @@ func (c *DBFT) newBlockFromContextCb(ctx *dbft.Context[common.Hash]) dbft.Block[
545550
if ctx.IsPrimary() {
546551
res.state = c.sealingState
547552
res.receipts = c.sealingReceipts
553+
res.sidecars = c.sealingSidecars
548554
}
549555
return res
550556
}
@@ -557,6 +563,7 @@ func (c *DBFT) newBlockFromContextCb(ctx *dbft.Context[common.Hash]) dbft.Block[
557563
header: c.sealingBlock.Header(),
558564
withdrawals: c.sealingBlock.Withdrawals(),
559565
transactions: c.sealingBlock.Transactions(),
566+
sidecars: c.sealingBlock.Sidecars(),
560567
localSignatureBytes: nil,
561568
state: c.sealingState,
562569
receipts: c.sealingReceipts,
@@ -715,6 +722,9 @@ func (c *DBFT) newPrepareRequestCb(ts uint64, nonce uint64, txHashes []common.Ha
715722
log.Crit("Failed to finalize and assemble proposed block",
716723
"err", err)
717724
}
725+
if c.chain.Config().IsCancun(res.Header().Number, res.Header().Time) {
726+
res = res.WithSidecars(c.sealingSidecars)
727+
}
718728

719729
c.sealingProposal = res.Header()
720730
c.sealingState = state
@@ -904,6 +914,7 @@ func (c *DBFT) verifyPrepareRequestCb(p dbft.ConsensusPayload[common.Hash]) erro
904914
// sealingTransactions are not needed for proper dBFT functioning (dBFT will collect
905915
// transactions via internal mechanism in this consensus view).
906916
c.sealingTransactions = nil
917+
c.sealingSidecars = nil
907918

908919
return nil
909920
}
@@ -980,6 +991,9 @@ func (c *DBFT) verifyBlockCb(b dbft.Block[common.Hash]) bool {
980991
}
981992

982993
ethBlock := types.NewBlockWithHeader(dbftBlock.header).WithBody(types.Body{Transactions: dbftBlock.transactions, Uncles: nil, Withdrawals: dbftBlock.withdrawals})
994+
if c.chain.Config().IsCancun(ethBlock.Header().Number, ethBlock.Header().Time) {
995+
ethBlock = ethBlock.WithSidecars(dbftBlock.sidecars)
996+
}
983997
state, receipts, _, err := c.chain.VerifyBlock(ethBlock, true)
984998
if err != nil {
985999
log.Warn("proposed block verification failed",
@@ -2270,11 +2284,13 @@ func (c *DBFT) waitForNewSealingProposal(desiredHeight uint64, updateContext boo
22702284

22712285
c.sealingProposal = lastProposal.Header()
22722286
c.sealingTransactions = lastProposal.Transactions()
2287+
c.sealingSidecars = lastProposal.Sidecars()
22732288
log.Info("Sealing proposal updated",
22742289
"number", c.sealingProposal.Number,
22752290
"sealhash", c.SealHash(c.sealingProposal),
22762291
"parent hash", c.sealingProposal.ParentHash,
2277-
"txs", len(c.sealingTransactions))
2292+
"txs", len(c.sealingTransactions),
2293+
"sidecars", len(c.sealingSidecars))
22782294

22792295
if updateContext {
22802296
// dBFT can't update its PrevHash in the middle of consensus process, thus,

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 {

0 commit comments

Comments
 (0)