Skip to content
This repository has been archived by the owner on Jun 9, 2024. It is now read-only.

Commit

Permalink
feat(abi): Propagate error from TooManyTopics (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
Devon Bear authored Jan 16, 2023
1 parent 1d75d74 commit 2edfa85
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 12 deletions.
6 changes: 4 additions & 2 deletions core/vm/precompile/ethlog_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

// ==============================================================================
Expand Down
3 changes: 2 additions & 1 deletion core/vm/precompile/ethlog_factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
10 changes: 7 additions & 3 deletions core/vm/precompile/event/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 3 additions & 3 deletions types/abi/abi.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
10 changes: 7 additions & 3 deletions types/abi/abi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})
})
})
23 changes: 23 additions & 0 deletions types/abi/errors.go
Original file line number Diff line number Diff line change
@@ -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")
)

0 comments on commit 2edfa85

Please sign in to comment.