mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-11 22:43:47 +00:00
accounts/abi/bind: simplifications
This commit is contained in:
parent
2949b1a11d
commit
3436fb984e
2 changed files with 17 additions and 28 deletions
|
|
@ -3,10 +3,11 @@ package bind
|
||||||
import (
|
import (
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/ethereum/go-ethereum/common"
|
|
||||||
"github.com/ethereum/go-ethereum/core/types"
|
|
||||||
"maps"
|
"maps"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/ethereum/go-ethereum/common"
|
||||||
|
"github.com/ethereum/go-ethereum/core/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
// DeploymentParams represents parameters needed to deploy a
|
// DeploymentParams represents parameters needed to deploy a
|
||||||
|
|
@ -54,18 +55,24 @@ type DeployFn func(input, deployer []byte) (common.Address, *types.Transaction,
|
||||||
// depTreeDeployer is responsible for taking a dependency, deploying-and-linking its components in the proper
|
// depTreeDeployer is responsible for taking a dependency, deploying-and-linking its components in the proper
|
||||||
// order. A depTreeDeployer cannot be used after calling LinkAndDeploy other than to retrieve the deployment result.
|
// order. A depTreeDeployer cannot be used after calling LinkAndDeploy other than to retrieve the deployment result.
|
||||||
type depTreeDeployer struct {
|
type depTreeDeployer struct {
|
||||||
overrideAddrs map[string]common.Address
|
|
||||||
deployedAddrs map[string]common.Address
|
deployedAddrs map[string]common.Address
|
||||||
deployerTxs map[string]*types.Transaction
|
deployerTxs map[string]*types.Transaction
|
||||||
input map[string][]byte // map of the root contract pattern to the constructor input (if there is any)
|
input map[string][]byte // map of the root contract pattern to the constructor input (if there is any)
|
||||||
deploy DeployFn
|
deploy DeployFn
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func newDepTreeDeployer(overrides map[string]common.Address, deploy DeployFn) *depTreeDeployer {
|
||||||
|
return &depTreeDeployer{
|
||||||
|
deploy: deploy,
|
||||||
|
deployedAddrs: maps.Clone(overrides),
|
||||||
|
deployerTxs: make(map[string]*types.Transaction)}
|
||||||
|
}
|
||||||
|
|
||||||
// linkAndDeploy recursively deploys a contract and its dependencies: starting by linking/deploying its dependencies.
|
// linkAndDeploy recursively deploys a contract and its dependencies: starting by linking/deploying its dependencies.
|
||||||
// The deployment result (deploy addresses/txs or an error) is stored in the depTreeDeployer object.
|
// The deployment result (deploy addresses/txs or an error) is stored in the depTreeDeployer object.
|
||||||
func (d *depTreeDeployer) linkAndDeploy(metadata *MetaData) error {
|
func (d *depTreeDeployer) linkAndDeploy(metadata *MetaData) error {
|
||||||
// don't deploy contracts specified as overrides. don't deploy their dependencies.
|
// Don't deploy already deployed contracts
|
||||||
if _, ok := d.overrideAddrs[metadata.Pattern]; ok {
|
if _, ok := d.deployedAddrs[metadata.Pattern]; ok {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
// if this contract/library depends on other libraries deploy them (and their dependencies) first
|
// if this contract/library depends on other libraries deploy them (and their dependencies) first
|
||||||
|
|
@ -78,10 +85,7 @@ func (d *depTreeDeployer) linkAndDeploy(metadata *MetaData) error {
|
||||||
// a deployer bytecode for this contract.
|
// a deployer bytecode for this contract.
|
||||||
deployerCode := metadata.Bin
|
deployerCode := metadata.Bin
|
||||||
for _, dep := range metadata.Deps {
|
for _, dep := range metadata.Deps {
|
||||||
linkAddr, ok := d.deployedAddrs[dep.Pattern]
|
linkAddr, _ := d.deployedAddrs[dep.Pattern]
|
||||||
if !ok {
|
|
||||||
linkAddr = d.overrideAddrs[dep.Pattern]
|
|
||||||
}
|
|
||||||
deployerCode = strings.ReplaceAll(deployerCode, "__$"+dep.Pattern+"$__", strings.ToLower(linkAddr.String()[2:]))
|
deployerCode = strings.ReplaceAll(deployerCode, "__$"+dep.Pattern+"$__", strings.ToLower(linkAddr.String()[2:]))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -108,33 +112,18 @@ func (d *depTreeDeployer) result() *DeploymentResult {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func newDepTreeDeployer(overrides map[string]common.Address, deploy DeployFn) *depTreeDeployer {
|
|
||||||
return &depTreeDeployer{
|
|
||||||
deploy: deploy,
|
|
||||||
overrideAddrs: overrides,
|
|
||||||
deployedAddrs: make(map[string]common.Address),
|
|
||||||
deployerTxs: make(map[string]*types.Transaction)}
|
|
||||||
}
|
|
||||||
|
|
||||||
// LinkAndDeploy deploys a specified set of contracts and their dependent
|
// LinkAndDeploy deploys a specified set of contracts and their dependent
|
||||||
// libraries. If an error occurs, only contracts which were successfully
|
// libraries. If an error occurs, only contracts which were successfully
|
||||||
// deployed are returned in the result.
|
// deployed are returned in the result.
|
||||||
func LinkAndDeploy(deployParams *DeploymentParams, deploy DeployFn) (res *DeploymentResult, err error) {
|
func LinkAndDeploy(deployParams *DeploymentParams, deploy DeployFn) (res *DeploymentResult, err error) {
|
||||||
accumRes := &DeploymentResult{
|
|
||||||
Txs: make(map[string]*types.Transaction),
|
|
||||||
Addrs: make(map[string]common.Address),
|
|
||||||
}
|
|
||||||
deployer := newDepTreeDeployer(deployParams.overrides, deploy)
|
deployer := newDepTreeDeployer(deployParams.overrides, deploy)
|
||||||
for _, contract := range deployParams.contracts {
|
for _, contract := range deployParams.contracts {
|
||||||
if deployParams.inputs != nil {
|
if deployParams.inputs != nil {
|
||||||
deployer.input = map[string][]byte{contract.Pattern: deployParams.inputs[contract.Pattern]}
|
deployer.input = map[string][]byte{contract.Pattern: deployParams.inputs[contract.Pattern]}
|
||||||
}
|
}
|
||||||
err := deployer.linkAndDeploy(contract)
|
if err := deployer.linkAndDeploy(contract); err != nil {
|
||||||
res := deployer.result()
|
return deployer.result(), err
|
||||||
accumRes.Accumulate(res)
|
|
||||||
if err != nil {
|
|
||||||
return accumRes, err
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return accumRes, nil
|
return deployer.result(), nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -193,7 +193,7 @@ func testLinkCase(t *testing.T, tcInput linkTestCaseInput) {
|
||||||
t.Fatalf("got error from LinkAndDeploy: %v\n", err)
|
t.Fatalf("got error from LinkAndDeploy: %v\n", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(res.Addrs) != len(tcInput.expectDeployed) {
|
if len(res.Txs) != len(tcInput.expectDeployed) {
|
||||||
t.Fatalf("got %d deployed contracts. expected %d.\n", len(res.Addrs), len(tcInput.expectDeployed))
|
t.Fatalf("got %d deployed contracts. expected %d.\n", len(res.Addrs), len(tcInput.expectDeployed))
|
||||||
}
|
}
|
||||||
for contract, _ := range tcInput.expectDeployed {
|
for contract, _ := range tcInput.expectDeployed {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue