Skip to content

Commit

Permalink
style: N-02 Code Clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
DevDynamo2024 committed Mar 5, 2024
1 parent 0506013 commit 435c7c4
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 88 deletions.
4 changes: 2 additions & 2 deletions op-chain-ops/crossdomain/withdrawal.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ func (w *Withdrawal) Decode(data []byte) error {
w.Nonce = nonce
w.Sender = &sender
w.Target = &target
w.ETHValue = ethValue
w.MNTValue = mntValue
w.ETHValue = ethValue
w.GasLimit = gasLimit
w.Data = hexutil.Bytes(msgData)
return nil
Expand Down Expand Up @@ -159,8 +159,8 @@ func (w *Withdrawal) WithdrawalTransaction() bindings.TypesWithdrawalTransaction
Nonce: w.Nonce,
Sender: *w.Sender,
Target: *w.Target,
EthValue: w.ETHValue,
MntValue: w.MNTValue,
EthValue: w.ETHValue,
GasLimit: w.GasLimit,
Data: []byte(w.Data),
}
Expand Down
7 changes: 2 additions & 5 deletions op-node/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

"github.com/hashicorp/go-multierror"
"github.com/libp2p/go-libp2p/core/peer"

"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/log"
Expand Down Expand Up @@ -201,10 +201,7 @@ func (n *OpNode) initL2(ctx context.Context, cfg *Config, snapshotLog log.Logger
return err
}

n.daSyncer, err = da.NewMantleDataStore(ctx, &cfg.DatastoreConfig)
if err != nil {
return err
}
n.daSyncer = da.NewMantleDataStore(ctx, &cfg.DatastoreConfig)

n.l2Driver = driver.NewDriver(&cfg.Driver, &cfg.Rollup, n.l2Source, n.l1Source, n.daSyncer, n, n, n.log, snapshotLog, n.metrics, &cfg.Sync)

Expand Down
14 changes: 8 additions & 6 deletions op-node/rollup/da/datastore.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type MantleDataStore struct {
GraphqlClient *graphql.Client
}

func NewMantleDataStore(ctx context.Context, cfg *MantleDataStoreConfig) (*MantleDataStore, error) {
func NewMantleDataStore(ctx context.Context, cfg *MantleDataStoreConfig) *MantleDataStore {
graphClient := graphView.NewGraphClient(cfg.GraphProvider, nil)
graphqlClient := graphql.NewClient(graphClient.GetEndpoint(), nil)
mDatastore := &MantleDataStore{
Expand All @@ -47,7 +47,7 @@ func NewMantleDataStore(ctx context.Context, cfg *MantleDataStoreConfig) (*Mantl
GraphClient: graphClient,
GraphqlClient: graphqlClient,
}
return mDatastore, nil
return mDatastore
}

func (mda *MantleDataStore) getDataStoreById(dataStoreId uint32) (*graphView.DataStore, error) {
Expand Down Expand Up @@ -91,8 +91,9 @@ func (mda *MantleDataStore) getFramesByDataStoreId(dataStoreId uint32) ([]byte,
log.Error("Retrieve frames and data fail", "err", err)
return nil, err
}
log.Debug("Get reply data success", "replyLength", len(reply.GetData()))
return reply.GetData(), nil
replyData := reply.GetData()
log.Debug("Get reply data success", "replyLength", len(replyData))
return replyData, nil
}

func (mda *MantleDataStore) getFramesFromIndexerByDataStoreId(dataStoreId uint32) ([]byte, error) {
Expand All @@ -112,8 +113,9 @@ func (mda *MantleDataStore) getFramesFromIndexerByDataStoreId(dataStoreId uint32
log.Error("Retrieve frames and data fail", "err", err)
return nil, err
}
log.Debug("Get reply data from mantle da success", "replyLength", len(reply.GetData()))
return reply.GetData(), nil
replyData := reply.GetData()
log.Debug("Get reply data from mantle da success", "replyLength", len(replyData))
return replyData, nil
}

func (mda *MantleDataStore) RetrievalFramesFromDa(dataStoreId uint32) ([]byte, error) {
Expand Down
15 changes: 9 additions & 6 deletions op-node/rollup/derive/calldata_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,16 @@ func (ds *DataSource) Next(ctx context.Context) (eth.Data, error) {
} else {
return nil, NewTemporaryError(fmt.Errorf("failed to open mantle da calldata source: %w", err))
}
} else if _, txs, err := ds.fetcher.InfoAndTxsByHash(ctx, ds.id.Hash); err == nil { // fetch data from EOA
ds.open = true
ds.data = DataFromEVMTransactions(ds.cfg, ds.batcherAddr, txs, log.New("origin", ds.id))
} else if errors.Is(err, ethereum.NotFound) {
return nil, NewResetError(fmt.Errorf("failed to open eoa calldata source: %w", err))
} else {
return nil, NewTemporaryError(fmt.Errorf("failed to open eoa calldata source: %w", err))
_, txs, err := ds.fetcher.InfoAndTxsByHash(ctx, ds.id.Hash)
if err == nil { // fetch data from EOA
ds.open = true
ds.data = DataFromEVMTransactions(ds.cfg, ds.batcherAddr, txs, log.New("origin", ds.id))
} else if errors.Is(err, ethereum.NotFound) {
return nil, NewResetError(fmt.Errorf("failed to open eoa calldata source: %w", err))
} else {
return nil, NewTemporaryError(fmt.Errorf("failed to open eoa calldata source: %w", err))
}
}
}
if len(ds.data) == 0 {
Expand Down
59 changes: 3 additions & 56 deletions op-node/rollup/derive/deposit_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func unmarshalDepositVersion0(dep *types.DepositTx, to common.Address, opaqueDat
dep.Value = new(big.Int).SetBytes(opaqueData[offset : offset+32])
offset += 32

//uint256 value for ethValue
// uint256 value for ethValue
dep.EthValue = new(big.Int).SetBytes(opaqueData[offset : offset+32])
// 0 mint is represented as nil to skip minting code
if dep.EthValue.Cmp(new(big.Int)) == 0 {
Expand Down Expand Up @@ -167,15 +167,15 @@ func unmarshalDepositVersion1(dep *types.DepositTx, to common.Address, opaqueDat
dep.Value = new(big.Int).SetBytes(opaqueData[offset : offset+32])
offset += 32

//uint256 value for ethValue
// uint256 value for ethValue
dep.EthValue = new(big.Int).SetBytes(opaqueData[offset : offset+32])
// 0 mint is represented as nil to skip minting code
if dep.EthValue.Cmp(new(big.Int)) == 0 {
dep.EthValue = nil
}
offset += 32

//uint256 value for ethTxValue
// uint256 value for ethTxValue
dep.EthTxValue = new(big.Int).SetBytes(opaqueData[offset : offset+32])
// 0 mint is represented as nil to skip minting code
if dep.EthTxValue.Cmp(new(big.Int)) == 0 {
Expand Down Expand Up @@ -302,56 +302,3 @@ func marshalDepositVersion0(deposit *types.DepositTx) ([]byte, error) {

return opaqueData, nil
}

func marshalDepositVersion1(deposit *types.DepositTx) ([]byte, error) {
opaqueData := make([]byte, 32+32+32+32+8+1, 32+32+32+32+8+1+len(deposit.Data))
offset := 0

// uint256 mint
if deposit.Mint != nil {
if deposit.Mint.BitLen() > 256 {
return nil, fmt.Errorf("mint value exceeds 256 bits: %d", deposit.Mint)
}
deposit.Mint.FillBytes(opaqueData[offset : offset+32])
}
offset += 32

// uint256 value
if deposit.Value.BitLen() > 256 {
return nil, fmt.Errorf("value value exceeds 256 bits: %d", deposit.Value)
}
deposit.Value.FillBytes(opaqueData[offset : offset+32])
offset += 32

// uint256 value
if deposit.EthValue != nil {
if deposit.EthValue.BitLen() > 256 {
return nil, fmt.Errorf("ethValue value exceeds 256 bits: %d", deposit.EthValue)
}
deposit.EthValue.FillBytes(opaqueData[offset : offset+32])
}
offset += 32

// uint256 value
if deposit.EthTxValue != nil {
if deposit.EthTxValue.BitLen() > 256 {
return nil, fmt.Errorf("ethValue value exceeds 256 bits: %d", deposit.EthTxValue)
}
deposit.EthTxValue.FillBytes(opaqueData[offset : offset+32])
}
offset += 32

// uint64 gas
binary.BigEndian.PutUint64(opaqueData[offset:offset+8], deposit.Gas)
offset += 8

// uint8 isCreation
if deposit.To == nil { // isCreation
opaqueData[offset] = 1
}

// Deposit data then fills the remaining event data
opaqueData = append(opaqueData, deposit.Data...)

return opaqueData, nil
}
6 changes: 3 additions & 3 deletions op-node/rollup/derive/engine_consolidate.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ func AttributesMatchBlock(attrs *eth.PayloadAttributes, parentHash common.Hash,
}
if attrs.BaseFee != nil {
log.Info("attributes match block check", "blockNumber", block.BlockNumber, "attrs", attrs.BaseFee.String(), "block", block.BaseFeePerGas.String())
}
if attrs.BaseFee != nil && block.BaseFeePerGas.ToBig().Cmp(attrs.BaseFee) != 0 {
return fmt.Errorf("base fee does not match. blockNumber %d. expected %s. got: %s", block.BlockNumber, attrs.BaseFee.String(), block.BaseFeePerGas.String())
if block.BaseFeePerGas.ToBig().Cmp(attrs.BaseFee) != 0 {
return fmt.Errorf("base fee does not match. blockNumber %d. expected %s. got: %s", block.BlockNumber, attrs.BaseFee.String(), block.BaseFeePerGas.String())
}
}
return nil
}
Expand Down
9 changes: 3 additions & 6 deletions op-node/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,7 @@ func NewConfig(ctx *cli.Context, log log.Logger) (*node.Config, error) {

l2SyncEndpoint := NewL2SyncEndpointConfig(ctx)

datastoreConfig, err := NewMantleDataStoreConfig(ctx)
if err != nil {
return nil, fmt.Errorf("failed to mantle datastore config: %w", err)
}
datastoreConfig := NewMantleDataStoreConfig(ctx)

syncConfig := NewSyncConfig(ctx)

Expand Down Expand Up @@ -208,7 +205,7 @@ func NewSnapshotLogger(ctx *cli.Context) (log.Logger, error) {
return logger, nil
}

func NewMantleDataStoreConfig(ctx *cli.Context) (datastore.MantleDataStoreConfig, error) {
func NewMantleDataStoreConfig(ctx *cli.Context) datastore.MantleDataStoreConfig {
retrieverSocket := ctx.GlobalString(flags.RetrieverSocketFlag.Name)
retrieverTimeout := ctx.GlobalDuration(flags.RetrieverTimeoutFlag.Name)
graphProvider := ctx.GlobalString(flags.GraphProviderFlag.Name)
Expand All @@ -222,7 +219,7 @@ func NewMantleDataStoreConfig(ctx *cli.Context) (datastore.MantleDataStoreConfig
DataStorePollingDuration: dataStorePollingDuration,
MantleDaIndexerSocket: mantleDaIndexerSocket,
MantleDAIndexerEnable: mantleDAIndexerEnable,
}, nil
}
}

func NewSyncConfig(ctx *cli.Context) *sync.Config {
Expand Down
5 changes: 1 addition & 4 deletions op-program/client/program.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,7 @@ func runDerivation(logger log.Logger, cfg *rollup.Config, l2Cfg *params.ChainCon
return fmt.Errorf("failed to create oracle-backed L2 chain: %w", err)
}
l2Source := l2.NewOracleEngine(cfg, logger, engineBackend)
daSyncer, err := da.NewMantleDataStore(context.Background(), daCfg)
if err != nil {
return fmt.Errorf("failed to create mantle data store: %w", err)
}
daSyncer := da.NewMantleDataStore(context.Background(), daCfg)
logger.Info("Starting derivation")
d := cldr.NewDriver(logger, cfg, l1Source, l2Source, daSyncer, l2ClaimBlockNum)
for {
Expand Down

0 comments on commit 435c7c4

Please sign in to comment.