-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathmock_beacon_instance.go
More file actions
139 lines (110 loc) · 3.46 KB
/
Copy pathmock_beacon_instance.go
File metadata and controls
139 lines (110 loc) · 3.46 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package beaconclient
import (
"sync"
"time"
"github.com/flashbots/mev-boost-relay/common"
)
type MockBeaconInstance struct {
mu sync.RWMutex
validatorSet map[common.PubkeyHex]ValidatorResponseEntry
MockSyncStatus *SyncStatusPayloadData
MockSyncStatusErr error
MockProposerDuties *ProposerDutiesResponse
MockProposerDutiesErr error
MockFetchValidatorsErr error
ResponseDelay time.Duration
}
func NewMockBeaconInstance() *MockBeaconInstance {
return &MockBeaconInstance{
validatorSet: make(map[common.PubkeyHex]ValidatorResponseEntry),
MockSyncStatus: &SyncStatusPayloadData{
HeadSlot: 1,
IsSyncing: false,
},
MockProposerDuties: &ProposerDutiesResponse{
Data: []ProposerDutiesResponseData{},
},
MockSyncStatusErr: nil,
MockProposerDutiesErr: nil,
MockFetchValidatorsErr: nil,
ResponseDelay: 0,
mu: sync.RWMutex{},
}
}
func (c *MockBeaconInstance) AddValidator(entry ValidatorResponseEntry) {
c.mu.Lock()
c.validatorSet[common.NewPubkeyHex(entry.Validator.Pubkey)] = entry
c.mu.Unlock()
}
func (c *MockBeaconInstance) SetValidators(validatorSet map[common.PubkeyHex]ValidatorResponseEntry) {
c.mu.Lock()
c.validatorSet = validatorSet
c.mu.Unlock()
}
func (c *MockBeaconInstance) IsValidator(pubkey common.PubkeyHex) bool {
c.mu.RLock()
_, found := c.validatorSet[pubkey]
c.mu.RUnlock()
return found
}
func (c *MockBeaconInstance) NumValidators() uint64 {
c.mu.RLock()
defer c.mu.RUnlock()
return uint64(len(c.validatorSet))
}
func (c *MockBeaconInstance) GetStateValidators(stateID string) (*GetStateValidatorsResponse, error) {
c.addDelay()
validatorResp := &GetStateValidatorsResponse{ //nolint:exhaustruct
Data: make([]ValidatorResponseEntry, 0),
}
for _, entry := range c.validatorSet {
validatorResp.Data = append(validatorResp.Data, entry)
}
return validatorResp, c.MockFetchValidatorsErr
}
func (c *MockBeaconInstance) SyncStatus() (*SyncStatusPayloadData, error) {
c.addDelay()
return c.MockSyncStatus, c.MockSyncStatusErr
}
func (c *MockBeaconInstance) CurrentSlot() (uint64, error) {
c.addDelay()
return c.MockSyncStatus.HeadSlot, nil
}
func (c *MockBeaconInstance) SubscribeToHeadEvents(slotC chan HeadEventData) {}
func (c *MockBeaconInstance) SubscribeToPayloadAttributesEvents(slotC chan PayloadAttributesEvent) {}
func (c *MockBeaconInstance) GetProposerDuties(epoch uint64) (*ProposerDutiesResponse, error) {
c.addDelay()
return c.MockProposerDuties, c.MockProposerDutiesErr
}
func (c *MockBeaconInstance) GetURI() string {
return ""
}
func (c *MockBeaconInstance) GetPublishURI() string {
return ""
}
func (c *MockBeaconInstance) addDelay() {
if c.ResponseDelay > 0 {
time.Sleep(c.ResponseDelay)
}
}
func (c *MockBeaconInstance) PublishBlock(block *common.VersionedSignedProposal, broadcaseMode BroadcastMode) (code int, err error) {
return 0, nil
}
func (c *MockBeaconInstance) GetGenesis() (*GetGenesisResponse, error) {
return nil, nil
}
func (c *MockBeaconInstance) GetSpec() (spec *GetSpecResponse, err error) {
return nil, nil
}
func (c *MockBeaconInstance) GetSpecRaw() (spec map[string]interface{}, err error) {
return nil, nil
}
func (c *MockBeaconInstance) GetForkSchedule() (spec *GetForkScheduleResponse, err error) {
return nil, nil
}
func (c *MockBeaconInstance) GetRandao(slot uint64) (spec *GetRandaoResponse, err error) {
return nil, nil
}
func (c *MockBeaconInstance) GetWithdrawals(slot uint64) (spec *GetWithdrawalsResponse, err error) {
return nil, nil
}