From 2edfa857adabc91237206191fe1b5c53512bb844 Mon Sep 17 00:00:00 2001 From: Devon Bear Date: Mon, 16 Jan 2023 11:04:23 -0500 Subject: [PATCH] feat(abi): Propagate error from TooManyTopics (#22) --- core/vm/precompile/ethlog_factory.go | 6 ++++-- core/vm/precompile/ethlog_factory_test.go | 3 ++- core/vm/precompile/event/event.go | 10 +++++++--- types/abi/abi.go | 6 +++--- types/abi/abi_test.go | 10 +++++++--- types/abi/errors.go | 23 +++++++++++++++++++++++ 6 files changed, 46 insertions(+), 12 deletions(-) create mode 100644 types/abi/errors.go diff --git a/core/vm/precompile/ethlog_factory.go b/core/vm/precompile/ethlog_factory.go index bf71d787d..d7a54fae5 100644 --- a/core/vm/precompile/ethlog_factory.go +++ b/core/vm/precompile/ethlog_factory.go @@ -56,12 +56,14 @@ func (pef *EthereumLogFactory) RegisterEvent( moduleEthAddress common.Address, abiEvent abi.Event, customModuleAttributes event.ValueDecoders, -) { - pef.precompileEvents[EventType(abiEvent.Name)] = event.NewPrecompileEvent( +) error { + var err error + pef.precompileEvents[EventType(abiEvent.Name)], err = event.NewPrecompileEvent( moduleEthAddress, abiEvent, customModuleAttributes, ) + return err } // ============================================================================== diff --git a/core/vm/precompile/ethlog_factory_test.go b/core/vm/precompile/ethlog_factory_test.go index e4856c22e..3dd6293d8 100644 --- a/core/vm/precompile/ethlog_factory_test.go +++ b/core/vm/precompile/ethlog_factory_test.go @@ -40,7 +40,8 @@ var _ = Describe("Events Registry", func() { BeforeEach(func() { stakingModuleAddr = common.BytesToAddress(authtypes.NewModuleAddress("staking").Bytes()) factory = precompile.NewEthereumLogFactory() - factory.RegisterEvent(stakingModuleAddr, getMockAbiEvent(), nil) + err := factory.RegisterEvent(stakingModuleAddr, getMockAbiEvent(), nil) + Expect(err).To(BeNil()) valAddr = sdk.ValAddress([]byte("alice")) delAddr = sdk.AccAddress([]byte("bob")) amt = sdk.NewCoin("denom", sdk.NewInt(1)) diff --git a/core/vm/precompile/event/event.go b/core/vm/precompile/event/event.go index cf4d7826f..634008d76 100644 --- a/core/vm/precompile/event/event.go +++ b/core/vm/precompile/event/event.go @@ -49,15 +49,19 @@ func NewPrecompileEvent( moduleAddr common.Address, abiEvent abi.Event, customValueDecoders ValueDecoders, -) *PrecompileEvent { +) (*PrecompileEvent, error) { + indexedInputs, err := abi.GetIndexed(abiEvent.Inputs) + if err != nil { + return nil, err + } pe := &PrecompileEvent{ moduleAddr: moduleAddr, id: abiEvent.ID, - indexedInputs: abi.GetIndexed(abiEvent.Inputs), + indexedInputs: indexedInputs, nonIndexedInputs: abiEvent.Inputs.NonIndexed(), customValueDecoders: customValueDecoders, } - return pe + return pe, nil } // `ModuleAddress` returns the Ethereum address corresponding to a PrecompileEvent's Cosmos module. diff --git a/types/abi/abi.go b/types/abi/abi.go index 98ebde056..e5ce89a42 100644 --- a/types/abi/abi.go +++ b/types/abi/abi.go @@ -50,7 +50,7 @@ func ToMixedCase(input string) string { // `GetIndexed` extracts indexed arguments from a set of arguments. Will panic if more than 3 // indexed arguments are provided by the inputs ABI. -func GetIndexed(args abi.Arguments) abi.Arguments { +func GetIndexed(args abi.Arguments) (abi.Arguments, error) { var indexed abi.Arguments for _, arg := range args { if arg.Indexed { @@ -59,8 +59,8 @@ func GetIndexed(args abi.Arguments) abi.Arguments { } if len(indexed) > maxIndexedArgs { - panic("number of indexed arguments is more than allowed by Eth event log") + return nil, ErrTooManyIndexedArgs } - return indexed + return indexed, nil } diff --git a/types/abi/abi_test.go b/types/abi/abi_test.go index 7b31cb173..03ca062f1 100644 --- a/types/abi/abi_test.go +++ b/types/abi/abi_test.go @@ -74,12 +74,16 @@ var _ = Describe("ABI Test Suite", func() { Indexed: true, }, } - Expect(abi.GetIndexed(allArgs)).To(Equal(indexedArgs)) + args, err := abi.GetIndexed(allArgs) + Expect(err).To(BeNil()) + Expect(args).To(Equal(indexedArgs)) }) - It("should panic if more than 3 indexed args are present", func() { + It("should return an error if more than 3 indexed args are given", func() { allArgs = append(allArgs, abi.Argument{Indexed: true}) - Expect(func() { abi.GetIndexed(allArgs) }).To(Panic()) + args, err := abi.GetIndexed(allArgs) + Expect(args).To(BeNil()) + Expect(err).To(Equal(abi.ErrTooManyIndexedArgs)) }) }) }) diff --git a/types/abi/errors.go b/types/abi/errors.go new file mode 100644 index 000000000..e33284d47 --- /dev/null +++ b/types/abi/errors.go @@ -0,0 +1,23 @@ +// Copyright (C) 2023, Berachain Foundation. All rights reserved. +// See the file LICENSE for licensing terms. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package abi + +import "errors" + +var ( + // `ErrTooManyIndexedArgs` is returned when the number of indexed arguments + // in an event exceeds the maximum allowed by the Ethereum event log. + ErrTooManyIndexedArgs = errors.New("number of indexed arguments is more than allowed by Eth event log") +)