Skip to content

Commit 9f1e9f2

Browse files
authored
Merge pull request #523 from onflow/feature/deploy-update-smarter
Feature/deploy update smarter
2 parents 9b2997f + 1826a78 commit 9f1e9f2

File tree

5 files changed

+45
-10
lines changed

5 files changed

+45
-10
lines changed

docs/deploy-project-contracts.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,8 @@ in the `admin-account` account in `accounts` field of the configuration, the res
180180
- Valid inputs: `true`, `false`
181181
- Default: `false`
182182

183-
Indicate whether to overwrite and upgrade existing contracts.
184-
185-
⚠️ _Warning: contract upgrades are a dangerous experimental feature._
183+
Indicate whether to overwrite and upgrade existing contracts. Only contracts with difference with existing contracts
184+
will be overwritten.
186185

187186
### Host
188187

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ require (
77
github.com/getsentry/sentry-go v0.13.0
88
github.com/go-git/go-git/v5 v5.4.2
99
github.com/gosuri/uilive v0.0.4
10+
github.com/hexops/gotextdiff v1.0.3
1011
github.com/joho/godotenv v1.4.0
1112
github.com/manifoldco/promptui v0.9.0
1213
github.com/onflow/cadence v0.24.3

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,8 @@ github.com/hashicorp/memberlist v0.3.0/go.mod h1:MS2lj3INKhZjWNqd3N0m3J+Jxf3DAOn
656656
github.com/hashicorp/serf v0.8.2/go.mod h1:6hOLApaqBFA1NXqRQAsxw9QxuDEvNxSQRwA/JwenrHc=
657657
github.com/hashicorp/serf v0.9.5/go.mod h1:UWDWwZeL5cuWDJdl0C6wrvrUwEqtQ4ZKBKKENpqIUyk=
658658
github.com/hashicorp/serf v0.9.6/go.mod h1:TXZNMjZQijwlDvp+r0b63xZ45H7JmCmgg4gpTwn9UV4=
659+
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
660+
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
659661
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
660662
github.com/hudl/fargo v1.3.0/go.mod h1:y3CKSmjA+wD2gak7sUSXTAoopbhU08POFhmITJgmKTg=
661663
github.com/huin/goupnp v0.0.0-20161224104101-679507af18f3/go.mod h1:MZ2ZmwcBpvOoJ22IJsc7va19ZwoheaBk43rKg12SKag=

pkg/flowkit/contracts/contracts.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ func (c *Contract) Name() string {
8383
return c.name
8484
}
8585

86+
func (c *Contract) Source() string {
87+
return c.source
88+
}
89+
8690
func (c *Contract) Code() string {
8791
return c.code
8892
}

pkg/flowkit/services/project.go

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package services
2020

2121
import (
22+
"bytes"
2223
"fmt"
2324
"strings"
2425

@@ -155,6 +156,7 @@ func (p *Project) Deploy(network string, update bool) ([]*contracts.Contract, er
155156
defer p.logger.StopProgress()
156157

157158
deployErr := false
159+
numOfUpdates := 0
158160
for _, contract := range orderedContracts {
159161
block, err := p.gateway.GetLatestBlock()
160162
if err != nil {
@@ -184,7 +186,9 @@ func (p *Project) Deploy(network string, update bool) ([]*contracts.Contract, er
184186
return nil, err
185187
}
186188
// check if contract exists on account
187-
_, exists := targetAccountInfo.Contracts[contract.Name()]
189+
existingContract, exists := targetAccountInfo.Contracts[contract.Name()]
190+
noDiffInContract := bytes.Equal([]byte(contract.Code()), existingContract)
191+
188192
if exists && !update {
189193
p.logger.Error(fmt.Sprintf(
190194
"contract %s is already deployed to this account. Use the --update flag to force update",
@@ -200,10 +204,19 @@ func (p *Project) Deploy(network string, update bool) ([]*contracts.Contract, er
200204
deployErr = true
201205
continue
202206
} else if exists {
207+
//only update contract if there is diff
208+
if noDiffInContract {
209+
p.logger.Info(fmt.Sprintf(
210+
"no diff found in %s, skipping update",
211+
contract.Name(),
212+
))
213+
continue
214+
}
203215
tx, err = flowkit.NewUpdateAccountContractTransaction(targetAccount, contract.Name(), contract.TranspiledCode())
204216
if err != nil {
205217
return nil, err
206218
}
219+
numOfUpdates++
207220
}
208221

209222
tx.SetBlockReference(block)
@@ -246,25 +259,41 @@ func (p *Project) Deploy(network string, update bool) ([]*contracts.Contract, er
246259
if result.Error != nil {
247260
deployErr = true
248261
p.logger.StopProgress()
249-
p.logger.Error(fmt.Sprintf(
250-
"Error deploying %s: (%s)\n",
251-
output.Red(contract.Name()),
252-
result.Error.Error(),
253-
))
262+
if exists && update {
263+
p.logger.Error(fmt.Sprintf(
264+
"Error updating %s: (%s)\n",
265+
output.Red(contract.Name()),
266+
result.Error.Error(),
267+
))
268+
} else {
269+
p.logger.Error(fmt.Sprintf(
270+
"Error deploying %s: (%s)\n",
271+
output.Red(contract.Name()),
272+
result.Error.Error(),
273+
))
274+
}
254275
}
255276

256277
if result.Error == nil && !deployErr {
278+
changeStatus := ""
279+
if exists && update {
280+
changeStatus = "(update)"
281+
}
257282
p.logger.StopProgress()
258283
p.logger.Info(fmt.Sprintf(
259-
"%s -> 0x%s (%s)\n",
284+
"%s -> 0x%s (%s) %s\n",
260285
output.Green(contract.Name()),
261286
contract.Target(),
262287
sentTx.ID().String(),
288+
changeStatus,
263289
))
264290
}
265291
}
266292

267293
if !deployErr {
294+
if update && numOfUpdates > 0 {
295+
p.logger.Info(fmt.Sprintf("%d contracts updated successfully", numOfUpdates))
296+
}
268297
p.logger.Info(fmt.Sprintf("\n%s All contracts deployed successfully", output.SuccessEmoji()))
269298
} else {
270299
err = fmt.Errorf("failed to deploy all contracts")

0 commit comments

Comments
 (0)