Commit ba2e0432 authored by Mark Tyneway's avatar Mark Tyneway Committed by GitHub

l2geth: add `VerifiedIndex` to DB and API (#520)

* l2geth: add verified index to db and api

* l2geth: add patch changeset
parent 1743a6d1
---
"@eth-optimism/l2geth": patch
---
Add `VerifiedIndex` to db and api
...@@ -7,6 +7,7 @@ import ( ...@@ -7,6 +7,7 @@ import (
"github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/log"
) )
// ReadHeadIndex will read the known tip of the CTC
func ReadHeadIndex(db ethdb.KeyValueReader) *uint64 { func ReadHeadIndex(db ethdb.KeyValueReader) *uint64 {
data, _ := db.Get(headIndexKey) data, _ := db.Get(headIndexKey)
if len(data) == 0 { if len(data) == 0 {
...@@ -16,6 +17,7 @@ func ReadHeadIndex(db ethdb.KeyValueReader) *uint64 { ...@@ -16,6 +17,7 @@ func ReadHeadIndex(db ethdb.KeyValueReader) *uint64 {
return &ret return &ret
} }
// WriteHeadIndex will write the known tip of the CTC
func WriteHeadIndex(db ethdb.KeyValueWriter, index uint64) { func WriteHeadIndex(db ethdb.KeyValueWriter, index uint64) {
value := new(big.Int).SetUint64(index).Bytes() value := new(big.Int).SetUint64(index).Bytes()
if index == 0 { if index == 0 {
...@@ -26,6 +28,7 @@ func WriteHeadIndex(db ethdb.KeyValueWriter, index uint64) { ...@@ -26,6 +28,7 @@ func WriteHeadIndex(db ethdb.KeyValueWriter, index uint64) {
} }
} }
// ReadHeadQueueIndex will read the known tip of the queue
func ReadHeadQueueIndex(db ethdb.KeyValueReader) *uint64 { func ReadHeadQueueIndex(db ethdb.KeyValueReader) *uint64 {
data, _ := db.Get(headQueueIndexKey) data, _ := db.Get(headQueueIndexKey)
if len(data) == 0 { if len(data) == 0 {
...@@ -35,6 +38,7 @@ func ReadHeadQueueIndex(db ethdb.KeyValueReader) *uint64 { ...@@ -35,6 +38,7 @@ func ReadHeadQueueIndex(db ethdb.KeyValueReader) *uint64 {
return &ret return &ret
} }
// WriteHeadQueueIndex will write the known tip of the queue
func WriteHeadQueueIndex(db ethdb.KeyValueWriter, index uint64) { func WriteHeadQueueIndex(db ethdb.KeyValueWriter, index uint64) {
value := new(big.Int).SetUint64(index).Bytes() value := new(big.Int).SetUint64(index).Bytes()
if index == 0 { if index == 0 {
...@@ -44,3 +48,24 @@ func WriteHeadQueueIndex(db ethdb.KeyValueWriter, index uint64) { ...@@ -44,3 +48,24 @@ func WriteHeadQueueIndex(db ethdb.KeyValueWriter, index uint64) {
log.Crit("Failed to store queue index", "err", err) log.Crit("Failed to store queue index", "err", err)
} }
} }
// ReadHeadVerifiedIndex will read the known tip of the batched transactions
func ReadHeadVerifiedIndex(db ethdb.KeyValueReader) *uint64 {
data, _ := db.Get(headVerifiedIndexKey)
if len(data) == 0 {
return nil
}
ret := new(big.Int).SetBytes(data).Uint64()
return &ret
}
// WriteHeadVerifiedIndex will write the known tip of the batched transactions
func WriteHeadVerifiedIndex(db ethdb.KeyValueWriter, index uint64) {
value := new(big.Int).SetUint64(index).Bytes()
if index == 0 {
value = []byte{0}
}
if err := db.Put(headVerifiedIndexKey, value); err != nil {
log.Crit("Failed to store verifier index", "err", err)
}
}
...@@ -60,6 +60,8 @@ var ( ...@@ -60,6 +60,8 @@ var (
headIndexKey = []byte("LastIndex") headIndexKey = []byte("LastIndex")
// headQueueIndexKey tracks th last processed queue index // headQueueIndexKey tracks th last processed queue index
headQueueIndexKey = []byte("LastQueueIndex") headQueueIndexKey = []byte("LastQueueIndex")
// headVerifiedIndexKey tracks the latest verified index
headVerifiedIndexKey = []byte("LastVerifiedIndex")
preimagePrefix = []byte("secure-key-") // preimagePrefix + hash -> preimage preimagePrefix = []byte("secure-key-") // preimagePrefix + hash -> preimage
configPrefix = []byte("ethereum-config-") // config prefix for the db configPrefix = []byte("ethereum-config-") // config prefix for the db
......
...@@ -71,18 +71,21 @@ func (b *EthAPIBackend) GetEthContext() (uint64, uint64) { ...@@ -71,18 +71,21 @@ func (b *EthAPIBackend) GetEthContext() (uint64, uint64) {
return bn, ts return bn, ts
} }
func (b *EthAPIBackend) GetRollupContext() (uint64, uint64) { func (b *EthAPIBackend) GetRollupContext() (uint64, uint64, uint64) {
i := uint64(0) index := uint64(0)
q := uint64(0) queueIndex := uint64(0)
index := b.eth.syncService.GetLatestIndex() verifiedIndex := uint64(0)
if index != nil {
i = *index if latest := b.eth.syncService.GetLatestIndex(); latest != nil {
index = *latest
}
if latest := b.eth.syncService.GetLatestEnqueueIndex(); latest != nil {
queueIndex = *latest
} }
queueIndex := b.eth.syncService.GetLatestEnqueueIndex() if latest := b.eth.syncService.GetLatestVerifiedIndex(); latest != nil {
if queueIndex != nil { verifiedIndex = *latest
q = *queueIndex
} }
return i, q return index, queueIndex, verifiedIndex
} }
// ChainConfig returns the active chain configuration. // ChainConfig returns the active chain configuration.
......
...@@ -1913,9 +1913,15 @@ type EthContext struct { ...@@ -1913,9 +1913,15 @@ type EthContext struct {
BlockNumber uint64 `json:"blockNumber"` BlockNumber uint64 `json:"blockNumber"`
Timestamp uint64 `json:"timestamp"` Timestamp uint64 `json:"timestamp"`
} }
// RollupContext represents the height of the rollup.
// Index is the last processed CanonicalTransactionChain index
// QueueIndex is the last processed `enqueue` index
// VerifiedIndex is the last processed CTC index that was batched
type RollupContext struct { type RollupContext struct {
Index uint64 `json:"index"` Index uint64 `json:"index"`
QueueIndex uint64 `json:"queueIndex"` QueueIndex uint64 `json:"queueIndex"`
VerifiedIndex uint64 `json:"verifiedIndex"`
} }
type rollupInfo struct { type rollupInfo struct {
...@@ -1932,7 +1938,7 @@ func (api *PublicRollupAPI) GetInfo(ctx context.Context) rollupInfo { ...@@ -1932,7 +1938,7 @@ func (api *PublicRollupAPI) GetInfo(ctx context.Context) rollupInfo {
} }
syncing := api.b.IsSyncing() syncing := api.b.IsSyncing()
bn, ts := api.b.GetEthContext() bn, ts := api.b.GetEthContext()
index, queueIndex := api.b.GetRollupContext() index, queueIndex, verifiedIndex := api.b.GetRollupContext()
return rollupInfo{ return rollupInfo{
Mode: mode, Mode: mode,
...@@ -1942,8 +1948,9 @@ func (api *PublicRollupAPI) GetInfo(ctx context.Context) rollupInfo { ...@@ -1942,8 +1948,9 @@ func (api *PublicRollupAPI) GetInfo(ctx context.Context) rollupInfo {
Timestamp: ts, Timestamp: ts,
}, },
RollupContext: RollupContext{ RollupContext: RollupContext{
Index: index, Index: index,
QueueIndex: queueIndex, QueueIndex: queueIndex,
VerifiedIndex: verifiedIndex,
}, },
} }
} }
......
...@@ -91,7 +91,7 @@ type Backend interface { ...@@ -91,7 +91,7 @@ type Backend interface {
IsVerifier() bool IsVerifier() bool
IsSyncing() bool IsSyncing() bool
GetEthContext() (uint64, uint64) GetEthContext() (uint64, uint64)
GetRollupContext() (uint64, uint64) GetRollupContext() (uint64, uint64, uint64)
GasLimit() uint64 GasLimit() uint64
GetDiff(*big.Int) (diffdb.Diff, error) GetDiff(*big.Int) (diffdb.Diff, error)
SuggestDataPrice(ctx context.Context) (*big.Int, error) SuggestDataPrice(ctx context.Context) (*big.Int, error)
......
...@@ -58,8 +58,8 @@ func (b *LesApiBackend) GetEthContext() (uint64, uint64) { ...@@ -58,8 +58,8 @@ func (b *LesApiBackend) GetEthContext() (uint64, uint64) {
return 0, 0 return 0, 0
} }
func (b *LesApiBackend) GetRollupContext() (uint64, uint64) { func (b *LesApiBackend) GetRollupContext() (uint64, uint64, uint64) {
return 0, 0 return 0, 0, 0
} }
func (b *LesApiBackend) IsSyncing() bool { func (b *LesApiBackend) IsSyncing() bool {
......
...@@ -548,40 +548,90 @@ func (s *SyncService) syncTransactionsToTip() error { ...@@ -548,40 +548,90 @@ func (s *SyncService) syncTransactionsToTip() error {
// Methods for safely accessing and storing the latest // Methods for safely accessing and storing the latest
// L1 blocknumber and timestamp. These are held in memory. // L1 blocknumber and timestamp. These are held in memory.
// GetLatestL1Timestamp returns the OVMContext timestamp
func (s *SyncService) GetLatestL1Timestamp() uint64 { func (s *SyncService) GetLatestL1Timestamp() uint64 {
return atomic.LoadUint64(&s.OVMContext.timestamp) return atomic.LoadUint64(&s.OVMContext.timestamp)
} }
// GetLatestL1BlockNumber returns the OVMContext blocknumber
func (s *SyncService) GetLatestL1BlockNumber() uint64 { func (s *SyncService) GetLatestL1BlockNumber() uint64 {
return atomic.LoadUint64(&s.OVMContext.blockNumber) return atomic.LoadUint64(&s.OVMContext.blockNumber)
} }
// SetLatestL1Timestamp will set the OVMContext timestamp
func (s *SyncService) SetLatestL1Timestamp(ts uint64) { func (s *SyncService) SetLatestL1Timestamp(ts uint64) {
atomic.StoreUint64(&s.OVMContext.timestamp, ts) atomic.StoreUint64(&s.OVMContext.timestamp, ts)
} }
// SetLatestL1BlockNumber will set the OVMContext blocknumber
func (s *SyncService) SetLatestL1BlockNumber(bn uint64) { func (s *SyncService) SetLatestL1BlockNumber(bn uint64) {
atomic.StoreUint64(&s.OVMContext.blockNumber, bn) atomic.StoreUint64(&s.OVMContext.blockNumber, bn)
} }
// GetLatestEnqueueIndex reads the last queue index processed
func (s *SyncService) GetLatestEnqueueIndex() *uint64 { func (s *SyncService) GetLatestEnqueueIndex() *uint64 {
return rawdb.ReadHeadQueueIndex(s.db) return rawdb.ReadHeadQueueIndex(s.db)
} }
// GetNextEnqueueIndex returns the next queue index to process
func (s *SyncService) GetNextEnqueueIndex() uint64 {
latest := s.GetLatestEnqueueIndex()
if latest == nil {
return 0
}
return *latest + 1
}
// SetLatestEnqueueIndex writes the last queue index that was processed
func (s *SyncService) SetLatestEnqueueIndex(index *uint64) { func (s *SyncService) SetLatestEnqueueIndex(index *uint64) {
if index != nil { if index != nil {
rawdb.WriteHeadQueueIndex(s.db, *index) rawdb.WriteHeadQueueIndex(s.db, *index)
} }
} }
// GetLatestIndex reads the last CTC index that was processed
func (s *SyncService) GetLatestIndex() *uint64 {
return rawdb.ReadHeadIndex(s.db)
}
// GetNextIndex reads the next CTC index to process
func (s *SyncService) GetNextIndex() uint64 {
latest := s.GetLatestIndex()
if latest == nil {
return 0
}
return *latest + 1
}
// SetLatestIndex writes the last CTC index that was processed
func (s *SyncService) SetLatestIndex(index *uint64) { func (s *SyncService) SetLatestIndex(index *uint64) {
if index != nil { if index != nil {
rawdb.WriteHeadIndex(s.db, *index) rawdb.WriteHeadIndex(s.db, *index)
} }
} }
func (s *SyncService) GetLatestIndex() *uint64 { // GetLatestVerifiedIndex reads the last verified CTC index that was processed
return rawdb.ReadHeadIndex(s.db) // These are set by processing batches of transactions that were submitted to
// the Canonical Transaction Chain.
func (s *SyncService) GetLatestVerifiedIndex() *uint64 {
return rawdb.ReadHeadVerifiedIndex(s.db)
}
// GetNextVerifiedIndex reads the next verified index
func (s *SyncService) GetNextVerifiedIndex() uint64 {
index := s.GetLatestVerifiedIndex()
if index == nil {
return 0
}
return *index + 1
}
// SetLatestVerifiedIndex writes the last verified index that was processed
func (s *SyncService) SetLatestVerifiedIndex(index *uint64) {
if index != nil {
rawdb.WriteHeadVerifiedIndex(s.db, *index)
}
} }
// reorganize will reorganize to directly to the index passed in. // reorganize will reorganize to directly to the index passed in.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment