@@ -27,9 +27,9 @@ func NewMockCometRPC(respQuery abci.QueryResponse) MockCometRPC {
|
||||
return MockCometRPC{responseQuery: respQuery}
|
||||
}
|
||||
|
||||
// NewMockCometRPCWithValue returns a mock CometBFT RPC implementation with value only.
|
||||
// NewMockCometRPCWithResponseQueryValue returns a mock CometBFT RPC implementation with value only.
|
||||
// It is used for CLI testing.
|
||||
func NewMockCometRPCWithValue(bz []byte) MockCometRPC {
|
||||
func NewMockCometRPCWithResponseQueryValue(bz []byte) MockCometRPC {
|
||||
return MockCometRPC{responseQuery: abci.QueryResponse{
|
||||
Value: bz,
|
||||
}}
|
||||
@@ -47,3 +47,80 @@ func (m MockCometRPC) ABCIQueryWithOptions(
|
||||
) (*coretypes.ResultABCIQuery, error) {
|
||||
return &coretypes.ResultABCIQuery{Response: m.responseQuery}, nil
|
||||
}
|
||||
|
||||
type FilterTxsFn = func(query string, start, end int) ([][]byte, error)
|
||||
|
||||
type MockCometTxSearchRPC struct {
|
||||
rpcclientmock.Client
|
||||
|
||||
txConfig client.TxConfig
|
||||
txs []cmttypes.Tx
|
||||
filterTxsFn FilterTxsFn
|
||||
}
|
||||
|
||||
func (m MockCometTxSearchRPC) Txs() []cmttypes.Tx {
|
||||
return m.txs
|
||||
}
|
||||
|
||||
// accept [][]byte so that module that use this for testing dont have to import comet directly
|
||||
func (m *MockCometTxSearchRPC) WithTxs(txs [][]byte) {
|
||||
cmtTxs := make([]cmttypes.Tx, len(txs))
|
||||
for i, tx := range txs {
|
||||
cmtTxs[i] = tx
|
||||
}
|
||||
m.txs = cmtTxs
|
||||
}
|
||||
|
||||
func (m *MockCometTxSearchRPC) WithTxConfig(cfg client.TxConfig) {
|
||||
m.txConfig = cfg
|
||||
}
|
||||
|
||||
func (m *MockCometTxSearchRPC) WithFilterTxsFn(fn FilterTxsFn) {
|
||||
m.filterTxsFn = fn
|
||||
}
|
||||
|
||||
func (MockCometTxSearchRPC) BroadcastTxSync(context.Context, cmttypes.Tx) (*coretypes.ResultBroadcastTx, error) {
|
||||
return &coretypes.ResultBroadcastTx{Code: 0}, nil
|
||||
}
|
||||
|
||||
func (mock MockCometTxSearchRPC) TxSearch(ctx context.Context, query string, prove bool, page, perPage *int, orderBy string) (*coretypes.ResultTxSearch, error) {
|
||||
if page == nil {
|
||||
*page = 0
|
||||
}
|
||||
|
||||
if perPage == nil {
|
||||
*perPage = 0
|
||||
}
|
||||
|
||||
start, end := client.Paginate(len(mock.txs), *page, *perPage, 100)
|
||||
if start < 0 || end < 0 {
|
||||
// nil result with nil error crashes utils.QueryTxsByEvents
|
||||
return &coretypes.ResultTxSearch{}, nil
|
||||
}
|
||||
|
||||
var txs []cmttypes.Tx
|
||||
if mock.filterTxsFn != nil {
|
||||
filterTxs, err := mock.filterTxsFn(query, start, end)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
cmtTxs := make([]cmttypes.Tx, len(filterTxs))
|
||||
for i, tx := range filterTxs {
|
||||
cmtTxs[i] = tx
|
||||
}
|
||||
txs = append(txs, cmtTxs...)
|
||||
} else {
|
||||
txs = mock.txs[start:end]
|
||||
}
|
||||
|
||||
rst := &coretypes.ResultTxSearch{Txs: make([]*coretypes.ResultTx, len(txs)), TotalCount: len(txs)}
|
||||
for i := range txs {
|
||||
rst.Txs[i] = &coretypes.ResultTx{Tx: txs[i]}
|
||||
}
|
||||
return rst, nil
|
||||
}
|
||||
|
||||
func (mock MockCometTxSearchRPC) Block(ctx context.Context, height *int64) (*coretypes.ResultBlock, error) {
|
||||
return &coretypes.ResultBlock{Block: &cmttypes.Block{}}, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user