keep-network / keep-common

Common libraries and tools used across Keep repositories
MIT License
5 stars 14 forks source link

Contract binding generator: handle conflicting struct declarations #117

Open pdyraga opened 1 year ago

pdyraga commented 1 year ago

May depend on #45

See https://github.com/keep-network/keep-core/pull/3630

after_abi_hook in keep-core renames structures that were incorrectly re-declared in the same Go package given this bug: https://github.com/ethereum/go-ethereum/issues/24627.

The generated tbtc/gen/abi/Bridge.go, tbtc/gen/abi/WalletCoordinator.go, and tbtc/gen/abi/MaintainerProxy.go declare BitcoinTxInfo struct. To make the Go compiler happy, we rename BitcoinTxInfo to BitcoinTxInfo2 and BitcoinTxInfo3.

Those Go files are generated with @go run github.com/ethereum/go-ethereum/cmd/abigen.

But this is not enough! If we try to build the project with just this hook, we will encounter errors like:

pkg/chain/ethereum/tbtc/gen/contract/MaintainerProxy.go:730:3: cannot use arg_mainUtxo (variable of type "[github.com/keep-network/keep-core/pkg/chain/ethereum/tbtc/gen/abi](http://github.com/keep-network/keep-core/pkg/chain/ethereum/tbtc/gen/abi)".BitcoinTxUTXO) as type "[github.com/keep-network/keep-core/pkg/chain/ethereum/tbtc/gen/abi](http://github.com/keep-network/keep-core/pkg/chain/ethereum/tbtc/gen/abi)".BitcoinTxUTXO2 in argument to mp.contract.NotifyMovingFundsBelowDust

Looking at the generated code at the problematic line:

transaction, err := mp.contract.NotifyMovingFundsBelowDust(
  transactorOptions,
  arg_walletPubKeyHash,
  arg_mainUtxo,
)

mp.contract.NotifyFundsBelowDust is declared in the tbtc/gen/abi/MaintainerProxy.go and expects BitcoinTxUTXO2.

arg_mainUtxo from tbtc/gen/contract/MaintainerProxy.go has an incorrect type though: arg_mainUtxo abi.BitcoinTxUTXO,

Just like tbtc/gen/abi/MaintainerProxy.go this file is generated based on tbtc/gen/abi/MaintainerProxy.abi and in the *.abi file we don't have the rename performed by after_abi_hook.

The compilation error was solved with the after_contract_hook which is partially a result of https://github.com/ethereum/go-ethereum/issues/24627 and partially how our Go contract bindings work. The fix from https://github.com/ethereum/go-ethereum/pull/24924 will change the generated tbtc/gen/abi/*.go files but we'll have to adjust the @go run github.com/keep-network/keep-common/tools/generators/ethereum logic to follow the same naming rules as the *.abi file will remain unchanged.