|
| 1 | +// Copyright © 2026 Kaleido, Inc. |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: Apache-2.0 |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +// you may not use this file except in compliance with the License. |
| 7 | +// You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, software |
| 12 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +// See the License for the specific language governing permissions and |
| 15 | +// limitations under the License. |
| 16 | + |
| 17 | +package ethblocklistener |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "github.com/hyperledger/firefly-common/pkg/fftypes" |
| 24 | + "github.com/hyperledger/firefly-common/pkg/i18n" |
| 25 | + "github.com/hyperledger/firefly-evmconnect/mocks/rpcbackendmocks" |
| 26 | + "github.com/hyperledger/firefly-evmconnect/pkg/ethrpc" |
| 27 | + "github.com/hyperledger/firefly-signer/pkg/ethtypes" |
| 28 | + "github.com/hyperledger/firefly-signer/pkg/rpcbackend" |
| 29 | + "github.com/stretchr/testify/assert" |
| 30 | + "github.com/stretchr/testify/mock" |
| 31 | +) |
| 32 | + |
| 33 | +func TestFetchBlockReceiptsAsyncOptimizedOk(t *testing.T) { |
| 34 | + _, bl, mRPC, done := newTestBlockListener(t, func(conf *BlockListenerConfig, mRPC *rpcbackendmocks.Backend, cancelCtx context.CancelFunc) { |
| 35 | + conf.UseGetBlockReceipts = true |
| 36 | + }) |
| 37 | + defer done() |
| 38 | + |
| 39 | + blockHash := ethtypes.MustNewHexBytes0xPrefix(fftypes.NewRandB32().String()) |
| 40 | + blockNumber := ethtypes.NewHexIntegerU64(12346) |
| 41 | + |
| 42 | + receipt := ðrpc.TxReceiptJSONRPC{ |
| 43 | + TransactionHash: ethtypes.MustNewHexBytes0xPrefix(fftypes.NewRandB32().String()), |
| 44 | + BlockHash: blockHash, |
| 45 | + } |
| 46 | + |
| 47 | + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getBlockReceipts", mock.Anything).Return(nil).Run(func(args mock.Arguments) { |
| 48 | + assert.Equal(t, blockNumber, args[3]) |
| 49 | + res := args[1].(*[]*ethrpc.TxReceiptJSONRPC) |
| 50 | + *res = []*ethrpc.TxReceiptJSONRPC{receipt} |
| 51 | + }) |
| 52 | + |
| 53 | + fetched := make(chan struct{}) |
| 54 | + bl.FetchBlockReceiptsAsync(blockNumber, blockHash, func(receipts []*ethrpc.TxReceiptJSONRPC, err error) { |
| 55 | + defer close(fetched) |
| 56 | + assert.NoError(t, err) |
| 57 | + assert.Equal(t, []*ethrpc.TxReceiptJSONRPC{receipt}, receipts) |
| 58 | + }) |
| 59 | + <-fetched |
| 60 | +} |
| 61 | + |
| 62 | +func TestFetchBlockReceiptsAsyncOptimizedBlockMismatch(t *testing.T) { |
| 63 | + _, bl, mRPC, done := newTestBlockListener(t, func(conf *BlockListenerConfig, mRPC *rpcbackendmocks.Backend, cancelCtx context.CancelFunc) { |
| 64 | + conf.UseGetBlockReceipts = true |
| 65 | + }) |
| 66 | + defer done() |
| 67 | + |
| 68 | + blockHash := ethtypes.MustNewHexBytes0xPrefix(fftypes.NewRandB32().String()) |
| 69 | + blockNumber := ethtypes.NewHexIntegerU64(12346) |
| 70 | + |
| 71 | + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getBlockReceipts", mock.Anything).Return(nil).Run(func(args mock.Arguments) { |
| 72 | + assert.Equal(t, blockNumber, args[3]) |
| 73 | + res := args[1].(*[]*ethrpc.TxReceiptJSONRPC) |
| 74 | + *res = []*ethrpc.TxReceiptJSONRPC{ |
| 75 | + { |
| 76 | + TransactionHash: ethtypes.MustNewHexBytes0xPrefix(fftypes.NewRandB32().String()), |
| 77 | + BlockHash: ethtypes.MustNewHexBytes0xPrefix(fftypes.NewRandB32().String()), |
| 78 | + }, |
| 79 | + } |
| 80 | + }) |
| 81 | + |
| 82 | + fetched := make(chan struct{}) |
| 83 | + bl.FetchBlockReceiptsAsync(blockNumber, blockHash, func(receipts []*ethrpc.TxReceiptJSONRPC, err error) { |
| 84 | + defer close(fetched) |
| 85 | + assert.Regexp(t, "FF23068.*"+blockHash.String(), err) |
| 86 | + }) |
| 87 | + <-fetched |
| 88 | +} |
| 89 | + |
| 90 | +func TestFetchBlockReceiptsAsyncOptimizedBlockHandleError(t *testing.T) { |
| 91 | + ctx, bl, mRPC, done := newTestBlockListener(t, func(conf *BlockListenerConfig, mRPC *rpcbackendmocks.Backend, cancelCtx context.CancelFunc) { |
| 92 | + conf.UseGetBlockReceipts = true |
| 93 | + }) |
| 94 | + defer done() |
| 95 | + |
| 96 | + blockHash := ethtypes.MustNewHexBytes0xPrefix(fftypes.NewRandB32().String()) |
| 97 | + blockNumber := ethtypes.NewHexIntegerU64(12346) |
| 98 | + |
| 99 | + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getBlockReceipts", mock.Anything). |
| 100 | + Return(rpcbackend.NewRPCError(ctx, rpcbackend.RPCCodeInternalError, i18n.Msg404NotFound)) |
| 101 | + |
| 102 | + fetched := make(chan struct{}) |
| 103 | + bl.FetchBlockReceiptsAsync(blockNumber, blockHash, func(receipts []*ethrpc.TxReceiptJSONRPC, err error) { |
| 104 | + defer close(fetched) |
| 105 | + assert.Regexp(t, "FF00167", err) |
| 106 | + }) |
| 107 | + <-fetched |
| 108 | +} |
| 109 | + |
| 110 | +func TestFetchBlockReceiptsAsyncOptimizedBlockHandlePanic(t *testing.T) { |
| 111 | + _, bl, mRPC, done := newTestBlockListener(t, func(conf *BlockListenerConfig, mRPC *rpcbackendmocks.Backend, cancelCtx context.CancelFunc) { |
| 112 | + conf.UseGetBlockReceipts = true |
| 113 | + }) |
| 114 | + defer done() |
| 115 | + |
| 116 | + blockHash := ethtypes.MustNewHexBytes0xPrefix(fftypes.NewRandB32().String()) |
| 117 | + blockNumber := ethtypes.NewHexIntegerU64(12346) |
| 118 | + |
| 119 | + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getBlockReceipts", mock.Anything).Panic("pop") |
| 120 | + |
| 121 | + fetched := make(chan struct{}) |
| 122 | + bl.FetchBlockReceiptsAsync(blockNumber, blockHash, func(receipts []*ethrpc.TxReceiptJSONRPC, err error) { |
| 123 | + defer close(fetched) |
| 124 | + assert.Regexp(t, "FF23067.*pop", err) |
| 125 | + }) |
| 126 | + <-fetched |
| 127 | +} |
| 128 | + |
| 129 | +func TestFetchBlockReceiptsAsyncNonOptimizedOk(t *testing.T) { |
| 130 | + _, bl, mRPC, done := newTestBlockListener(t) |
| 131 | + defer done() |
| 132 | + |
| 133 | + blockHash := ethtypes.MustNewHexBytes0xPrefix(fftypes.NewRandB32().String()) |
| 134 | + blockNumber := ethtypes.NewHexIntegerU64(12346) |
| 135 | + txHash := ethtypes.MustNewHexBytes0xPrefix(fftypes.NewRandB32().String()) |
| 136 | + |
| 137 | + block := ðrpc.BlockInfoJSONRPC{ |
| 138 | + Number: blockNumber, |
| 139 | + Hash: blockHash, |
| 140 | + Transactions: []ethtypes.HexBytes0xPrefix{txHash}, |
| 141 | + } |
| 142 | + |
| 143 | + receipt := ðrpc.TxReceiptJSONRPC{ |
| 144 | + TransactionHash: txHash, |
| 145 | + BlockHash: blockHash, |
| 146 | + } |
| 147 | + |
| 148 | + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getBlockByHash", mock.Anything, false).Return(nil).Run(func(args mock.Arguments) { |
| 149 | + assert.Equal(t, blockHash.String(), args[3]) |
| 150 | + res := args[1].(**ethrpc.BlockInfoJSONRPC) |
| 151 | + *res = block |
| 152 | + }) |
| 153 | + mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getTransactionReceipt", mock.Anything).Return(nil).Run(func(args mock.Arguments) { |
| 154 | + assert.Equal(t, txHash.String(), args[3]) |
| 155 | + res := args[1].(**ethrpc.TxReceiptJSONRPC) |
| 156 | + *res = receipt |
| 157 | + }) |
| 158 | + |
| 159 | + fetched := make(chan struct{}) |
| 160 | + bl.FetchBlockReceiptsAsync(blockNumber, blockHash, func(receipts []*ethrpc.TxReceiptJSONRPC, err error) { |
| 161 | + defer close(fetched) |
| 162 | + assert.NoError(t, err) |
| 163 | + assert.Equal(t, []*ethrpc.TxReceiptJSONRPC{receipt}, receipts) |
| 164 | + }) |
| 165 | + <-fetched |
| 166 | +} |
0 commit comments