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

WIP:Refactor oracle rollup epoch #538

Open
wants to merge 3 commits into
base: main
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
7 changes: 6 additions & 1 deletion oracle/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ type Config struct {
// MetricsPort is the port at which the metrics server is running.
MetricsPort uint64

RollupAddr common.Address
RollupAddr common.Address
L1StakingAddr common.Address

MaxSize uint64
MinSize uint64
Expand All @@ -71,6 +72,8 @@ type Config struct {
ExternalSignChain string
ExternalSignUrl string
ExternalSignRsaPriv string

MockRecord bool
}

// NewConfig parses the Config from the provided flags or environment variables.
Expand Down Expand Up @@ -98,6 +101,8 @@ func NewConfig(ctx *cli.Context) (Config, error) {
ExternalSignChain: ctx.GlobalString(flags.ExternalSignChain.Name),
ExternalSignUrl: ctx.GlobalString(flags.ExternalSignUrl.Name),
ExternalSignRsaPriv: ctx.GlobalString(flags.ExternalSignRsaPriv.Name),
// mock flag
MockRecord: ctx.GlobalBool(flags.MockRecordFlag.Name),
}

if ctx.GlobalIsSet(flags.LogFilenameFlag.Name) {
Expand Down
41 changes: 41 additions & 0 deletions oracle/db/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package db

import (
"github.com/urfave/cli"

"morph-l2/node/flags"
)

type Config struct {
DBPath string `json:"db_path"`
Namespace string `json:"namespace"`
DatabaseHandles int `json:"database_handles"`
DatabaseCache int `json:"database_cache"`
DatabaseFreezer string `json:"database_freezer"`
}

func DefaultConfig() *Config {
return &Config{
Namespace: "staking-oracle",
DatabaseHandles: 256,
DatabaseCache: 256,
}
}

func (c *Config) SetCliContext(ctx *cli.Context) {
if ctx.GlobalIsSet(flags.DBDataDir.Name) {
c.DBPath = ctx.GlobalString(flags.DBDataDir.Name)
}
if ctx.GlobalIsSet(flags.DBNamespace.Name) {
c.Namespace = ctx.GlobalString(flags.DBNamespace.Name)
}
if ctx.GlobalIsSet(flags.DBHandles.Name) {
c.DatabaseHandles = ctx.GlobalInt(flags.DBHandles.Name)
}
if ctx.GlobalIsSet(flags.DBCache.Name) {
c.DatabaseCache = ctx.GlobalInt(flags.DBCache.Name)
}
if ctx.GlobalIsSet(flags.DBFreezer.Name) {
c.DatabaseFreezer = ctx.GlobalString(flags.DBFreezer.Name)
}
}
1 change: 1 addition & 0 deletions oracle/db/iterator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package db
8 changes: 8 additions & 0 deletions oracle/db/keys.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package db

var (
syncedL1HeightKey = []byte("LastSyncedL1Height")
latestL1ChangePointKey = []byte("LastSyncedL1Height")
syncedL2HeightKey = []byte("LastSyncedL1Height")
changePointsKey = []byte("ChangePoints")
)
85 changes: 85 additions & 0 deletions oracle/db/store.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package db

import (
"fmt"

"path/filepath"
"sync"

"github.com/morph-l2/go-ethereum/core/rawdb"
"github.com/morph-l2/go-ethereum/ethdb"
"github.com/morph-l2/go-ethereum/rlp"
"github.com/syndtr/goleveldb/leveldb"

"morph-l2/oracle/types"
)

type Store struct {
db ethdb.Database
ChainPointSync sync.Mutex
}

func NewMemoryStore() *Store {
return &Store{
db: rawdb.NewMemoryDatabase(),
}
}

func NewStore(config *Config, home string) (*Store, error) {
var (
db ethdb.Database
err error
dbPath = config.DBPath
freezer = config.DatabaseFreezer
)

if dbPath == "" {
if home == "" {
return nil, fmt.Errorf("either Home or DB path has to be provided")
}
dbPath = filepath.Join(home, "node-data")
}

if config.DatabaseFreezer == "" {
freezer = filepath.Join(dbPath, "ancient")
}
db, err = rawdb.NewLevelDBDatabaseWithFreezer(dbPath, config.DatabaseCache, config.DatabaseHandles, freezer, config.Namespace, false)
if err != nil {
return nil, err
}

return &Store{
db: db,
ChainPointSync: sync.Mutex{},
}, nil
}

func (s *Store) WriteLatestChangeContext(changePoints types.ChangeContext) error {
data, err := rlp.EncodeToBytes(changePoints)
if err != nil {
return err
}
if err := s.db.Put(changePointsKey, data); err != nil {
panic(fmt.Sprintf("failed to update change points failed, err: %v", err))
}
return nil
}

func (s *Store) ReadLatestChangePoints() types.ChangeContext {
data, err := s.db.Get(changePointsKey)
if err != nil && !isNotFoundErr(err) {
panic(fmt.Sprintf("failed to read change points, err: %v", err))
}
if err != nil {
panic(fmt.Sprintf("failed to sync change points, err: %v", err))
}
var changeCtx types.ChangeContext
if err := rlp.DecodeBytes(data, &changeCtx); err != nil {
panic(fmt.Sprintf("decode data to changepoint error:%v", err))
}
return changeCtx
}

func isNotFoundErr(err error) bool {
return err.Error() == leveldb.ErrNotFound.Error() || err.Error() == types.ErrMemoryDBNotFound.Error()
}
61 changes: 61 additions & 0 deletions oracle/db/store_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package db

import (
"testing"

"morph-l2/oracle/types"

"github.com/morph-l2/go-ethereum/common"
"github.com/stretchr/testify/require"
)

func TestLatestSyncedL1Height(t *testing.T) {
db := NewMemoryStore()
err := db.WriteLatestChangeContext(types.ChangeContext{
L2Sequencers: []types.L2Sequencer{
{
TimeStamp: 1627891200,
EpochInterval: 10,
Addresses: []common.Address{common.HexToAddress("0x123")},
},
{
TimeStamp: 1627891260,
EpochInterval: 10,
Addresses: []common.Address{},
},
},
ActiveStakersByTime: []types.ActiveStakers{
{
TimeStamp: 1627891200,
BlockNumber: 100,
Addresses: []common.Address{},
},
{
TimeStamp: 1627891260,
BlockNumber: 101,
Addresses: []common.Address{},
},
},
ChangePoints: []types.ChangePoint{
{
TimeStamp: 1627891200,
BlockNumber: 100,
EpochInterval: 10,
Submitters: []common.Address{},
ChangeType: 1,
},
{
TimeStamp: 1627891260,
BlockNumber: 101,
EpochInterval: 10,
Submitters: []common.Address{},
ChangeType: 2,
},
},
L1Synced: 1,
L2synced: 1,
})
require.NoError(t, err)
changePoints := db.ReadLatestChangePoints()
t.Log(changePoints)
}
7 changes: 7 additions & 0 deletions oracle/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,11 @@ var (
Usage: "The rsa private key of the external sign",
EnvVar: prefixEnvVar("EXTERNAL_SIGN_RSA_PRIV"),
}
MockRecordFlag = cli.BoolFlag{
Name: "MOCK_RECORD",
Usage: "mock record client",
EnvVar: prefixEnvVar("MOCK_RECORD"),
}
)

var requiredFlags = []cli.Flag{
Expand Down Expand Up @@ -179,6 +184,8 @@ var optionalFlags = []cli.Flag{
ExternalSignChain,
ExternalSignUrl,
ExternalSignRsaPriv,
// mock record client
MockRecordFlag,
}

// Flags contains the list of configuration options available to the binary.
Expand Down
31 changes: 4 additions & 27 deletions oracle/oracle/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,14 @@ import (
"math/big"
"time"

"morph-l2/bindings/bindings"
"morph-l2/node/derivation"
"morph-l2/oracle/backoff"

"github.com/morph-l2/go-ethereum/accounts/abi/bind"
"github.com/morph-l2/go-ethereum/common"
"github.com/morph-l2/go-ethereum/common/hexutil"
"github.com/morph-l2/go-ethereum/core/types"
"github.com/morph-l2/go-ethereum/eth"
"github.com/morph-l2/go-ethereum/log"
"morph-l2/bindings/bindings"
"morph-l2/node/derivation"
)

type BatchInfoMap map[common.Hash][]BatchInfo
Expand Down Expand Up @@ -195,7 +193,7 @@ func (o *Oracle) LastBatchIndex(opts *bind.CallOpts) (*big.Int, error) {
}

func (o *Oracle) submitRecord() error {
nextBatchSubmissionIndex, err := o.GetNextBatchSubmissionIndex()
nextBatchSubmissionIndex, err := o.rm.NextBatchEpochIndex()
if err != nil {
return fmt.Errorf("get next batch submission index failed:%v", err)
}
Expand All @@ -218,26 +216,5 @@ func (o *Oracle) submitRecord() error {
if err != nil {
return fmt.Errorf("get batch submission error:%v", err)
}
callData, err := o.recordAbi.Pack("recordFinalizedBatchSubmissions", batchSubmissions)
if err != nil {
return err
}
tx, err := o.newRecordTxAndSign(callData)
if err != nil {
return fmt.Errorf("record finalized batch error:%v,batchLength:%v", err, len(batchSubmissions))
}
log.Info("record finalized batch success", "txHash", tx.Hash(), "batchLength", len(batchSubmissions))
var receipt *types.Receipt
err = backoff.Do(30, backoff.Exponential(), func() error {
var err error
receipt, err = o.waitReceiptWithCtx(o.ctx, tx.Hash())
return err
})
if err != nil {
return fmt.Errorf("wait tx receipt error:%v,txHash:%v", err, tx.Hash())
}
if receipt.Status != types.ReceiptStatusSuccessful {
return fmt.Errorf("record batch receipt failed,txHash:%v", tx.Hash())
}
return nil
return o.rm.UploadBatchEpoch(batchSubmissions)
}
Loading
Loading