From d3dfbf9bfaeba2cf11f8c08eabdd95071c91cfbf Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Tue, 28 Jan 2025 00:16:42 +0100 Subject: [PATCH] accounts/abi/bind/v2: export DeploymentParams fields The constructor function doesn't make sense. The struct has one required and two optional fields, and we may want to add more configuration later. With the constructor function, all parameters always need to be given, even the optional ones. And we can't extend the function without breaking the API. So it's better to use require users to write the literal. --- accounts/abi/bind/v2/dep_tree.go | 26 ++++++++++---------------- accounts/abi/bind/v2/dep_tree_test.go | 5 ++++- accounts/abi/bind/v2/lib_test.go | 22 +++++++++++++++------- 3 files changed, 29 insertions(+), 24 deletions(-) diff --git a/accounts/abi/bind/v2/dep_tree.go b/accounts/abi/bind/v2/dep_tree.go index 98462a79b2..aa74c3d858 100644 --- a/accounts/abi/bind/v2/dep_tree.go +++ b/accounts/abi/bind/v2/dep_tree.go @@ -14,22 +14,15 @@ import ( // set of contracts. It takes an optional override // list to specify contracts/libraries that have already been deployed on-chain. type DeploymentParams struct { - contracts []*MetaData - // map of solidity library pattern to constructor input. - inputs map[string][]byte + Contracts []*MetaData + + // Map of solidity library pattern to constructor input. + Inputs map[string][]byte + // Overrides is an optional map of pattern to deployment address. // Contracts/libraries that refer to dependencies in the override // set are linked to the provided address (an already-deployed contract). - overrides map[string]common.Address -} - -// NewDeploymentParams instantiates a DeploymentParams instance. -func NewDeploymentParams(contracts []*MetaData, inputs map[string][]byte, overrides map[string]common.Address) *DeploymentParams { - return &DeploymentParams{ - contracts, - inputs, - overrides, - } + Overrides map[string]common.Address } // DeploymentResult encapsulates information about the result of the deployment @@ -38,6 +31,7 @@ func NewDeploymentParams(contracts []*MetaData, inputs map[string][]byte, overri type DeploymentResult struct { // map of contract library pattern -> deploy transaction Txs map[string]*types.Transaction + // map of contract library pattern -> deployed address Addrs map[string]common.Address } @@ -56,11 +50,11 @@ type depTreeDeployer struct { } func newDepTreeDeployer(deployParams *DeploymentParams, deployFn DeployFn) *depTreeDeployer { - deployedAddrs := maps.Clone(deployParams.overrides) + deployedAddrs := maps.Clone(deployParams.Overrides) if deployedAddrs == nil { deployedAddrs = make(map[string]common.Address) } - inputs := deployParams.inputs + inputs := deployParams.Inputs if inputs == nil { inputs = make(map[string][]byte) } @@ -123,7 +117,7 @@ func (d *depTreeDeployer) result() *DeploymentResult { // deployed are returned in the result. func LinkAndDeploy(deployParams *DeploymentParams, deploy DeployFn) (res *DeploymentResult, err error) { deployer := newDepTreeDeployer(deployParams, deploy) - for _, contract := range deployParams.contracts { + for _, contract := range deployParams.Contracts { if _, err := deployer.linkAndDeploy(contract); err != nil { return deployer.result(), err } diff --git a/accounts/abi/bind/v2/dep_tree_test.go b/accounts/abi/bind/v2/dep_tree_test.go index 3d0dd0fe5e..dc60fae81c 100644 --- a/accounts/abi/bind/v2/dep_tree_test.go +++ b/accounts/abi/bind/v2/dep_tree_test.go @@ -193,7 +193,10 @@ func testLinkCase(tcInput linkTestCaseInput) error { overrides[pattern] = override } - deployParams := NewDeploymentParams(contractsList, nil, overrides) + deployParams := &DeploymentParams{ + Contracts: contractsList, + Overrides: overrides, + } res, err := LinkAndDeploy(deployParams, mockDeploy) if err != nil { return err diff --git a/accounts/abi/bind/v2/lib_test.go b/accounts/abi/bind/v2/lib_test.go index 27412c2a34..0da77d2da3 100644 --- a/accounts/abi/bind/v2/lib_test.go +++ b/accounts/abi/bind/v2/lib_test.go @@ -108,8 +108,10 @@ func TestDeploymentLibraries(t *testing.T) { c := nested_libraries.NewC1() constructorInput := c.PackConstructor(big.NewInt(42), big.NewInt(1)) - deploymentParams := bind.NewDeploymentParams([]*bind.MetaData{&nested_libraries.C1MetaData}, map[string][]byte{nested_libraries.C1MetaData.Pattern: constructorInput}, nil) - + deploymentParams := &bind.DeploymentParams{ + Contracts: []*bind.MetaData{&nested_libraries.C1MetaData}, + Inputs: map[string][]byte{nested_libraries.C1MetaData.Pattern: constructorInput}, + } res, err := bind.LinkAndDeploy(deploymentParams, makeTestDeployer(opts, bindBackend)) if err != nil { t.Fatalf("err: %+v\n", err) @@ -149,8 +151,9 @@ func TestDeploymentWithOverrides(t *testing.T) { defer bindBackend.Backend.Close() // deploy all the library dependencies of our target contract, but not the target contract itself. - deploymentParams := bind.NewDeploymentParams(nested_libraries.C1MetaData.Deps, nil, nil) - + deploymentParams := &bind.DeploymentParams{ + Contracts: nested_libraries.C1MetaData.Deps, + } res, err := bind.LinkAndDeploy(deploymentParams, makeTestDeployer(opts, bindBackend)) if err != nil { t.Fatalf("err: %+v\n", err) @@ -170,8 +173,13 @@ func TestDeploymentWithOverrides(t *testing.T) { c := nested_libraries.NewC1() constructorInput := c.PackConstructor(big.NewInt(42), big.NewInt(1)) overrides := res.Addrs + // deploy the contract - deploymentParams = bind.NewDeploymentParams([]*bind.MetaData{&nested_libraries.C1MetaData}, map[string][]byte{nested_libraries.C1MetaData.Pattern: constructorInput}, overrides) + deploymentParams = &bind.DeploymentParams{ + Contracts: []*bind.MetaData{&nested_libraries.C1MetaData}, + Inputs: map[string][]byte{nested_libraries.C1MetaData.Pattern: constructorInput}, + Overrides: overrides, + } res, err = bind.LinkAndDeploy(deploymentParams, makeTestDeployer(opts, bindBackend)) if err != nil { t.Fatalf("err: %+v\n", err) @@ -208,7 +216,7 @@ func TestEvents(t *testing.T) { t.Fatalf("error setting up testing env: %v", err) } - deploymentParams := bind.NewDeploymentParams([]*bind.MetaData{&events.CMetaData}, nil, nil) + deploymentParams := &bind.DeploymentParams{Contracts: []*bind.MetaData{&events.CMetaData}} res, err := bind.LinkAndDeploy(deploymentParams, makeTestDeployer(txAuth, backend)) if err != nil { t.Fatalf("error deploying contract for testing: %v", err) @@ -307,7 +315,7 @@ func TestErrors(t *testing.T) { t.Fatalf("error setting up testing env: %v", err) } - deploymentParams := bind.NewDeploymentParams([]*bind.MetaData{&solc_errors.CMetaData}, nil, nil) + deploymentParams := &bind.DeploymentParams{Contracts: []*bind.MetaData{&solc_errors.CMetaData}} res, err := bind.LinkAndDeploy(deploymentParams, makeTestDeployer(txAuth, backend)) if err != nil { t.Fatalf("error deploying contract for testing: %v", err)