Commit 43412820 authored by inphi's avatar inphi

improve test coverage

parent 58ad9ccf
......@@ -269,3 +269,11 @@ func RandomOutputResponse(rng *rand.Rand) *eth.OutputResponse {
},
}
}
func RandomOutputV0(rng *rand.Rand) *eth.OutputV0 {
return &eth.OutputV0{
StateRoot: eth.Bytes32(RandomHash(rng)),
MessagePasserStorageRoot: eth.Bytes32(RandomHash(rng)),
BlockHash: RandomHash(rng),
}
}
......@@ -4,6 +4,7 @@ import (
"math/rand"
"testing"
"github.com/ethereum-optimism/optimism/op-node/eth"
"github.com/ethereum-optimism/optimism/op-node/testutils"
"github.com/ethereum-optimism/optimism/op-program/client/l2/test"
"github.com/ethereum/go-ethereum/common"
......@@ -66,3 +67,22 @@ func TestCodeByHash(t *testing.T) {
actual = oracle.CodeByHash(hash)
require.Equal(t, node, actual)
}
func TestOutputByRoot(t *testing.T) {
stub, _ := test.NewStubOracle(t)
oracle := NewCachingOracle(stub)
rng := rand.New(rand.NewSource(1))
output := testutils.RandomOutputV0(rng)
// Initial call retrieves from the stub
root := common.Hash(eth.OutputRoot(output))
stub.Outputs[root] = output
actual := oracle.OutputByRoot(root)
require.Equal(t, output, actual)
// Later calls should retrieve from cache
delete(stub.Outputs, root)
actual = oracle.OutputByRoot(root)
require.Equal(t, output, actual)
}
......@@ -122,3 +122,21 @@ func TestPreimageOracleCodeByHash(t *testing.T) {
})
}
}
func TestPreimageOracleOutputByRoot(t *testing.T) {
rng := rand.New(rand.NewSource(123))
for i := 0; i < 10; i++ {
t.Run(fmt.Sprintf("output_%d", i), func(t *testing.T) {
po, hints, preimages := mockPreimageOracle(t)
output := testutils.RandomOutputV0(rng)
h := common.Hash(eth.OutputRoot(output))
preimages[preimage.Keccak256Key(h).PreimageKey()] = output.Marshal()
hints.On("hint", L2OutputHint(h).Hint()).Once().Return()
gotOutput := po.OutputByRoot(h)
hints.AssertExpectations(t)
require.Equal(t, hexutil.Bytes(output.Marshal()), hexutil.Bytes(gotOutput.Marshal()), "output matches")
})
}
}
......@@ -119,6 +119,8 @@ func TestRetryingL2Source(t *testing.T) {
&types.Transaction{},
}
data := []byte{1, 2, 3, 4, 5}
output := &eth.OutputV0{}
wrongOutput := &eth.OutputV0{BlockHash: common.Hash{0x99}}
t.Run("InfoAndTxsByHash Success", func(t *testing.T) {
source, mock := createL2Source(t)
......@@ -187,6 +189,28 @@ func TestRetryingL2Source(t *testing.T) {
require.NoError(t, err)
require.Equal(t, data, actual)
})
t.Run("OutputByRoot Success", func(t *testing.T) {
source, mock := createL2Source(t)
defer mock.AssertExpectations(t)
mock.ExpectOutputByRoot(hash, output, nil)
actualOutput, err := source.OutputByRoot(ctx, hash)
require.NoError(t, err)
require.Equal(t, output, actualOutput)
})
t.Run("OutputByRoot Error", func(t *testing.T) {
source, mock := createL2Source(t)
defer mock.AssertExpectations(t)
expectedErr := errors.New("boom")
mock.ExpectOutputByRoot(hash, wrongOutput, expectedErr)
mock.ExpectOutputByRoot(hash, output, nil)
actualOutput, err := source.OutputByRoot(ctx, hash)
require.NoError(t, err)
require.Equal(t, output, actualOutput)
})
}
func createL2Source(t *testing.T) (*RetryingL2Source, *MockL2Source) {
......@@ -217,6 +241,11 @@ func (m *MockL2Source) CodeByHash(ctx context.Context, hash common.Hash) ([]byte
return out[0].([]byte), *out[1].(*error)
}
func (m *MockL2Source) OutputByRoot(ctx context.Context, root common.Hash) (eth.Output, error) {
out := m.Mock.MethodCalled("OutputByRoot", root)
return out[0].(eth.Output), *out[1].(*error)
}
func (m *MockL2Source) ExpectInfoAndTxsByHash(blockHash common.Hash, info eth.BlockInfo, txs types.Transactions, err error) {
m.Mock.On("InfoAndTxsByHash", blockHash).Once().Return(info, txs, &err)
}
......@@ -229,11 +258,6 @@ func (m *MockL2Source) ExpectCodeByHash(hash common.Hash, code []byte, err error
m.Mock.On("CodeByHash", hash).Once().Return(code, &err)
}
func (m *MockL2Source) OutputByRoot(ctx context.Context, root common.Hash) (eth.Output, error) {
out := m.Mock.MethodCalled("OutputByRoot", root)
return out[0].(eth.Output), *out[1].(*error)
}
func (m *MockL2Source) ExpectOutputByRoot(root common.Hash, output eth.Output, err error) {
m.Mock.On("OutputByRoot", root).Once().Return(output, &err)
}
......
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