Skip to content

Commit 03d4a31

Browse files
committed
fix(cli): check if calculated addresses are available before deploying.
for: - PRT (dave consensus) - Selfhosted (application + authority) - application - authority
1 parent ce2dec4 commit 03d4a31

4 files changed

Lines changed: 81 additions & 4 deletions

File tree

pkg/ethutil/application.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,21 @@ func (me *ApplicationDeployment) Deploy(
7575
return zero, nil, fmt.Errorf("failed to instantiate contract: %v", err)
7676
}
7777

78+
// check if addresses are available (have no code)
79+
applicationAddress, err := factory.CalculateApplicationAddress(nil, me.Consensus, me.OwnerAddress, me.TemplateHash, me.DataAvailability, me.Salt)
80+
if err != nil {
81+
return zero, nil, err
82+
}
83+
84+
applicationCode, err := client.CodeAt(ctx, applicationAddress, nil)
85+
if err != nil {
86+
return zero, nil, err
87+
}
88+
if len(applicationCode) != 0 {
89+
return zero, nil, fmt.Errorf("application with address: %v already exists. Try a different salt.", applicationAddress)
90+
}
91+
92+
// deploy the contracts
7893
tx, err := factory.NewApplication(txOpts, me.Consensus, me.OwnerAddress, me.TemplateHash, me.DataAvailability, me.Salt)
7994
if err != nil {
8095
return zero, nil, fmt.Errorf("transaction failed: %v", err)

pkg/ethutil/authority.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,33 @@ func (me *AuthorityDeployment) String() string {
3434
return result
3535
}
3636

37-
func (me AuthorityDeployment) Deploy(
37+
func (me *AuthorityDeployment) Deploy(
3838
ctx context.Context,
3939
client *ethclient.Client,
4040
txOpts *bind.TransactOpts,
4141
) (common.Address, error) {
42-
contract, err := iauthorityfactory.NewIAuthorityFactory(me.FactoryAddress, client)
42+
zero := common.Address{}
43+
factory, err := iauthorityfactory.NewIAuthorityFactory(me.FactoryAddress, client)
4344
if err != nil {
4445
return common.Address{}, fmt.Errorf("failed to instantiate contract: %v", err)
4546
}
4647

47-
tx, err := contract.NewAuthority0(txOpts, me.OwnerAddress, big.NewInt(int64(me.EpochLength)), me.Salt)
48+
// check if addresses are available (have no code)
49+
authorityAddress, err := factory.CalculateAuthorityAddress(nil, me.OwnerAddress, new(big.Int).SetUint64(me.EpochLength), me.Salt)
50+
if err != nil {
51+
return zero, err
52+
}
53+
54+
authorityCode, err := client.CodeAt(ctx, authorityAddress, nil)
55+
if err != nil {
56+
return zero, err
57+
}
58+
if len(authorityCode) != 0 {
59+
return zero, fmt.Errorf("authority with address: %v already exists. Try a different salt.", authorityAddress)
60+
}
61+
62+
// deploy the contracts
63+
tx, err := factory.NewAuthority0(txOpts, me.OwnerAddress, new(big.Int).SetUint64(me.EpochLength), me.Salt)
4864
if err != nil {
4965
return common.Address{}, fmt.Errorf("failed to create new authority: %v", err)
5066
}
@@ -60,7 +76,7 @@ func (me AuthorityDeployment) Deploy(
6076

6177
// search for the matching event
6278
for _, vLog := range receipt.Logs {
63-
event, err := contract.ParseAuthorityCreated(*vLog)
79+
event, err := factory.ParseAuthorityCreated(*vLog)
6480
if err != nil {
6581
continue // Skip logs that don't match
6682
}

pkg/ethutil/prt.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,29 @@ func (me *PRTApplicationDeployment) deployPRT(
6161
if err != nil {
6262
return zero, zero, fmt.Errorf("failed to instantiate contract binding: %v", err)
6363
}
64+
65+
// check if addresses are available (have no code)
66+
addresses, err := factory.CalculateDaveAppAddress(nil, me.TemplateHash, me.Salt)
67+
if err != nil {
68+
return zero, zero, err
69+
}
70+
applicationCode, err := client.CodeAt(ctx, addresses.AppContractAddress, nil)
71+
if err != nil {
72+
return zero, zero, err
73+
}
74+
if len(applicationCode) != 0 {
75+
return zero, zero, fmt.Errorf("application with address: %v already exists. Try a different salt.", addresses.AppContractAddress)
76+
}
77+
78+
daveConsensusCode, err := client.CodeAt(ctx, addresses.DaveConsensusAddress, nil)
79+
if err != nil {
80+
return zero, zero, err
81+
}
82+
if len(daveConsensusCode) != 0 {
83+
return zero, zero, fmt.Errorf("dave consensus with address: %v already exists. Try a different salt.", addresses.DaveConsensusAddress)
84+
}
85+
86+
// deploy the contracts
6487
tx, err := factory.NewDaveApp(txOpts, me.TemplateHash, me.Salt)
6588
if err != nil {
6689
return zero, zero, fmt.Errorf("transaction failed: %v", err)

pkg/ethutil/selfhosted.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,29 @@ func (me *SelfhostedApplicationDeployment) Deploy(
8181
return zero, nil, err
8282
}
8383

84+
// check if addresses are available (have no code)
85+
applicationAddress, authorityAddress, err := factory.CalculateAddresses(nil, me.AuthorityOwnerAddress, new(big.Int).SetUint64(me.EpochLength), me.ApplicationOwnerAddress, me.TemplateHash, me.DataAvailability, me.Salt)
86+
if err != nil {
87+
return zero, nil, err
88+
}
89+
90+
applicationCode, err := client.CodeAt(ctx, applicationAddress, nil)
91+
if err != nil {
92+
return zero, nil, err
93+
}
94+
if len(applicationCode) != 0 {
95+
return zero, nil, fmt.Errorf("application with address: %v already exists. Try a different salt.", applicationAddress)
96+
}
97+
98+
authorityCode, err := client.CodeAt(ctx, authorityAddress, nil)
99+
if err != nil {
100+
return zero, nil, err
101+
}
102+
if len(authorityCode) != 0 {
103+
return zero, nil, fmt.Errorf("authority with address: %v already exists. Try a different salt.", authorityAddress)
104+
}
105+
106+
// deploy the contracts
84107
receipt, err := sendTransaction(
85108
ctx, client, txOpts, big.NewInt(0), GasLimit,
86109
func(txOpts *bind.TransactOpts) (*types.Transaction, error) {

0 commit comments

Comments
 (0)