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 (
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
func LoadContracts() (*Contracts, error) {
var mips, oracle Contract
......@@ -35,9 +50,6 @@ func LoadContracts() (*Contracts, error) {
mips.DeployedBytecode.SourceMap = bindings.MIPSDeployedSourceMap
oracle.DeployedBytecode.Object = hexutil.MustDecode(bindings.PreimageOracleDeployedBin)
oracle.DeployedBytecode.SourceMap = bindings.PreimageOracleDeployedSourceMap
if err := InitSelectors(); err != nil {
return nil, err
}
return &Contracts{
MIPS: &mips,
Oracle: &oracle,
......@@ -54,32 +66,12 @@ func LoadContractsFromFiles() (*Contracts, error) {
if err != nil {
return nil, err
}
if err := InitSelectors(); err != nil {
return nil, err
}
return &Contracts{
MIPS: mips,
Oracle: oracle,
}, 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) {
dat, err := os.ReadFile(fmt.Sprintf("../../packages/contracts-bedrock/forge-artifacts/%s.sol/%s.json", name, name))
if err != nil {
......
......@@ -8,6 +8,8 @@ import (
preimage "github.com/ethereum-optimism/optimism/op-preimage"
)
type LocalContext uint64
type StepWitness struct {
// encoded state witness
State []byte
......@@ -25,7 +27,7 @@ func uint32ToBytes32(v uint32) []byte {
return out[:]
}
func (wit *StepWitness) EncodeStepInput(localContext uint32) []byte {
func (wit *StepWitness) EncodeStepInput(localContext LocalContext) []byte {
abiStateLen := len(wit.State)
if abiStateLen%32 != 0 {
abiStateLen += 32 - (abiStateLen % 32)
......@@ -38,7 +40,7 @@ func (wit *StepWitness) EncodeStepInput(localContext uint32) []byte {
input = append(input, StepBytes4...)
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(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, abiState[:]...)
......@@ -51,7 +53,7 @@ func (wit *StepWitness) HasPreimage() bool {
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{}) {
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,
var input []byte
input = append(input, LoadLocalDataBytes4...)
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:]
var tmp [32]byte
......
......@@ -6,6 +6,7 @@ import (
"math/big"
"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-service/testlog"
......@@ -104,7 +105,9 @@ func TestCannonUpdater_BuildLocalOracleData(t *testing.T) {
txData, err := updater.BuildLocalOracleData(oracleData)
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.
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