Commit 93b59bf7 authored by clabby's avatar clabby

Add custom type for `LocalContext` - @tynes review

Co-Authored-By: default avatarMark Tyneway <mark.tyneway@gmail.com>
parent 330c2f74
...@@ -28,6 +28,21 @@ var ( ...@@ -28,6 +28,21 @@ var (
LoadLocalDataBytes4 []byte LoadLocalDataBytes4 []byte
) )
func init() {
mipsAbi, err := bindings.MIPSMetaData.GetAbi()
if err != nil {
panic(fmt.Errorf("failed to load MIPS ABI: %w", err))
}
StepBytes4 = mipsAbi.Methods["step"].ID[:4]
preimageAbi, err := bindings.PreimageOracleMetaData.GetAbi()
if err != nil {
panic(fmt.Errorf("failed to load pre-image oracle ABI: %w", err))
}
LoadKeccak256PreimagePartBytes4 = preimageAbi.Methods["loadKeccak256PreimagePart"].ID[:4]
LoadLocalDataBytes4 = preimageAbi.Methods["loadLocalData"].ID[:4]
}
// LoadContracts loads the Cannon contracts, from op-bindings package // LoadContracts loads the Cannon contracts, from op-bindings package
func LoadContracts() (*Contracts, error) { func LoadContracts() (*Contracts, error) {
var mips, oracle Contract var mips, oracle Contract
...@@ -35,9 +50,6 @@ func LoadContracts() (*Contracts, error) { ...@@ -35,9 +50,6 @@ func LoadContracts() (*Contracts, error) {
mips.DeployedBytecode.SourceMap = bindings.MIPSDeployedSourceMap mips.DeployedBytecode.SourceMap = bindings.MIPSDeployedSourceMap
oracle.DeployedBytecode.Object = hexutil.MustDecode(bindings.PreimageOracleDeployedBin) oracle.DeployedBytecode.Object = hexutil.MustDecode(bindings.PreimageOracleDeployedBin)
oracle.DeployedBytecode.SourceMap = bindings.PreimageOracleDeployedSourceMap oracle.DeployedBytecode.SourceMap = bindings.PreimageOracleDeployedSourceMap
if err := InitSelectors(); err != nil {
return nil, err
}
return &Contracts{ return &Contracts{
MIPS: &mips, MIPS: &mips,
Oracle: &oracle, Oracle: &oracle,
...@@ -54,32 +66,12 @@ func LoadContractsFromFiles() (*Contracts, error) { ...@@ -54,32 +66,12 @@ func LoadContractsFromFiles() (*Contracts, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
if err := InitSelectors(); err != nil {
return nil, err
}
return &Contracts{ return &Contracts{
MIPS: mips, MIPS: mips,
Oracle: oracle, Oracle: oracle,
}, nil }, nil
} }
func InitSelectors() error {
mipsAbi, err := bindings.MIPSMetaData.GetAbi()
if err != nil {
return fmt.Errorf("failed to load MIPS ABI: %w", err)
}
StepBytes4 = mipsAbi.Methods["step"].ID[:4]
preimageAbi, err := bindings.PreimageOracleMetaData.GetAbi()
if err != nil {
return fmt.Errorf("failed to load pre-image oracle ABI: %w", err)
}
LoadKeccak256PreimagePartBytes4 = preimageAbi.Methods["loadKeccak256PreimagePart"].ID[:4]
LoadLocalDataBytes4 = preimageAbi.Methods["loadLocalData"].ID[:4]
return nil
}
func LoadContract(name string) (*Contract, error) { func LoadContract(name string) (*Contract, error) {
dat, err := os.ReadFile(fmt.Sprintf("../../packages/contracts-bedrock/forge-artifacts/%s.sol/%s.json", name, name)) dat, err := os.ReadFile(fmt.Sprintf("../../packages/contracts-bedrock/forge-artifacts/%s.sol/%s.json", name, name))
if err != nil { if err != nil {
......
...@@ -8,6 +8,8 @@ import ( ...@@ -8,6 +8,8 @@ import (
preimage "github.com/ethereum-optimism/optimism/op-preimage" preimage "github.com/ethereum-optimism/optimism/op-preimage"
) )
type LocalContext uint64
type StepWitness struct { type StepWitness struct {
// encoded state witness // encoded state witness
State []byte State []byte
...@@ -25,7 +27,7 @@ func uint32ToBytes32(v uint32) []byte { ...@@ -25,7 +27,7 @@ func uint32ToBytes32(v uint32) []byte {
return out[:] return out[:]
} }
func (wit *StepWitness) EncodeStepInput(localContext uint32) []byte { func (wit *StepWitness) EncodeStepInput(localContext LocalContext) []byte {
abiStateLen := len(wit.State) abiStateLen := len(wit.State)
if abiStateLen%32 != 0 { if abiStateLen%32 != 0 {
abiStateLen += 32 - (abiStateLen % 32) abiStateLen += 32 - (abiStateLen % 32)
...@@ -38,7 +40,7 @@ func (wit *StepWitness) EncodeStepInput(localContext uint32) []byte { ...@@ -38,7 +40,7 @@ func (wit *StepWitness) EncodeStepInput(localContext uint32) []byte {
input = append(input, StepBytes4...) input = append(input, StepBytes4...)
input = append(input, uint32ToBytes32(32*3)...) // state data offset in bytes input = append(input, uint32ToBytes32(32*3)...) // state data offset in bytes
input = append(input, uint32ToBytes32(32*3+32+uint32(len(abiState)))...) // proof data offset in bytes input = append(input, uint32ToBytes32(32*3+32+uint32(len(abiState)))...) // proof data offset in bytes
input = append(input, uint32ToBytes32(localContext)...) // local context in bytes input = append(input, uint32ToBytes32(uint32(localContext))...) // local context in bytes
input = append(input, uint32ToBytes32(uint32(len(wit.State)))...) // state data length in bytes input = append(input, uint32ToBytes32(uint32(len(wit.State)))...) // state data length in bytes
input = append(input, abiState[:]...) input = append(input, abiState[:]...)
...@@ -51,7 +53,7 @@ func (wit *StepWitness) HasPreimage() bool { ...@@ -51,7 +53,7 @@ func (wit *StepWitness) HasPreimage() bool {
return wit.PreimageKey != ([32]byte{}) return wit.PreimageKey != ([32]byte{})
} }
func (wit *StepWitness) EncodePreimageOracleInput(localContext uint32) ([]byte, error) { func (wit *StepWitness) EncodePreimageOracleInput(localContext LocalContext) ([]byte, error) {
if wit.PreimageKey == ([32]byte{}) { if wit.PreimageKey == ([32]byte{}) {
return nil, errors.New("cannot encode pre-image oracle input, witness has no pre-image to proof") return nil, errors.New("cannot encode pre-image oracle input, witness has no pre-image to proof")
} }
...@@ -64,7 +66,7 @@ func (wit *StepWitness) EncodePreimageOracleInput(localContext uint32) ([]byte, ...@@ -64,7 +66,7 @@ func (wit *StepWitness) EncodePreimageOracleInput(localContext uint32) ([]byte,
var input []byte var input []byte
input = append(input, LoadLocalDataBytes4...) input = append(input, LoadLocalDataBytes4...)
input = append(input, wit.PreimageKey[:]...) input = append(input, wit.PreimageKey[:]...)
input = append(input, uint32ToBytes32(localContext)...) // local context in bytes input = append(input, uint32ToBytes32(uint32(localContext))...) // local context in bytes
preimagePart := wit.PreimageValue[8:] preimagePart := wit.PreimageValue[8:]
var tmp [32]byte var tmp [32]byte
......
...@@ -6,6 +6,7 @@ import ( ...@@ -6,6 +6,7 @@ import (
"math/big" "math/big"
"testing" "testing"
"github.com/ethereum-optimism/optimism/op-bindings/bindings"
"github.com/ethereum-optimism/optimism/op-challenger/game/fault/types" "github.com/ethereum-optimism/optimism/op-challenger/game/fault/types"
"github.com/ethereum-optimism/optimism/op-service/testlog" "github.com/ethereum-optimism/optimism/op-service/testlog"
...@@ -104,7 +105,9 @@ func TestCannonUpdater_BuildLocalOracleData(t *testing.T) { ...@@ -104,7 +105,9 @@ func TestCannonUpdater_BuildLocalOracleData(t *testing.T) {
txData, err := updater.BuildLocalOracleData(oracleData) txData, err := updater.BuildLocalOracleData(oracleData)
require.NoError(t, err) require.NoError(t, err)
var addLocalDataBytes4 = crypto.Keccak256([]byte("addLocalData(uint256,uint256,uint256)"))[:4] fdgAbi, err := bindings.FaultDisputeGameMetaData.GetAbi()
require.NoError(t, err)
addLocalDataBytes4 := fdgAbi.Methods["addLocalData"].ID[:4]
// Pack the tx data manually. // Pack the tx data manually.
var expected []byte var expected []byte
......
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