This repository has been archived by the owner on Nov 22, 2023. It is now read-only.
/
testutils.go
197 lines (164 loc) · 5.71 KB
/
testutils.go
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package filecoin
import (
"context"
"fmt"
"sync"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/crypto"
"github.com/filecoin-project/go-state-types/dline"
"github.com/filecoin-project/go-state-types/network"
"github.com/ipfs/go-cid"
)
func testBlockHeader() *BlockHeader {
addr, err := address.NewIDAddress(12512063)
if err != nil {
panic(err)
}
c, err := cid.Decode("bafyreicmaj5hhoy5mgqvamfhgexxyergw7hdeshizghodwkjg6qmpoco7i")
if err != nil {
panic(err)
}
return &BlockHeader{
Miner: addr,
Ticket: &Ticket{
VRFProof: []byte("vrf proof0000000vrf proof0000000"),
},
ElectionProof: &ElectionProof{
VRFProof: []byte("vrf proof0000000vrf proof0000000"),
},
Parents: []cid.Cid{c, c},
ParentMessageReceipts: c,
BLSAggregate: &crypto.Signature{Type: crypto.SigTypeBLS, Data: []byte("bls signature")},
ParentWeight: NewInt(123125126212),
Messages: c,
Height: 85919298723,
ParentStateRoot: c,
BlockSig: &crypto.Signature{Type: crypto.SigTypeBLS, Data: []byte("block signature")},
ParentBaseFee: NewInt(3432432843291),
}
}
// MockLotusAPI is for testing purposes only
type MockLotusAPI struct {
act *Actor // actor to return when calling StateGetActor
head *TipSet // head returned when calling ChainHead
actMu sync.Mutex
actState *ActorState // state returned when calling StateReadState
obj []byte // bytes returned when calling ChainReadObj
objReader func(cid.Cid) []byte // bytes to return given a specific cid
msgLookup chan *MsgLookup // msgLookup to return when calling StateWaitMsg
accMu sync.Mutex
accountKeys map[address.Address]address.Address // address returned when calling StateAccountKey
lookupID address.Address // address returned when calling StateLookupID
invocResult *InvocResult // invocResult returned when calling StateCall
}
func NewMockLotusAPI() *MockLotusAPI {
head, err := NewTipSet([]*BlockHeader{testBlockHeader()})
if err != nil {
panic(err)
}
return &MockLotusAPI{
msgLookup: make(chan *MsgLookup),
accountKeys: make(map[address.Address]address.Address),
head: head,
}
}
func (m *MockLotusAPI) ChainHead(context.Context) (*TipSet, error) {
return m.head, nil
}
func (m *MockLotusAPI) GasEstimateMessageGas(ctx context.Context, msg *Message, spec *MessageSendSpec, tsk TipSetKey) (*Message, error) {
return msg, nil
}
func (m *MockLotusAPI) StateGetActor(ctx context.Context, addr address.Address, tsk TipSetKey) (*Actor, error) {
m.actMu.Lock()
defer m.actMu.Unlock()
return m.act, nil
}
func (m *MockLotusAPI) MpoolPush(ctx context.Context, smsg *SignedMessage) (cid.Cid, error) {
return smsg.Cid(), nil
}
func (m *MockLotusAPI) StateWaitMsg(ctx context.Context, c cid.Cid, conf uint64) (*MsgLookup, error) {
select {
case lkp := <-m.msgLookup:
return lkp, nil
case <-ctx.Done():
return nil, fmt.Errorf("context timeout")
}
}
func (m *MockLotusAPI) StateSearchMsg(ctx context.Context, c cid.Cid) (*MsgLookup, error) {
select {
case lkp := <-m.msgLookup:
return lkp, nil
case <-ctx.Done():
return nil, fmt.Errorf("context timeout")
}
}
func (m *MockLotusAPI) StateAccountKey(ctx context.Context, addr address.Address, tsk TipSetKey) (address.Address, error) {
m.accMu.Lock()
defer m.accMu.Unlock()
if key, ok := m.accountKeys[addr]; ok {
return key, nil
}
return addr, nil
}
func (m *MockLotusAPI) StateLookupID(ctx context.Context, addr address.Address, tsk TipSetKey) (address.Address, error) {
return m.lookupID, nil
}
func (m *MockLotusAPI) StateReadState(ctx context.Context, addr address.Address, tsk TipSetKey) (*ActorState, error) {
return m.actState, nil
}
func (m *MockLotusAPI) StateNetworkVersion(ctx context.Context, tsk TipSetKey) (network.Version, error) {
return network.Version10, nil
}
func (m *MockLotusAPI) StateMarketBalance(ctx context.Context, addr address.Address, tsk TipSetKey) (MarketBalance, error) {
return MarketBalance{}, nil
}
func (m *MockLotusAPI) StateDealProviderCollateralBounds(ctx context.Context, s abi.PaddedPieceSize, b bool, tsk TipSetKey) (DealCollateralBounds, error) {
return DealCollateralBounds{}, nil
}
func (m *MockLotusAPI) StateMinerProvingDeadline(ctx context.Context, addr address.Address, tsk TipSetKey) (*dline.Info, error) {
return nil, nil
}
func (m *MockLotusAPI) StateCall(ctx context.Context, msg *Message, tsk TipSetKey) (*InvocResult, error) {
return m.invocResult, nil
}
func (m *MockLotusAPI) ChainGetMessage(ctx context.Context, c cid.Cid) (*Message, error) {
return nil, nil
}
func (m *MockLotusAPI) ChainReadObj(ctx context.Context, c cid.Cid) ([]byte, error) {
if m.objReader != nil {
return m.objReader(c), nil
}
return m.obj, nil
}
func (m *MockLotusAPI) StateMinerInfo(ctx context.Context, addr address.Address, tsk TipSetKey) (MinerInfo, error) {
return MinerInfo{}, nil
}
func (m *MockLotusAPI) Close() {}
// Set lotus data
func (m *MockLotusAPI) SetActor(act *Actor) {
m.act = act
}
func (m *MockLotusAPI) SetActorState(state *ActorState) {
m.actMu.Lock()
m.actState = state
m.actMu.Unlock()
}
func (m *MockLotusAPI) SetObject(obj []byte) {
m.obj = obj
}
func (m *MockLotusAPI) SetObjectReader(r func(cid.Cid) []byte) {
m.objReader = r
}
func (m *MockLotusAPI) SetAccountKey(addr address.Address, key address.Address) {
m.accMu.Lock()
m.accountKeys[addr] = key
m.accMu.Unlock()
}
// SetMsgLookup to release the StateWaitMsg request
func (m *MockLotusAPI) SetMsgLookup(lkp *MsgLookup) {
m.msgLookup <- lkp
}
func (m *MockLotusAPI) SetInvocResult(i *InvocResult) {
m.invocResult = i
}