From 9f990e139249dfd407e9c050dd1c0f0aceaf2617 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20Negovanovi=C4=87?= Date: Fri, 22 Sep 2023 14:31:18 +0200 Subject: [PATCH] Simplify EventAbi.Encode function in bindings --- consensus/polybft/checkpoint_manager.go | 7 +-- consensus/polybft/consensus_runtime_test.go | 4 +- .../polybft/contractsapi/bindings-gen/main.go | 4 +- .../polybft/contractsapi/contractsapi.go | 60 +++++++++---------- consensus/polybft/contractsapi/helper.go | 2 +- consensus/polybft/sc_integration_test.go | 3 +- 6 files changed, 37 insertions(+), 43 deletions(-) diff --git a/consensus/polybft/checkpoint_manager.go b/consensus/polybft/checkpoint_manager.go index 02a604d74f..efde527c09 100644 --- a/consensus/polybft/checkpoint_manager.go +++ b/consensus/polybft/checkpoint_manager.go @@ -405,9 +405,7 @@ func (c *checkpointManager) GenerateExitProof(exitID uint64) (types.Proof, error return types.Proof{}, fmt.Errorf("checkpoint block not found for exit ID %d", exitID) } - var exitEventABI contractsapi.L2StateSyncedEvent - - exitEventEncoded, err := exitEventABI.Encode(exitEvent.L2StateSyncedEvent) + exitEventEncoded, err := exitEvent.L2StateSyncedEvent.Encode() if err != nil { return types.Proof{}, err } @@ -451,9 +449,8 @@ func createExitTree(exitEvents []*ExitEvent) (*merkle.MerkleTree, error) { numOfEvents := len(exitEvents) data := make([][]byte, numOfEvents) - var exitEventAPI contractsapi.L2StateSyncedEvent for i := 0; i < numOfEvents; i++ { - b, err := exitEventAPI.Encode(exitEvents[i].L2StateSyncedEvent) + b, err := exitEvents[i].L2StateSyncedEvent.Encode() if err != nil { return nil, err } diff --git a/consensus/polybft/consensus_runtime_test.go b/consensus/polybft/consensus_runtime_test.go index 168169959a..8b20ee4916 100644 --- a/consensus/polybft/consensus_runtime_test.go +++ b/consensus/polybft/consensus_runtime_test.go @@ -11,7 +11,6 @@ import ( "github.com/0xPolygon/go-ibft/messages/proto" "github.com/0xPolygon/polygon-edge/consensus" "github.com/0xPolygon/polygon-edge/consensus/polybft/bitmap" - "github.com/0xPolygon/polygon-edge/consensus/polybft/contractsapi" bls "github.com/0xPolygon/polygon-edge/consensus/polybft/signer" "github.com/0xPolygon/polygon-edge/consensus/polybft/validator" "github.com/0xPolygon/polygon-edge/consensus/polybft/wallet" @@ -1078,9 +1077,8 @@ func encodeExitEvents(t *testing.T, exitEvents []*ExitEvent) [][]byte { encodedEvents := make([][]byte, len(exitEvents)) - var exitEventAPI contractsapi.L2StateSyncedEvent for i, e := range exitEvents { - encodedEvent, err := exitEventAPI.Encode(e.L2StateSyncedEvent) + encodedEvent, err := e.L2StateSyncedEvent.Encode() require.NoError(t, err) encodedEvents[i] = encodedEvent diff --git a/consensus/polybft/contractsapi/bindings-gen/main.go b/consensus/polybft/contractsapi/bindings-gen/main.go index 8e79c8abd7..d302d15b6e 100644 --- a/consensus/polybft/contractsapi/bindings-gen/main.go +++ b/consensus/polybft/contractsapi/bindings-gen/main.go @@ -649,8 +649,8 @@ func (*{{.TName}}) Sig() ethgo.Hash { return {{.ContractName}}.Abi.Events["{{.Name}}"].ID() } -func (*{{.TName}}) Encode(inputs interface{}) ([]byte, error) { - return {{.ContractName}}.Abi.Events["{{.Name}}"].Inputs.Encode(inputs) +func ({{.Sig}} *{{.TName}}) Encode() ([]byte, error) { + return {{.ContractName}}.Abi.Events["{{.Name}}"].Inputs.Encode({{.Sig}}) } func ({{.Sig}} *{{.TName}}) ParseLog(log *ethgo.Log) (bool, error) { diff --git a/consensus/polybft/contractsapi/contractsapi.go b/consensus/polybft/contractsapi/contractsapi.go index ff377694c4..36ce2faeb5 100644 --- a/consensus/polybft/contractsapi/contractsapi.go +++ b/consensus/polybft/contractsapi/contractsapi.go @@ -87,8 +87,8 @@ func (*StateSyncResultEvent) Sig() ethgo.Hash { return StateReceiver.Abi.Events["StateSyncResult"].ID() } -func (*StateSyncResultEvent) Encode(inputs interface{}) ([]byte, error) { - return StateReceiver.Abi.Events["StateSyncResult"].Inputs.Encode(inputs) +func (s *StateSyncResultEvent) Encode() ([]byte, error) { + return StateReceiver.Abi.Events["StateSyncResult"].Inputs.Encode(s) } func (s *StateSyncResultEvent) ParseLog(log *ethgo.Log) (bool, error) { @@ -113,8 +113,8 @@ func (*NewCommitmentEvent) Sig() ethgo.Hash { return StateReceiver.Abi.Events["NewCommitment"].ID() } -func (*NewCommitmentEvent) Encode(inputs interface{}) ([]byte, error) { - return StateReceiver.Abi.Events["NewCommitment"].Inputs.Encode(inputs) +func (n *NewCommitmentEvent) Encode() ([]byte, error) { + return StateReceiver.Abi.Events["NewCommitment"].Inputs.Encode(n) } func (n *NewCommitmentEvent) ParseLog(log *ethgo.Log) (bool, error) { @@ -157,8 +157,8 @@ func (*StateSyncedEvent) Sig() ethgo.Hash { return StateSender.Abi.Events["StateSynced"].ID() } -func (*StateSyncedEvent) Encode(inputs interface{}) ([]byte, error) { - return StateSender.Abi.Events["StateSynced"].Inputs.Encode(inputs) +func (s *StateSyncedEvent) Encode() ([]byte, error) { + return StateSender.Abi.Events["StateSynced"].Inputs.Encode(s) } func (s *StateSyncedEvent) ParseLog(log *ethgo.Log) (bool, error) { @@ -184,8 +184,8 @@ func (*L2StateSyncedEvent) Sig() ethgo.Hash { return L2StateSender.Abi.Events["L2StateSynced"].ID() } -func (*L2StateSyncedEvent) Encode(inputs interface{}) ([]byte, error) { - return L2StateSender.Abi.Events["L2StateSynced"].Inputs.Encode(inputs) +func (l *L2StateSyncedEvent) Encode() ([]byte, error) { + return L2StateSender.Abi.Events["L2StateSynced"].Inputs.Encode(l) } func (l *L2StateSyncedEvent) ParseLog(log *ethgo.Log) (bool, error) { @@ -564,8 +564,8 @@ func (*TokenMappedEvent) Sig() ethgo.Hash { return RootERC20Predicate.Abi.Events["TokenMapped"].ID() } -func (*TokenMappedEvent) Encode(inputs interface{}) ([]byte, error) { - return RootERC20Predicate.Abi.Events["TokenMapped"].Inputs.Encode(inputs) +func (t *TokenMappedEvent) Encode() ([]byte, error) { + return RootERC20Predicate.Abi.Events["TokenMapped"].Inputs.Encode(t) } func (t *TokenMappedEvent) ParseLog(log *ethgo.Log) (bool, error) { @@ -608,8 +608,8 @@ func (*MintableTokenMappedEvent) Sig() ethgo.Hash { return ChildMintableERC20Predicate.Abi.Events["MintableTokenMapped"].ID() } -func (*MintableTokenMappedEvent) Encode(inputs interface{}) ([]byte, error) { - return ChildMintableERC20Predicate.Abi.Events["MintableTokenMapped"].Inputs.Encode(inputs) +func (m *MintableTokenMappedEvent) Encode() ([]byte, error) { + return ChildMintableERC20Predicate.Abi.Events["MintableTokenMapped"].Inputs.Encode(m) } func (m *MintableTokenMappedEvent) ParseLog(log *ethgo.Log) (bool, error) { @@ -913,8 +913,8 @@ func (*L2MintableTokenMappedEvent) Sig() ethgo.Hash { return RootMintableERC1155PredicateACL.Abi.Events["L2MintableTokenMapped"].ID() } -func (*L2MintableTokenMappedEvent) Encode(inputs interface{}) ([]byte, error) { - return RootMintableERC1155PredicateACL.Abi.Events["L2MintableTokenMapped"].Inputs.Encode(inputs) +func (l *L2MintableTokenMappedEvent) Encode() ([]byte, error) { + return RootMintableERC1155PredicateACL.Abi.Events["L2MintableTokenMapped"].Inputs.Encode(l) } func (l *L2MintableTokenMappedEvent) ParseLog(log *ethgo.Log) (bool, error) { @@ -1284,8 +1284,8 @@ func (*ValidatorRegisteredEvent) Sig() ethgo.Hash { return CustomSupernetManager.Abi.Events["ValidatorRegistered"].ID() } -func (*ValidatorRegisteredEvent) Encode(inputs interface{}) ([]byte, error) { - return CustomSupernetManager.Abi.Events["ValidatorRegistered"].Inputs.Encode(inputs) +func (v *ValidatorRegisteredEvent) Encode() ([]byte, error) { + return CustomSupernetManager.Abi.Events["ValidatorRegistered"].Inputs.Encode(v) } func (v *ValidatorRegisteredEvent) ParseLog(log *ethgo.Log) (bool, error) { @@ -1308,8 +1308,8 @@ func (*AddedToWhitelistEvent) Sig() ethgo.Hash { return CustomSupernetManager.Abi.Events["AddedToWhitelist"].ID() } -func (*AddedToWhitelistEvent) Encode(inputs interface{}) ([]byte, error) { - return CustomSupernetManager.Abi.Events["AddedToWhitelist"].Inputs.Encode(inputs) +func (a *AddedToWhitelistEvent) Encode() ([]byte, error) { + return CustomSupernetManager.Abi.Events["AddedToWhitelist"].Inputs.Encode(a) } func (a *AddedToWhitelistEvent) ParseLog(log *ethgo.Log) (bool, error) { @@ -1433,8 +1433,8 @@ func (*ChildManagerRegisteredEvent) Sig() ethgo.Hash { return StakeManager.Abi.Events["ChildManagerRegistered"].ID() } -func (*ChildManagerRegisteredEvent) Encode(inputs interface{}) ([]byte, error) { - return StakeManager.Abi.Events["ChildManagerRegistered"].Inputs.Encode(inputs) +func (c *ChildManagerRegisteredEvent) Encode() ([]byte, error) { + return StakeManager.Abi.Events["ChildManagerRegistered"].Inputs.Encode(c) } func (c *ChildManagerRegisteredEvent) ParseLog(log *ethgo.Log) (bool, error) { @@ -1459,8 +1459,8 @@ func (*StakeAddedEvent) Sig() ethgo.Hash { return StakeManager.Abi.Events["StakeAdded"].ID() } -func (*StakeAddedEvent) Encode(inputs interface{}) ([]byte, error) { - return StakeManager.Abi.Events["StakeAdded"].Inputs.Encode(inputs) +func (s *StakeAddedEvent) Encode() ([]byte, error) { + return StakeManager.Abi.Events["StakeAdded"].Inputs.Encode(s) } func (s *StakeAddedEvent) ParseLog(log *ethgo.Log) (bool, error) { @@ -1485,8 +1485,8 @@ func (*StakeWithdrawnEvent) Sig() ethgo.Hash { return StakeManager.Abi.Events["StakeWithdrawn"].ID() } -func (*StakeWithdrawnEvent) Encode(inputs interface{}) ([]byte, error) { - return StakeManager.Abi.Events["StakeWithdrawn"].Inputs.Encode(inputs) +func (s *StakeWithdrawnEvent) Encode() ([]byte, error) { + return StakeManager.Abi.Events["StakeWithdrawn"].Inputs.Encode(s) } func (s *StakeWithdrawnEvent) ParseLog(log *ethgo.Log) (bool, error) { @@ -1595,8 +1595,8 @@ func (*TransferEvent) Sig() ethgo.Hash { return ValidatorSet.Abi.Events["Transfer"].ID() } -func (*TransferEvent) Encode(inputs interface{}) ([]byte, error) { - return ValidatorSet.Abi.Events["Transfer"].Inputs.Encode(inputs) +func (t *TransferEvent) Encode() ([]byte, error) { + return ValidatorSet.Abi.Events["Transfer"].Inputs.Encode(t) } func (t *TransferEvent) ParseLog(log *ethgo.Log) (bool, error) { @@ -1620,8 +1620,8 @@ func (*WithdrawalRegisteredEvent) Sig() ethgo.Hash { return ValidatorSet.Abi.Events["WithdrawalRegistered"].ID() } -func (*WithdrawalRegisteredEvent) Encode(inputs interface{}) ([]byte, error) { - return ValidatorSet.Abi.Events["WithdrawalRegistered"].Inputs.Encode(inputs) +func (w *WithdrawalRegisteredEvent) Encode() ([]byte, error) { + return ValidatorSet.Abi.Events["WithdrawalRegistered"].Inputs.Encode(w) } func (w *WithdrawalRegisteredEvent) ParseLog(log *ethgo.Log) (bool, error) { @@ -1645,8 +1645,8 @@ func (*WithdrawalEvent) Sig() ethgo.Hash { return ValidatorSet.Abi.Events["Withdrawal"].ID() } -func (*WithdrawalEvent) Encode(inputs interface{}) ([]byte, error) { - return ValidatorSet.Abi.Events["Withdrawal"].Inputs.Encode(inputs) +func (w *WithdrawalEvent) Encode() ([]byte, error) { + return ValidatorSet.Abi.Events["Withdrawal"].Inputs.Encode(w) } func (w *WithdrawalEvent) ParseLog(log *ethgo.Log) (bool, error) { diff --git a/consensus/polybft/contractsapi/helper.go b/consensus/polybft/contractsapi/helper.go index 6332104a6f..d71b585972 100644 --- a/consensus/polybft/contractsapi/helper.go +++ b/consensus/polybft/contractsapi/helper.go @@ -19,7 +19,7 @@ type EventAbi interface { // Sig returns the event ABI signature or ID (which is unique for all event types) Sig() ethgo.Hash // Encode does abi encoding of given event - Encode(inputs interface{}) ([]byte, error) + Encode() ([]byte, error) // ParseLog parses the provided receipt log to given event type ParseLog(log *ethgo.Log) (bool, error) } diff --git a/consensus/polybft/sc_integration_test.go b/consensus/polybft/sc_integration_test.go index 30a2eb8ad9..0bf377ccfa 100644 --- a/consensus/polybft/sc_integration_test.go +++ b/consensus/polybft/sc_integration_test.go @@ -233,8 +233,7 @@ func TestIntegration_PerformExit(t *testing.T) { res := getField(exitHelperContractAddress, contractsapi.ExitHelper.Abi, "processedExits", exits[0].ID) require.Equal(t, 0, int(res[31])) - var exitEventAPI contractsapi.L2StateSyncedEvent - proofExitEvent, err := exitEventAPI.Encode(exits[0].L2StateSyncedEvent) + proofExitEvent, err := exits[0].L2StateSyncedEvent.Encode() require.NoError(t, err) proof, err := exitTree.GenerateProof(proofExitEvent)