From a60121569342ffe37424b5b30c54c7bc1099dfb0 Mon Sep 17 00:00:00 2001 From: bendanzhentan <455462586@qq.com> Date: Thu, 14 Dec 2023 12:30:57 +0800 Subject: [PATCH] fix: locate log within receipt using logIndex Log.LogIndex is indexed at the block level, so it's incorrect to locate a log using receipt.Logs[log.LogIndex]. Instead, we should traverse through receipt.Logs and find the matching log based on log.LogIndex. --- core/processor.go | 30 ++++++++++++++++++++++-------- core/utils.go | 21 +++++++++++++++++++++ 2 files changed, 43 insertions(+), 8 deletions(-) create mode 100644 core/utils.go diff --git a/core/processor.go b/core/processor.go index 22e8fb5..82d5843 100644 --- a/core/processor.go +++ b/core/processor.go @@ -61,9 +61,12 @@ func (b *Processor) toWithdrawal(botDelegatedWithdrawToEvent *BotDelegatedWithdr return nil, fmt.Errorf("invalid botDelegatedWithdrawToEvent: %v", botDelegatedWithdrawToEvent) } - messagePassedLog := receipt.Logs[botDelegatedWithdrawToEvent.LogIndex-3] - sentMessageLog := receipt.Logs[botDelegatedWithdrawToEvent.LogIndex-2] - sentMessageExtension1Log := receipt.Logs[botDelegatedWithdrawToEvent.LogIndex-1] + messagePassedLog := GetLogByLogIndex(receipt, uint(botDelegatedWithdrawToEvent.LogIndex-3)) + sentMessageLog := GetLogByLogIndex(receipt, uint(botDelegatedWithdrawToEvent.LogIndex-2)) + sentMessageExtension1Log := GetLogByLogIndex(receipt, uint(botDelegatedWithdrawToEvent.LogIndex-1)) + if messagePassedLog == nil || sentMessageLog == nil || sentMessageExtension1Log == nil { + return nil, fmt.Errorf("invalid botDelegatedWithdrawToEvent: %v", botDelegatedWithdrawToEvent) + } sentMessageEvent, err := b.toL2CrossDomainMessengerSentMessageExtension1(sentMessageLog, sentMessageExtension1Log) if err != nil { @@ -88,7 +91,12 @@ func (b *Processor) ProveWithdrawalTransaction(ctx context.Context, botDelegated return err } - err = b.CheckByFilterOptions(botDelegatedWithdrawToEvent, receipt) + vlog := GetLogByLogIndex(receipt, uint(botDelegatedWithdrawToEvent.LogIndex)) + if vlog == nil { + return fmt.Errorf("cannot find log within receipt, logIndex: %d, receitp: %v", botDelegatedWithdrawToEvent.LogIndex, receipt) + } + + err = b.CheckByFilterOptions(vlog) if err != nil { return err } @@ -192,7 +200,12 @@ func (b *Processor) FinalizeMessage(ctx context.Context, botDelegatedWithdrawToE return err } - err = b.CheckByFilterOptions(botDelegatedWithdrawToEvent, receipt) + vlog := GetLogByLogIndex(receipt, uint(botDelegatedWithdrawToEvent.LogIndex)) + if vlog == nil { + return fmt.Errorf("cannot find log within receipt, logIndex: %d, receitp: %v", botDelegatedWithdrawToEvent.LogIndex, receipt) + } + + err = b.CheckByFilterOptions(vlog) if err != nil { return err } @@ -530,7 +543,7 @@ func (b *Processor) toLowLevelMessage( return &withdrawalTx, nil } -func (b *Processor) CheckByFilterOptions(botDelegatedWithdrawToEvent *BotDelegatedWithdrawal, receipt *types.Receipt) error { +func (b *Processor) CheckByFilterOptions(vlog *types.Log) error { L2StandardBridgeBotAbi, _ := bindings2.L2StandardBridgeBotMetaData.GetAbi() withdrawToEvent := bindings2.L2StandardBridgeBotWithdrawTo{} indexedArgs := func(arguments abi.Arguments) abi.Arguments { @@ -542,12 +555,13 @@ func (b *Processor) CheckByFilterOptions(botDelegatedWithdrawToEvent *BotDelegat } return indexedArgs } - err := abi.ParseTopics(&withdrawToEvent, indexedArgs(L2StandardBridgeBotAbi.Events["WithdrawTo"].Inputs), receipt.Logs[botDelegatedWithdrawToEvent.LogIndex].Topics[1:]) + + err := abi.ParseTopics(&withdrawToEvent, indexedArgs(L2StandardBridgeBotAbi.Events["WithdrawTo"].Inputs), vlog.Topics[1:]) if err != nil { return fmt.Errorf("parse indexed event arguments from log.topics of L2StandardBridgeBotWithdrawTo event, err: %v", err) } - err = L2StandardBridgeBotAbi.UnpackIntoInterface(&withdrawToEvent, "WithdrawTo", receipt.Logs[botDelegatedWithdrawToEvent.LogIndex].Data) + err = L2StandardBridgeBotAbi.UnpackIntoInterface(&withdrawToEvent, "WithdrawTo", vlog.Data) if err != nil { return fmt.Errorf("parse non-indexed event arguments from log.data of L2StandardBridgeBotWithdrawTo event, err: %v", err) } diff --git a/core/utils.go b/core/utils.go new file mode 100644 index 0000000..19dfd3d --- /dev/null +++ b/core/utils.go @@ -0,0 +1,21 @@ +package core + +import "github.com/ethereum/go-ethereum/core/types" + +// GetLogByLogIndex searches through receipt.Logs using the provided logIndex and return the corresponding log. +// +// Be aware that: +// - Log.Index is accumulated for the entire block. +// - There is no guarantee that receipt.Logs will be ordered by index. +func GetLogByLogIndex(receipt *types.Receipt, logIndex uint) *types.Log { + if receipt == nil { + return nil + } + + for _, vlog := range receipt.Logs { + if vlog.Index == logIndex { + return vlog + } + } + return nil +}