Skip to content

Commit

Permalink
[fix]: import gasorale connection with l1 node
Browse files Browse the repository at this point in the history
  • Loading branch information
Sha3nS committed Aug 8, 2023
1 parent 440a741 commit 30019c0
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,4 @@ ops/data/**
dist/
packages/contracts/test/data/
packages/contracts/abi/
gas-oracle/gas-oracle-data/
7 changes: 7 additions & 0 deletions gas-oracle/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ var (
Usage: "Setup StateRollupGasUsed",
EnvVar: "GAS_PRICE_ORACLE_STATE_ROLLUP_GAS_USED",
}
StateHashGasUsed = cli.IntFlag{
Name: "set-state-hash-gas-used",
Value: 1412,
Usage: "Setup StateHashGasUsed",
EnvVar: "GAS_PRICE_ORACLE_STATE_HASH_GAS_USED",
}
DataRollupGasUsed = cli.IntFlag{
Name: "set-data-rollup-gas-used",
Value: 137893,
Expand Down Expand Up @@ -323,6 +329,7 @@ var Flags = []cli.Flag{
BatchSizeCap,
SizeGap,
StateRollupGasUsed,
StateHashGasUsed,
DataRollupGasUsed,
EnableHsmFlag,
HsmAddressFlag,
Expand Down
2 changes: 2 additions & 0 deletions gas-oracle/oracle/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ type Config struct {
batchSizeCap int
sizeGap int
stateRollupGasUsed *big.Int
stateHashGasUsed *big.Int
dataRollupGasUsed *big.Int
// Metrics config
MetricsEnabled bool
Expand Down Expand Up @@ -108,6 +109,7 @@ func NewConfig(ctx *cli.Context) *Config {
cfg.batchSizeBottom = ctx.GlobalInt(flags.BatchSizeBottom.Name)
cfg.sizeGap = ctx.GlobalInt(flags.SizeGap.Name)
cfg.stateRollupGasUsed = big.NewInt(ctx.GlobalInt64(flags.StateRollupGasUsed.Name))
cfg.stateHashGasUsed = big.NewInt(ctx.GlobalInt64(flags.StateHashGasUsed.Name))
cfg.dataRollupGasUsed = big.NewInt(ctx.GlobalInt64(flags.DataRollupGasUsed.Name))

if cfg.EnableHsm {
Expand Down
98 changes: 91 additions & 7 deletions gas-oracle/oracle/gas_price_oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package oracle

import (
"context"
"encoding/binary"
"errors"
"fmt"
"math/big"
Expand All @@ -17,8 +18,12 @@ import (
"github.com/mantlenetworkio/mantle/gas-oracle/gasprices"
ometrics "github.com/mantlenetworkio/mantle/gas-oracle/metrics"
"github.com/mantlenetworkio/mantle/gas-oracle/tokenprice"
"github.com/mantlenetworkio/mantle/l2geth/core/rawdb"
"github.com/mantlenetworkio/mantle/l2geth/ethdb"
)

const GAS_ORACLE_SYNC_HEIGHT = "GAS_ORACLE_SYNC_HEIGHT"

var (
// errInvalidSigningKey represents the error when the signing key used
// is not the Owner of the contract and therefore cannot update the gasprice
Expand Down Expand Up @@ -194,12 +199,16 @@ func (g *GasPriceOracle) DaFeeLoop() {
}

func (g *GasPriceOracle) OverHeadLoop() {
// set ticker
ticker := time.NewTicker(5 * time.Second)

// read gas-oracle synced height
db, height := readGasOracleSyncHeight()
log.Info("ReadGasOracleSyncHeight", "height", height)
// set channel
stateBatchAppendChan := make(chan *bindings.StateCommitmentChainStateBatchAppended, 10)
stateAppendSub, err := g.sccBackend.WatchStateBatchAppended(&bind.WatchOpts{Context: g.ctx}, stateBatchAppendChan, nil)
if err != nil {
panic(err)
}
defer stateAppendSub.Unsubscribe()

// set context
ctcTotalBatches, err := g.ctcBackend.GetTotalBatches(&bind.CallOpts{})
if err != nil {
panic(err)
Expand All @@ -209,8 +218,41 @@ func (g *GasPriceOracle) OverHeadLoop() {
panic(err)
}

var end uint64
for {
select {
case <-ticker.C:
log.Info("OverHeadLoop is living, HeartBeat It!")
latestHeader, err := g.l1Backend.HeaderByNumber(g.ctx, nil)
if err != nil {
log.Warn("get latest header in error", "error", err)
continue
}
// repeat query latest block is not allowed
if height != nil && height.Uint64() != 0 && height.Uint64() == latestHeader.Number.Uint64() {
continue
}
if height == nil || height.Uint64() == 0 {
height = latestHeader.Number
}
end = latestHeader.Number.Uint64()

iter, err := g.sccBackend.FilterStateBatchAppended(&bind.FilterOpts{
Start: height.Uint64(),
End: &end,
Context: g.ctx,
}, nil)
for iter.Next() {
select {
case stateBatchAppendChan <- iter.Event:
log.Info("write event into channel", "channel length is", len(stateBatchAppendChan))
default:
log.Error("write too many event into channel, increase channel length")
}
}
_ = writeGasOracleSyncHeight(db, latestHeader.Number)
height = latestHeader.Number
log.Info("Update synced height", "height", height)
case ev := <-stateBatchAppendChan:
currentCtcBatches, err := g.ctcBackend.GetTotalBatches(&bind.CallOpts{})
if err != nil {
Expand All @@ -219,10 +261,11 @@ func (g *GasPriceOracle) OverHeadLoop() {
log.Info("current scc batch size", "size", ev.BatchSize)
log.Info("CTC circle num in SCC circle", "count", new(big.Int).Sub(currentCtcBatches, ctcTotalBatches))
if err := updateOverhead(new(big.Int).Sub(currentCtcBatches, ctcTotalBatches), ev.BatchSize); err != nil {
log.Error("cannot update da fee", "messgae", err)
log.Error("cannot update overhead", "message", err)
}
ctcTotalBatches = currentCtcBatches
case <-g.ctx.Done():
db.Close()
g.Stop()
}
}
Expand Down Expand Up @@ -266,7 +309,7 @@ func NewGasPriceOracle(cfg *Config) (*GasPriceOracle, error) {
return nil, err
}

l1Client, err := NewL1Client(cfg.ethereumWssUrl, tokenPricer)
l1Client, err := NewL1Client(cfg.ethereumHttpUrl, tokenPricer)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -435,3 +478,44 @@ func ensureConnection(client *ethclient.Client) error {
}
return nil
}

func readGasOracleSyncHeight() (ethdb.Database, *big.Int) {
// read synced height
db, err := rawdb.NewLevelDBDatabase("gas-oracle-data", 0, 0, "")
if err != nil {
log.Error("NewLevelDBDatabase in error", "err", err)
panic(err)
}
// will close connection at once
//defer db.Close()

has, err := db.Has([]byte(GAS_ORACLE_SYNC_HEIGHT))
if err != nil {
log.Error("check db has GAS_ORACLE_SYNC_HEIGHT in error", "err", err)
panic(err)
}
if !has {
return db, nil
}

height, err := db.Get([]byte(GAS_ORACLE_SYNC_HEIGHT))
if err != nil {
log.Error("check db Get GAS_ORACLE_SYNC_HEIGHT in error", "err", err)
panic(err)
}
return db, big.NewInt(0).SetUint64(binary.BigEndian.Uint64(height))
}

func writeGasOracleSyncHeight(db ethdb.Database, height *big.Int) error {
// will close connection at once
//defer db.Close()

var indexBz = make([]byte, 8)
binary.BigEndian.PutUint64(indexBz, height.Uint64())
err := db.Put([]byte(GAS_ORACLE_SYNC_HEIGHT), indexBz)
if err != nil {
log.Error("put GAS_ORACLE_SYNC_HEIGHT in error", "err", err)
return err
}
return nil
}
2 changes: 1 addition & 1 deletion gas-oracle/oracle/overhead.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func calculateJumpTable(diff *big.Int, cfg *Config) {
// calculate jump table
for levelSize := cfg.batchSizeBottom; levelSize <= cfg.batchSizeCap; {
orderedSizes = append(orderedSizes, levelSize)
jumpTable[levelSize] = new(big.Int).Add(new(big.Int).Div(OverheadGasUsedOnL1, new(big.Int).SetUint64(uint64(levelSize))), new(big.Int).SetUint64(1330))
jumpTable[levelSize] = new(big.Int).Add(new(big.Int).Div(OverheadGasUsedOnL1, new(big.Int).SetUint64(uint64(levelSize))), cfg.stateHashGasUsed)
levelSize += cfg.sizeGap
}
}
Expand Down

0 comments on commit 30019c0

Please sign in to comment.