181 lines
4.3 KiB
Go
181 lines
4.3 KiB
Go
package paychmgr
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"sync"
|
|
|
|
"github.com/filecoin-project/go-address"
|
|
"github.com/filecoin-project/lotus/api"
|
|
"github.com/filecoin-project/lotus/chain/types"
|
|
"github.com/filecoin-project/specs-actors/actors/builtin/account"
|
|
"github.com/filecoin-project/specs-actors/actors/builtin/paych"
|
|
"github.com/ipfs/go-cid"
|
|
)
|
|
|
|
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
|
|
accountState map[address.Address]account.State
|
|
paychState map[address.Address]mockPchState
|
|
response *api.InvocResult
|
|
}
|
|
|
|
func newMockStateManager() *mockStateManager {
|
|
return &mockStateManager{
|
|
accountState: make(map[address.Address]account.State),
|
|
paychState: make(map[address.Address]mockPchState),
|
|
}
|
|
}
|
|
|
|
func (sm *mockStateManager) setAccountState(a address.Address, state account.State) {
|
|
sm.lk.Lock()
|
|
defer sm.lk.Unlock()
|
|
sm.accountState[a] = state
|
|
}
|
|
|
|
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}
|
|
}
|
|
|
|
func (sm *mockStateManager) LoadActorState(ctx context.Context, a address.Address, out interface{}, ts *types.TipSet) (*types.Actor, error) {
|
|
sm.lk.Lock()
|
|
defer sm.lk.Unlock()
|
|
|
|
if outState, ok := out.(*account.State); ok {
|
|
*outState = sm.accountState[a]
|
|
return nil, nil
|
|
}
|
|
if outState, ok := out.(*paych.State); ok {
|
|
info := sm.paychState[a]
|
|
*outState = info.state
|
|
return info.actor, nil
|
|
}
|
|
panic(fmt.Sprintf("unexpected state type %v", out))
|
|
}
|
|
|
|
func (sm *mockStateManager) Call(ctx context.Context, msg *types.Message, ts *types.TipSet) (*api.InvocResult, error) {
|
|
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
|
|
}
|
|
|
|
func newMockPaychAPI() *mockPaychAPI {
|
|
return &mockPaychAPI{
|
|
messages: make(map[cid.Cid]*types.SignedMessage),
|
|
waitingCalls: make(map[cid.Cid]*waitingCall),
|
|
waitingResponses: make(map[cid.Cid]*waitingResponse),
|
|
}
|
|
}
|
|
|
|
func (pchapi *mockPaychAPI) StateWaitMsg(ctx context.Context, mcid cid.Cid, confidence uint64) (*api.MsgLookup, error) {
|
|
pchapi.lk.Lock()
|
|
|
|
response := make(chan types.MessageReceipt)
|
|
|
|
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()
|
|
|
|
receipt := <-response
|
|
return &api.MsgLookup{Receipt: receipt}, nil
|
|
}
|
|
|
|
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
|
|
}
|
|
}
|
|
|
|
func (pchapi *mockPaychAPI) MpoolPushMessage(ctx context.Context, msg *types.Message, spec *api.MessageSendSpec) (*types.SignedMessage, error) {
|
|
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)
|
|
}
|