2020-10-28 11:38:06 +00:00
|
|
|
package test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"sync"
|
|
|
|
|
2022-06-07 03:14:16 +00:00
|
|
|
blocks "github.com/ipfs/go-block-format"
|
2022-06-14 15:00:51 +00:00
|
|
|
"github.com/ipfs/go-cid"
|
|
|
|
"golang.org/x/xerrors"
|
2022-06-07 03:14:16 +00:00
|
|
|
|
2020-10-28 11:38:06 +00:00
|
|
|
"github.com/filecoin-project/go-address"
|
2022-06-14 15:00:51 +00:00
|
|
|
|
2021-01-29 20:01:00 +00:00
|
|
|
"github.com/filecoin-project/lotus/blockstore"
|
2020-10-28 11:38:06 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
type MockAPI struct {
|
|
|
|
bs blockstore.Blockstore
|
|
|
|
|
|
|
|
lk sync.Mutex
|
|
|
|
ts map[types.TipSetKey]*types.Actor
|
|
|
|
stateGetActorCalled int
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewMockAPI(bs blockstore.Blockstore) *MockAPI {
|
|
|
|
return &MockAPI{
|
|
|
|
bs: bs,
|
|
|
|
ts: make(map[types.TipSetKey]*types.Actor),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MockAPI) ChainHasObj(ctx context.Context, c cid.Cid) (bool, error) {
|
2021-12-14 15:05:10 +00:00
|
|
|
return m.bs.Has(ctx, c)
|
2020-10-28 11:38:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MockAPI) ChainReadObj(ctx context.Context, c cid.Cid) ([]byte, error) {
|
2021-12-14 15:05:10 +00:00
|
|
|
blk, err := m.bs.Get(ctx, c)
|
2020-10-28 11:38:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("blockstore get: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return blk.RawData(), nil
|
|
|
|
}
|
|
|
|
|
2022-06-07 03:14:16 +00:00
|
|
|
func (m *MockAPI) ChainPutObj(ctx context.Context, block blocks.Block) error {
|
|
|
|
return m.bs.Put(ctx, block)
|
|
|
|
}
|
|
|
|
|
2020-10-28 11:38:06 +00:00
|
|
|
func (m *MockAPI) StateGetActor(ctx context.Context, actor address.Address, tsk types.TipSetKey) (*types.Actor, error) {
|
|
|
|
m.lk.Lock()
|
|
|
|
defer m.lk.Unlock()
|
|
|
|
|
|
|
|
m.stateGetActorCalled++
|
|
|
|
return m.ts[tsk], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MockAPI) StateGetActorCallCount() int {
|
|
|
|
m.lk.Lock()
|
|
|
|
defer m.lk.Unlock()
|
|
|
|
|
|
|
|
return m.stateGetActorCalled
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MockAPI) ResetCallCounts() {
|
|
|
|
m.lk.Lock()
|
|
|
|
defer m.lk.Unlock()
|
|
|
|
|
|
|
|
m.stateGetActorCalled = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MockAPI) SetActor(tsk types.TipSetKey, act *types.Actor) {
|
|
|
|
m.lk.Lock()
|
|
|
|
defer m.lk.Unlock()
|
|
|
|
|
|
|
|
m.ts[tsk] = act
|
|
|
|
}
|