Skip to content

Commit

Permalink
adding warning logs for rebuiding in memory chain
Browse files Browse the repository at this point in the history
Signed-off-by: Chengxuan Xing <[email protected]>
  • Loading branch information
Chengxuan committed Nov 13, 2024
1 parent d6c1c57 commit 2b8b704
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 2 deletions.
10 changes: 8 additions & 2 deletions internal/ethereum/blocklistener.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,8 +381,7 @@ func (bl *blockListener) handleNewBlock(mbi *minimalBlockInfo, addAfter *list.El
// a recent block advertisement. So we need to work backwards to the last point of consistency with the current
// chain and re-query the chain state from there.
func (bl *blockListener) rebuildCanonicalChain() *list.Element {

log.L(bl.ctx).Debugf("Rebuilding in-memory canonical chain")
log.L(bl.ctx).Warn("Rebuilding in-memory canonical chain")

// If none of our blocks were valid, start from the first block number we've notified about previously
lastValidBlock := bl.trimToLastValidBlock()
Expand Down Expand Up @@ -452,10 +451,14 @@ func (bl *blockListener) rebuildCanonicalChain() *list.Element {
func (bl *blockListener) trimToLastValidBlock() (lastValidBlock *minimalBlockInfo) {
// First remove from the end until we get a block that matches the current un-cached query view from the chain
lastElem := bl.canonicalChain.Back()
var startingNumber *int64
for lastElem != nil && lastElem.Value != nil {

// Query the block that is no at this blockNumber
currentViewBlock := lastElem.Value.(*minimalBlockInfo)
if startingNumber == nil {
startingNumber = &currentViewBlock.number
}
var freshBlockInfo *blockInfoJSONRPC
var reason ffcapi.ErrorReason
err := bl.c.retry.Do(bl.ctx, "rebuild listener canonical chain", func(_ int) (retry bool, err error) {
Expand All @@ -481,7 +484,10 @@ func (bl *blockListener) trimToLastValidBlock() (lastValidBlock *minimalBlockInf
break
}
lastElem = lastElem.Prev()
}

if startingNumber != nil && lastValidBlock != nil && *startingNumber != lastValidBlock.number {
log.L(bl.ctx).Warnf("Trimmed canonical chain to from block %d to block %d (total number of in memory blocks: %d)", startingNumber, lastValidBlock.number, bl.unstableHeadLength)
}
return lastValidBlock
}
Expand Down
6 changes: 6 additions & 0 deletions internal/ethereum/event_enricher.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ import (
"context"

"github.com/hyperledger/firefly-common/pkg/fftypes"
"github.com/hyperledger/firefly-common/pkg/i18n"
"github.com/hyperledger/firefly-common/pkg/log"
"github.com/hyperledger/firefly-evmconnect/internal/msgs"
"github.com/hyperledger/firefly-signer/pkg/abi"
"github.com/hyperledger/firefly-signer/pkg/ethtypes"
"github.com/hyperledger/firefly-transaction-manager/pkg/ffcapi"
Expand Down Expand Up @@ -84,6 +86,10 @@ func (ee *eventEnricher) filterEnrichEthLog(ctx context.Context, f *eventFilter,
}
}

if blockNumber < 0 || transactionIndex < 0 || logIndex < 0 {
log.L(ctx).Errorf("Invalid block number, transaction index or log index for event '%s'", protoID)
return nil, matched, decoded, i18n.NewError(ctx, msgs.MsgInvalidProtocolID, protoID)
}
signature := f.Signature
return &ffcapi.Event{
ID: ffcapi.EventID{
Expand Down
32 changes: 32 additions & 0 deletions internal/ethereum/event_listener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,38 @@ func TestFilterEnrichEthLogMethodInputsOk(t *testing.T) {

}

func TestFilterEnrichEthLogInvalidNegativeID(t *testing.T) {

l, mRPC, _ := newTestListener(t, true)

var abiEvent *abi.Entry
err := json.Unmarshal([]byte(abiTransferEvent), &abiEvent)
assert.NoError(t, err)

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("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{
From: ethtypes.MustNewAddress("0x3968ef051b422d3d1cdc182a88bba8dd922e6fa4"),
Input: ethtypes.MustNewHexBytes0xPrefix("0xa9059cbb000000000000000000000000d0f2f5103fd050739a9fb567251bc460cc24d09100000000000000000000000000000000000000000000000000000000000003e8"),
}
}).Once()

ethLogWithNegativeLogIndex := sampleTransferLog()
ethLogWithNegativeLogIndex.LogIndex = ethtypes.NewHexInteger64(-1)
_, ok, err := l.filterEnrichEthLog(context.Background(), l.config.filters[0], l.config.options.Methods, ethLogWithNegativeLogIndex) // cache miss
assert.False(t, ok)
assert.Regexp(t, "FF23055", err)

}

func TestFilterEnrichEthLogMethodInputsTxInfoWithErr(t *testing.T) {

l, mRPC, _ := newTestListener(t, true)
Expand Down
1 change: 1 addition & 0 deletions internal/msgs/en_error_messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,5 @@ var (
MsgUnableToCallDebug = ffe("FF23052", "Failed to call debug_traceTransaction to get error detail: %s")
MsgReturnValueNotDecoded = ffe("FF23053", "Error return value for custom error: %s")
MsgReturnValueNotAvailable = ffe("FF23054", "Error return value unavailable")
MsgInvalidProtocolID = ffe("FF23055", "Invalid protocol ID in event log: %s")
)

0 comments on commit 2b8b704

Please sign in to comment.