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

feat: store celestia and sequencer heights #13

Closed
wants to merge 2 commits into from
Closed
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
43 changes: 37 additions & 6 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,9 @@ type BlockChain struct {
currentFinalBlock atomic.Pointer[types.Header] // Latest (consensus) finalized block
currentSafeBlock atomic.Pointer[types.Header] // Latest (consensus) safe block

currentBaseCelestiaHeight atomic.Uint32 // Latest finalized block height on Celestia
nextSequencerHeight atomic.Uint32 // Next sequencer block height

bodyCache *lru.Cache[common.Hash, *types.Body]
bodyRLPCache *lru.Cache[common.Hash, rlp.RawValue]
receiptsCache *lru.Cache[common.Hash, []*types.Receipt]
Expand Down Expand Up @@ -323,7 +326,8 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, genesis *Genesis
bc.currentBlock.Store(bc.genesisBlock.Header())
bc.currentFinalBlock.Store(bc.genesisBlock.Header())
bc.currentSafeBlock.Store(bc.genesisBlock.Header())

bc.currentBaseCelestiaHeight.Store(bc.Config().AstriaCelestiaInitialHeight)
bc.nextSequencerHeight.Store(bc.Config().AstriaSequencerInitialHeight)
// Update chain info data metrics
chainInfoGauge.Update(metrics.GaugeInfoValue{"chain_id": bc.chainConfig.ChainID.String()})

Expand Down Expand Up @@ -529,16 +533,27 @@ func (bc *BlockChain) loadLastState() error {
}

// Restore the last known finalized block and safe block
// Note: the safe block is not stored on disk and it is set to the last
// known finalized block on startup
if head := rawdb.ReadFinalizedBlockHash(bc.db); head != (common.Hash{}) {
if head := rawdb.ReadSafeBlockHash(bc.db); head != (common.Hash{}) {
if block := bc.GetBlockByHash(head); block != nil {
bc.currentFinalBlock.Store(block.Header())
headFinalizedBlockGauge.Update(int64(block.NumberU64()))
bc.currentSafeBlock.Store(block.Header())
headSafeBlockGauge.Update(int64(block.NumberU64()))
}
}
if final := rawdb.ReadFinalizedBlockHash(bc.db); final != (common.Hash{}) {
if block := bc.GetBlockByHash(final); block != nil {
bc.currentFinalBlock.Store(block.Header())
headFinalizedBlockGauge.Update(int64(block.NumberU64()))
}
}

// Set the Astria related sequencer height values
if height := rawdb.ReadBaseCelestiaBlockHeight(bc.db); *height != 0 {
bc.currentBaseCelestiaHeight.Store(*height)
}
if height := rawdb.ReadNextSequencerBlockHeight(bc.db); *height != 0 {
bc.nextSequencerHeight.Store(*height)
}

// Issue a status log for the user
var (
currentSnapBlock = bc.CurrentSnapBlock()
Expand Down Expand Up @@ -620,16 +635,32 @@ func (bc *BlockChain) SetFinalized(header *types.Header) {
}
}

// SetCelestiaFinalized sets the finalized block and the lowest Celestia height to find next finalized at.
func (bc *BlockChain) SetCelestiaFinalized(header *types.Header, celHeight uint32) {
rawdb.WriteBaseCelestiaBlockHeight(bc.db, celHeight)
bc.currentBaseCelestiaHeight.Store(celHeight)
bc.SetFinalized(header)
}

// SetSafe sets the safe block.
func (bc *BlockChain) SetSafe(header *types.Header) {
bc.currentSafeBlock.Store(header)
if header != nil {
rawdb.WriteSafeBlockHash(bc.db, header.Hash())
headSafeBlockGauge.Update(int64(header.Number.Uint64()))
} else {
rawdb.WriteSafeBlockHash(bc.db, common.Hash{})
headSafeBlockGauge.Update(0)
}
}

// SetSafeSequencer sets the safe block and the next height to use for sequencer block derivation.
func (bc *BlockChain) SetSafeSequencer(header *types.Header, seqHeight uint32) {
rawdb.WriteNextSequencerBlockHeight(bc.db, seqHeight)
bc.nextSequencerHeight.Store(seqHeight)
bc.SetSafe(header)
}

// setHeadBeyondRoot rewinds the local chain to a new head with the extra condition
// that the rewind must pass the specified state root. This method is meant to be
// used when rewinding with snapshots enabled to ensure that we go back further than
Expand Down
8 changes: 8 additions & 0 deletions core/blockchain_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ func (bc *BlockChain) CurrentFinalBlock() *types.Header {
return bc.currentFinalBlock.Load()
}

func (bc *BlockChain) CurrentBaseCelestiaHeight() uint32 {
return bc.currentBaseCelestiaHeight.Load()
}

func (bc *BlockChain) CurrentNextSequencerHeight() uint32 {
return bc.nextSequencerHeight.Load()
}

// CurrentSafeBlock retrieves the current safe block of the canonical
// chain. The block is retrieved from the blockchain's internal cache.
func (bc *BlockChain) CurrentSafeBlock() *types.Header {
Expand Down
54 changes: 53 additions & 1 deletion core/rawdb/accessors_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,59 @@ func ReadFinalizedBlockHash(db ethdb.KeyValueReader) common.Hash {
// WriteFinalizedBlockHash stores the hash of the finalized block.
func WriteFinalizedBlockHash(db ethdb.KeyValueWriter, hash common.Hash) {
if err := db.Put(headFinalizedBlockKey, hash.Bytes()); err != nil {
log.Crit("Failed to store last finalized block's hash", "err", err)
log.Crit("Failed to store last safe block's hash", "err", err)
}
}

// ReadSafeBlockHash retrieves the hash of the safe block.
func ReadSafeBlockHash(db ethdb.KeyValueReader) common.Hash {
data, _ := db.Get(headSafeBlockKey)
if len(data) == 0 {
return common.Hash{}
}
return common.BytesToHash(data)
}

// WriteSafeBlockHash stores the hash of the finalized block.
func WriteSafeBlockHash(db ethdb.KeyValueWriter, hash common.Hash) {
if err := db.Put(headSafeBlockKey, hash.Bytes()); err != nil {
log.Crit("Failed to store last safe block's hash", "err", err)
}
}

// ReadFinalizedCelestiaBlockHeight retrieves the height of the finalized block.
func ReadBaseCelestiaBlockHeight(db ethdb.KeyValueReader) *uint32 {
data, _ := db.Get(headBaseCelestiaHeightKey)
if len(data) != 8 {
return nil
}
number := binary.BigEndian.Uint32(data)
return &number
}

// WriteFinalizedCelestiaBlockHeight stores the height of the finalized block.
func WriteBaseCelestiaBlockHeight(db ethdb.KeyValueWriter, height uint32) {
byteHeight := encodeCometbftBlockNumber(height)
if err := db.Put(headBaseCelestiaHeightKey, byteHeight); err != nil {
log.Crit("Failed to store base celestia height", "err", err)
}
}

// ReadFinalizedCelestiaBlockHeight retrieves the hash of the finalized block.
func ReadNextSequencerBlockHeight(db ethdb.KeyValueReader) *uint32 {
data, _ := db.Get(headNextSequencerHeightKey)
if len(data) != 8 {
return nil
}
number := binary.BigEndian.Uint32(data)
return &number
}

// WriteFinalizedCelestiaBlockHeight stores the hash of the finalized block.
func WriteNextSequencerBlockHeight(db ethdb.KeyValueWriter, height uint32) {
byteHeight := encodeCometbftBlockNumber(height)
if err := db.Put(headNextSequencerHeightKey, byteHeight); err != nil {
log.Crit("Failed to store base celestia height", "err", err)
}
}

Expand Down
1 change: 1 addition & 0 deletions core/rawdb/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,7 @@ func InspectDatabase(db ethdb.Database, keyPrefix, keyStart []byte) error {
snapshotGeneratorKey, snapshotRecoveryKey, txIndexTailKey, fastTxLookupLimitKey,
uncleanShutdownKey, badBlockKey, transitionStatusKey, skeletonSyncStatusKey,
persistentStateIDKey, trieJournalKey, snapshotSyncStatusKey, snapSyncStatusFlagKey,
headBaseCelestiaHeightKey, headNextSequencerHeightKey, headSafeBlockKey,
} {
if bytes.Equal(key, meta) {
metadata.Add(size)
Expand Down
16 changes: 16 additions & 0 deletions core/rawdb/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ var (
// headFinalizedBlockKey tracks the latest known finalized block hash.
headFinalizedBlockKey = []byte("LastFinalized")

// headFinalizedBlockKey tracks the latest known finalized block hash.
headSafeBlockKey = []byte("LastSafe")

// headBaseCelestiaHeightKey tracks the lowest celestia height from which to attempt derivation.
headBaseCelestiaHeightKey = []byte("LastBaseCelestiaHeight")

// headNextSequencerHeightKey tracks the next sequencer height a block should be derived from.
headNextSequencerHeightKey = []byte("LastNextSequencerHeight")

// persistentStateIDKey tracks the id of latest stored state(for path-based only).
persistentStateIDKey = []byte("LastStateID")

Expand Down Expand Up @@ -151,6 +160,13 @@ func encodeBlockNumber(number uint64) []byte {
return enc
}

// encodeCometbftBlockNumber encodes a block number as big endian uint64
func encodeCometbftBlockNumber(number uint32) []byte {
enc := make([]byte, 8)
binary.BigEndian.PutUint32(enc, number)
return enc
}

// headerKeyPrefix = headerPrefix + num (uint64 big endian)
func headerKeyPrefix(number uint64) []byte {
return append(headerPrefix, encodeBlockNumber(number)...)
Expand Down
30 changes: 16 additions & 14 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ module github.com/ethereum/go-ethereum
go 1.21

require (
buf.build/gen/go/astria/execution-apis/grpc/go v1.3.0-20240423053323-ccf38db75f2f.2
buf.build/gen/go/astria/execution-apis/protocolbuffers/go v1.33.0-20240423053323-ccf38db75f2f.1
buf.build/gen/go/astria/primitives/protocolbuffers/go v1.33.0-20240422195039-812e347acd6b.1
buf.build/gen/go/astria/execution-apis/grpc/go v1.3.0-20240508004959-d652f1959029.3
buf.build/gen/go/astria/execution-apis/protocolbuffers/go v1.34.1-20240508004959-d652f1959029.1
buf.build/gen/go/astria/primitives/protocolbuffers/go v1.34.1-20240506224838-1cfc870032cf.1
github.com/Azure/azure-sdk-for-go/sdk/storage/azblob v1.2.0
github.com/Microsoft/go-winio v0.6.1
github.com/VictoriaMetrics/fastcache v1.12.1
Expand Down Expand Up @@ -36,7 +36,7 @@ require (
github.com/golang/protobuf v1.5.4
github.com/golang/snappy v0.0.5-0.20220116011046-fa5810519dcb
github.com/google/gofuzz v1.1.1-0.20200604201612-c04b05f3adfa
github.com/google/uuid v1.3.0
github.com/google/uuid v1.4.0
github.com/gorilla/websocket v1.5.0
github.com/graph-gophers/graphql-go v1.3.0
github.com/hashicorp/go-bexpr v0.1.10
Expand Down Expand Up @@ -66,21 +66,21 @@ require (
github.com/tyler-smith/go-bip39 v1.1.0
github.com/urfave/cli/v2 v2.25.7
go.uber.org/automaxprocs v1.5.2
golang.org/x/crypto v0.14.0
golang.org/x/crypto v0.15.0
golang.org/x/exp v0.0.0-20230905200255-921286631fa9
golang.org/x/sync v0.3.0
golang.org/x/sys v0.13.0
golang.org/x/text v0.13.0
golang.org/x/sync v0.5.0
golang.org/x/sys v0.14.0
golang.org/x/text v0.14.0
golang.org/x/time v0.3.0
golang.org/x/tools v0.13.0
google.golang.org/grpc v1.53.0
google.golang.org/protobuf v1.33.0
google.golang.org/grpc v1.61.2
google.golang.org/protobuf v1.34.1
gopkg.in/natefinch/lumberjack.v2 v2.0.0
gopkg.in/yaml.v3 v3.0.1
)

require (
buf.build/gen/go/astria/sequencerblock-apis/protocolbuffers/go v1.33.0-20240423053322-44396ca8658a.1 // indirect
buf.build/gen/go/astria/sequencerblock-apis/protocolbuffers/go v1.34.1-20240506224839-9f8a6e5fbe52.1 // indirect
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.7.0 // indirect
github.com/Azure/azure-sdk-for-go/sdk/internal v1.3.0 // indirect
github.com/DataDog/zstd v1.4.5 // indirect
Expand Down Expand Up @@ -133,7 +133,7 @@ require (
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.12.0 // indirect
github.com/prometheus/client_model v0.2.1-0.20210607210712-147c58e9608a // indirect
github.com/prometheus/client_model v0.4.0 // indirect
github.com/prometheus/common v0.32.1 // indirect
github.com/prometheus/procfs v0.7.3 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
Expand All @@ -143,8 +143,10 @@ require (
github.com/tklauser/numcpus v0.6.1 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
golang.org/x/mod v0.12.0 // indirect
golang.org/x/net v0.17.0 // indirect
google.golang.org/genproto v0.0.0-20230110181048-76db0878b65f // indirect
golang.org/x/net v0.18.0 // indirect
google.golang.org/genproto v0.0.0-20231106174013-bbf56f31fb17 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20231106174013-bbf56f31fb17 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231106174013-bbf56f31fb17 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
rsc.io/tmplfunc v0.0.3 // indirect
)
Loading
Loading