Commit 51bb4d1a authored by Adrian Sutton's avatar Adrian Sutton Committed by GitHub

op-challenger: Support posting sha256 preimages (#9373)

parent 0d9c9b20
...@@ -12,6 +12,7 @@ import ( ...@@ -12,6 +12,7 @@ import (
"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-challenger/game/keccak/merkle" "github.com/ethereum-optimism/optimism/op-challenger/game/keccak/merkle"
keccakTypes "github.com/ethereum-optimism/optimism/op-challenger/game/keccak/types" keccakTypes "github.com/ethereum-optimism/optimism/op-challenger/game/keccak/types"
preimage "github.com/ethereum-optimism/optimism/op-preimage"
"github.com/ethereum-optimism/optimism/op-service/sources/batching" "github.com/ethereum-optimism/optimism/op-service/sources/batching"
"github.com/ethereum-optimism/optimism/op-service/txmgr" "github.com/ethereum-optimism/optimism/op-service/txmgr"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
...@@ -22,6 +23,7 @@ const ( ...@@ -22,6 +23,7 @@ const (
methodAddLeavesLPP = "addLeavesLPP" methodAddLeavesLPP = "addLeavesLPP"
methodSqueezeLPP = "squeezeLPP" methodSqueezeLPP = "squeezeLPP"
methodLoadKeccak256PreimagePart = "loadKeccak256PreimagePart" methodLoadKeccak256PreimagePart = "loadKeccak256PreimagePart"
methodLoadSha256PreimagePart = "loadSha256PreimagePart"
methodProposalCount = "proposalCount" methodProposalCount = "proposalCount"
methodProposals = "proposals" methodProposals = "proposals"
methodProposalMetadata = "proposalMetadata" methodProposalMetadata = "proposalMetadata"
...@@ -37,6 +39,8 @@ const ( ...@@ -37,6 +39,8 @@ const (
var ( var (
ErrInvalidAddLeavesCall = errors.New("tx is not a valid addLeaves call") ErrInvalidAddLeavesCall = errors.New("tx is not a valid addLeaves call")
ErrInvalidPreimageKey = errors.New("invalid preimage key")
ErrUnsupportedKeyType = errors.New("unsupported preimage key type")
) )
// PreimageOracleContract is a binding that works with contracts implementing the IPreimageOracle interface // PreimageOracleContract is a binding that works with contracts implementing the IPreimageOracle interface
...@@ -73,8 +77,20 @@ func (c *PreimageOracleContract) Addr() common.Address { ...@@ -73,8 +77,20 @@ func (c *PreimageOracleContract) Addr() common.Address {
} }
func (c *PreimageOracleContract) AddGlobalDataTx(data *types.PreimageOracleData) (txmgr.TxCandidate, error) { func (c *PreimageOracleContract) AddGlobalDataTx(data *types.PreimageOracleData) (txmgr.TxCandidate, error) {
call := c.contract.Call(methodLoadKeccak256PreimagePart, new(big.Int).SetUint64(uint64(data.OracleOffset)), data.GetPreimageWithoutSize()) if len(data.OracleKey) == 0 {
return call.ToTxCandidate() return txmgr.TxCandidate{}, ErrInvalidPreimageKey
}
keyType := preimage.KeyType(data.OracleKey[0])
switch keyType {
case preimage.Keccak256KeyType:
call := c.contract.Call(methodLoadKeccak256PreimagePart, new(big.Int).SetUint64(uint64(data.OracleOffset)), data.GetPreimageWithoutSize())
return call.ToTxCandidate()
case preimage.Sha256KeyType:
call := c.contract.Call(methodLoadSha256PreimagePart, new(big.Int).SetUint64(uint64(data.OracleOffset)), data.GetPreimageWithoutSize())
return call.ToTxCandidate()
default:
return txmgr.TxCandidate{}, fmt.Errorf("%w: %v", ErrUnsupportedKeyType, keyType)
}
} }
func (c *PreimageOracleContract) InitLargePreimage(uuid *big.Int, partOffset uint32, claimedSize uint32) (txmgr.TxCandidate, error) { func (c *PreimageOracleContract) InitLargePreimage(uuid *big.Int, partOffset uint32, claimedSize uint32) (txmgr.TxCandidate, error) {
......
...@@ -11,24 +11,46 @@ import ( ...@@ -11,24 +11,46 @@ import (
"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-challenger/game/keccak/merkle" "github.com/ethereum-optimism/optimism/op-challenger/game/keccak/merkle"
keccakTypes "github.com/ethereum-optimism/optimism/op-challenger/game/keccak/types" keccakTypes "github.com/ethereum-optimism/optimism/op-challenger/game/keccak/types"
preimage "github.com/ethereum-optimism/optimism/op-preimage"
"github.com/ethereum-optimism/optimism/op-service/sources/batching" "github.com/ethereum-optimism/optimism/op-service/sources/batching"
batchingTest "github.com/ethereum-optimism/optimism/op-service/sources/batching/test" batchingTest "github.com/ethereum-optimism/optimism/op-service/sources/batching/test"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
func TestPreimageOracleContract_LoadKeccak256(t *testing.T) { func TestPreimageOracleContract_AddGlobalDataTx(t *testing.T) {
stubRpc, oracle := setupPreimageOracleTest(t) t.Run("UnknownType", func(t *testing.T) {
_, oracle := setupPreimageOracleTest(t)
data := types.NewPreimageOracleData(common.Hash{0xcc}.Bytes(), make([]byte, 20), uint32(545))
_, err := oracle.AddGlobalDataTx(data)
require.ErrorIs(t, err, ErrUnsupportedKeyType)
})
data := types.NewPreimageOracleData(common.Hash{0xcc}.Bytes(), make([]byte, 20), 545) t.Run("Keccak256", func(t *testing.T) {
stubRpc.SetResponse(oracleAddr, methodLoadKeccak256PreimagePart, batching.BlockLatest, []interface{}{ stubRpc, oracle := setupPreimageOracleTest(t)
new(big.Int).SetUint64(uint64(data.OracleOffset)), data := types.NewPreimageOracleData(common.Hash{byte(preimage.Keccak256KeyType), 0xcc}.Bytes(), make([]byte, 20), uint32(545))
data.GetPreimageWithoutSize(), stubRpc.SetResponse(oracleAddr, methodLoadKeccak256PreimagePart, batching.BlockLatest, []interface{}{
}, nil) new(big.Int).SetUint64(uint64(data.OracleOffset)),
data.GetPreimageWithoutSize(),
}, nil)
tx, err := oracle.AddGlobalDataTx(data) tx, err := oracle.AddGlobalDataTx(data)
require.NoError(t, err) require.NoError(t, err)
stubRpc.VerifyTxCandidate(tx) stubRpc.VerifyTxCandidate(tx)
})
t.Run("Sha256", func(t *testing.T) {
stubRpc, oracle := setupPreimageOracleTest(t)
data := types.NewPreimageOracleData(common.Hash{byte(preimage.Sha256KeyType), 0xcc}.Bytes(), make([]byte, 20), uint32(545))
stubRpc.SetResponse(oracleAddr, methodLoadSha256PreimagePart, batching.BlockLatest, []interface{}{
new(big.Int).SetUint64(uint64(data.OracleOffset)),
data.GetPreimageWithoutSize(),
}, nil)
tx, err := oracle.AddGlobalDataTx(data)
require.NoError(t, err)
stubRpc.VerifyTxCandidate(tx)
})
} }
func TestPreimageOracleContract_ChallengePeriod(t *testing.T) { func TestPreimageOracleContract_ChallengePeriod(t *testing.T) {
......
...@@ -6,6 +6,7 @@ import ( ...@@ -6,6 +6,7 @@ import (
"github.com/ethereum-optimism/optimism/op-bindings/bindings" "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"
preimage "github.com/ethereum-optimism/optimism/op-preimage"
"github.com/ethereum-optimism/optimism/op-service/sources/batching" "github.com/ethereum-optimism/optimism/op-service/sources/batching"
batchingTest "github.com/ethereum-optimism/optimism/op-service/sources/batching/test" batchingTest "github.com/ethereum-optimism/optimism/op-service/sources/batching/test"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
...@@ -24,7 +25,7 @@ func TestVMContract_Oracle(t *testing.T) { ...@@ -24,7 +25,7 @@ func TestVMContract_Oracle(t *testing.T) {
oracleContract, err := vmContract.Oracle(context.Background()) oracleContract, err := vmContract.Oracle(context.Background())
require.NoError(t, err) require.NoError(t, err)
tx, err := oracleContract.AddGlobalDataTx(types.NewPreimageOracleData(common.Hash{}.Bytes(), make([]byte, 20), 0)) tx, err := oracleContract.AddGlobalDataTx(types.NewPreimageOracleData(common.Hash{byte(preimage.Keccak256KeyType)}.Bytes(), make([]byte, 20), 0))
require.NoError(t, err) require.NoError(t, err)
// This test doesn't care about all the tx details, we just want to confirm the contract binding is using the // This test doesn't care about all the tx details, we just want to confirm the contract binding is using the
// correct address // correct address
......
...@@ -6,6 +6,7 @@ import ( ...@@ -6,6 +6,7 @@ import (
"math/big" "math/big"
"time" "time"
preimage "github.com/ethereum-optimism/optimism/op-preimage"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
) )
...@@ -48,7 +49,7 @@ func (p *PreimageOracleData) GetPreimageWithSize() []byte { ...@@ -48,7 +49,7 @@ func (p *PreimageOracleData) GetPreimageWithSize() []byte {
// NewPreimageOracleData creates a new [PreimageOracleData] instance. // NewPreimageOracleData creates a new [PreimageOracleData] instance.
func NewPreimageOracleData(key []byte, data []byte, offset uint32) *PreimageOracleData { func NewPreimageOracleData(key []byte, data []byte, offset uint32) *PreimageOracleData {
return &PreimageOracleData{ return &PreimageOracleData{
IsLocal: len(key) > 0 && key[0] == byte(1), IsLocal: len(key) > 0 && key[0] == byte(preimage.LocalKeyType),
OracleKey: key, OracleKey: key,
oracleData: data, oracleData: data,
OracleOffset: offset, OracleOffset: offset,
......
...@@ -34,7 +34,7 @@ const ( ...@@ -34,7 +34,7 @@ const (
LocalKeyType KeyType = 1 LocalKeyType KeyType = 1
// Keccak256KeyType is for keccak256 pre-images, for any global shared pre-images. // Keccak256KeyType is for keccak256 pre-images, for any global shared pre-images.
Keccak256KeyType KeyType = 2 Keccak256KeyType KeyType = 2
// GlobalGenericKeyType is a reseved key type for generic global data. // GlobalGenericKeyType is a reserved key type for generic global data.
GlobalGenericKeyType KeyType = 3 GlobalGenericKeyType KeyType = 3
// Sha256KeyType is for sha256 pre-images, for any global shared pre-images. // Sha256KeyType is for sha256 pre-images, for any global shared pre-images.
Sha256KeyType KeyType = 4 Sha256KeyType KeyType = 4
......
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