Commit 2b0c5664 authored by Andreas Bigger's avatar Andreas Bigger

Update the cannon oracle updater with apriori data tx crafting

parent 9fa619e7
......@@ -2,6 +2,8 @@ package cannon
import (
"context"
"fmt"
"math/big"
"github.com/ethereum-optimism/optimism/op-bindings/bindings"
"github.com/ethereum-optimism/optimism/op-challenger/fault/types"
......@@ -9,13 +11,14 @@ import (
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/log"
)
// cannonUpdater is a [types.OracleUpdater] that exposes a method
// to update onchain cannon oracles with required data.
type cannonUpdater struct {
logger log.Logger
log log.Logger
txMgr txmgr.TxManager
fdgAbi abi.ABI
......@@ -42,7 +45,7 @@ func NewOracleUpdater(
}
return &cannonUpdater{
logger: logger,
log: logger,
txMgr: txMgr,
fdgAbi: *fdgAbi,
......@@ -55,5 +58,67 @@ func NewOracleUpdater(
// UpdateOracle updates the oracle with the given data.
func (u *cannonUpdater) UpdateOracle(ctx context.Context, data types.PreimageOracleData) error {
panic("oracle updates not implemented")
if data.IsLocal {
return u.sendLocalOracleData(ctx, data)
}
return u.sendGlobalOracleData(ctx, data)
}
// sendLocalOracleData sends the local oracle data to the [txmgr].
func (u *cannonUpdater) sendLocalOracleData(ctx context.Context, data types.PreimageOracleData) error {
txData, err := u.buildLocalOracleData(data)
if err != nil {
return fmt.Errorf("local oracle tx data build: %w", err)
}
return u.sendTxAndWait(ctx, u.fdgAddr, txData)
}
// sendGlobalOracleData sends the global oracle data to the [txmgr].
func (u *cannonUpdater) sendGlobalOracleData(ctx context.Context, data types.PreimageOracleData) error {
txData, err := u.buildGlobalOracleData(data)
if err != nil {
return fmt.Errorf("global oracle tx data build: %w", err)
}
return u.sendTxAndWait(ctx, u.fdgAddr, txData)
}
// buildLocalOracleData takes the local preimage key and data
// and creates tx data to load the key, data pair into the
// PreimageOracle contract from the FaultDisputeGame contract call.
func (u *cannonUpdater) buildLocalOracleData(data types.PreimageOracleData) ([]byte, error) {
return u.fdgAbi.Pack(
"addLocalData",
data.OracleKey,
big.NewInt(0),
)
}
// buildGlobalOracleData takes the global preimage key and data
// and creates tx data to load the key, data pair into the
// PreimageOracle contract.
func (u *cannonUpdater) buildGlobalOracleData(data types.PreimageOracleData) ([]byte, error) {
return u.preimageOracleAbi.Pack(
"loadKeccak256PreimagePart",
big.NewInt(0),
data.OracleData,
)
}
// sendTxAndWait sends a transaction through the [txmgr] and waits for a receipt.
// This sets the tx GasLimit to 0, performing gas estimation online through the [txmgr].
func (u *cannonUpdater) sendTxAndWait(ctx context.Context, addr common.Address, txData []byte) error {
receipt, err := u.txMgr.Send(ctx, txmgr.TxCandidate{
To: &addr,
TxData: txData,
GasLimit: 0,
})
if err != nil {
return err
}
if receipt.Status == ethtypes.ReceiptStatusFailed {
u.log.Error("Responder tx successfully published but reverted", "tx_hash", receipt.TxHash)
} else {
u.log.Debug("Responder tx successfully published", "tx_hash", receipt.TxHash)
}
return nil
}
......@@ -3,6 +3,7 @@ package types
import (
"context"
"errors"
"math/big"
"github.com/ethereum/go-ethereum/common"
)
......@@ -27,6 +28,11 @@ type PreimageOracleData struct {
OracleData []byte
}
// GetIdent returns the ident for the preimage oracle data.
func (p *PreimageOracleData) GetIdent() *big.Int {
return big.NewInt(int64(p.OracleData[0]))
}
// NewPreimageOracleData creates a new [PreimageOracleData] instance.
func NewPreimageOracleData(key []byte, data []byte) PreimageOracleData {
return PreimageOracleData{
......
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