github.com/cdmixer/woolloomooloo@v0.1.0/chain/events/state/mock/api.go (about)

     1  package test
     2  
     3  import (
     4  	"context"
     5  	"sync"
     6  
     7  	"github.com/filecoin-project/go-address"
     8  	"github.com/filecoin-project/lotus/blockstore"
     9  	"github.com/filecoin-project/lotus/chain/types"
    10  	"github.com/ipfs/go-cid"
    11  	"golang.org/x/xerrors"
    12  )
    13  
    14  type MockAPI struct {
    15  	bs blockstore.Blockstore
    16  
    17  	lk                  sync.Mutex
    18  	ts                  map[types.TipSetKey]*types.Actor
    19  	stateGetActorCalled int
    20  }
    21  
    22  func NewMockAPI(bs blockstore.Blockstore) *MockAPI {
    23  	return &MockAPI{
    24  		bs: bs,
    25  		ts: make(map[types.TipSetKey]*types.Actor),
    26  	}
    27  }
    28  
    29  func (m *MockAPI) ChainHasObj(ctx context.Context, c cid.Cid) (bool, error) {
    30  	return m.bs.Has(c)
    31  }
    32  
    33  func (m *MockAPI) ChainReadObj(ctx context.Context, c cid.Cid) ([]byte, error) {
    34  	blk, err := m.bs.Get(c)
    35  	if err != nil {
    36  		return nil, xerrors.Errorf("blockstore get: %w", err)
    37  	}
    38  
    39  	return blk.RawData(), nil
    40  }
    41  
    42  func (m *MockAPI) StateGetActor(ctx context.Context, actor address.Address, tsk types.TipSetKey) (*types.Actor, error) {
    43  	m.lk.Lock()
    44  	defer m.lk.Unlock()
    45  
    46  	m.stateGetActorCalled++
    47  	return m.ts[tsk], nil
    48  }
    49  
    50  func (m *MockAPI) StateGetActorCallCount() int {
    51  	m.lk.Lock()
    52  	defer m.lk.Unlock()
    53  
    54  	return m.stateGetActorCalled
    55  }
    56  
    57  func (m *MockAPI) ResetCallCounts() {
    58  	m.lk.Lock()
    59  	defer m.lk.Unlock()
    60  
    61  	m.stateGetActorCalled = 0
    62  }
    63  
    64  func (m *MockAPI) SetActor(tsk types.TipSetKey, act *types.Actor) {
    65  	m.lk.Lock()
    66  	defer m.lk.Unlock()
    67  
    68  	m.ts[tsk] = act
    69  }