Skip to content

Commit

Permalink
feat!: store celestia height updates
Browse files Browse the repository at this point in the history
  • Loading branch information
joroshiba committed May 29, 2024
1 parent 8e07060 commit 88f7f4b
Show file tree
Hide file tree
Showing 8 changed files with 1,210 additions and 43 deletions.
16 changes: 16 additions & 0 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,8 @@ 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

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,6 +325,7 @@ 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)

// Update chain info data metrics
chainInfoGauge.Update(metrics.GaugeInfoValue{"chain_id": bc.chainConfig.ChainID.String()})
Expand Down Expand Up @@ -539,6 +542,11 @@ func (bc *BlockChain) loadLastState() error {
headSafeBlockGauge.Update(int64(block.NumberU64()))
}
}

if height := rawdb.ReadBaseCelestiaHeight(bc.db); height != 0 {
bc.currentBaseCelestiaHeight.Store(height)
}

// Issue a status log for the user
var (
currentSnapBlock = bc.CurrentSnapBlock()
Expand All @@ -547,6 +555,7 @@ func (bc *BlockChain) loadLastState() error {
headerTd = bc.GetTd(headHeader.Hash(), headHeader.Number.Uint64())
blockTd = bc.GetTd(headBlock.Hash(), headBlock.NumberU64())
)
log.Info("Loaded celestia base height", "height", bc.currentBaseCelestiaHeight.Load())
if headHeader.Hash() != headBlock.Hash() {
log.Info("Loaded most recent local header", "number", headHeader.Number, "hash", headHeader.Hash(), "td", headerTd, "age", common.PrettyAge(time.Unix(int64(headHeader.Time), 0)))
}
Expand Down Expand Up @@ -620,6 +629,13 @@ 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.WriteBaseCelestiaHeight(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)
Expand Down
6 changes: 6 additions & 0 deletions core/blockchain_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ func (bc *BlockChain) CurrentSafeBlock() *types.Header {
return bc.currentSafeBlock.Load()
}

// CurrentBaseCelestiaHeight retrieves the current base celestia height of the
// canonical chain. The height is retrieved from the blockchain's internal cache.
func (bc *BlockChain) CurrentBaseCelestiaHeight() uint32 {
return bc.currentBaseCelestiaHeight.Load()
}

// HasHeader checks if a block header is present in the database or not, caching
// it if present.
func (bc *BlockChain) HasHeader(hash common.Hash, number uint64) bool {
Expand Down
18 changes: 18 additions & 0 deletions core/rawdb/accessors_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,24 @@ func WriteFinalizedBlockHash(db ethdb.KeyValueWriter, hash common.Hash) {
}
}

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

// WriteFinalizedCelestiaBlockHeight stores the height of the finalized block.
func WriteBaseCelestiaHeight(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)
}
}

// ReadLastPivotNumber retrieves the number of the last pivot block. If the node
// full synced, the last pivot will always be nil.
func ReadLastPivotNumber(db ethdb.KeyValueReader) *uint64 {
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,
} {
if bytes.Equal(key, meta) {
metadata.Add(size)
Expand Down
10 changes: 10 additions & 0 deletions core/rawdb/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ var (
// headFinalizedBlockKey tracks the latest known finalized block hash.
headFinalizedBlockKey = []byte("LastFinalized")

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

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

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

// encodeCometbftBlockNumber encodes a block number as big endian uint32
func encodeCometbftBlockNumber(number uint32) []byte {
enc := make([]byte, 4)
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-00000000000000-84e5e35facb9.3
buf.build/gen/go/astria/execution-apis/protocolbuffers/go v1.34.1-00000000000000-84e5e35facb9.1
buf.build/gen/go/astria/primitives/protocolbuffers/go v1.34.1-20240529204957-2697e2110d78.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-20240529204957-1b3cb2034833.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

0 comments on commit 88f7f4b

Please sign in to comment.