diff --git a/Makefile b/Makefile index df88c5f..4dfc24b 100644 --- a/Makefile +++ b/Makefile @@ -24,7 +24,7 @@ ${LINT}: $(VGO) install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.47.0 mockpaths: $(eval FFTM_PATH := $(shell $(VGO) list -f '{{.Dir}}' github.com/hyperledger/firefly-transaction-manager/pkg/fftm)) - + $(eval FF_SIGNER_PATH := $(shell $(VGO) list -f '{{.Dir}}' github.com/hyperledger/firefly-signer/pkg/rpcbackend)) define makemock mocks: mocks-$(strip $(1))-$(strip $(2)) @@ -32,7 +32,7 @@ mocks-$(strip $(1))-$(strip $(2)): ${MOCKERY} mockpaths ${MOCKERY} --case underscore --dir $(1) --name $(2) --outpkg $(3) --output mocks/$(strip $(3)) endef -$(eval $(call makemock, internal/jsonrpc, Client, jsonrpcmocks)) +$(eval $(call makemock, $$(FF_SIGNER_PATH), Backend, rpcbackendmocks)) $(eval $(call makemock, $$(FFTM_PATH), Manager, fftmmocks)) firefly-evmconnect: ${GOFILES} diff --git a/go.mod b/go.mod index 98cfb4a..ebfd19a 100644 --- a/go.mod +++ b/go.mod @@ -3,10 +3,9 @@ module github.com/hyperledger/firefly-evmconnect go 1.17 require ( - github.com/go-resty/resty/v2 v2.7.0 github.com/hashicorp/golang-lru v0.5.4 github.com/hyperledger/firefly-common v0.1.20 - github.com/hyperledger/firefly-signer v0.9.13 + github.com/hyperledger/firefly-signer v0.9.16 github.com/hyperledger/firefly-transaction-manager v0.9.7 github.com/sirupsen/logrus v1.8.1 github.com/spf13/cobra v1.4.0 @@ -26,6 +25,7 @@ require ( github.com/ghodss/yaml v1.0.0 // indirect github.com/go-openapi/jsonpointer v0.19.5 // indirect github.com/go-openapi/swag v0.19.15 // indirect + github.com/go-resty/resty/v2 v2.7.0 // indirect github.com/golang/snappy v0.0.4 // indirect github.com/google/uuid v1.3.0 // indirect github.com/gorilla/mux v1.8.0 // indirect diff --git a/go.sum b/go.sum index 77b86c7..961fe01 100644 --- a/go.sum +++ b/go.sum @@ -291,10 +291,8 @@ github.com/hyperledger/firefly-common v0.1.13/go.mod h1:2NqPi5Ud9H6rSlZXkLbotxW7 github.com/hyperledger/firefly-common v0.1.17-0.20220808193503-961a6b241a1a/go.mod h1:MNbaI2spBsdZYOub6Duj9xueE7Qyu9itOmJ4vE8tjYw= github.com/hyperledger/firefly-common v0.1.20 h1:0dShkjlIShyBxkXRmu3vLmpEK6xrqmfc8GhF6k0Vgbg= github.com/hyperledger/firefly-common v0.1.20/go.mod h1:gMlv4Iy5JjnzXmSEdb+tWVDIc/2GhL9MRcgNX+VmI4M= -github.com/hyperledger/firefly-signer v0.9.13 h1:yvKxYTsEmE0XWl0vcZBQV353YmmePvWwIPMr4Lie67o= -github.com/hyperledger/firefly-signer v0.9.13/go.mod h1:GPQRUZOFOAjkLmg8GDjZUjEdUD0gcar+CSVhwltIwyw= -github.com/hyperledger/firefly-transaction-manager v0.9.6 h1:mX2CN82wbq0izm0ANZxMAjsBeEnzeQy9xWUtsQd6N5Y= -github.com/hyperledger/firefly-transaction-manager v0.9.6/go.mod h1:GuwXVVXI6p3tNk99jbYi4PopMzipVBwy3uu5wSKXJEc= +github.com/hyperledger/firefly-signer v0.9.16 h1:qItr8SbEgvnc7yxseD/XE5fPDLQhTb90I+lklc78pLM= +github.com/hyperledger/firefly-signer v0.9.16/go.mod h1:GPQRUZOFOAjkLmg8GDjZUjEdUD0gcar+CSVhwltIwyw= github.com/hyperledger/firefly-transaction-manager v0.9.7 h1:Uoy2xtqO38pufEeRoxqq4rW3xGDjc6qJZyRXo9WnJnk= github.com/hyperledger/firefly-transaction-manager v0.9.7/go.mod h1:GuwXVVXI6p3tNk99jbYi4PopMzipVBwy3uu5wSKXJEc= github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= diff --git a/internal/ethereum/blocklistener.go b/internal/ethereum/blocklistener.go index 91251ef..383dc12 100644 --- a/internal/ethereum/blocklistener.go +++ b/internal/ethereum/blocklistener.go @@ -75,7 +75,7 @@ func newBlockListener(ctx context.Context, c *ethConnector, conf config.Section) func (bl *blockListener) establishBlockHeightWithRetry() error { return bl.c.retry.Do(bl.ctx, "get initial block height", func(attempt int) (retry bool, err error) { var hexBlockHeight ethtypes.HexInteger - err = bl.c.backend.Invoke(bl.ctx, &hexBlockHeight, "eth_blockNumber") + err = bl.c.backend.CallRPC(bl.ctx, &hexBlockHeight, "eth_blockNumber") if err != nil { log.L(bl.ctx).Warnf("Block height could not be obtained: %s", err) return true, err @@ -116,7 +116,7 @@ func (bl *blockListener) listenLoop() { } if filter == "" { - err := bl.c.backend.Invoke(bl.ctx, &filter, "eth_newBlockFilter") + err := bl.c.backend.CallRPC(bl.ctx, &filter, "eth_newBlockFilter") if err != nil { log.L(bl.ctx).Errorf("Failed to establish new block filter: %s", err) failCount++ @@ -125,7 +125,7 @@ func (bl *blockListener) listenLoop() { } var blockHashes []ethtypes.HexBytes0xPrefix - err := bl.c.backend.Invoke(bl.ctx, &blockHashes, "eth_getFilterChanges", filter) + err := bl.c.backend.CallRPC(bl.ctx, &blockHashes, "eth_getFilterChanges", filter) if err != nil { if mapError(filterRPCMethods, err) == ffcapi.ErrorReasonNotFound { log.L(bl.ctx).Warnf("Block filter '%v' no longer valid. Recreating filter: %s", filter, err) diff --git a/internal/ethereum/blocklistener_test.go b/internal/ethereum/blocklistener_test.go index 4ac91a1..0c2e508 100644 --- a/internal/ethereum/blocklistener_test.go +++ b/internal/ethereum/blocklistener_test.go @@ -34,9 +34,9 @@ func TestBlockListenerStartGettingHighestBlockRetry(t *testing.T) { _, c, mRPC, done := newTestConnector(t) bl := c.blockListener - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_blockNumber"). + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_blockNumber"). Return(fmt.Errorf("pop")).Once() - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_blockNumber").Return(nil).Run(func(args mock.Arguments) { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_blockNumber").Return(nil).Run(func(args mock.Arguments) { hbh := args[1].(*ethtypes.HexInteger) *hbh = *ethtypes.NewHexInteger64(12345) }) @@ -56,7 +56,7 @@ func TestBlockListenerStartGettingHighestBlockFailBeforeStop(t *testing.T) { done() // Stop before we start bl := c.blockListener - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_blockNumber"). + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_blockNumber"). Return(fmt.Errorf("pop")).Maybe() assert.Equal(t, int64(-1), bl.getHighestBlock(bl.ctx)) @@ -79,30 +79,30 @@ func TestBlockListenerOKSequential(t *testing.T) { block1002Hash := ethtypes.MustNewHexBytes0xPrefix(fftypes.NewRandB32().String()) block1003Hash := ethtypes.MustNewHexBytes0xPrefix(fftypes.NewRandB32().String()) - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_blockNumber").Return(nil).Run(func(args mock.Arguments) { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_blockNumber").Return(nil).Run(func(args mock.Arguments) { hbh := args[1].(*ethtypes.HexInteger) *hbh = *ethtypes.NewHexInteger64(1000) }).Once() - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_newBlockFilter").Return(nil).Run(func(args mock.Arguments) { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_newBlockFilter").Return(nil).Run(func(args mock.Arguments) { hbh := args[1].(*string) *hbh = "filter_id1" }) - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getFilterChanges", "filter_id1").Return(nil).Run(func(args mock.Arguments) { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getFilterChanges", "filter_id1").Return(nil).Run(func(args mock.Arguments) { hbh := args[1].(*[]ethtypes.HexBytes0xPrefix) *hbh = []ethtypes.HexBytes0xPrefix{ block1001Hash, block1002Hash, } }).Once() - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getFilterChanges", "filter_id1").Return(nil).Run(func(args mock.Arguments) { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getFilterChanges", "filter_id1").Return(nil).Run(func(args mock.Arguments) { hbh := args[1].(*[]ethtypes.HexBytes0xPrefix) *hbh = []ethtypes.HexBytes0xPrefix{ block1003Hash, } }).Once() - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getFilterChanges", mock.Anything).Return(nil) + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getFilterChanges", mock.Anything).Return(nil) - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getBlockByHash", mock.MatchedBy(func(bh string) bool { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getBlockByHash", mock.MatchedBy(func(bh string) bool { return bh == block1001Hash.String() }), false).Return(nil).Run(func(args mock.Arguments) { *args[1].(**blockInfoJSONRPC) = &blockInfoJSONRPC{ @@ -111,7 +111,7 @@ func TestBlockListenerOKSequential(t *testing.T) { ParentHash: block1000Hash, } }) - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getBlockByHash", mock.MatchedBy(func(bh string) bool { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getBlockByHash", mock.MatchedBy(func(bh string) bool { return bh == block1002Hash.String() }), false).Return(nil).Run(func(args mock.Arguments) { *args[1].(**blockInfoJSONRPC) = &blockInfoJSONRPC{ @@ -120,7 +120,7 @@ func TestBlockListenerOKSequential(t *testing.T) { ParentHash: block1001Hash, } }) - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getBlockByHash", mock.MatchedBy(func(bh string) bool { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getBlockByHash", mock.MatchedBy(func(bh string) bool { return bh == block1003Hash.String() }), false).Return(nil).Run(func(args mock.Arguments) { *args[1].(**blockInfoJSONRPC) = &blockInfoJSONRPC{ @@ -170,28 +170,28 @@ func TestBlockListenerOKDuplicates(t *testing.T) { block1002Hash := ethtypes.MustNewHexBytes0xPrefix(fftypes.NewRandB32().String()) block1003Hash := ethtypes.MustNewHexBytes0xPrefix(fftypes.NewRandB32().String()) - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_blockNumber").Return(nil).Run(func(args mock.Arguments) { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_blockNumber").Return(nil).Run(func(args mock.Arguments) { hbh := args[1].(*ethtypes.HexInteger) *hbh = *ethtypes.NewHexInteger64(1000) }).Once() - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_newBlockFilter").Return(nil).Run(func(args mock.Arguments) { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_newBlockFilter").Return(nil).Run(func(args mock.Arguments) { hbh := args[1].(*string) *hbh = "filter_id1" }) - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getFilterChanges", "filter_id1").Return(nil).Run(func(args mock.Arguments) { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getFilterChanges", "filter_id1").Return(nil).Run(func(args mock.Arguments) { hbh := args[1].(*[]ethtypes.HexBytes0xPrefix) *hbh = []ethtypes.HexBytes0xPrefix{ block1001Hash, block1002Hash, } }).Once() - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getFilterChanges", "filter_id1").Return(nil).Run(func(args mock.Arguments) { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getFilterChanges", "filter_id1").Return(nil).Run(func(args mock.Arguments) { hbh := args[1].(*[]ethtypes.HexBytes0xPrefix) *hbh = []ethtypes.HexBytes0xPrefix{ block1003Hash, } }).Once() - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getFilterChanges", "filter_id1").Return(nil).Run(func(args mock.Arguments) { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getFilterChanges", "filter_id1").Return(nil).Run(func(args mock.Arguments) { hbh := args[1].(*[]ethtypes.HexBytes0xPrefix) *hbh = []ethtypes.HexBytes0xPrefix{ block1002Hash, @@ -199,9 +199,9 @@ func TestBlockListenerOKDuplicates(t *testing.T) { } go done() // once we've detected these duplicates, we can close }).Once() - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getFilterChanges", mock.Anything).Return(nil) + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getFilterChanges", mock.Anything).Return(nil) - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getBlockByHash", mock.MatchedBy(func(bh string) bool { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getBlockByHash", mock.MatchedBy(func(bh string) bool { return bh == block1001Hash.String() }), false).Return(nil).Run(func(args mock.Arguments) { *args[1].(**blockInfoJSONRPC) = &blockInfoJSONRPC{ @@ -210,7 +210,7 @@ func TestBlockListenerOKDuplicates(t *testing.T) { ParentHash: block1000Hash, } }) - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getBlockByHash", mock.MatchedBy(func(bh string) bool { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getBlockByHash", mock.MatchedBy(func(bh string) bool { return bh == block1002Hash.String() }), false).Return(nil).Run(func(args mock.Arguments) { *args[1].(**blockInfoJSONRPC) = &blockInfoJSONRPC{ @@ -219,7 +219,7 @@ func TestBlockListenerOKDuplicates(t *testing.T) { ParentHash: block1001Hash, } }) - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getBlockByHash", mock.MatchedBy(func(bh string) bool { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getBlockByHash", mock.MatchedBy(func(bh string) bool { return bh == block1003Hash.String() }), false).Return(nil).Run(func(args mock.Arguments) { *args[1].(**blockInfoJSONRPC) = &blockInfoJSONRPC{ @@ -267,36 +267,36 @@ func TestBlockListenerReorgReplaceTail(t *testing.T) { block1003HashA := ethtypes.MustNewHexBytes0xPrefix(fftypes.NewRandB32().String()) block1003HashB := ethtypes.MustNewHexBytes0xPrefix(fftypes.NewRandB32().String()) - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_blockNumber").Return(nil).Run(func(args mock.Arguments) { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_blockNumber").Return(nil).Run(func(args mock.Arguments) { hbh := args[1].(*ethtypes.HexInteger) *hbh = *ethtypes.NewHexInteger64(1000) }).Once() - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_newBlockFilter").Return(nil).Run(func(args mock.Arguments) { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_newBlockFilter").Return(nil).Run(func(args mock.Arguments) { hbh := args[1].(*string) *hbh = "filter_id1" }) - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getFilterChanges", "filter_id1").Return(nil).Run(func(args mock.Arguments) { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getFilterChanges", "filter_id1").Return(nil).Run(func(args mock.Arguments) { hbh := args[1].(*[]ethtypes.HexBytes0xPrefix) *hbh = []ethtypes.HexBytes0xPrefix{ block1001Hash, block1002Hash, } }).Once() - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getFilterChanges", "filter_id1").Return(nil).Run(func(args mock.Arguments) { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getFilterChanges", "filter_id1").Return(nil).Run(func(args mock.Arguments) { hbh := args[1].(*[]ethtypes.HexBytes0xPrefix) *hbh = []ethtypes.HexBytes0xPrefix{ block1003HashA, } }).Once() - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getFilterChanges", "filter_id1").Return(nil).Run(func(args mock.Arguments) { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getFilterChanges", "filter_id1").Return(nil).Run(func(args mock.Arguments) { hbh := args[1].(*[]ethtypes.HexBytes0xPrefix) *hbh = []ethtypes.HexBytes0xPrefix{ block1003HashB, } }).Once() - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getFilterChanges", mock.Anything).Return(nil) + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getFilterChanges", mock.Anything).Return(nil) - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getBlockByHash", mock.MatchedBy(func(bh string) bool { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getBlockByHash", mock.MatchedBy(func(bh string) bool { return bh == block1001Hash.String() }), false).Return(nil).Run(func(args mock.Arguments) { *args[1].(**blockInfoJSONRPC) = &blockInfoJSONRPC{ @@ -305,7 +305,7 @@ func TestBlockListenerReorgReplaceTail(t *testing.T) { ParentHash: block1000Hash, } }) - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getBlockByHash", mock.MatchedBy(func(bh string) bool { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getBlockByHash", mock.MatchedBy(func(bh string) bool { return bh == block1002Hash.String() }), false).Return(nil).Run(func(args mock.Arguments) { *args[1].(**blockInfoJSONRPC) = &blockInfoJSONRPC{ @@ -314,7 +314,7 @@ func TestBlockListenerReorgReplaceTail(t *testing.T) { ParentHash: block1001Hash, } }) - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getBlockByHash", mock.MatchedBy(func(bh string) bool { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getBlockByHash", mock.MatchedBy(func(bh string) bool { return bh == block1003HashA.String() }), false).Return(nil).Run(func(args mock.Arguments) { *args[1].(**blockInfoJSONRPC) = &blockInfoJSONRPC{ @@ -323,7 +323,7 @@ func TestBlockListenerReorgReplaceTail(t *testing.T) { ParentHash: block1002Hash, } }) - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getBlockByHash", mock.MatchedBy(func(bh string) bool { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getBlockByHash", mock.MatchedBy(func(bh string) bool { return bh == block1003HashB.String() }), false).Return(nil).Run(func(args mock.Arguments) { *args[1].(**blockInfoJSONRPC) = &blockInfoJSONRPC{ @@ -384,30 +384,30 @@ func TestBlockListenerGap(t *testing.T) { block1004Hash := ethtypes.MustNewHexBytes0xPrefix(fftypes.NewRandB32().String()) block1005Hash := ethtypes.MustNewHexBytes0xPrefix(fftypes.NewRandB32().String()) - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_blockNumber").Return(nil).Run(func(args mock.Arguments) { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_blockNumber").Return(nil).Run(func(args mock.Arguments) { hbh := args[1].(*ethtypes.HexInteger) *hbh = *ethtypes.NewHexInteger64(1000) }).Once() - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_newBlockFilter").Return(nil).Run(func(args mock.Arguments) { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_newBlockFilter").Return(nil).Run(func(args mock.Arguments) { hbh := args[1].(*string) *hbh = "filter_id1" }) - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getFilterChanges", "filter_id1").Return(nil).Run(func(args mock.Arguments) { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getFilterChanges", "filter_id1").Return(nil).Run(func(args mock.Arguments) { hbh := args[1].(*[]ethtypes.HexBytes0xPrefix) *hbh = []ethtypes.HexBytes0xPrefix{ block1001Hash, block1002HashA, } }).Once() - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getFilterChanges", "filter_id1").Return(nil).Run(func(args mock.Arguments) { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getFilterChanges", "filter_id1").Return(nil).Run(func(args mock.Arguments) { hbh := args[1].(*[]ethtypes.HexBytes0xPrefix) *hbh = []ethtypes.HexBytes0xPrefix{ block1004Hash, } }).Once() - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getFilterChanges", mock.Anything).Return(nil) + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getFilterChanges", mock.Anything).Return(nil) - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getBlockByHash", mock.MatchedBy(func(bh string) bool { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getBlockByHash", mock.MatchedBy(func(bh string) bool { return bh == block1001Hash.String() }), false).Return(nil).Run(func(args mock.Arguments) { *args[1].(**blockInfoJSONRPC) = &blockInfoJSONRPC{ @@ -416,7 +416,7 @@ func TestBlockListenerGap(t *testing.T) { ParentHash: block1000Hash, } }) - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getBlockByHash", mock.MatchedBy(func(bh string) bool { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getBlockByHash", mock.MatchedBy(func(bh string) bool { return bh == block1002HashA.String() }), false).Return(nil).Run(func(args mock.Arguments) { *args[1].(**blockInfoJSONRPC) = &blockInfoJSONRPC{ @@ -425,7 +425,7 @@ func TestBlockListenerGap(t *testing.T) { ParentHash: block1001Hash, } }) - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getBlockByHash", mock.MatchedBy(func(bh string) bool { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getBlockByHash", mock.MatchedBy(func(bh string) bool { return bh == block1004Hash.String() }), false).Return(nil).Run(func(args mock.Arguments) { *args[1].(**blockInfoJSONRPC) = &blockInfoJSONRPC{ @@ -434,7 +434,7 @@ func TestBlockListenerGap(t *testing.T) { ParentHash: block1003Hash, } }) - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getBlockByNumber", mock.MatchedBy(func(bn *ethtypes.HexInteger) bool { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getBlockByNumber", mock.MatchedBy(func(bn *ethtypes.HexInteger) bool { return bn.BigInt().Int64() == 1001 }), false).Return(nil).Run(func(args mock.Arguments) { *args[1].(**blockInfoJSONRPC) = &blockInfoJSONRPC{ @@ -443,7 +443,7 @@ func TestBlockListenerGap(t *testing.T) { ParentHash: block1000Hash, } }) - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getBlockByNumber", mock.MatchedBy(func(bn *ethtypes.HexInteger) bool { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getBlockByNumber", mock.MatchedBy(func(bn *ethtypes.HexInteger) bool { return bn.BigInt().Int64() == 1002 }), false).Return(nil).Run(func(args mock.Arguments) { *args[1].(**blockInfoJSONRPC) = &blockInfoJSONRPC{ @@ -452,7 +452,7 @@ func TestBlockListenerGap(t *testing.T) { ParentHash: block1001Hash, } }) - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getBlockByNumber", mock.MatchedBy(func(bn *ethtypes.HexInteger) bool { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getBlockByNumber", mock.MatchedBy(func(bn *ethtypes.HexInteger) bool { return bn.BigInt().Int64() == 1003 }), false).Return(nil).Run(func(args mock.Arguments) { *args[1].(**blockInfoJSONRPC) = &blockInfoJSONRPC{ @@ -461,7 +461,7 @@ func TestBlockListenerGap(t *testing.T) { ParentHash: block1002HashB, } }) - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getBlockByNumber", mock.MatchedBy(func(bn *ethtypes.HexInteger) bool { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getBlockByNumber", mock.MatchedBy(func(bn *ethtypes.HexInteger) bool { return bn.BigInt().Int64() == 1004 }), false).Return(nil).Run(func(args mock.Arguments) { *args[1].(**blockInfoJSONRPC) = &blockInfoJSONRPC{ @@ -470,7 +470,7 @@ func TestBlockListenerGap(t *testing.T) { ParentHash: block1003Hash, } }) - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getBlockByNumber", mock.MatchedBy(func(bn *ethtypes.HexInteger) bool { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getBlockByNumber", mock.MatchedBy(func(bn *ethtypes.HexInteger) bool { return bn.BigInt().Int64() == 1005 }), false).Return(nil).Run(func(args mock.Arguments) { *args[1].(**blockInfoJSONRPC) = &blockInfoJSONRPC{ @@ -479,7 +479,7 @@ func TestBlockListenerGap(t *testing.T) { ParentHash: block1004Hash, } }) - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getBlockByNumber", mock.MatchedBy(func(bn *ethtypes.HexInteger) bool { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getBlockByNumber", mock.MatchedBy(func(bn *ethtypes.HexInteger) bool { return bn.BigInt().Int64() == 1006 // not found }), false).Return(nil) @@ -526,29 +526,29 @@ func TestBlockListenerReorgWhileRebuilding(t *testing.T) { block1003HashA := ethtypes.MustNewHexBytes0xPrefix(fftypes.NewRandB32().String()) block1003HashB := ethtypes.MustNewHexBytes0xPrefix(fftypes.NewRandB32().String()) - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_blockNumber").Return(nil).Run(func(args mock.Arguments) { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_blockNumber").Return(nil).Run(func(args mock.Arguments) { hbh := args[1].(*ethtypes.HexInteger) *hbh = *ethtypes.NewHexInteger64(1000) }).Once() - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_newBlockFilter").Return(nil).Run(func(args mock.Arguments) { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_newBlockFilter").Return(nil).Run(func(args mock.Arguments) { hbh := args[1].(*string) *hbh = "filter_id1" }) - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getFilterChanges", "filter_id1").Return(nil).Run(func(args mock.Arguments) { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getFilterChanges", "filter_id1").Return(nil).Run(func(args mock.Arguments) { hbh := args[1].(*[]ethtypes.HexBytes0xPrefix) *hbh = []ethtypes.HexBytes0xPrefix{ block1001Hash, } }).Once() - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getFilterChanges", "filter_id1").Return(nil).Run(func(args mock.Arguments) { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getFilterChanges", "filter_id1").Return(nil).Run(func(args mock.Arguments) { hbh := args[1].(*[]ethtypes.HexBytes0xPrefix) *hbh = []ethtypes.HexBytes0xPrefix{ block1003HashA, } }).Once() - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getFilterChanges", mock.Anything).Return(nil) + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getFilterChanges", mock.Anything).Return(nil) - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getBlockByHash", mock.MatchedBy(func(bh string) bool { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getBlockByHash", mock.MatchedBy(func(bh string) bool { return bh == block1001Hash.String() }), false).Return(nil).Run(func(args mock.Arguments) { *args[1].(**blockInfoJSONRPC) = &blockInfoJSONRPC{ @@ -557,7 +557,7 @@ func TestBlockListenerReorgWhileRebuilding(t *testing.T) { ParentHash: block1000Hash, } }) - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getBlockByHash", mock.MatchedBy(func(bh string) bool { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getBlockByHash", mock.MatchedBy(func(bh string) bool { return bh == block1003HashA.String() }), false).Return(nil).Run(func(args mock.Arguments) { *args[1].(**blockInfoJSONRPC) = &blockInfoJSONRPC{ @@ -566,7 +566,7 @@ func TestBlockListenerReorgWhileRebuilding(t *testing.T) { ParentHash: block1001Hash, } }) - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getBlockByNumber", mock.MatchedBy(func(bn *ethtypes.HexInteger) bool { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getBlockByNumber", mock.MatchedBy(func(bn *ethtypes.HexInteger) bool { return bn.BigInt().Int64() == 1001 }), false).Return(nil).Run(func(args mock.Arguments) { *args[1].(**blockInfoJSONRPC) = &blockInfoJSONRPC{ @@ -575,7 +575,7 @@ func TestBlockListenerReorgWhileRebuilding(t *testing.T) { ParentHash: block1000Hash, } }) - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getBlockByNumber", mock.MatchedBy(func(bn *ethtypes.HexInteger) bool { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getBlockByNumber", mock.MatchedBy(func(bn *ethtypes.HexInteger) bool { return bn.BigInt().Int64() == 1002 }), false).Return(nil).Run(func(args mock.Arguments) { *args[1].(**blockInfoJSONRPC) = &blockInfoJSONRPC{ @@ -584,7 +584,7 @@ func TestBlockListenerReorgWhileRebuilding(t *testing.T) { ParentHash: block1001Hash, } }) - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getBlockByNumber", mock.MatchedBy(func(bn *ethtypes.HexInteger) bool { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getBlockByNumber", mock.MatchedBy(func(bn *ethtypes.HexInteger) bool { return bn.BigInt().Int64() == 1003 }), false).Return(nil).Run(func(args mock.Arguments) { *args[1].(**blockInfoJSONRPC) = &blockInfoJSONRPC{ @@ -631,30 +631,30 @@ func TestBlockListenerReorgReplaceWholeCanonicalChain(t *testing.T) { block1002HashB := ethtypes.MustNewHexBytes0xPrefix(fftypes.NewRandB32().String()) block1003HashB := ethtypes.MustNewHexBytes0xPrefix(fftypes.NewRandB32().String()) - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_blockNumber").Return(nil).Run(func(args mock.Arguments) { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_blockNumber").Return(nil).Run(func(args mock.Arguments) { hbh := args[1].(*ethtypes.HexInteger) *hbh = *ethtypes.NewHexInteger64(1000) }).Once() - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_newBlockFilter").Return(nil).Run(func(args mock.Arguments) { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_newBlockFilter").Return(nil).Run(func(args mock.Arguments) { hbh := args[1].(*string) *hbh = "filter_id1" }) - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getFilterChanges", "filter_id1").Return(nil).Run(func(args mock.Arguments) { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getFilterChanges", "filter_id1").Return(nil).Run(func(args mock.Arguments) { hbh := args[1].(*[]ethtypes.HexBytes0xPrefix) *hbh = []ethtypes.HexBytes0xPrefix{ block1002HashA, block1003HashA, } }).Once() - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getFilterChanges", "filter_id1").Return(nil).Run(func(args mock.Arguments) { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getFilterChanges", "filter_id1").Return(nil).Run(func(args mock.Arguments) { hbh := args[1].(*[]ethtypes.HexBytes0xPrefix) *hbh = []ethtypes.HexBytes0xPrefix{ block1003HashB, } }).Once() - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getFilterChanges", mock.Anything).Return(nil) + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getFilterChanges", mock.Anything).Return(nil) - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getBlockByHash", mock.MatchedBy(func(bh string) bool { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getBlockByHash", mock.MatchedBy(func(bh string) bool { return bh == block1002HashA.String() }), false).Return(nil).Run(func(args mock.Arguments) { *args[1].(**blockInfoJSONRPC) = &blockInfoJSONRPC{ @@ -663,7 +663,7 @@ func TestBlockListenerReorgReplaceWholeCanonicalChain(t *testing.T) { ParentHash: block1001Hash, } }) - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getBlockByHash", mock.MatchedBy(func(bh string) bool { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getBlockByHash", mock.MatchedBy(func(bh string) bool { return bh == block1003HashA.String() }), false).Return(nil).Run(func(args mock.Arguments) { *args[1].(**blockInfoJSONRPC) = &blockInfoJSONRPC{ @@ -672,7 +672,7 @@ func TestBlockListenerReorgReplaceWholeCanonicalChain(t *testing.T) { ParentHash: block1002HashA, } }) - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getBlockByHash", mock.MatchedBy(func(bh string) bool { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getBlockByHash", mock.MatchedBy(func(bh string) bool { return bh == block1003HashB.String() }), false).Return(nil).Run(func(args mock.Arguments) { *args[1].(**blockInfoJSONRPC) = &blockInfoJSONRPC{ @@ -681,7 +681,7 @@ func TestBlockListenerReorgReplaceWholeCanonicalChain(t *testing.T) { ParentHash: block1002HashB, } }) - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getBlockByNumber", mock.MatchedBy(func(bn *ethtypes.HexInteger) bool { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getBlockByNumber", mock.MatchedBy(func(bn *ethtypes.HexInteger) bool { return bn.BigInt().Int64() == 1002 }), false).Return(nil).Run(func(args mock.Arguments) { *args[1].(**blockInfoJSONRPC) = &blockInfoJSONRPC{ @@ -690,7 +690,7 @@ func TestBlockListenerReorgReplaceWholeCanonicalChain(t *testing.T) { ParentHash: block1001Hash, } }) - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getBlockByNumber", mock.MatchedBy(func(bn *ethtypes.HexInteger) bool { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getBlockByNumber", mock.MatchedBy(func(bn *ethtypes.HexInteger) bool { return bn.BigInt().Int64() == 1003 }), false).Return(nil).Run(func(args mock.Arguments) { *args[1].(**blockInfoJSONRPC) = &blockInfoJSONRPC{ @@ -699,7 +699,7 @@ func TestBlockListenerReorgReplaceWholeCanonicalChain(t *testing.T) { ParentHash: block1002HashB, } }) - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getBlockByNumber", mock.MatchedBy(func(bn *ethtypes.HexInteger) bool { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getBlockByNumber", mock.MatchedBy(func(bn *ethtypes.HexInteger) bool { return bn.BigInt().Int64() == 1004 // not found }), false).Return(nil) @@ -739,27 +739,27 @@ func TestBlockListenerClosed(t *testing.T) { block1002Hash := ethtypes.MustNewHexBytes0xPrefix(fftypes.NewRandB32().String()) block1003Hash := ethtypes.MustNewHexBytes0xPrefix(fftypes.NewRandB32().String()) - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_blockNumber").Return(nil).Run(func(args mock.Arguments) { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_blockNumber").Return(nil).Run(func(args mock.Arguments) { hbh := args[1].(*ethtypes.HexInteger) *hbh = *ethtypes.NewHexInteger64(1000) }).Once() - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_newBlockFilter").Return(nil).Run(func(args mock.Arguments) { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_newBlockFilter").Return(nil).Run(func(args mock.Arguments) { hbh := args[1].(*string) *hbh = "filter_id1" }) - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getFilterChanges", "filter_id1").Return(nil).Run(func(args mock.Arguments) { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getFilterChanges", "filter_id1").Return(nil).Run(func(args mock.Arguments) { hbh := args[1].(*[]ethtypes.HexBytes0xPrefix) *hbh = []ethtypes.HexBytes0xPrefix{ block1003Hash, } }).Once() - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getFilterChanges", mock.Anything).Return(nil).Run(func(args mock.Arguments) { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getFilterChanges", mock.Anything).Return(nil).Run(func(args mock.Arguments) { if len(bl.consumers) == 0 { go done() // Close after we've processed the log } }) - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getBlockByHash", mock.MatchedBy(func(bh string) bool { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getBlockByHash", mock.MatchedBy(func(bh string) bool { return bh == block1003Hash.String() }), false).Return(nil).Run(func(args mock.Arguments) { *args[1].(**blockInfoJSONRPC) = &blockInfoJSONRPC{ @@ -790,25 +790,25 @@ func TestBlockListenerBlockNotFound(t *testing.T) { bl.blockPollingInterval = 1 * time.Microsecond block1003Hash := ethtypes.MustNewHexBytes0xPrefix(fftypes.NewRandB32().String()) - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_blockNumber").Return(nil).Run(func(args mock.Arguments) { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_blockNumber").Return(nil).Run(func(args mock.Arguments) { hbh := args[1].(*ethtypes.HexInteger) *hbh = *ethtypes.NewHexInteger64(1000) }).Once() - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_newBlockFilter").Return(nil).Run(func(args mock.Arguments) { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_newBlockFilter").Return(nil).Run(func(args mock.Arguments) { hbh := args[1].(*string) *hbh = "filter_id1" }) - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getFilterChanges", "filter_id1").Return(nil).Run(func(args mock.Arguments) { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getFilterChanges", "filter_id1").Return(nil).Run(func(args mock.Arguments) { hbh := args[1].(*[]ethtypes.HexBytes0xPrefix) *hbh = []ethtypes.HexBytes0xPrefix{ block1003Hash, } }).Once() - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getFilterChanges", mock.Anything).Return(nil).Run(func(args mock.Arguments) { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getFilterChanges", mock.Anything).Return(nil).Run(func(args mock.Arguments) { go done() // Close after we've processed the log }) - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getBlockByHash", mock.MatchedBy(func(bh string) bool { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getBlockByHash", mock.MatchedBy(func(bh string) bool { return bh == block1003Hash.String() }), false).Return(nil) @@ -827,25 +827,25 @@ func TestBlockListenerBlockHashFailed(t *testing.T) { bl.blockPollingInterval = 1 * time.Microsecond block1003Hash := ethtypes.MustNewHexBytes0xPrefix(fftypes.NewRandB32().String()) - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_blockNumber").Return(nil).Run(func(args mock.Arguments) { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_blockNumber").Return(nil).Run(func(args mock.Arguments) { hbh := args[1].(*ethtypes.HexInteger) *hbh = *ethtypes.NewHexInteger64(1000) }).Once() - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_newBlockFilter").Return(nil).Run(func(args mock.Arguments) { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_newBlockFilter").Return(nil).Run(func(args mock.Arguments) { hbh := args[1].(*string) *hbh = "filter_id1" }) - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getFilterChanges", "filter_id1").Return(nil).Run(func(args mock.Arguments) { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getFilterChanges", "filter_id1").Return(nil).Run(func(args mock.Arguments) { hbh := args[1].(*[]ethtypes.HexBytes0xPrefix) *hbh = []ethtypes.HexBytes0xPrefix{ block1003Hash, } }).Once() - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getFilterChanges", mock.Anything).Return(nil).Run(func(args mock.Arguments) { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getFilterChanges", mock.Anything).Return(nil).Run(func(args mock.Arguments) { go done() // Close after we've processed the log }) - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getBlockByHash", mock.MatchedBy(func(bh string) bool { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getBlockByHash", mock.MatchedBy(func(bh string) bool { return bh == block1003Hash.String() }), false).Return(fmt.Errorf("pop")) @@ -863,20 +863,20 @@ func TestBlockListenerReestablishBlockFilter(t *testing.T) { bl := c.blockListener bl.blockPollingInterval = 1 * time.Microsecond - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_blockNumber").Return(nil).Run(func(args mock.Arguments) { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_blockNumber").Return(nil).Run(func(args mock.Arguments) { hbh := args[1].(*ethtypes.HexInteger) *hbh = *ethtypes.NewHexInteger64(1000) }).Once() - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_newBlockFilter").Return(nil).Run(func(args mock.Arguments) { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_newBlockFilter").Return(nil).Run(func(args mock.Arguments) { hbh := args[1].(*string) *hbh = "filter_id1" }).Once() - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_newBlockFilter").Return(nil).Run(func(args mock.Arguments) { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_newBlockFilter").Return(nil).Run(func(args mock.Arguments) { hbh := args[1].(*string) *hbh = "filter_id2" }).Once() - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getFilterChanges", "filter_id1").Return(fmt.Errorf("filter not found")).Once() - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getFilterChanges", mock.Anything).Return(nil).Run(func(args mock.Arguments) { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getFilterChanges", "filter_id1").Return(fmt.Errorf("filter not found")).Once() + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getFilterChanges", mock.Anything).Return(nil).Run(func(args mock.Arguments) { go done() // Close after we've processed the log }) @@ -894,11 +894,11 @@ func TestBlockListenerReestablishBlockFilterFail(t *testing.T) { bl := c.blockListener bl.blockPollingInterval = 1 * time.Microsecond - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_blockNumber").Return(nil).Run(func(args mock.Arguments) { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_blockNumber").Return(nil).Run(func(args mock.Arguments) { hbh := args[1].(*ethtypes.HexInteger) *hbh = *ethtypes.NewHexInteger64(1000) }).Once() - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_newBlockFilter").Return(fmt.Errorf("pop")).Run(func(args mock.Arguments) { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_newBlockFilter").Return(fmt.Errorf("pop")).Run(func(args mock.Arguments) { go done() }) @@ -942,7 +942,7 @@ func TestBlockListenerRebuildCanonicalFailTerminate(t *testing.T) { parentHash: ethtypes.MustNewHexBytes0xPrefix(fftypes.NewRandB32().String()).String(), }) - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getBlockByNumber", mock.Anything, false). + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getBlockByNumber", mock.Anything, false). Return(fmt.Errorf("pop")). Run(func(args mock.Arguments) { done() diff --git a/internal/ethereum/deploy_contract_prepare_test.go b/internal/ethereum/deploy_contract_prepare_test.go index 4fa593a..c310ca5 100644 --- a/internal/ethereum/deploy_contract_prepare_test.go +++ b/internal/ethereum/deploy_contract_prepare_test.go @@ -76,8 +76,8 @@ func TestDeployContractPrepareWithEstimateRevert(t *testing.T) { ctx, c, mRPC, done := newTestConnector(t) defer done() - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_estimateGas", mock.Anything).Return(fmt.Errorf("pop")) - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_call", mock.Anything, "latest").Run( + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_estimateGas", mock.Anything).Return(fmt.Errorf("pop")) + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_call", mock.Anything, "latest").Run( func(args mock.Arguments) { *(args[1].(*ethtypes.HexBytes0xPrefix)) = ethtypes.MustNewHexBytes0xPrefix("0x08c379a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000114d75707065747279206465746563746564000000000000000000000000000000") }, @@ -149,7 +149,7 @@ func TestDeployContractPrepareEstimateNoConstructor(t *testing.T) { ctx, c, mRPC, done := newTestConnector(t) defer done() - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_estimateGas", mock.Anything).Return(nil).Run(func(args mock.Arguments) { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_estimateGas", mock.Anything).Return(nil).Run(func(args mock.Arguments) { *(args[1].(*ethtypes.HexInteger)) = *ethtypes.NewHexInteger64(12345) }) diff --git a/internal/ethereum/estimate_gas.go b/internal/ethereum/estimate_gas.go index d20cbf0..ff9b1d3 100644 --- a/internal/ethereum/estimate_gas.go +++ b/internal/ethereum/estimate_gas.go @@ -31,7 +31,7 @@ func (c *ethConnector) estimateGas(ctx context.Context, tx *ethsigner.Transactio // Do the gas estimation var gasEstimate ethtypes.HexInteger - err := c.backend.Invoke(ctx, &gasEstimate, "eth_estimateGas", tx) + err := c.backend.CallRPC(ctx, &gasEstimate, "eth_estimateGas", tx) if err != nil { // If it fails, fall back to an eth_call to see if we get a reverted reason _, reason, errCall := c.callTransaction(ctx, tx, method) diff --git a/internal/ethereum/ethereum.go b/internal/ethereum/ethereum.go index fcf32f8..913444b 100644 --- a/internal/ethereum/ethereum.go +++ b/internal/ethereum/ethereum.go @@ -30,14 +30,14 @@ import ( "github.com/hyperledger/firefly-common/pkg/i18n" "github.com/hyperledger/firefly-common/pkg/log" "github.com/hyperledger/firefly-common/pkg/retry" - "github.com/hyperledger/firefly-evmconnect/internal/jsonrpc" "github.com/hyperledger/firefly-evmconnect/internal/msgs" "github.com/hyperledger/firefly-signer/pkg/abi" + "github.com/hyperledger/firefly-signer/pkg/rpcbackend" "github.com/hyperledger/firefly-transaction-manager/pkg/ffcapi" ) type ethConnector struct { - backend jsonrpc.Client + backend rpcbackend.Backend serializer *abi.Serializer gasEstimationFactor *big.Float catchupPageSize int64 @@ -81,7 +81,7 @@ func NewEthereumConnector(ctx context.Context, conf config.Section) (cc ffcapi.A } c.gasEstimationFactor = big.NewFloat(conf.GetFloat64(ConfigGasEstimationFactor)) - c.backend = jsonrpc.NewRPCClient(ffresty.New(ctx, conf)) + c.backend = rpcbackend.NewRPCClient(ffresty.New(ctx, conf)) c.serializer = abi.NewSerializer().SetByteSerializer(abi.HexByteSerializer0xPrefix) switch conf.Get(ConfigDataFormat) { diff --git a/internal/ethereum/ethereum_test.go b/internal/ethereum/ethereum_test.go index b68b64a..2ec2698 100644 --- a/internal/ethereum/ethereum_test.go +++ b/internal/ethereum/ethereum_test.go @@ -22,7 +22,7 @@ import ( "github.com/hyperledger/firefly-common/pkg/config" "github.com/hyperledger/firefly-common/pkg/ffresty" - "github.com/hyperledger/firefly-evmconnect/mocks/jsonrpcmocks" + "github.com/hyperledger/firefly-evmconnect/mocks/rpcbackendmocks" "github.com/hyperledger/firefly-signer/pkg/abi" "github.com/sirupsen/logrus" "github.com/stretchr/testify/assert" @@ -30,9 +30,9 @@ import ( func strPtr(s string) *string { return &s } -func newTestConnector(t *testing.T) (context.Context, *ethConnector, *jsonrpcmocks.Client, func()) { +func newTestConnector(t *testing.T) (context.Context, *ethConnector, *rpcbackendmocks.Backend, func()) { - mRPC := &jsonrpcmocks.Client{} + mRPC := &rpcbackendmocks.Backend{} config.RootConfigReset() conf := config.RootSection("unittest") InitConfig(conf) diff --git a/internal/ethereum/event_actions_test.go b/internal/ethereum/event_actions_test.go index 7b7b607..6b0ae01 100644 --- a/internal/ethereum/event_actions_test.go +++ b/internal/ethereum/event_actions_test.go @@ -21,7 +21,7 @@ import ( "testing" "github.com/hyperledger/firefly-common/pkg/fftypes" - "github.com/hyperledger/firefly-evmconnect/mocks/jsonrpcmocks" + "github.com/hyperledger/firefly-evmconnect/mocks/rpcbackendmocks" "github.com/hyperledger/firefly-signer/pkg/ethtypes" "github.com/hyperledger/firefly-transaction-manager/pkg/ffcapi" "github.com/stretchr/testify/assert" @@ -77,21 +77,21 @@ const abiTransferFn = `{ const testHighBlock = 212121 -func mockStreamLoopEmpty(mRPC *jsonrpcmocks.Client) { - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_blockNumber").Return(nil).Run(func(args mock.Arguments) { +func mockStreamLoopEmpty(mRPC *rpcbackendmocks.Backend) { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_blockNumber").Return(nil).Run(func(args mock.Arguments) { hbh := args[1].(*ethtypes.HexInteger) *hbh = *ethtypes.NewHexInteger64(testHighBlock) }).Maybe() - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_newFilter", mock.Anything).Return(nil).Run(func(args mock.Arguments) { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_newFilter", mock.Anything).Return(nil).Run(func(args mock.Arguments) { *args[1].(*string) = "filter_id1" }).Maybe() - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getFilterLogs", mock.Anything).Return(nil).Run(func(args mock.Arguments) { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getFilterLogs", mock.Anything).Return(nil).Run(func(args mock.Arguments) { *args[1].(*[]*logJSONRPC) = make([]*logJSONRPC, 0) }).Maybe() - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getFilterChanges", mock.Anything).Return(nil).Run(func(args mock.Arguments) { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getFilterChanges", mock.Anything).Return(nil).Run(func(args mock.Arguments) { *args[1].(*[]*logJSONRPC) = make([]*logJSONRPC, 0) }).Maybe() - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_uninstallFilter", mock.Anything).Return(nil).Run(func(args mock.Arguments) { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_uninstallFilter", mock.Anything).Return(nil).Run(func(args mock.Arguments) { *args[1].(*bool) = true }).Maybe() } diff --git a/internal/ethereum/event_listener_test.go b/internal/ethereum/event_listener_test.go index 1429a15..c514b89 100644 --- a/internal/ethereum/event_listener_test.go +++ b/internal/ethereum/event_listener_test.go @@ -24,7 +24,7 @@ import ( "time" "github.com/hyperledger/firefly-common/pkg/fftypes" - "github.com/hyperledger/firefly-evmconnect/mocks/jsonrpcmocks" + "github.com/hyperledger/firefly-evmconnect/mocks/rpcbackendmocks" "github.com/hyperledger/firefly-signer/pkg/abi" "github.com/hyperledger/firefly-signer/pkg/ethtypes" "github.com/hyperledger/firefly-transaction-manager/pkg/ffcapi" @@ -84,7 +84,7 @@ func sampleTransferLog() *logJSONRPC { } } -func newTestListener(t *testing.T, withMethods bool) (*listener, *jsonrpcmocks.Client, func()) { +func newTestListener(t *testing.T, withMethods bool) (*listener, *rpcbackendmocks.Backend, func()) { lID := fftypes.NewUUID() options := ffcapi.EventListenerOptions{ Filters: []fftypes.JSONAny{ @@ -127,7 +127,7 @@ func TestGetInitialBlockTimeout(t *testing.T) { } blockRPC := make(chan struct{}) - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_blockNumber").Return(fmt.Errorf("pop")).Run(func(args mock.Arguments) { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_blockNumber").Return(fmt.Errorf("pop")).Run(func(args mock.Arguments) { <-blockRPC // make it timeout }) @@ -150,7 +150,7 @@ func TestGetHWMNotInit(t *testing.T) { } blockRPC := make(chan struct{}) - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_blockNumber").Return(fmt.Errorf("pop")).Run(func(args mock.Arguments) { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_blockNumber").Return(fmt.Errorf("pop")).Run(func(args mock.Arguments) { <-blockRPC // make it timeout }) @@ -168,15 +168,15 @@ func TestListenerCatchupErrorsThenDeliveryExit(t *testing.T) { l.catchupLoopDone = make(chan struct{}) l.hwmBlock = 0 - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getBlockByHash", mock.MatchedBy(func(bh string) bool { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getBlockByHash", mock.MatchedBy(func(bh string) bool { return bh == "0x6b012339fbb85b70c58ecfd97b31950c4a28bcef5226e12dbe551cb1abaf3b4c" }), false).Return(nil).Run(func(args mock.Arguments) { *args[1].(**blockInfoJSONRPC) = &blockInfoJSONRPC{ Number: ethtypes.NewHexInteger64(1001), } }) - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getLogs", mock.Anything).Return(fmt.Errorf("pop")).Once() - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getLogs", mock.Anything).Return(nil).Run(func(args mock.Arguments) { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getLogs", mock.Anything).Return(fmt.Errorf("pop")).Once() + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getLogs", mock.Anything).Return(nil).Run(func(args mock.Arguments) { *args[1].(*[]*logJSONRPC) = []*logJSONRPC{sampleTransferLog()} // Cancel the context here so we exit pushing the event cancelCtx() @@ -193,7 +193,7 @@ func TestListenerCatchupErrorThenExit(t *testing.T) { l.catchupLoopDone = make(chan struct{}) l.hwmBlock = 0 - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getLogs", mock.Anything).Return(fmt.Errorf("pop")).Run(func(args mock.Arguments) { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getLogs", mock.Anything).Return(fmt.Errorf("pop")).Run(func(args mock.Arguments) { cancelCtx() }) @@ -278,14 +278,14 @@ func TestFilterEnrichEthLogMethodInputsOk(t *testing.T) { err := json.Unmarshal([]byte(abiTransferEvent), &abiEvent) assert.NoError(t, err) - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getBlockByHash", mock.MatchedBy(func(bh string) bool { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getBlockByHash", mock.MatchedBy(func(bh string) bool { return bh == "0x6b012339fbb85b70c58ecfd97b31950c4a28bcef5226e12dbe551cb1abaf3b4c" }), false).Return(nil).Run(func(args mock.Arguments) { *args[1].(**blockInfoJSONRPC) = &blockInfoJSONRPC{ Number: ethtypes.NewHexInteger64(1024), } }) - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getTransactionByHash", mock.MatchedBy(func(th ethtypes.HexBytes0xPrefix) bool { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getTransactionByHash", mock.MatchedBy(func(th ethtypes.HexBytes0xPrefix) bool { return th.String() == "0x1a1f797ee000c529b6a2dd330cedd0d081417a30d16a4eecb3f863ab4657246f" })).Return(nil).Run(func(args mock.Arguments) { *args[1].(**txInfoJSONRPC) = &txInfoJSONRPC{ @@ -312,10 +312,10 @@ func TestFilterEnrichEthLogTXInfoFail(t *testing.T) { err := json.Unmarshal([]byte(abiTransferEvent), &abiEvent) assert.NoError(t, err) - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getBlockByHash", mock.MatchedBy(func(bh string) bool { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getBlockByHash", mock.MatchedBy(func(bh string) bool { return bh == "0x6b012339fbb85b70c58ecfd97b31950c4a28bcef5226e12dbe551cb1abaf3b4c" }), false).Return(fmt.Errorf("pop1")) - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getTransactionByHash", mock.MatchedBy(func(th ethtypes.HexBytes0xPrefix) bool { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getTransactionByHash", mock.MatchedBy(func(th ethtypes.HexBytes0xPrefix) bool { return th.String() == "0x1a1f797ee000c529b6a2dd330cedd0d081417a30d16a4eecb3f863ab4657246f" })).Return(fmt.Errorf("pop2")) @@ -334,14 +334,14 @@ func TestFilterEnrichEthLogMethodBadInputTooShort(t *testing.T) { err := json.Unmarshal([]byte(abiTransferEvent), &abiEvent) assert.NoError(t, err) - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getBlockByHash", mock.MatchedBy(func(bh string) bool { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getBlockByHash", mock.MatchedBy(func(bh string) bool { return bh == "0x6b012339fbb85b70c58ecfd97b31950c4a28bcef5226e12dbe551cb1abaf3b4c" }), false).Return(nil).Run(func(args mock.Arguments) { *args[1].(**blockInfoJSONRPC) = &blockInfoJSONRPC{ Number: ethtypes.NewHexInteger64(1024), } }) - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getTransactionByHash", mock.MatchedBy(func(th ethtypes.HexBytes0xPrefix) bool { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getTransactionByHash", mock.MatchedBy(func(th ethtypes.HexBytes0xPrefix) bool { return th.String() == "0x1a1f797ee000c529b6a2dd330cedd0d081417a30d16a4eecb3f863ab4657246f" })).Return(nil).Run(func(args mock.Arguments) { *args[1].(**txInfoJSONRPC) = &txInfoJSONRPC{ @@ -365,14 +365,14 @@ func TestFilterEnrichEthLogMethodBadInputTooMismatchFunctionID(t *testing.T) { err := json.Unmarshal([]byte(abiTransferEvent), &abiEvent) assert.NoError(t, err) - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getBlockByHash", mock.MatchedBy(func(bh string) bool { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getBlockByHash", mock.MatchedBy(func(bh string) bool { return bh == "0x6b012339fbb85b70c58ecfd97b31950c4a28bcef5226e12dbe551cb1abaf3b4c" }), false).Return(nil).Run(func(args mock.Arguments) { *args[1].(**blockInfoJSONRPC) = &blockInfoJSONRPC{ Number: ethtypes.NewHexInteger64(1024), } }) - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getTransactionByHash", mock.MatchedBy(func(th ethtypes.HexBytes0xPrefix) bool { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getTransactionByHash", mock.MatchedBy(func(th ethtypes.HexBytes0xPrefix) bool { return th.String() == "0x1a1f797ee000c529b6a2dd330cedd0d081417a30d16a4eecb3f863ab4657246f" })).Return(nil).Run(func(args mock.Arguments) { *args[1].(**txInfoJSONRPC) = &txInfoJSONRPC{ @@ -396,14 +396,14 @@ func TestFilterEnrichEthLogMethodBadInputABIData(t *testing.T) { err := json.Unmarshal([]byte(abiTransferEvent), &abiEvent) assert.NoError(t, err) - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getBlockByHash", mock.MatchedBy(func(bh string) bool { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getBlockByHash", mock.MatchedBy(func(bh string) bool { return bh == "0x6b012339fbb85b70c58ecfd97b31950c4a28bcef5226e12dbe551cb1abaf3b4c" }), false).Return(nil).Run(func(args mock.Arguments) { *args[1].(**blockInfoJSONRPC) = &blockInfoJSONRPC{ Number: ethtypes.NewHexInteger64(1024), } }) - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getTransactionByHash", mock.MatchedBy(func(th ethtypes.HexBytes0xPrefix) bool { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getTransactionByHash", mock.MatchedBy(func(th ethtypes.HexBytes0xPrefix) bool { return th.String() == "0x1a1f797ee000c529b6a2dd330cedd0d081417a30d16a4eecb3f863ab4657246f" })).Return(nil).Run(func(args mock.Arguments) { *args[1].(**txInfoJSONRPC) = &txInfoJSONRPC{ diff --git a/internal/ethereum/event_stream.go b/internal/ethereum/event_stream.go index 06286cd..ab0becb 100644 --- a/internal/ethereum/event_stream.go +++ b/internal/ethereum/event_stream.go @@ -279,7 +279,7 @@ func (es *eventStream) leadGroupSteadyState() bool { var filter string uninstallFilter := func() { var res bool - if err := es.c.backend.Invoke(es.ctx, &res, "eth_uninstallFilter", filter); err != nil { + if err := es.c.backend.CallRPC(es.ctx, &res, "eth_uninstallFilter", filter); err != nil { log.L(es.ctx).Warnf("Error uninstalling filter '%v': %s", filter, err) } filter = "" @@ -334,7 +334,7 @@ func (es *eventStream) leadGroupSteadyState() bool { } // Create the new filter - err := es.c.backend.Invoke(es.ctx, &filter, "eth_newFilter", &logFilterJSONRPC{ + err := es.c.backend.CallRPC(es.ctx, &filter, "eth_newFilter", &logFilterJSONRPC{ FromBlock: ethtypes.NewHexInteger64(fromBlock), Topics: [][]ethtypes.HexBytes0xPrefix{ ag.signatureSet, @@ -350,7 +350,7 @@ func (es *eventStream) leadGroupSteadyState() bool { } // Get the next batch of logs var ethLogs []*logJSONRPC - err := es.c.backend.Invoke(es.ctx, ðLogs, filterRPC, filter) + err := es.c.backend.CallRPC(es.ctx, ðLogs, filterRPC, filter) // If we fail to query we just retry - setting filter to nil if not found if err != nil { if mapError(filterRPCMethods, err) == ffcapi.ErrorReasonNotFound { @@ -482,7 +482,7 @@ func (es *eventStream) filterEnrichSort(ctx context.Context, ag *aggregatedListe func (es *eventStream) getBlockRangeEvents(ctx context.Context, ag *aggregatedListener, fromBlock, toBlock int64) (ffcapi.ListenerEvents, error) { var ethLogs []*logJSONRPC - err := es.c.backend.Invoke(ctx, ðLogs, "eth_getLogs", &logFilterJSONRPC{ + err := es.c.backend.CallRPC(ctx, ðLogs, "eth_getLogs", &logFilterJSONRPC{ FromBlock: ethtypes.NewHexInteger64(fromBlock), ToBlock: ethtypes.NewHexInteger64(toBlock), Topics: [][]ethtypes.HexBytes0xPrefix{ diff --git a/internal/ethereum/event_stream_test.go b/internal/ethereum/event_stream_test.go index e7018f9..58bea1c 100644 --- a/internal/ethereum/event_stream_test.go +++ b/internal/ethereum/event_stream_test.go @@ -24,20 +24,20 @@ import ( "time" "github.com/hyperledger/firefly-common/pkg/fftypes" - "github.com/hyperledger/firefly-evmconnect/mocks/jsonrpcmocks" + "github.com/hyperledger/firefly-evmconnect/mocks/rpcbackendmocks" "github.com/hyperledger/firefly-signer/pkg/ethtypes" "github.com/hyperledger/firefly-transaction-manager/pkg/ffcapi" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" ) -func testEventStream(t *testing.T, listeners ...*ffcapi.EventListenerAddRequest) (*eventStream, chan *ffcapi.ListenerEvent, *jsonrpcmocks.Client, func()) { +func testEventStream(t *testing.T, listeners ...*ffcapi.EventListenerAddRequest) (*eventStream, chan *ffcapi.ListenerEvent, *rpcbackendmocks.Backend, func()) { ctx, c, mRPC, done := newTestConnector(t) mockStreamLoopEmpty(mRPC) return testEventStreamExistingConnector(t, ctx, done, c, mRPC, listeners...) } -func testEventStreamExistingConnector(t *testing.T, ctx context.Context, done func(), c *ethConnector, mRPC *jsonrpcmocks.Client, listeners ...*ffcapi.EventListenerAddRequest) (*eventStream, chan *ffcapi.ListenerEvent, *jsonrpcmocks.Client, func()) { +func testEventStreamExistingConnector(t *testing.T, ctx context.Context, done func(), c *ethConnector, mRPC *rpcbackendmocks.Backend, listeners ...*ffcapi.EventListenerAddRequest) (*eventStream, chan *ffcapi.ListenerEvent, *rpcbackendmocks.Backend, func()) { events := make(chan *ffcapi.ListenerEvent) esID := fftypes.NewUUID() _, _, err := c.EventStreamStart(ctx, &ffcapi.EventStreamStartRequest{ @@ -219,7 +219,7 @@ func TestCatchupThenRejoinLeadGroup(t *testing.T) { } listenerCaughtUp := make(chan struct{}) - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getLogs", mock.Anything).Return(nil).Run(func(args mock.Arguments) { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getLogs", mock.Anything).Return(nil).Run(func(args mock.Arguments) { ethLogs := make([]*logJSONRPC, 0) filter := *args[3].(*logFilterJSONRPC) fromBlock := filter.FromBlock.BigInt().Int64() @@ -244,7 +244,7 @@ func TestCatchupThenRejoinLeadGroup(t *testing.T) { } *args[1].(*[]*logJSONRPC) = ethLogs }) - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getBlockByHash", "0x6b012339fbb85b70c58ecfd97b31950c4a28bcef5226e12dbe551cb1abaf3b4c", false).Return(nil).Run(func(args mock.Arguments) { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getBlockByHash", "0x6b012339fbb85b70c58ecfd97b31950c4a28bcef5226e12dbe551cb1abaf3b4c", false).Return(nil).Run(func(args mock.Arguments) { *args[1].(**blockInfoJSONRPC) = &blockInfoJSONRPC{ Number: ethtypes.NewHexInteger64(1024), Hash: ethtypes.MustNewHexBytes0xPrefix("0x6b012339fbb85b70c58ecfd97b31950c4a28bcef5226e12dbe551cb1abaf3b4c"), @@ -334,7 +334,7 @@ func TestExitDuringCatchup(t *testing.T) { _, _, mRPC, done := testEventStream(t, l1req) completed := make(chan struct{}) - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getLogs", mock.Anything).Return(nil).Run(func(args mock.Arguments) { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getLogs", mock.Anything).Return(nil).Run(func(args mock.Arguments) { ethLogs := make([]*logJSONRPC, 0) filter := *args[3].(*logFilterJSONRPC) fromBlock := filter.FromBlock.BigInt().Int64() @@ -347,7 +347,7 @@ func TestExitDuringCatchup(t *testing.T) { } *args[1].(*[]*logJSONRPC) = ethLogs }).Once() - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getLogs", mock.Anything).Return(nil) + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getLogs", mock.Anything).Return(nil) <-completed } @@ -367,17 +367,17 @@ func TestLeadGroupDeliverEvents(t *testing.T) { ctx, c, mRPC, done := newTestConnector(t) - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_blockNumber").Return(nil).Run(func(args mock.Arguments) { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_blockNumber").Return(nil).Run(func(args mock.Arguments) { *args[1].(*ethtypes.HexInteger) = *ethtypes.NewHexInteger64(testHighBlock) }) - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_newFilter", mock.Anything).Return(nil). + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_newFilter", mock.Anything).Return(nil). Run(func(args mock.Arguments) { *args[1].(*string) = "filter_id1" }).Once() - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getFilterLogs", mock.Anything).Return(nil).Run(func(args mock.Arguments) { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getFilterLogs", mock.Anything).Return(nil).Run(func(args mock.Arguments) { *args[1].(*[]*logJSONRPC) = make([]*logJSONRPC, 0) }).Maybe() - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getFilterChanges", mock.Anything).Return(nil).Run(func(args mock.Arguments) { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getFilterChanges", mock.Anything).Return(nil).Run(func(args mock.Arguments) { *args[1].(*[]*logJSONRPC) = []*logJSONRPC{ { BlockNumber: ethtypes.NewHexInteger64(212122), @@ -394,16 +394,16 @@ func TestLeadGroupDeliverEvents(t *testing.T) { }, } }).Once() - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getBlockByHash", "0x6b012339fbb85b70c58ecfd97b31950c4a28bcef5226e12dbe551cb1abaf3b4c", false).Return(nil).Run(func(args mock.Arguments) { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getBlockByHash", "0x6b012339fbb85b70c58ecfd97b31950c4a28bcef5226e12dbe551cb1abaf3b4c", false).Return(nil).Run(func(args mock.Arguments) { *args[1].(**blockInfoJSONRPC) = &blockInfoJSONRPC{ Number: ethtypes.NewHexInteger64(212122), Hash: ethtypes.MustNewHexBytes0xPrefix("0x6b012339fbb85b70c58ecfd97b31950c4a28bcef5226e12dbe551cb1abaf3b4c"), } }) - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getFilterChanges", mock.Anything).Return(nil).Run(func(args mock.Arguments) { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getFilterChanges", mock.Anything).Return(nil).Run(func(args mock.Arguments) { *args[1].(*[]*logJSONRPC) = []*logJSONRPC{} }) - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_uninstallFilter", mock.Anything).Return(nil).Run(func(args mock.Arguments) { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_uninstallFilter", mock.Anything).Return(nil).Run(func(args mock.Arguments) { *args[1].(*bool) = true }).Maybe() @@ -439,21 +439,21 @@ func TestLeadGroupNearBlockZeroEnsureNonNegative(t *testing.T) { ctx, c, mRPC, done := newTestConnector(t) filtered := make(chan struct{}) - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_blockNumber").Return(nil).Run(func(args mock.Arguments) { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_blockNumber").Return(nil).Run(func(args mock.Arguments) { *args[1].(*ethtypes.HexInteger) = *ethtypes.NewHexInteger64(10) }) - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_newFilter", mock.Anything).Return(nil). + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_newFilter", mock.Anything).Return(nil). Run(func(args mock.Arguments) { assert.Equal(t, int64(0), args[3].(*logFilterJSONRPC).FromBlock.BigInt().Int64()) *args[1].(*string) = "filter_id1" }).Once() - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getFilterLogs", mock.Anything).Return(nil).Run(func(args mock.Arguments) { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getFilterLogs", mock.Anything).Return(nil).Run(func(args mock.Arguments) { *args[1].(*[]*logJSONRPC) = make([]*logJSONRPC, 0) }).Once().Run(func(args mock.Arguments) { close(filtered) }) - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getFilterChanges", mock.Anything).Return(nil).Maybe() - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_uninstallFilter", mock.Anything).Return(nil).Run(func(args mock.Arguments) { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getFilterChanges", mock.Anything).Return(nil).Maybe() + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_uninstallFilter", mock.Anything).Return(nil).Run(func(args mock.Arguments) { *args[1].(*bool) = true }).Maybe() @@ -479,15 +479,15 @@ func TestLeadGroupCatchupRetry(t *testing.T) { ctx, c, mRPC, done := newTestConnector(t) retried := make(chan struct{}) - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_blockNumber").Return(nil).Run(func(args mock.Arguments) { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_blockNumber").Return(nil).Run(func(args mock.Arguments) { hbh := args[1].(*ethtypes.HexInteger) *hbh = *ethtypes.NewHexInteger64(testHighBlock) }) - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getLogs", mock.Anything).Return(fmt.Errorf("pop")). + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getLogs", mock.Anything).Return(fmt.Errorf("pop")). Run(func(args mock.Arguments) { close(retried) }).Once() - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getLogs", mock.Anything).Return(fmt.Errorf("pop")) + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getLogs", mock.Anything).Return(fmt.Errorf("pop")) _, _, mRPC, done = testEventStreamExistingConnector(t, ctx, done, c, mRPC, l1req) defer done() @@ -511,16 +511,16 @@ func TestStreamLoopNewFilterFail(t *testing.T) { ctx, c, mRPC, done := newTestConnector(t) retried := make(chan struct{}) - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_blockNumber").Return(nil).Run(func(args mock.Arguments) { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_blockNumber").Return(nil).Run(func(args mock.Arguments) { hbh := args[1].(*ethtypes.HexInteger) *hbh = *ethtypes.NewHexInteger64(testHighBlock) }) - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_newFilter", mock.Anything).Return(fmt.Errorf("pop")). + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_newFilter", mock.Anything).Return(fmt.Errorf("pop")). Run(func(args mock.Arguments) { close(retried) }).Once() - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_newFilter", mock.Anything).Return(fmt.Errorf("pop")).Maybe() - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_uninstallFilter", mock.Anything).Return(fmt.Errorf("pop")).Maybe() + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_newFilter", mock.Anything).Return(fmt.Errorf("pop")).Maybe() + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_uninstallFilter", mock.Anything).Return(fmt.Errorf("pop")).Maybe() _, _, mRPC, done = testEventStreamExistingConnector(t, ctx, done, c, mRPC, l1req) defer done() @@ -555,29 +555,29 @@ func TestStreamLoopChangeFilter(t *testing.T) { var es *eventStream reestablishedFilter := make(chan struct{}) - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_blockNumber").Return(nil).Run(func(args mock.Arguments) { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_blockNumber").Return(nil).Run(func(args mock.Arguments) { hbh := args[1].(*ethtypes.HexInteger) *hbh = *ethtypes.NewHexInteger64(testHighBlock) }) - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_newFilter", mock.Anything).Return(nil). + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_newFilter", mock.Anything).Return(nil). Run(func(args mock.Arguments) { l2req.StreamID = es.id _, _, err := c.EventListenerAdd(ctx, l2req) assert.NoError(t, err) *args[1].(*string) = "filter_id1" }).Once() - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_newFilter", mock.Anything).Return(nil). + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_newFilter", mock.Anything).Return(nil). Run(func(args mock.Arguments) { *args[1].(*string) = "filter_id2" close(reestablishedFilter) }) - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getFilterLogs", mock.Anything).Return(nil).Run(func(args mock.Arguments) { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getFilterLogs", mock.Anything).Return(nil).Run(func(args mock.Arguments) { *args[1].(*[]*logJSONRPC) = make([]*logJSONRPC, 0) }).Maybe() - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getFilterChanges", mock.Anything).Return(nil).Run(func(args mock.Arguments) { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getFilterChanges", mock.Anything).Return(nil).Run(func(args mock.Arguments) { *args[1].(*[]*logJSONRPC) = make([]*logJSONRPC, 0) }).Maybe() - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_uninstallFilter", mock.Anything).Return(nil).Run(func(args mock.Arguments) { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_uninstallFilter", mock.Anything).Return(nil).Run(func(args mock.Arguments) { *args[1].(*bool) = true }).Maybe() @@ -603,27 +603,27 @@ func TestStreamLoopFilterReset(t *testing.T) { ctx, c, mRPC, done := newTestConnector(t) reestablishedFilter := make(chan struct{}) - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_blockNumber").Return(nil).Run(func(args mock.Arguments) { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_blockNumber").Return(nil).Run(func(args mock.Arguments) { hbh := args[1].(*ethtypes.HexInteger) *hbh = *ethtypes.NewHexInteger64(testHighBlock) }) - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_newFilter", mock.Anything).Return(nil). + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_newFilter", mock.Anything).Return(nil). Run(func(args mock.Arguments) { *args[1].(*string) = "filter_id1" }).Once() - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_newFilter", mock.Anything).Return(nil). + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_newFilter", mock.Anything).Return(nil). Run(func(args mock.Arguments) { *args[1].(*string) = "filter_id2" close(reestablishedFilter) }) - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getFilterLogs", mock.Anything).Return(fmt.Errorf("filter not found")).Once() - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getFilterLogs", mock.Anything).Return(nil).Run(func(args mock.Arguments) { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getFilterLogs", mock.Anything).Return(fmt.Errorf("filter not found")).Once() + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getFilterLogs", mock.Anything).Return(nil).Run(func(args mock.Arguments) { *args[1].(*[]*logJSONRPC) = make([]*logJSONRPC, 0) }).Maybe() - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getFilterChanges", mock.Anything).Return(nil).Run(func(args mock.Arguments) { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getFilterChanges", mock.Anything).Return(nil).Run(func(args mock.Arguments) { *args[1].(*[]*logJSONRPC) = make([]*logJSONRPC, 0) }).Maybe() - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_uninstallFilter", mock.Anything).Return(nil).Run(func(args mock.Arguments) { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_uninstallFilter", mock.Anything).Return(nil).Run(func(args mock.Arguments) { *args[1].(*bool) = true }).Maybe() diff --git a/internal/ethereum/exec_query.go b/internal/ethereum/exec_query.go index 2602fa1..70f73dd 100644 --- a/internal/ethereum/exec_query.go +++ b/internal/ethereum/exec_query.go @@ -74,7 +74,7 @@ func (c *ethConnector) callTransaction(ctx context.Context, tx *ethsigner.Transa // Do the raw call var outputData ethtypes.HexBytes0xPrefix - err := c.backend.Invoke(ctx, &outputData, "eth_call", tx, "latest") + err := c.backend.CallRPC(ctx, &outputData, "eth_call", tx, "latest") if err != nil { return nil, mapError(callRPCMethods, err), err } diff --git a/internal/ethereum/exec_query_test.go b/internal/ethereum/exec_query_test.go index 97975a4..696df4b 100644 --- a/internal/ethereum/exec_query_test.go +++ b/internal/ethereum/exec_query_test.go @@ -67,7 +67,7 @@ func TestExecQueryOKResponse(t *testing.T) { ctx, c, mRPC, done := newTestConnector(t) defer done() - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_call", + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_call", mock.MatchedBy(func(tx *ethsigner.Transaction) bool { assert.Equal(t, "0x60fe47b100000000000000000000000000000000000000000000000000000000feedbeef", tx.Data.String()) return true @@ -94,7 +94,7 @@ func TestExecQueryOKNilResponse(t *testing.T) { ctx, c, mRPC, done := newTestConnector(t) defer done() - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_call", + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_call", mock.MatchedBy(func(tx *ethsigner.Transaction) bool { assert.Equal(t, "0x60fe47b100000000000000000000000000000000000000000000000000000000feedbeef", tx.Data.String()) return true @@ -120,7 +120,7 @@ func TestExecQueryBadRevertData(t *testing.T) { ctx, c, mRPC, done := newTestConnector(t) defer done() - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_call", mock.Anything, "latest"). + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_call", mock.Anything, "latest"). Run(func(args mock.Arguments) { *(args[1].(*ethtypes.HexBytes0xPrefix)) = ethtypes.MustNewHexBytes0xPrefix("0x08c379a000000000000000000000000000000000000000000000000000000000baadf00d") }). @@ -140,7 +140,7 @@ func TestExecQueryBadReturnData(t *testing.T) { ctx, c, mRPC, done := newTestConnector(t) defer done() - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_call", mock.Anything, "latest"). + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_call", mock.Anything, "latest"). Run(func(args mock.Arguments) { *(args[1].(*ethtypes.HexBytes0xPrefix)) = ethtypes.MustNewHexBytes0xPrefix("0x00000000000000000000000000000000000000000000000000000000baadf00d") }). @@ -178,7 +178,7 @@ func TestExecQueryFailCall(t *testing.T) { ctx, c, mRPC, done := newTestConnector(t) defer done() - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_call", mock.Anything, "latest").Return(fmt.Errorf("pop")) + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_call", mock.Anything, "latest").Return(fmt.Errorf("pop")) var req ffcapi.QueryInvokeRequest err := json.Unmarshal([]byte(sampleExecQuery), &req) diff --git a/internal/ethereum/get_block_info.go b/internal/ethereum/get_block_info.go index abc146f..d840f06 100644 --- a/internal/ethereum/get_block_info.go +++ b/internal/ethereum/get_block_info.go @@ -67,7 +67,7 @@ func (c *ethConnector) getBlockInfoByNumber(ctx context.Context, blockNumber int } if blockInfo == nil { - err := c.backend.Invoke(ctx, &blockInfo, "eth_getBlockByNumber", ethtypes.NewHexInteger64(blockNumber), false /* only the txn hashes */) + err := c.backend.CallRPC(ctx, &blockInfo, "eth_getBlockByNumber", ethtypes.NewHexInteger64(blockNumber), false /* only the txn hashes */) if err != nil { if mapError(blockRPCMethods, err) == ffcapi.ErrorReasonNotFound { log.L(ctx).Debugf("Received error signifying 'block not found': '%s'", err) @@ -107,7 +107,7 @@ func (c *ethConnector) getBlockInfoByHash(ctx context.Context, hash0xString stri } if blockInfo == nil { - err := c.backend.Invoke(ctx, &blockInfo, "eth_getBlockByHash", hash0xString, false /* only the txn hashes */) + err := c.backend.CallRPC(ctx, &blockInfo, "eth_getBlockByHash", hash0xString, false /* only the txn hashes */) if err != nil || blockInfo == nil { return nil, err } diff --git a/internal/ethereum/get_block_info_test.go b/internal/ethereum/get_block_info_test.go index 31b9a45..1b31966 100644 --- a/internal/ethereum/get_block_info_test.go +++ b/internal/ethereum/get_block_info_test.go @@ -75,7 +75,7 @@ func TestGetBlockInfoByNumberOK(t *testing.T) { ctx, c, mRPC, done := newTestConnector(t) defer done() - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getBlockByNumber", + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getBlockByNumber", mock.MatchedBy( func(blockNumber *ethtypes.HexInteger) bool { return blockNumber.BigInt().String() == "12345" @@ -114,7 +114,7 @@ func TestGetBlockInfoByNumberBlockNotFoundError(t *testing.T) { ctx, c, mRPC, done := newTestConnector(t) defer done() - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getBlockByNumber", mock.Anything, false). + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getBlockByNumber", mock.Anything, false). Return(fmt.Errorf("cannot query unfinalized data")) var req ffcapi.BlockInfoByNumberRequest @@ -131,7 +131,7 @@ func TestGetBlockInfoByNumberNotFound(t *testing.T) { ctx, c, mRPC, done := newTestConnector(t) defer done() - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getBlockByNumber", mock.Anything, false). + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getBlockByNumber", mock.Anything, false). Return(nil). Run(func(args mock.Arguments) { err := json.Unmarshal([]byte("null"), args[1]) @@ -153,7 +153,7 @@ func TestGetBlockInfoByNumberFail(t *testing.T) { ctx, c, mRPC, done := newTestConnector(t) defer done() - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getBlockByNumber", mock.Anything, false). + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getBlockByNumber", mock.Anything, false). Return(fmt.Errorf("pop")) var req ffcapi.BlockInfoByNumberRequest @@ -171,7 +171,7 @@ func TestGetBlockInfoByHashOK(t *testing.T) { ctx, c, mRPC, done := newTestConnector(t) defer done() - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getBlockByHash", "0x6197ef1a58a2a592bb447efb651f0db7945de21aa8048801b250bd7b7431f9b6", false). + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getBlockByHash", "0x6197ef1a58a2a592bb447efb651f0db7945de21aa8048801b250bd7b7431f9b6", false). Return(nil). Run(func(args mock.Arguments) { err := json.Unmarshal([]byte(sampleBlockJSONRPC), args[1]) @@ -202,7 +202,7 @@ func TestGetBlockInfoByHashNotFound(t *testing.T) { ctx, c, mRPC, done := newTestConnector(t) defer done() - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getBlockByHash", mock.Anything, false). + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getBlockByHash", mock.Anything, false). Return(nil). Run(func(args mock.Arguments) { err := json.Unmarshal([]byte("null"), args[1]) @@ -224,7 +224,7 @@ func TestGetBlockInfoByHashFail(t *testing.T) { ctx, c, mRPC, done := newTestConnector(t) defer done() - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getBlockByHash", mock.Anything, false). + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getBlockByHash", mock.Anything, false). Return(fmt.Errorf("pop")) var req ffcapi.BlockInfoByHashRequest diff --git a/internal/ethereum/get_gas_price.go b/internal/ethereum/get_gas_price.go index 44cf38c..83f7e17 100644 --- a/internal/ethereum/get_gas_price.go +++ b/internal/ethereum/get_gas_price.go @@ -30,7 +30,7 @@ func (c *ethConnector) GasPriceEstimate(ctx context.Context, req *ffcapi.GasPric // Note we use simple (pre London fork) gas fee approach. // See https://github.com/ethereum/pm/issues/328#issuecomment-853234014 for a bit of color var gasPrice ethtypes.HexInteger - err := c.backend.Invoke(ctx, &gasPrice, "eth_gasPrice") + err := c.backend.CallRPC(ctx, &gasPrice, "eth_gasPrice") if err != nil { return nil, "", err } diff --git a/internal/ethereum/get_gas_price_test.go b/internal/ethereum/get_gas_price_test.go index 8cf4319..cea5431 100644 --- a/internal/ethereum/get_gas_price_test.go +++ b/internal/ethereum/get_gas_price_test.go @@ -41,7 +41,7 @@ func TestGetGasPriceOK(t *testing.T) { ctx, c, mRPC, done := newTestConnector(t) defer done() - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_gasPrice"). + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_gasPrice"). Return(nil). Run(func(args mock.Arguments) { (args[1].(*ethtypes.HexInteger)).BigInt().SetString("12345", 10) @@ -63,7 +63,7 @@ func TestGetGasPriceFail(t *testing.T) { ctx, c, mRPC, done := newTestConnector(t) defer done() - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_gasPrice"). + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_gasPrice"). Return(fmt.Errorf("pop")) var req ffcapi.GasPriceEstimateRequest diff --git a/internal/ethereum/get_next_nonce.go b/internal/ethereum/get_next_nonce.go index 0bc907d..d1792ea 100644 --- a/internal/ethereum/get_next_nonce.go +++ b/internal/ethereum/get_next_nonce.go @@ -27,7 +27,7 @@ import ( func (c *ethConnector) NextNonceForSigner(ctx context.Context, req *ffcapi.NextNonceForSignerRequest) (*ffcapi.NextNonceForSignerResponse, ffcapi.ErrorReason, error) { var txnCount ethtypes.HexInteger - err := c.backend.Invoke(ctx, &txnCount, "eth_getTransactionCount", req.Signer, "pending") + err := c.backend.CallRPC(ctx, &txnCount, "eth_getTransactionCount", req.Signer, "pending") if err != nil { return nil, "", err } diff --git a/internal/ethereum/get_next_nonce_test.go b/internal/ethereum/get_next_nonce_test.go index 691dd17..90e8d4b 100644 --- a/internal/ethereum/get_next_nonce_test.go +++ b/internal/ethereum/get_next_nonce_test.go @@ -41,7 +41,7 @@ func TestGetNextNonceOK(t *testing.T) { ctx, c, mRPC, done := newTestConnector(t) defer done() - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getTransactionCount", "0x302259069aaa5b10dc6f29a9a3f72a8e52837cc3", "pending"). + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getTransactionCount", "0x302259069aaa5b10dc6f29a9a3f72a8e52837cc3", "pending"). Return(nil). Run(func(args mock.Arguments) { args[1].(*ethtypes.HexInteger).BigInt().SetString("12345", 10) @@ -63,7 +63,7 @@ func TestGetNextNonceFail(t *testing.T) { ctx, c, mRPC, done := newTestConnector(t) defer done() - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getTransactionCount", "0x302259069aaa5b10dc6f29a9a3f72a8e52837cc3", "pending"). + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getTransactionCount", "0x302259069aaa5b10dc6f29a9a3f72a8e52837cc3", "pending"). Return(fmt.Errorf("pop")) var req ffcapi.NextNonceForSignerRequest diff --git a/internal/ethereum/get_receipt.go b/internal/ethereum/get_receipt.go index 7ffcd12..41241e0 100644 --- a/internal/ethereum/get_receipt.go +++ b/internal/ethereum/get_receipt.go @@ -74,7 +74,7 @@ type txInfoJSONRPC struct { func (c *ethConnector) getTransactionInfo(ctx context.Context, hash ethtypes.HexBytes0xPrefix) (*txInfoJSONRPC, error) { var txInfo *txInfoJSONRPC - err := c.backend.Invoke(ctx, &txInfo, "eth_getTransactionByHash", hash) + err := c.backend.CallRPC(ctx, &txInfo, "eth_getTransactionByHash", hash) return txInfo, err } @@ -82,7 +82,7 @@ func (c *ethConnector) TransactionReceipt(ctx context.Context, req *ffcapi.Trans // Get the receipt in the back-end JSON/RPC format var ethReceipt *txReceiptJSONRPC - err := c.backend.Invoke(ctx, ðReceipt, "eth_getTransactionReceipt", req.TransactionHash) + err := c.backend.CallRPC(ctx, ðReceipt, "eth_getTransactionReceipt", req.TransactionHash) if err != nil { return nil, "", err } diff --git a/internal/ethereum/get_receipt_test.go b/internal/ethereum/get_receipt_test.go index dfd8517..c8112a3 100644 --- a/internal/ethereum/get_receipt_test.go +++ b/internal/ethereum/get_receipt_test.go @@ -71,7 +71,7 @@ func TestGetReceiptOkSuccess(t *testing.T) { ctx, c, mRPC, done := newTestConnector(t) defer done() - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getTransactionReceipt", + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getTransactionReceipt", mock.MatchedBy(func(txHash string) bool { assert.Equal(t, "0x7d48ae971faf089878b57e3c28e3035540d34f38af395958d2c73c36c57c83a2", txHash) return true @@ -100,7 +100,7 @@ func TestGetReceiptNotFound(t *testing.T) { ctx, c, mRPC, done := newTestConnector(t) defer done() - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getTransactionReceipt", mock.Anything). + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getTransactionReceipt", mock.Anything). Return(nil). Run(func(args mock.Arguments) { err := json.Unmarshal([]byte("null"), args[1]) @@ -122,7 +122,7 @@ func TestGetReceiptError(t *testing.T) { ctx, c, mRPC, done := newTestConnector(t) defer done() - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getTransactionReceipt", mock.Anything). + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getTransactionReceipt", mock.Anything). Return(fmt.Errorf("pop")) var req ffcapi.TransactionReceiptRequest diff --git a/internal/ethereum/new_block_listener_test.go b/internal/ethereum/new_block_listener_test.go index d82b937..0529a40 100644 --- a/internal/ethereum/new_block_listener_test.go +++ b/internal/ethereum/new_block_listener_test.go @@ -31,15 +31,15 @@ func TestNewBlockListenerOK(t *testing.T) { ctx, c, mRPC, done := newTestConnector(t) defer done() - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_blockNumber").Return(nil).Run(func(args mock.Arguments) { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_blockNumber").Return(nil).Run(func(args mock.Arguments) { hbh := args[1].(*ethtypes.HexInteger) *hbh = *ethtypes.NewHexInteger64(1000) }).Maybe() - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_newBlockFilter").Return(nil).Run(func(args mock.Arguments) { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_newBlockFilter").Return(nil).Run(func(args mock.Arguments) { hbh := args[1].(*string) *hbh = "filter_id1" }).Maybe() - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getFilterChanges", "filter_id1").Return(nil).Run(func(args mock.Arguments) { + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getFilterChanges", "filter_id1").Return(nil).Run(func(args mock.Arguments) { hbh := args[1].(*[]ethtypes.HexBytes0xPrefix) *hbh = []ethtypes.HexBytes0xPrefix{} }).Maybe() diff --git a/internal/ethereum/prepare_transaction_test.go b/internal/ethereum/prepare_transaction_test.go index 3c13fdd..3a62f46 100644 --- a/internal/ethereum/prepare_transaction_test.go +++ b/internal/ethereum/prepare_transaction_test.go @@ -157,7 +157,7 @@ func TestPrepareTransactionWithEstimate(t *testing.T) { ctx, c, mRPC, done := newTestConnector(t) defer done() - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_estimateGas", + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_estimateGas", mock.MatchedBy(func(tx *ethsigner.Transaction) bool { assert.Equal(t, "0x60fe47b100000000000000000000000000000000000000000000000000000000feedbeef", tx.Data.String()) return true @@ -183,8 +183,8 @@ func TestPrepareTransactionWithEstimateRevert(t *testing.T) { ctx, c, mRPC, done := newTestConnector(t) defer done() - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_estimateGas", mock.Anything).Return(fmt.Errorf("pop")) - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_call", mock.Anything, "latest").Run( + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_estimateGas", mock.Anything).Return(fmt.Errorf("pop")) + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_call", mock.Anything, "latest").Run( func(args mock.Arguments) { *(args[1].(*ethtypes.HexBytes0xPrefix)) = ethtypes.MustNewHexBytes0xPrefix("0x08c379a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000114d75707065747279206465746563746564000000000000000000000000000000") }, @@ -205,8 +205,8 @@ func TestPrepareTransactionWithEstimateFail(t *testing.T) { ctx, c, mRPC, done := newTestConnector(t) defer done() - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_estimateGas", mock.Anything).Return(fmt.Errorf("pop")) - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_call", mock.Anything, "latest").Return(fmt.Errorf("pop")) + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_estimateGas", mock.Anything).Return(fmt.Errorf("pop")) + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_call", mock.Anything, "latest").Return(fmt.Errorf("pop")) var req ffcapi.TransactionPrepareRequest err := json.Unmarshal([]byte(samplePrepareTXEstimateGas), &req) diff --git a/internal/ethereum/send_transaction.go b/internal/ethereum/send_transaction.go index c29d927..c160677 100644 --- a/internal/ethereum/send_transaction.go +++ b/internal/ethereum/send_transaction.go @@ -49,7 +49,7 @@ func (c *ethConnector) TransactionSend(ctx context.Context, req *ffcapi.Transact } var txHash ethtypes.HexBytes0xPrefix - err = c.backend.Invoke(ctx, &txHash, "eth_sendTransaction", tx) + err = c.backend.CallRPC(ctx, &txHash, "eth_sendTransaction", tx) if err == nil && len(txHash) != 32 { err = i18n.NewError(ctx, msgs.MsgInvalidTXHashReturned, len(txHash)) } diff --git a/internal/ethereum/send_transaction_test.go b/internal/ethereum/send_transaction_test.go index b4e87c2..42a0dd9 100644 --- a/internal/ethereum/send_transaction_test.go +++ b/internal/ethereum/send_transaction_test.go @@ -109,7 +109,7 @@ func TestSendTransactionBadHash(t *testing.T) { ctx, c, mRPC, done := newTestConnector(t) defer done() - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_sendTransaction", + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_sendTransaction", mock.MatchedBy(func(tx *ethsigner.Transaction) bool { assert.Equal(t, "0x60fe47b100000000000000000000000000000000000000000000000000000000feedbeef", tx.Data.String()) return true @@ -135,7 +135,7 @@ func TestSendTransactionOK(t *testing.T) { ctx, c, mRPC, done := newTestConnector(t) defer done() - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_sendTransaction", + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_sendTransaction", mock.MatchedBy(func(tx *ethsigner.Transaction) bool { assert.Equal(t, "0x60fe47b100000000000000000000000000000000000000000000000000000000feedbeef", tx.Data.String()) return true @@ -163,7 +163,7 @@ func TestSendTransactionFail(t *testing.T) { ctx, c, mRPC, done := newTestConnector(t) defer done() - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_sendTransaction", + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_sendTransaction", mock.MatchedBy(func(tx *ethsigner.Transaction) bool { assert.Equal(t, "0x60fe47b100000000000000000000000000000000000000000000000000000000feedbeef", tx.Data.String()) return true @@ -256,7 +256,7 @@ func TestSendTransactionGasPriceEIP1559(t *testing.T) { ctx, c, mRPC, done := newTestConnector(t) defer done() - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_sendTransaction", + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_sendTransaction", mock.MatchedBy(func(tx *ethsigner.Transaction) bool { assert.Equal(t, int64(65535), tx.MaxFeePerGas.BigInt().Int64()) assert.Equal(t, int64(12345), tx.MaxPriorityFeePerGas.BigInt().Int64()) @@ -282,7 +282,7 @@ func TestSendTransactionGasPriceLegacyNested(t *testing.T) { ctx, c, mRPC, done := newTestConnector(t) defer done() - mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_sendTransaction", + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_sendTransaction", mock.MatchedBy(func(tx *ethsigner.Transaction) bool { assert.Equal(t, int64(65535), tx.GasPrice.BigInt().Int64()) return true diff --git a/internal/jsonrpc/rpc.go b/internal/jsonrpc/rpc.go deleted file mode 100644 index c3d4484..0000000 --- a/internal/jsonrpc/rpc.go +++ /dev/null @@ -1,118 +0,0 @@ -// Copyright © 2022 Kaleido, Inc. -// -// SPDX-License-Identifier: Apache-2.0 -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package jsonrpc - -import ( - "context" - "encoding/json" - "fmt" - "sync/atomic" - - "github.com/go-resty/resty/v2" - "github.com/hyperledger/firefly-common/pkg/i18n" - "github.com/hyperledger/firefly-common/pkg/log" - "github.com/hyperledger/firefly-evmconnect/internal/msgs" - "github.com/sirupsen/logrus" -) - -type Client interface { - Invoke(ctx context.Context, result interface{}, method string, params ...interface{}) error -} - -func NewRPCClient(httpClient *resty.Client) Client { - return &jsonRPC{ - httpClient: httpClient, - } -} - -type jsonRPC struct { - httpClient *resty.Client - nextRPCRequestID int64 -} - -type RPCRequest struct { - JSONRpc string `json:"jsonrpc"` - ID json.RawMessage `json:"id"` - Method string `json:"method"` - Params []interface{} `json:"params,omitempty"` -} - -type RPCError struct { - Code int64 `json:"code"` - Message string `json:"message"` - Data []interface{} `json:"data,omitempty"` -} - -type RPCResponse struct { - JSONRpc string `json:"jsonrpc"` - ID json.RawMessage `json:"id"` - Result json.RawMessage `json:"result,omitempty"` - Error *RPCError `json:"error,omitempty"` -} - -func (r *RPCResponse) Message() string { - if r.Error != nil { - return r.Error.Message - } - return "" -} - -func (r *jsonRPC) Invoke(ctx context.Context, result interface{}, method string, params ...interface{}) error { - id := atomic.AddInt64(&r.nextRPCRequestID, 1) - rpcReq := &RPCRequest{ - JSONRpc: "2.0", - ID: json.RawMessage(fmt.Sprintf(`"%d"`, id)), - Method: method, - Params: params, - } - rpcRes := &RPCResponse{} - - log.L(ctx).Debugf("RPC:%s:%s --> %s", rpcReq.ID, rpcReq.ID, rpcReq.Method) - if logrus.IsLevelEnabled(logrus.TraceLevel) { - jsonInput, _ := json.Marshal(rpcReq) - log.L(ctx).Tracef("RPC:%s:%s INPUT: %s", rpcReq.ID, rpcReq.ID, jsonInput) - } - res, err := r.httpClient.R(). - SetContext(ctx). - SetBody(rpcReq). - SetResult(&rpcRes). - SetError(rpcRes). - Post("") - - // Restore the original ID - rpcRes.ID = rpcReq.ID - if err != nil { - err := i18n.NewError(ctx, msgs.MsgRPCRequestFailed) - log.L(ctx).Errorf("RPC[%d] <-- ERROR: %s", id, err) - return err - } - if logrus.IsLevelEnabled(logrus.TraceLevel) { - jsonOutput, _ := json.Marshal(rpcRes) - log.L(ctx).Tracef("RPC:%s:%s OUTPUT: %s", rpcReq.ID, rpcReq.ID, jsonOutput) - } - // JSON/RPC allows errors to be returned with a 200 status code, as well as other status codes - if res.IsError() || rpcRes.Error != nil && rpcRes.Error.Code != 0 { - log.L(ctx).Errorf("RPC[%d] <-- [%d]: %d '%s'", id, res.StatusCode(), rpcRes.Error.Code, rpcRes.Message()) - err := fmt.Errorf(rpcRes.Message()) - return err - } - log.L(ctx).Debugf("RPC[%d] <-- [%d] OK", id, res.StatusCode()) - if rpcRes.Result == nil { - return nil - } - return json.Unmarshal(rpcRes.Result, &result) -} diff --git a/mocks/rpcbackendmocks/backend.go b/mocks/rpcbackendmocks/backend.go new file mode 100644 index 0000000..26d0a96 --- /dev/null +++ b/mocks/rpcbackendmocks/backend.go @@ -0,0 +1,55 @@ +// Code generated by mockery v1.0.0. DO NOT EDIT. + +package rpcbackendmocks + +import ( + context "context" + + rpcbackend "github.com/hyperledger/firefly-signer/pkg/rpcbackend" + mock "github.com/stretchr/testify/mock" +) + +// Backend is an autogenerated mock type for the Backend type +type Backend struct { + mock.Mock +} + +// CallRPC provides a mock function with given fields: ctx, result, method, params +func (_m *Backend) CallRPC(ctx context.Context, result interface{}, method string, params ...interface{}) error { + var _ca []interface{} + _ca = append(_ca, ctx, result, method) + _ca = append(_ca, params...) + ret := _m.Called(_ca...) + + var r0 error + if rf, ok := ret.Get(0).(func(context.Context, interface{}, string, ...interface{}) error); ok { + r0 = rf(ctx, result, method, params...) + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// SyncRequest provides a mock function with given fields: ctx, rpcReq +func (_m *Backend) SyncRequest(ctx context.Context, rpcReq *rpcbackend.RPCRequest) (*rpcbackend.RPCResponse, error) { + ret := _m.Called(ctx, rpcReq) + + var r0 *rpcbackend.RPCResponse + if rf, ok := ret.Get(0).(func(context.Context, *rpcbackend.RPCRequest) *rpcbackend.RPCResponse); ok { + r0 = rf(ctx, rpcReq) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*rpcbackend.RPCResponse) + } + } + + var r1 error + if rf, ok := ret.Get(1).(func(context.Context, *rpcbackend.RPCRequest) error); ok { + r1 = rf(ctx, rpcReq) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +}