Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MaintainerProxy bindings #3630

Merged
merged 3 commits into from
Jun 16, 2023
Merged

MaintainerProxy bindings #3630

merged 3 commits into from
Jun 16, 2023

Conversation

tomaszslabon
Copy link
Contributor

@tomaszslabon tomaszslabon commented Jun 13, 2023

This PR adds binding generation for the MaintainerProxy contract.
The bindings were generated using the make mainnet command.

@tomaszslabon tomaszslabon requested review from pdyraga, nkuba and lukasz-zimnoch and removed request for nkuba June 14, 2023 10:11
@tomaszslabon tomaszslabon marked this pull request as ready for review June 14, 2023 10:11
@tomaszslabon tomaszslabon self-assigned this Jun 14, 2023
Comment on lines +35 to +46
define after_contract_hook
$(eval type := $(1))
$(if $(filter $(type),MaintainerProxy),$(call fix_maintainer_proxy_contract_collision))
endef
define fix_maintainer_proxy_contract_collision
@perl -pi -e s,BitcoinTxUTXO,BitcoinTxUTXO2,g ./contract/MaintainerProxy.go
@perl -pi -e s,BitcoinTxProof,BitcoinTxProof2,g ./contract/MaintainerProxy.go
@perl -pi -e s,BitcoinTxInfo,BitcoinTxInfo3,g ./contract/MaintainerProxy.go
@perl -pi -e s,BitcoinTxUTXO,BitcoinTxUTXO2,g ./cmd/MaintainerProxy.go
@perl -pi -e s,BitcoinTxProof,BitcoinTxProof2,g ./cmd/MaintainerProxy.go
@perl -pi -e s,BitcoinTxInfo,BitcoinTxInfo3,g ./cmd/MaintainerProxy.go
endef
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand why we need this piece. Can you elaborate, please?

Contract bindings are generated based on the ABI. If we fixed the ABI in after_abi_hook, why the additional work?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was about to ask the same question 😀

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I comment this function and run make mainnet, I'm getting compilation errors like this:

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".BitcoinTxUTXO) as type "github.com/keep-network/keep-core/pkg/chain/ethereum/tbtc/gen/abi".BitcoinTxUTXO2 in argument to mp.contract.NotifyMovingFundsBelowDust

It seems the generated contract bindings still contain the wrong type (BitcoinTxUTXO instead of BitcoinTxUTXO2):

func (mp *MaintainerProxy) NotifyMovingFundsBelowDust(
	arg_walletPubKeyHash [20]byte,
	arg_mainUtxo abi.BitcoinTxUTXO,

	transactionOptions ...chainutil.TransactionOptions,
) (*types.Transaction, error) {
....
}

Copy link
Contributor Author

@tomaszslabon tomaszslabon Jun 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Contract bindings are generated based on the ABI. If we fixed the ABI in after_abi_hook, why the additional work?

In after_abi_hook we change *.go files.
It seems the contract bindings are generated based on *.abi files.
The $< argument in this invocation means the first prerequisite will be used:

contract/%.go cmd/%.go: abi/%.abi abi/%.go _address/% ${artifacts_dir}/%.json
	$(info $* - generating Keep bindings)
	@go run github.com/keep-network/keep-common/tools/generators/ethereum $< contract/$*.go cmd/$*.go

which will be MaintainerProxy.abi and not MaintainerProxy.go.

Therefore, we either leave the code as is or we remove it and change MaintainerProxy.abi instead.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me re-check my understanding of the issue.

after_abi_hook renames structures that were incorrectly re-declared in the same Go package given this bug: ethereum/go-ethereum#24627.

This is the version on main:

define fix_wallet_coordinator_collision
@perl -pi -e s,BitcoinTxInfo,BitcoinTxInfo2,g ./abi/WalletCoordinator.go
endef

My understanding of the bug, is that both the generated tbtc/gen/abi/Bridge.go and tbtc/gen/abi/WalletCoordinator.go declares BitcoinTxInfo struct. To make the Go compiler happy, we rename BitcoinTxInfo to BitcoinTxInfo2 in tbtc/gen/abi/WalletCoordinator.go.

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

Now, we add another contract that also uses BitcoinTx.Info in Solidity so it will have BitcoinTxInfo re-declared in the Go code given the issue from ethereum/go-ethereum#24627. We do the same trick as in main. We extend the after_abi_hook and do some renames:

define fix_wallet_coordinator_collision
@perl -pi -e s,BitcoinTxInfo,BitcoinTxInfo2,g ./abi/WalletCoordinator.go
endef
define fix_maintainer_proxy_collision
@perl -pi -e s,BitcoinTxUTXO,BitcoinTxUTXO2,g ./abi/MaintainerProxy.go
@perl -pi -e s,BitcoinTxProof,BitcoinTxProof2,g ./abi/MaintainerProxy.go
@perl -pi -e s,BitcoinTxInfo,BitcoinTxInfo3,g ./abi/MaintainerProxy.go
endef

This makes sense to me up to this point.

But this is not enough! If we try to build the project, 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 MaintainerProxy.go has an incorrect type though: arg_mainUtxo abi.BitcoinTxUTXO,

OK, that makes sense. 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.

In other words, the after_contract_hook is partially a result of ethereum/go-ethereum#24627 and partially how our Go contract bindings work. The fix from ethereum/go-ethereum#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.

Does it make sense?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pdyraga Yes, it makes sense.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense to me as well!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tomaszslabon Can you please add a comment to after_contract_hook referencing keep-network/keep-common#117 ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 800fdda.

@pdyraga pdyraga enabled auto-merge June 16, 2023 09:38
@pdyraga pdyraga merged commit 266b7a3 into main Jun 16, 2023
@pdyraga pdyraga deleted the maintainer-proxy-bindings branch June 16, 2023 10:51
pdyraga added a commit that referenced this pull request Jun 16, 2023
#Depends on #3630.
This PR adds a handle to the MaintainerProxy contract in the Tbtc chain.
@pdyraga pdyraga added this to the v2.0.0-m4 milestone Jul 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants