Skip to content
This repository was archived by the owner on Apr 11, 2021. It is now read-only.

feat: implement sync service spec #300

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
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
19 changes: 19 additions & 0 deletions core/rawdb/rollup_indexes.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,22 @@ func WriteHeadQueueIndex(db ethdb.KeyValueWriter, index uint64) {
log.Crit("Failed to store queue index", "err", err)
}
}

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
}

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 verfied index", "err", err)
}
}
4 changes: 3 additions & 1 deletion core/rawdb/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,10 @@ var (

// headIndexKey tracks the last processed ctc index
headIndexKey = []byte("LastIndex")
// headQueueIndexKey tracks th last processed queue index
// headQueueIndexKey tracks the last processed queue index
headQueueIndexKey = []byte("LastQueueIndex")
// headVerifiedIndexKey tracks the latest verified index
headVerifiedIndexKey = []byte("LastVerifiedIndex")

preimagePrefix = []byte("secure-key-") // preimagePrefix + hash -> preimage
configPrefix = []byte("ethereum-config-") // config prefix for the db
Expand Down
11 changes: 8 additions & 3 deletions eth/api_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,10 @@ func (b *EthAPIBackend) GetEthContext() (uint64, uint64) {
return bn, ts
}

func (b *EthAPIBackend) GetRollupContext() (uint64, uint64) {
func (b *EthAPIBackend) GetRollupContext() (uint64, uint64, uint64) {
i := uint64(0)
q := uint64(0)
v := uint64(0)
index := b.eth.syncService.GetLatestIndex()
if index != nil {
i = *index
Expand All @@ -81,7 +82,11 @@ func (b *EthAPIBackend) GetRollupContext() (uint64, uint64) {
if queueIndex != nil {
q = *queueIndex
}
return i, q
verifiedIndex := b.eth.syncService.GetLatestVerifiedIndex()
if verifiedIndex != nil {
v = *verifiedIndex
}
return i, q, v
}

// ChainConfig returns the active chain configuration.
Expand Down Expand Up @@ -312,7 +317,7 @@ func (b *EthAPIBackend) SendTx(ctx context.Context, signedTx *types.Transaction)
return fmt.Errorf("Calldata cannot be larger than %d, sent %d", b.MaxCallDataSize, len(signedTx.Data()))
}
}
return b.eth.syncService.ApplyTransaction(signedTx)
return b.eth.syncService.ValidateAndApplySequencerTransaction(signedTx)
}
// OVM Disabled
return b.eth.txPool.AddLocal(signedTx)
Expand Down
12 changes: 7 additions & 5 deletions internal/ethapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1888,8 +1888,9 @@ type EthContext struct {
Timestamp uint64 `json:"timestamp"`
}
type RollupContext struct {
Index uint64 `json:"index"`
QueueIndex uint64 `json:"queueIndex"`
Index uint64 `json:"index"`
QueueIndex uint64 `json:"queueIndex"`
VerifiedIndex uint64 `json:"verifiedIndex"`
}

type rollupInfo struct {
Expand All @@ -1906,7 +1907,7 @@ func (api *PublicRollupAPI) GetInfo(ctx context.Context) rollupInfo {
}
syncing := api.b.IsSyncing()
bn, ts := api.b.GetEthContext()
index, queueIndex := api.b.GetRollupContext()
index, queueIndex, verifiedIndex := api.b.GetRollupContext()

return rollupInfo{
Mode: mode,
Expand All @@ -1916,8 +1917,9 @@ func (api *PublicRollupAPI) GetInfo(ctx context.Context) rollupInfo {
Timestamp: ts,
},
RollupContext: RollupContext{
Index: index,
QueueIndex: queueIndex,
Index: index,
QueueIndex: queueIndex,
VerifiedIndex: verifiedIndex,
},
}
}
Expand Down
2 changes: 1 addition & 1 deletion internal/ethapi/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ type Backend interface {
IsVerifier() bool
IsSyncing() bool
GetEthContext() (uint64, uint64)
GetRollupContext() (uint64, uint64)
GetRollupContext() (uint64, uint64, uint64)
GasLimit() uint64
GetDiff(*big.Int) (diffdb.Diff, error)
}
Expand Down
4 changes: 2 additions & 2 deletions les/api_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ func (b *LesApiBackend) GetEthContext() (uint64, uint64) {
return 0, 0
}

func (b *LesApiBackend) GetRollupContext() (uint64, uint64) {
return 0, 0
func (b *LesApiBackend) GetRollupContext() (uint64, uint64, uint64) {
return 0, 0, 0
}

func (b *LesApiBackend) IsSyncing() bool {
Expand Down
26 changes: 4 additions & 22 deletions miner/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -863,32 +863,14 @@ func (w *worker) commitNewTx(tx *types.Transaction) error {
tstart := time.Now()

parent := w.chain.CurrentBlock()
// The L1Timestamp will always be set for a transaction
// coming from a batch submission because the transaction
// has been included in the canonical transaction chain.
// The only time that L1Timestamp is zero is for queue
// origin sequencer transactions that have yet to be included
// in the canonical transaction chain, meaning this code
// path is only relevant for the sequencer.
if tx.L1Timestamp() == 0 {
ts := w.eth.SyncService().GetLatestL1Timestamp()
bn := w.eth.SyncService().GetLatestL1BlockNumber()
tx.SetL1Timestamp(ts)
tx.SetL1BlockNumber(bn)
}
timestamp := tx.L1Timestamp()
if timestamp < parent.Time() {
log.Error("Monotonicity violation detected", "index", parent.NumberU64())
}

num := parent.Number()
// Fill in the index field in the tx meta if it is `nil`.
// This should only ever happen in the case of the sequencer
// receiving a queue origin sequencer transaction. The verifier
// should always receive transactions with an index as they
// have already been confirmed in the canonical transaction chain.
// Use the parent's block number because the CTC is 0 indexed.
if meta := tx.GetMeta(); meta.Index == nil {
index := num.Uint64()
meta.Index = &index
tx.SetTransactionMeta(meta)
log.Error("Index not found on transaction")
}
header := &types.Header{
ParentHash: parent.Hash(),
Expand Down
Loading