2022-03-09 19:33:18 +00:00
|
|
|
package mock
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2023-03-17 18:52:56 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/v4/beacon-chain/state"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/beacon-chain/state/stategen"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/consensus-types/primitives"
|
2022-03-09 19:33:18 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func NewMockReplayerBuilder(opt ...MockReplayerBuilderOption) *MockReplayerBuilder {
|
|
|
|
b := &MockReplayerBuilder{}
|
|
|
|
for _, o := range opt {
|
|
|
|
o(b)
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
type MockReplayerBuilderOption func(*MockReplayerBuilder)
|
|
|
|
|
|
|
|
func WithMockState(s state.BeaconState) MockReplayerBuilderOption {
|
|
|
|
return func(b *MockReplayerBuilder) {
|
|
|
|
b.SetMockState(s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type MockReplayerBuilder struct {
|
2023-01-26 14:40:12 +00:00
|
|
|
forSlot map[primitives.Slot]*MockReplayer
|
2022-03-09 19:33:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *MockReplayerBuilder) SetMockState(s state.BeaconState) {
|
|
|
|
if b.forSlot == nil {
|
2023-01-26 14:40:12 +00:00
|
|
|
b.forSlot = make(map[primitives.Slot]*MockReplayer)
|
2022-03-09 19:33:18 +00:00
|
|
|
}
|
|
|
|
b.forSlot[s.Slot()] = &MockReplayer{State: s}
|
|
|
|
}
|
|
|
|
|
2023-01-26 14:40:12 +00:00
|
|
|
func (b *MockReplayerBuilder) SetMockStateForSlot(s state.BeaconState, slot primitives.Slot) {
|
2022-04-04 13:55:55 +00:00
|
|
|
if b.forSlot == nil {
|
2023-01-26 14:40:12 +00:00
|
|
|
b.forSlot = make(map[primitives.Slot]*MockReplayer)
|
2022-04-04 13:55:55 +00:00
|
|
|
}
|
|
|
|
b.forSlot[slot] = &MockReplayer{State: s}
|
|
|
|
}
|
|
|
|
|
2023-01-26 14:40:12 +00:00
|
|
|
func (b *MockReplayerBuilder) SetMockSlotError(s primitives.Slot, e error) {
|
2022-03-09 19:33:18 +00:00
|
|
|
if b.forSlot == nil {
|
2023-01-26 14:40:12 +00:00
|
|
|
b.forSlot = make(map[primitives.Slot]*MockReplayer)
|
2022-03-09 19:33:18 +00:00
|
|
|
}
|
|
|
|
b.forSlot[s] = &MockReplayer{Err: e}
|
|
|
|
}
|
|
|
|
|
2023-01-26 14:40:12 +00:00
|
|
|
func (b *MockReplayerBuilder) ReplayerForSlot(target primitives.Slot) stategen.Replayer {
|
2022-03-09 19:33:18 +00:00
|
|
|
return b.forSlot[target]
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ stategen.ReplayerBuilder = &MockReplayerBuilder{}
|
|
|
|
|
|
|
|
type MockReplayer struct {
|
|
|
|
State state.BeaconState
|
|
|
|
Err error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MockReplayer) ReplayBlocks(_ context.Context) (state.BeaconState, error) {
|
|
|
|
return m.State, m.Err
|
|
|
|
}
|
|
|
|
|
2023-01-26 14:40:12 +00:00
|
|
|
func (m *MockReplayer) ReplayToSlot(_ context.Context, _ primitives.Slot) (state.BeaconState, error) {
|
2022-03-09 19:33:18 +00:00
|
|
|
return m.State, m.Err
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ stategen.Replayer = &MockReplayer{}
|
|
|
|
|
|
|
|
type MockCanonicalChecker struct {
|
|
|
|
Is bool
|
|
|
|
Err error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MockCanonicalChecker) IsCanonical(_ context.Context, _ [32]byte) (bool, error) {
|
|
|
|
return m.Is, m.Err
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ stategen.CanonicalChecker = &MockCanonicalChecker{}
|
|
|
|
|
|
|
|
type MockCurrentSlotter struct {
|
2023-01-26 14:40:12 +00:00
|
|
|
Slot primitives.Slot
|
2022-03-09 19:33:18 +00:00
|
|
|
}
|
|
|
|
|
2023-01-26 14:40:12 +00:00
|
|
|
func (c *MockCurrentSlotter) CurrentSlot() primitives.Slot {
|
2022-03-09 19:33:18 +00:00
|
|
|
return c.Slot
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ stategen.CurrentSlotter = &MockCurrentSlotter{}
|