2020-08-11 14:20:05 +00:00
|
|
|
package paychmgr
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-09-16 04:06:04 +00:00
|
|
|
"errors"
|
2020-08-11 14:20:05 +00:00
|
|
|
"sync"
|
|
|
|
|
2020-09-16 04:06:04 +00:00
|
|
|
"github.com/ipfs/go-cid"
|
2020-09-01 14:33:44 +00:00
|
|
|
|
2020-09-16 04:06:04 +00:00
|
|
|
"github.com/filecoin-project/go-address"
|
2021-04-05 19:34:03 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/abi"
|
2020-09-07 03:49:10 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/crypto"
|
2020-09-30 17:04:10 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/network"
|
2020-09-01 14:33:44 +00:00
|
|
|
|
2020-08-11 14:20:05 +00:00
|
|
|
"github.com/filecoin-project/lotus/api"
|
2020-09-30 17:04:10 +00:00
|
|
|
"github.com/filecoin-project/lotus/build"
|
2020-09-16 04:06:04 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/actors/builtin/paych"
|
2020-08-11 14:20:05 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
2020-09-16 04:06:04 +00:00
|
|
|
"github.com/filecoin-project/lotus/lib/sigs"
|
2020-08-11 14:20:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type mockManagerAPI struct {
|
|
|
|
*mockStateManager
|
|
|
|
*mockPaychAPI
|
|
|
|
}
|
|
|
|
|
|
|
|
func newMockManagerAPI() *mockManagerAPI {
|
|
|
|
return &mockManagerAPI{
|
|
|
|
mockStateManager: newMockStateManager(),
|
|
|
|
mockPaychAPI: newMockPaychAPI(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type mockPchState struct {
|
|
|
|
actor *types.Actor
|
|
|
|
state paych.State
|
|
|
|
}
|
|
|
|
|
|
|
|
type mockStateManager struct {
|
|
|
|
lk sync.Mutex
|
2020-09-16 04:06:04 +00:00
|
|
|
accountState map[address.Address]address.Address
|
2020-08-11 14:20:05 +00:00
|
|
|
paychState map[address.Address]mockPchState
|
|
|
|
response *api.InvocResult
|
2020-08-20 16:09:52 +00:00
|
|
|
lastCall *types.Message
|
2020-08-11 14:20:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newMockStateManager() *mockStateManager {
|
|
|
|
return &mockStateManager{
|
2020-09-16 04:06:04 +00:00
|
|
|
accountState: make(map[address.Address]address.Address),
|
2020-08-11 14:20:05 +00:00
|
|
|
paychState: make(map[address.Address]mockPchState),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-16 04:06:04 +00:00
|
|
|
func (sm *mockStateManager) setAccountAddress(a address.Address, lookup address.Address) {
|
2020-08-11 14:20:05 +00:00
|
|
|
sm.lk.Lock()
|
|
|
|
defer sm.lk.Unlock()
|
2020-09-16 04:06:04 +00:00
|
|
|
sm.accountState[a] = lookup
|
2020-08-11 14:20:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (sm *mockStateManager) setPaychState(a address.Address, actor *types.Actor, state paych.State) {
|
|
|
|
sm.lk.Lock()
|
|
|
|
defer sm.lk.Unlock()
|
|
|
|
sm.paychState[a] = mockPchState{actor, state}
|
|
|
|
}
|
|
|
|
|
2020-09-16 04:06:04 +00:00
|
|
|
func (sm *mockStateManager) ResolveToKeyAddress(ctx context.Context, addr address.Address, ts *types.TipSet) (address.Address, error) {
|
|
|
|
sm.lk.Lock()
|
|
|
|
defer sm.lk.Unlock()
|
|
|
|
keyAddr, ok := sm.accountState[addr]
|
|
|
|
if !ok {
|
|
|
|
return address.Undef, errors.New("not found")
|
2020-08-11 21:01:16 +00:00
|
|
|
}
|
2020-09-16 04:06:04 +00:00
|
|
|
return keyAddr, nil
|
2020-08-11 21:01:16 +00:00
|
|
|
}
|
|
|
|
|
2020-09-16 04:06:04 +00:00
|
|
|
func (sm *mockStateManager) GetPaychState(ctx context.Context, addr address.Address, ts *types.TipSet) (*types.Actor, paych.State, error) {
|
2020-08-11 14:20:05 +00:00
|
|
|
sm.lk.Lock()
|
|
|
|
defer sm.lk.Unlock()
|
2020-09-16 04:06:04 +00:00
|
|
|
info, ok := sm.paychState[addr]
|
|
|
|
if !ok {
|
|
|
|
return nil, nil, errors.New("not found")
|
2020-08-11 14:20:05 +00:00
|
|
|
}
|
2020-09-16 04:06:04 +00:00
|
|
|
return info.actor, info.state, nil
|
2020-08-11 14:20:05 +00:00
|
|
|
}
|
|
|
|
|
2020-08-20 16:09:52 +00:00
|
|
|
func (sm *mockStateManager) setCallResponse(response *api.InvocResult) {
|
|
|
|
sm.lk.Lock()
|
|
|
|
defer sm.lk.Unlock()
|
|
|
|
|
|
|
|
sm.response = response
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sm *mockStateManager) getLastCall() *types.Message {
|
|
|
|
sm.lk.Lock()
|
|
|
|
defer sm.lk.Unlock()
|
|
|
|
|
|
|
|
return sm.lastCall
|
|
|
|
}
|
|
|
|
|
2020-08-11 14:20:05 +00:00
|
|
|
func (sm *mockStateManager) Call(ctx context.Context, msg *types.Message, ts *types.TipSet) (*api.InvocResult, error) {
|
2020-08-20 16:09:52 +00:00
|
|
|
sm.lk.Lock()
|
|
|
|
defer sm.lk.Unlock()
|
|
|
|
|
|
|
|
sm.lastCall = msg
|
|
|
|
|
2020-08-11 14:20:05 +00:00
|
|
|
return sm.response, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type waitingCall struct {
|
|
|
|
response chan types.MessageReceipt
|
|
|
|
}
|
|
|
|
|
|
|
|
type waitingResponse struct {
|
|
|
|
receipt types.MessageReceipt
|
|
|
|
done chan struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
type mockPaychAPI struct {
|
|
|
|
lk sync.Mutex
|
|
|
|
messages map[cid.Cid]*types.SignedMessage
|
|
|
|
waitingCalls map[cid.Cid]*waitingCall
|
|
|
|
waitingResponses map[cid.Cid]*waitingResponse
|
2020-08-17 15:13:13 +00:00
|
|
|
wallet map[address.Address]struct{}
|
2020-09-01 14:33:44 +00:00
|
|
|
signingKey []byte
|
2020-08-11 14:20:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newMockPaychAPI() *mockPaychAPI {
|
|
|
|
return &mockPaychAPI{
|
|
|
|
messages: make(map[cid.Cid]*types.SignedMessage),
|
|
|
|
waitingCalls: make(map[cid.Cid]*waitingCall),
|
|
|
|
waitingResponses: make(map[cid.Cid]*waitingResponse),
|
2020-08-17 15:13:13 +00:00
|
|
|
wallet: make(map[address.Address]struct{}),
|
2020-08-11 14:20:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-05 19:34:03 +00:00
|
|
|
func (pchapi *mockPaychAPI) StateWaitMsg(ctx context.Context, mcid cid.Cid, confidence uint64, limit abi.ChainEpoch, allowReplaced bool) (*api.MsgLookup, error) {
|
2020-08-11 14:20:05 +00:00
|
|
|
pchapi.lk.Lock()
|
|
|
|
|
2022-06-09 08:36:50 +00:00
|
|
|
response := make(chan types.MessageReceipt, 1)
|
2020-08-11 14:20:05 +00:00
|
|
|
|
|
|
|
if response, ok := pchapi.waitingResponses[mcid]; ok {
|
|
|
|
defer pchapi.lk.Unlock()
|
|
|
|
defer func() {
|
|
|
|
go close(response.done)
|
|
|
|
}()
|
|
|
|
|
|
|
|
delete(pchapi.waitingResponses, mcid)
|
|
|
|
return &api.MsgLookup{Receipt: response.receipt}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
pchapi.waitingCalls[mcid] = &waitingCall{response: response}
|
|
|
|
pchapi.lk.Unlock()
|
|
|
|
|
2022-06-09 08:36:50 +00:00
|
|
|
select {
|
|
|
|
case receipt := <-response:
|
|
|
|
return &api.MsgLookup{Receipt: receipt}, nil
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil, ctx.Err()
|
|
|
|
}
|
2020-08-11 14:20:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (pchapi *mockPaychAPI) receiveMsgResponse(mcid cid.Cid, receipt types.MessageReceipt) {
|
|
|
|
pchapi.lk.Lock()
|
|
|
|
|
|
|
|
if call, ok := pchapi.waitingCalls[mcid]; ok {
|
|
|
|
defer pchapi.lk.Unlock()
|
|
|
|
|
|
|
|
delete(pchapi.waitingCalls, mcid)
|
|
|
|
call.response <- receipt
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
done := make(chan struct{})
|
|
|
|
pchapi.waitingResponses[mcid] = &waitingResponse{receipt: receipt, done: done}
|
|
|
|
|
|
|
|
pchapi.lk.Unlock()
|
|
|
|
|
|
|
|
<-done
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send success response for any waiting calls
|
|
|
|
func (pchapi *mockPaychAPI) close() {
|
|
|
|
pchapi.lk.Lock()
|
|
|
|
defer pchapi.lk.Unlock()
|
|
|
|
|
|
|
|
success := types.MessageReceipt{
|
|
|
|
ExitCode: 0,
|
|
|
|
Return: []byte{},
|
|
|
|
}
|
|
|
|
for mcid, call := range pchapi.waitingCalls {
|
|
|
|
delete(pchapi.waitingCalls, mcid)
|
|
|
|
call.response <- success
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-12 20:17:21 +00:00
|
|
|
func (pchapi *mockPaychAPI) MpoolPushMessage(ctx context.Context, msg *types.Message, spec *api.MessageSendSpec) (*types.SignedMessage, error) {
|
2020-08-11 14:20:05 +00:00
|
|
|
pchapi.lk.Lock()
|
|
|
|
defer pchapi.lk.Unlock()
|
|
|
|
|
|
|
|
smsg := &types.SignedMessage{Message: *msg}
|
|
|
|
pchapi.messages[smsg.Cid()] = smsg
|
|
|
|
return smsg, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pchapi *mockPaychAPI) pushedMessages(c cid.Cid) *types.SignedMessage {
|
|
|
|
pchapi.lk.Lock()
|
|
|
|
defer pchapi.lk.Unlock()
|
|
|
|
|
|
|
|
return pchapi.messages[c]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pchapi *mockPaychAPI) pushedMessageCount() int {
|
|
|
|
pchapi.lk.Lock()
|
|
|
|
defer pchapi.lk.Unlock()
|
|
|
|
|
|
|
|
return len(pchapi.messages)
|
|
|
|
}
|
2020-08-17 15:13:13 +00:00
|
|
|
|
2020-08-19 22:56:51 +00:00
|
|
|
func (pchapi *mockPaychAPI) StateAccountKey(ctx context.Context, addr address.Address, tsk types.TipSetKey) (address.Address, error) {
|
|
|
|
return addr, nil
|
|
|
|
}
|
|
|
|
|
2020-08-17 15:13:13 +00:00
|
|
|
func (pchapi *mockPaychAPI) WalletHas(ctx context.Context, addr address.Address) (bool, error) {
|
|
|
|
pchapi.lk.Lock()
|
|
|
|
defer pchapi.lk.Unlock()
|
|
|
|
|
|
|
|
_, ok := pchapi.wallet[addr]
|
|
|
|
return ok, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pchapi *mockPaychAPI) addWalletAddress(addr address.Address) {
|
|
|
|
pchapi.lk.Lock()
|
|
|
|
defer pchapi.lk.Unlock()
|
|
|
|
|
|
|
|
pchapi.wallet[addr] = struct{}{}
|
|
|
|
}
|
2020-09-01 14:33:44 +00:00
|
|
|
|
|
|
|
func (pchapi *mockPaychAPI) WalletSign(ctx context.Context, k address.Address, msg []byte) (*crypto.Signature, error) {
|
|
|
|
pchapi.lk.Lock()
|
|
|
|
defer pchapi.lk.Unlock()
|
|
|
|
|
|
|
|
return sigs.Sign(crypto.SigTypeSecp256k1, pchapi.signingKey, msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pchapi *mockPaychAPI) addSigningKey(key []byte) {
|
|
|
|
pchapi.lk.Lock()
|
|
|
|
defer pchapi.lk.Unlock()
|
|
|
|
|
|
|
|
pchapi.signingKey = key
|
|
|
|
}
|
2020-09-30 17:04:10 +00:00
|
|
|
|
|
|
|
func (pchapi *mockPaychAPI) StateNetworkVersion(ctx context.Context, tsk types.TipSetKey) (network.Version, error) {
|
2022-11-16 01:57:23 +00:00
|
|
|
return build.TestNetworkVersion, nil
|
2020-09-30 17:04:10 +00:00
|
|
|
}
|