From ee32e1ca0aa85250a2a3a1937e9dc3c1c375b661 Mon Sep 17 00:00:00 2001 From: Mark Cherepovskyi Date: Mon, 2 Sep 2024 12:28:27 +0300 Subject: [PATCH 1/3] rootupdater: take contract address from tx logs instead of msg --- x/rootupdater/keeper/keeper.go | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/x/rootupdater/keeper/keeper.go b/x/rootupdater/keeper/keeper.go index 4dc5b89b..5d6c383d 100644 --- a/x/rootupdater/keeper/keeper.go +++ b/x/rootupdater/keeper/keeper.go @@ -57,24 +57,18 @@ func NewKeeper( func (k Keeper) PostTxProcessing(ctx sdk.Context, msg core.Message, receipt *ethtypes.Receipt) error { params := k.GetParams(ctx) - k.Logger(ctx).Error("PostTxProcessing", "msg", msg, "receipt", receipt) + k.Logger(ctx).Info("PostTxProcessing", "msg", msg, "receipt", receipt) stateV2, err := abi.JSON(strings.NewReader(state.PoseidonSMTABI)) if err != nil { k.Logger(ctx).Error("failed to marshal poseidon smart abi", "error", err) - return err + return nil } contractAddress, err := hexutil.Decode(params.ContractAddress) if err != nil { // If return an error here, the whole EVM module won't work - k.Logger(ctx).Info("failed to decode contract address") - return nil - } - - // Validating message receiver address (should be our state smart contract) - if msg.To() == nil || bytes.Compare(msg.To().Bytes(), contractAddress) != 0 { - k.Logger(ctx).Info("inappropriate contract address") + k.Logger(ctx).Error("failed to decode contract address") return nil } @@ -85,6 +79,12 @@ func (k Keeper) PostTxProcessing(ctx sdk.Context, msg core.Message, receipt *eth } for _, log := range receipt.Logs { + // Validating message receiver address (should be our state smart contract) + if log.Address.Bytes() == nil || bytes.Compare(log.Address.Bytes(), contractAddress) != 0 { + k.Logger(ctx).Info(fmt.Sprintf("inappropriate contract address: expected %s got %s", params.ContractAddress, log.Address.String())) + continue + } + eventId := log.Topics[0] event, err := stateV2.EventByID(eventId) if err != nil { @@ -93,7 +93,7 @@ func (k Keeper) PostTxProcessing(ctx sdk.Context, msg core.Message, receipt *eth } if event.Name != params.EventName { - k.Logger(ctx).Info("unmatched event: got %s, expected %s", event.Name, params.EventName) + k.Logger(ctx).Info(fmt.Sprintf("unmatched event: got %s, expected %s", event.Name, params.EventName)) continue } From 7f1a05932ad17a35d43bc47e2ac1986ab62fcbb5 Mon Sep 17 00:00:00 2001 From: Mark Cherepovskyi Date: Mon, 2 Sep 2024 14:08:13 +0300 Subject: [PATCH 2/3] update address validation --- x/rootupdater/keeper/keeper.go | 54 +++++++++++++++++++--------------- 1 file changed, 31 insertions(+), 23 deletions(-) diff --git a/x/rootupdater/keeper/keeper.go b/x/rootupdater/keeper/keeper.go index 5d6c383d..dc111f30 100644 --- a/x/rootupdater/keeper/keeper.go +++ b/x/rootupdater/keeper/keeper.go @@ -78,37 +78,45 @@ func (k Keeper) PostTxProcessing(ctx sdk.Context, msg core.Message, receipt *eth k.Logger(ctx).Error("logs is empty") } + // This approach is used because the contract address we use for validation and the event we + //want to catch are in different logs. + isAppropriateAddress := false for _, log := range receipt.Logs { // Validating message receiver address (should be our state smart contract) - if log.Address.Bytes() == nil || bytes.Compare(log.Address.Bytes(), contractAddress) != 0 { - k.Logger(ctx).Info(fmt.Sprintf("inappropriate contract address: expected %s got %s", params.ContractAddress, log.Address.String())) - continue + if log != nil && log.Address.Bytes() != nil && bytes.Compare(log.Address.Bytes(), contractAddress) == 0 { + isAppropriateAddress = true } + } - eventId := log.Topics[0] - event, err := stateV2.EventByID(eventId) - if err != nil { - k.Logger(ctx).Error("failed to get event by ID") - continue - } + if isAppropriateAddress { + for _, log := range receipt.Logs { - if event.Name != params.EventName { - k.Logger(ctx).Info(fmt.Sprintf("unmatched event: got %s, expected %s", event.Name, params.EventName)) - continue - } + eventId := log.Topics[0] - eventBody := state.PoseidonSMTRootUpdated{} - if err := utils.UnpackLog(stateV2, &eventBody, event.Name, log); err != nil { - k.Logger(ctx).Error("failed to unpack event body") - continue - } + event, err := stateV2.EventByID(eventId) + if err != nil { + k.Logger(ctx).Error("failed to get event by ID") + continue + } - params.Root = hexutil.Encode(eventBody.Root[:]) - params.RootTimestamp = ctx.BlockTime().Unix() - params.BlockHeight = log.BlockNumber + if event.Name != params.EventName { + k.Logger(ctx).Info(fmt.Sprintf("unmatched event: got %s, expected %s", event.Name, params.EventName)) + continue + } - k.Logger(ctx).Info(fmt.Sprintf("Received PostTxProcessing event in %s module: %v", types.ModuleName, eventBody)) - k.SetParams(ctx, params) + eventBody := state.PoseidonSMTRootUpdated{} + if err := utils.UnpackLog(stateV2, &eventBody, event.Name, log); err != nil { + k.Logger(ctx).Error("failed to unpack event body") + continue + } + + params.Root = hexutil.Encode(eventBody.Root[:]) + params.RootTimestamp = ctx.BlockTime().Unix() + params.BlockHeight = log.BlockNumber + + k.Logger(ctx).Info(fmt.Sprintf("Received PostTxProcessing event in %s module: %v", types.ModuleName, eventBody)) + k.SetParams(ctx, params) + } } return nil From 076a8131f8762aca490688dd6dc0f790257b6605 Mon Sep 17 00:00:00 2001 From: Mark Cherepovskyi Date: Mon, 2 Sep 2024 14:18:21 +0300 Subject: [PATCH 3/3] code style --- x/rootupdater/keeper/keeper.go | 47 +++++++++++++++++----------------- 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/x/rootupdater/keeper/keeper.go b/x/rootupdater/keeper/keeper.go index dc111f30..563872d8 100644 --- a/x/rootupdater/keeper/keeper.go +++ b/x/rootupdater/keeper/keeper.go @@ -87,36 +87,37 @@ func (k Keeper) PostTxProcessing(ctx sdk.Context, msg core.Message, receipt *eth isAppropriateAddress = true } } + if !isAppropriateAddress { + return nil + } - if isAppropriateAddress { - for _, log := range receipt.Logs { + for _, log := range receipt.Logs { - eventId := log.Topics[0] + eventId := log.Topics[0] - event, err := stateV2.EventByID(eventId) - if err != nil { - k.Logger(ctx).Error("failed to get event by ID") - continue - } + event, err := stateV2.EventByID(eventId) + if err != nil { + k.Logger(ctx).Error("failed to get event by ID") + continue + } - if event.Name != params.EventName { - k.Logger(ctx).Info(fmt.Sprintf("unmatched event: got %s, expected %s", event.Name, params.EventName)) - continue - } + if event.Name != params.EventName { + k.Logger(ctx).Info(fmt.Sprintf("unmatched event: got %s, expected %s", event.Name, params.EventName)) + continue + } - eventBody := state.PoseidonSMTRootUpdated{} - if err := utils.UnpackLog(stateV2, &eventBody, event.Name, log); err != nil { - k.Logger(ctx).Error("failed to unpack event body") - continue - } + eventBody := state.PoseidonSMTRootUpdated{} + if err := utils.UnpackLog(stateV2, &eventBody, event.Name, log); err != nil { + k.Logger(ctx).Error("failed to unpack event body") + continue + } - params.Root = hexutil.Encode(eventBody.Root[:]) - params.RootTimestamp = ctx.BlockTime().Unix() - params.BlockHeight = log.BlockNumber + params.Root = hexutil.Encode(eventBody.Root[:]) + params.RootTimestamp = ctx.BlockTime().Unix() + params.BlockHeight = log.BlockNumber - k.Logger(ctx).Info(fmt.Sprintf("Received PostTxProcessing event in %s module: %v", types.ModuleName, eventBody)) - k.SetParams(ctx, params) - } + k.Logger(ctx).Info(fmt.Sprintf("Received PostTxProcessing event in %s module: %v", types.ModuleName, eventBody)) + k.SetParams(ctx, params) } return nil