mock_engine.go 1.78 KB
Newer Older
1 2 3 4
package testutils

import (
	"context"
5
	"encoding/json"
6

7 8
	"github.com/ethereum/go-ethereum/common"

9
	"github.com/ethereum-optimism/optimism/op-service/eth"
10 11 12
)

type MockEngine struct {
13
	MockL2Client
14 15
}

16 17
func (m *MockEngine) GetPayload(ctx context.Context, payloadInfo eth.PayloadInfo) (*eth.ExecutionPayloadEnvelope, error) {
	out := m.Mock.Called(payloadInfo.ID)
18
	return out.Get(0).(*eth.ExecutionPayloadEnvelope), out.Error(1)
19 20
}

21
func (m *MockEngine) ExpectGetPayload(payloadId eth.PayloadID, payload *eth.ExecutionPayloadEnvelope, err error) {
22
	m.Mock.On("GetPayload", payloadId).Once().Return(payload, err)
23 24 25
}

func (m *MockEngine) ForkchoiceUpdate(ctx context.Context, state *eth.ForkchoiceState, attr *eth.PayloadAttributes) (*eth.ForkchoiceUpdatedResult, error) {
26
	out := m.Mock.Called(mustJson(state), mustJson(attr))
27
	return out.Get(0).(*eth.ForkchoiceUpdatedResult), out.Error(1)
28 29 30
}

func (m *MockEngine) ExpectForkchoiceUpdate(state *eth.ForkchoiceState, attr *eth.PayloadAttributes, result *eth.ForkchoiceUpdatedResult, err error) {
31
	m.Mock.On("ForkchoiceUpdate", mustJson(state), mustJson(attr)).Once().Return(result, err)
32 33
}

34
func (m *MockEngine) NewPayload(ctx context.Context, payload *eth.ExecutionPayload, parentBeaconBlockRoot *common.Hash) (*eth.PayloadStatusV1, error) {
35
	out := m.Mock.Called(mustJson(payload), mustJson(parentBeaconBlockRoot))
36
	return out.Get(0).(*eth.PayloadStatusV1), out.Error(1)
37 38
}

39
func (m *MockEngine) ExpectNewPayload(payload *eth.ExecutionPayload, parentBeaconBlockRoot *common.Hash, result *eth.PayloadStatusV1, err error) {
40 41 42 43 44 45 46 47 48
	m.Mock.On("NewPayload", mustJson(payload), mustJson(parentBeaconBlockRoot)).Once().Return(result, err)
}

func mustJson[E any](elem E) string {
	data, err := json.MarshalIndent(elem, "  ", "  ")
	if err != nil {
		panic(err)
	}
	return string(data)
49
}