oracle.go 5.33 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
package testutil

import (
	"encoding/binary"
	"encoding/hex"
	"fmt"
	"strings"
	"testing"

	"github.com/ethereum/go-ethereum/common"
	"github.com/ethereum/go-ethereum/crypto"
	"github.com/stretchr/testify/require"

	"github.com/ethereum-optimism/optimism/cannon/mipsevm"
	preimage "github.com/ethereum-optimism/optimism/op-preimage"
)

type TestOracle struct {
	hint        func(v []byte)
	getPreimage func(k [32]byte) []byte
}

var _ mipsevm.PreimageOracle = (*TestOracle)(nil)

func (t *TestOracle) Hint(v []byte) {
	t.hint(v)
}

func (t *TestOracle) GetPreimage(k [32]byte) []byte {
	return t.getPreimage(k)
}

func StaticOracle(t *testing.T, preimageData []byte) *TestOracle {
	return &TestOracle{
		hint: func(v []byte) {},
		getPreimage: func(k [32]byte) []byte {
			if k != preimage.Keccak256Key(crypto.Keccak256Hash(preimageData)).PreimageKey() {
				t.Fatalf("invalid preimage request for %x", k)
			}
			return preimageData
		},
	}
}

45
func StaticPrecompileOracle(t *testing.T, precompile common.Address, requiredGas uint64, input []byte, result []byte) *TestOracle {
46 47 48
	return &TestOracle{
		hint: func(v []byte) {},
		getPreimage: func(k [32]byte) []byte {
49 50 51
			requiredGasB := binary.BigEndian.AppendUint64(nil, requiredGas)
			keyData := append(precompile.Bytes(), requiredGasB...)
			keyData = append(keyData, input...)
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
			switch k[0] {
			case byte(preimage.Keccak256KeyType):
				if k != preimage.Keccak256Key(crypto.Keccak256Hash(keyData)).PreimageKey() {
					t.Fatalf("invalid preimage request for %x", k)
				}
				return keyData
			case byte(preimage.PrecompileKeyType):
				if k != preimage.PrecompileKey(crypto.Keccak256Hash(keyData)).PreimageKey() {
					t.Fatalf("invalid preimage request for %x", k)
				}
				return result
			}
			panic("unreachable")
		},
	}
}

func ClaimTestOracle(t *testing.T) (po mipsevm.PreimageOracle, stdOut string, stdErr string) {
70
	s := uint64(0x00FFFFFF_00001000)
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
	a := uint64(3)
	b := uint64(4)

	encodeU64 := func(x uint64) []byte {
		return binary.BigEndian.AppendUint64(nil, x)
	}

	var diff []byte
	diff = append(diff, crypto.Keccak256(encodeU64(a))...)
	diff = append(diff, crypto.Keccak256(encodeU64(b))...)

	preHash := crypto.Keccak256Hash(encodeU64(s))
	diffHash := crypto.Keccak256Hash(diff)

	images := make(map[[32]byte][]byte)
	images[preimage.LocalIndexKey(0).PreimageKey()] = preHash[:]
	images[preimage.LocalIndexKey(1).PreimageKey()] = diffHash[:]
	images[preimage.LocalIndexKey(2).PreimageKey()] = encodeU64(s*a + b)

	oracle := &TestOracle{
		hint: func(v []byte) {
			parts := strings.Split(string(v), " ")
			require.Len(t, parts, 2)
			p, err := hex.DecodeString(parts[1])
			require.NoError(t, err)
			require.Len(t, p, 32)
			h := common.Hash(*(*[32]byte)(p))
			switch parts[0] {
			case "fetch-state":
				require.Equal(t, h, preHash, "expecting request for pre-state pre-image")
				images[preimage.Keccak256Key(preHash).PreimageKey()] = encodeU64(s)
			case "fetch-diff":
				require.Equal(t, h, diffHash, "expecting request for diff pre-images")
				images[preimage.Keccak256Key(diffHash).PreimageKey()] = diff
				images[preimage.Keccak256Key(crypto.Keccak256Hash(encodeU64(a))).PreimageKey()] = encodeU64(a)
				images[preimage.Keccak256Key(crypto.Keccak256Hash(encodeU64(b))).PreimageKey()] = encodeU64(b)
			default:
				t.Fatalf("unexpected hint: %q", parts[0])
			}
		},
		getPreimage: func(k [32]byte) []byte {
			p, ok := images[k]
			if !ok {
				t.Fatalf("missing pre-image %s", k)
			}
			return p
		},
	}

	return oracle, fmt.Sprintf("computing %d * %d + %d\nclaim %d is good!\n", s, a, b, s*a+b), "started!"
}

123
func AllocOracle(t *testing.T, numAllocs int, allocSize int) *TestOracle {
124 125 126
	return &TestOracle{
		hint: func(v []byte) {},
		getPreimage: func(k [32]byte) []byte {
127 128 129 130 131 132
			switch k {
			case preimage.LocalIndexKey(0).PreimageKey():
				return binary.LittleEndian.AppendUint64(nil, uint64(numAllocs))
			case preimage.LocalIndexKey(1).PreimageKey():
				return binary.LittleEndian.AppendUint64(nil, uint64(allocSize))
			default:
133 134
				t.Fatalf("invalid preimage request for %x", k)
			}
135
			panic("unreachable")
136 137 138 139 140 141 142 143 144
		},
	}
}

func SelectOracleFixture(t *testing.T, programName string) mipsevm.PreimageOracle {
	if strings.HasPrefix(programName, "oracle_kzg") {
		precompile := common.BytesToAddress([]byte{0xa})
		input := common.FromHex("01e798154708fe7789429634053cbf9f99b619f9f084048927333fce637f549b564c0a11a0f704f4fc3e8acfe0f8245f0ad1347b378fbf96e206da11a5d3630624d25032e67a7e6a4910df5834b8fe70e6bcfeeac0352434196bdf4b2485d5a18f59a8d2a1a625a17f3fea0fe5eb8c896db3764f3185481bc22f91b4aaffcca25f26936857bc3a7c2539ea8ec3a952b7873033e038326e87ed3e1276fd140253fa08e9fc25fb2d9a98527fc22a2c9612fbeafdad446cbc7bcdbdcd780af2c16a")
		blobPrecompileReturnValue := common.FromHex("000000000000000000000000000000000000000000000000000000000000100073eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000001")
145 146
		requiredGas := uint64(50_000)
		return StaticPrecompileOracle(t, precompile, requiredGas, input, append([]byte{0x1}, blobPrecompileReturnValue...))
147 148 149 150 151 152
	} else if strings.HasPrefix(programName, "oracle") {
		return StaticOracle(t, []byte("hello world"))
	} else {
		return nil
	}
}
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168

type HintTrackingOracle struct {
	hints [][]byte
}

func (t *HintTrackingOracle) Hint(v []byte) {
	t.hints = append(t.hints, v)
}

func (t *HintTrackingOracle) GetPreimage(k [32]byte) []byte {
	return nil
}

func (t *HintTrackingOracle) Hints() [][]byte {
	return t.hints
}