Skip to content

Commit

Permalink
Enable generating of functions with same names (#1331)
Browse files Browse the repository at this point in the history
  • Loading branch information
goran-ethernal committed Mar 24, 2023
1 parent a6fffdc commit dbbc230
Show file tree
Hide file tree
Showing 21 changed files with 95 additions and 102 deletions.
6 changes: 3 additions & 3 deletions command/bridge/deposit/deposit_erc20.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ func runCommand(cmd *cobra.Command, _ []string) {

// createDepositTxn encodes parameters for deposit function on rootchain predicate contract
func createDepositTxn(sender, receiver types.Address, amount *big.Int) (*ethgo.Transaction, error) {
depositToFn := &contractsapi.DepositToFunction{
depositToFn := &contractsapi.DepositToRootERC20PredicateFn{
RootToken: types.StringToAddress(dp.rootTokenAddr),
Receiver: receiver,
Amount: amount,
Expand All @@ -293,7 +293,7 @@ func createDepositTxn(sender, receiver types.Address, amount *big.Int) (*ethgo.T

// createMintTxn encodes parameters for mint function on rootchain token contract
func createMintTxn(sender, receiver types.Address, amount *big.Int) (*ethgo.Transaction, error) {
mintFn := &contractsapi.MintFunction{
mintFn := &contractsapi.MintRootERC20Fn{
To: receiver,
Amount: amount,
}
Expand All @@ -316,7 +316,7 @@ func createMintTxn(sender, receiver types.Address, amount *big.Int) (*ethgo.Tran
// to ERC20 token for ERC20 predicate so that it is able to spend given tokens
func createApproveERC20PredicateTxn(amount *big.Int,
rootERC20Predicate, rootERC20Token types.Address) (*ethgo.Transaction, error) {
approveFnParams := &contractsapi.ApproveFunction{
approveFnParams := &contractsapi.ApproveRootERC20Fn{
Spender: rootERC20Predicate,
Amount: amount,
}
Expand Down
2 changes: 1 addition & 1 deletion command/bridge/exit/exit.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ func createExitTxn(sender ethgo.Address, proof types.Proof) (*ethgo.Transaction,
return nil, nil, errors.New("failed to convert proof checkpoint block")
}

exitFn := &contractsapi.ExitFunction{
exitFn := &contractsapi.ExitExitHelperFn{
BlockNumber: new(big.Int).SetUint64(uint64(checkpointBlock)),
LeafIndex: new(big.Int).SetUint64(uint64(leafIndex)),
UnhashedLeaf: exitEventEncoded,
Expand Down
2 changes: 1 addition & 1 deletion command/bridge/withdraw/withdraw_erc20.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func run(cmd *cobra.Command, _ []string) {

// createWithdrawTxn encodes parameters for withdraw function on child chain predicate contract
func createWithdrawTxn(receiver types.Address, amount *big.Int) (*ethgo.Transaction, error) {
withdrawToFn := &contractsapi.WithdrawToFunction{
withdrawToFn := &contractsapi.WithdrawToChildERC20PredicateFn{
ChildToken: types.StringToAddress(wp.childTokenAddr),
Receiver: receiver,
Amount: amount,
Expand Down
4 changes: 2 additions & 2 deletions command/rootchain/initcontracts/init_contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ func initializeCheckpointManager(
return fmt.Errorf("failed to convert validators to map: %w", err)
}

initialize := contractsapi.InitializeCheckpointManagerFunction{
initialize := contractsapi.InitializeCheckpointManagerFn{
ChainID_: big.NewInt(manifest.ChainID),
NewBls: manifest.RootchainConfig.BLSAddress,
NewBn256G2: manifest.RootchainConfig.BN256G2Address,
Expand Down Expand Up @@ -403,7 +403,7 @@ func initializeExitHelper(txRelayer txrelayer.TxRelayer, rootchainConfig *polybf
// initializeRootERC20Predicate invokes initialize function on "RootERC20Predicate" smart contract
func initializeRootERC20Predicate(txRelayer txrelayer.TxRelayer, rootchainConfig *polybft.RootchainConfig,
deployerKey ethgo.Key) error {
rootERC20PredicateParams := &contractsapi.InitializeRootERC20PredicateFunction{
rootERC20PredicateParams := &contractsapi.InitializeRootERC20PredicateFn{
NewStateSender: rootchainConfig.StateSenderAddress,
NewExitHelper: rootchainConfig.ExitHelperAddress,
NewChildERC20Predicate: contracts.ChildERC20PredicateContract,
Expand Down
2 changes: 1 addition & 1 deletion command/sidechain/registration/register_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ func registerValidator(sender txrelayer.TxRelayer, account *wallet.Account,
return nil, fmt.Errorf("register validator failed: %w", err)
}

registerFn := &contractsapi.RegisterFunction{
registerFn := &contractsapi.RegisterChildValidatorSetFn{
Signature: sigMarshal,
Pubkey: account.Bls.PublicKey().ToBigInt(),
}
Expand Down
4 changes: 2 additions & 2 deletions consensus/polybft/checkpoint_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ func (c *checkpointManager) abiEncodeCheckpointBlock(blockNumber uint64, blockHa
return nil, err
}

submit := &contractsapi.SubmitFunction{
submit := &contractsapi.SubmitCheckpointManagerFn{
CheckpointMetadata: &contractsapi.CheckpointMetadata{
BlockHash: blockHash,
BlockRound: new(big.Int).SetUint64(extra.Checkpoint.BlockRound),
Expand Down Expand Up @@ -344,7 +344,7 @@ func (c *checkpointManager) GenerateExitProof(exitID uint64) (types.Proof, error
return types.Proof{}, err
}

getCheckpointBlockFn := &contractsapi.GetCheckpointBlockFunction{
getCheckpointBlockFn := &contractsapi.GetCheckpointBlockCheckpointManagerFn{
BlockNumber: new(big.Int).SetUint64(exitEvent.BlockNumber),
}

Expand Down
6 changes: 3 additions & 3 deletions consensus/polybft/checkpoint_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ func TestCheckpointManager_abiEncodeCheckpointBlock(t *testing.T) {
checkpointDataEncoded, err := c.abiEncodeCheckpointBlock(header.Number, header.Hash, extra, nextValidators.getPublicIdentities())
require.NoError(t, err)

submit := &contractsapi.SubmitFunction{}
submit := &contractsapi.SubmitCheckpointManagerFn{}
require.NoError(t, submit.DecodeAbi(checkpointDataEncoded))

require.Equal(t, new(big.Int).SetUint64(checkpoint.EpochNumber), submit.Checkpoint.Epoch)
Expand Down Expand Up @@ -389,7 +389,7 @@ func TestCheckpointManager_GenerateExitProof(t *testing.T) {
})
require.NoError(t, err)

getCheckpointBlockFn := &contractsapi.GetCheckpointBlockFunction{
getCheckpointBlockFn := &contractsapi.GetCheckpointBlockCheckpointManagerFn{
BlockNumber: new(big.Int).SetUint64(correctBlockToGetExit),
}

Expand Down Expand Up @@ -510,7 +510,7 @@ func (d *dummyTxRelayer) SendTransactionLocal(txn *ethgo.Transaction) (*ethgo.Re
func getBlockNumberCheckpointSubmitInput(t *testing.T, input []byte) uint64 {
t.Helper()

submit := &contractsapi.SubmitFunction{}
submit := &contractsapi.SubmitCheckpointManagerFn{}
require.NoError(t, submit.DecodeAbi(input))

return submit.Checkpoint.BlockNumber.Uint64()
Expand Down
4 changes: 2 additions & 2 deletions consensus/polybft/consensus_runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ func (c *consensusRuntime) restartEpoch(header *types.Header) (*epochMetadata, e
// in the current epoch, and ending at the last block of previous epoch
func (c *consensusRuntime) calculateCommitEpochInput(
currentBlock *types.Header,
epoch *epochMetadata) (*contractsapi.CommitEpochFunction, error) {
epoch *epochMetadata) (*contractsapi.CommitEpochChildValidatorSetFn, error) {
uptimeCounter := map[types.Address]int64{}
blockHeader := currentBlock
epochID := epoch.Number
Expand Down Expand Up @@ -529,7 +529,7 @@ func (c *consensusRuntime) calculateCommitEpochInput(
uptime.AddValidatorUptime(addr, uptimeCounter[addr])
}

commitEpoch := &contractsapi.CommitEpochFunction{
commitEpoch := &contractsapi.CommitEpochChildValidatorSetFn{
ID: new(big.Int).SetUint64(epochID),
Epoch: &contractsapi.Epoch{
StartBlock: new(big.Int).SetUint64(epoch.FirstBlockInEpoch),
Expand Down
4 changes: 2 additions & 2 deletions consensus/polybft/contracts_initializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func getInitChildValidatorSetInput(polyBFTConfig PolyBFTConfig) ([]byte, error)
apiValidators[i] = validatorData
}

params := &contractsapi.InitializeChildValidatorSetFunction{
params := &contractsapi.InitializeChildValidatorSetFn{
Init: &contractsapi.InitStruct{
EpochReward: new(big.Int).SetUint64(polyBFTConfig.EpochReward),
MinStake: big.NewInt(minStake),
Expand All @@ -62,7 +62,7 @@ func getInitChildERC20PredicateInput(config *BridgeConfig) ([]byte, error) {
rootERC20Addr = config.RootNativeERC20Addr
}

params := &contractsapi.InitializeChildERC20PredicateFunction{
params := &contractsapi.InitializeChildERC20PredicateFn{
NewL2StateSender: contracts.L2StateSenderContract,
NewStateReceiver: contracts.StateReceiverContract,
NewRootERC20Predicate: rootERC20PredicateAddr,
Expand Down
12 changes: 2 additions & 10 deletions consensus/polybft/contractsapi/bindings-gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
const (
abiTypeNameFormat = "var %sABIType = abi.MustNewType(\"%s\")"
eventNameFormat = "%sEvent"
functionNameFormat = "%sFunction"
functionNameFormat = "%sFn"
)

type generatedData struct {
Expand Down Expand Up @@ -358,15 +358,7 @@ func ({{.Sig}} *{{.TName}}) ParseLog(log *ethgo.Log) error {

// generateFunction generates code for smart contract function and its parameters
func generateFunction(generatedData *generatedData, contractName string, method *abi.Method) error {
methodName := method.Name
if methodName == "initialize" {
// most of the contracts have initialize function, which differ in params
// so make them unique somehow
methodName = strings.Title(methodName + contractName)
}

methodName = fmt.Sprintf(functionNameFormat, methodName)

methodName := fmt.Sprintf(functionNameFormat, strings.Title(method.Name+contractName))
res := []string{}

_, err := generateType(generatedData, methodName, method.Inputs, &res)
Expand Down
Loading

0 comments on commit dbbc230

Please sign in to comment.