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

add contract migration check on all calls #1547

Merged
merged 47 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
1d60627
remove version check in 1.0
ianthpun Apr 26, 2024
7d3d289
add is_validated
ianthpun May 6, 2024
a2d3035
udpate
ianthpun May 6, 2024
63d7a04
update
ianthpun May 6, 2024
8c088cf
update
ianthpun May 6, 2024
4b97f0f
add missing contracts
ianthpun May 7, 2024
bce8af0
update
ianthpun May 7, 2024
6d8a8d3
resolve comments
ianthpun May 7, 2024
fe3475d
some cleanup
ianthpun May 7, 2024
13d1582
fix slices
ianthpun May 7, 2024
71b47a9
linter
ianthpun May 7, 2024
1d5fd8e
update test
ianthpun May 7, 2024
aaa6383
update
ianthpun May 8, 2024
63af7e6
update
ianthpun May 8, 2024
8a16b1b
update
ianthpun May 8, 2024
1ce938b
move to util
ianthpun May 9, 2024
682c562
update
ianthpun May 9, 2024
427d845
update
ianthpun May 9, 2024
7a740d4
update
ianthpun May 9, 2024
0e92418
udpate
ianthpun May 9, 2024
339fb21
udpate
ianthpun May 9, 2024
49fd909
Merge branch 'feature/stable-cadence' into ianthpun/validation-check
ianthpun May 9, 2024
0ee2665
regenerate
ianthpun May 9, 2024
87af46c
renegrate mocks
ianthpun May 9, 2024
6e0170d
impoirt error
ianthpun May 9, 2024
e394a79
udpate
ianthpun May 9, 2024
805e3a0
update
ianthpun May 9, 2024
bedd97b
formatter
ianthpun May 9, 2024
f607271
test fix
ianthpun May 9, 2024
7796bac
fix test
ianthpun May 9, 2024
93e569f
update
ianthpun May 10, 2024
44d166d
update
ianthpun May 10, 2024
b518842
update
ianthpun May 10, 2024
87cb8d0
update
ianthpun May 10, 2024
37b40f1
update
ianthpun May 13, 2024
a49a609
fix skip
ianthpun May 13, 2024
751ebbc
update
ianthpun May 13, 2024
3a34a57
update
ianthpun May 14, 2024
a10057f
udpated with link check
ianthpun May 14, 2024
026e718
update with better wording
ianthpun May 14, 2024
21c87fb
update wording
ianthpun May 14, 2024
25e164b
format
ianthpun May 15, 2024
f923a07
update state check
ianthpun May 15, 2024
765164b
update
ianthpun May 16, 2024
a5f50ca
Update
ianthpun May 16, 2024
98d0884
Merge branch 'feature/stable-cadence' into ianthpun/validation-check
ianthpun May 16, 2024
67810c3
update copy
ianthpun May 16, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 0 additions & 32 deletions cmd/flow/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@
package main

import (
"fmt"
"os"
"strings"

"github.com/spf13/cobra"

"github.com/onflow/flow-cli/internal/accounts"
Expand Down Expand Up @@ -57,34 +53,6 @@ func main() {
var cmd = &cobra.Command{
Use: "flow",
TraverseChildren: true,
// Messaging for Cadence 1.0 upgrade
Copy link
Contributor Author

Choose a reason for hiding this comment

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

deleting this since this feature branch is literally 1.0

PersistentPreRun: func(cmd *cobra.Command, args []string) {
outputFlag, _ := cmd.Flags().GetString("output")
// If output is set to json, do not append any message
if outputFlag != "json" {

width := 80
url := "https://cadence-lang.org/docs/cadence_migration_guide"

// Function to center text within a given width
centerText := func(text string, width int) string {
space := (width - len(text)) / 2
if space < 0 {
space = 0
}
return fmt.Sprintf("%s%s%s", strings.Repeat(" ", space), text, strings.Repeat(" ", space))
}

fmt.Fprintln(os.Stderr, strings.Repeat("+", width))
fmt.Fprintln(os.Stderr, centerText("⚠ Upgrade to Cadence 1.0", width))
fmt.Fprintln(os.Stderr, centerText("The Crescendo network upgrade, including Cadence 1.0, is coming soon.", width))
fmt.Fprintln(os.Stderr, centerText("You may need to update your existing contracts to support this change.", width))
fmt.Fprintln(os.Stderr, centerText("Please visit our migration guide here:", width))
fmt.Fprintln(os.Stderr, centerText(url, width))
fmt.Fprintln(os.Stderr, strings.Repeat("+", width))

}
},
}

// quick commands
Expand Down
2 changes: 2 additions & 0 deletions internal/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ func (c Command) AddToParent(parent *cobra.Command) {
checkVersion(logger)
}

// command to validate contracts are sstill valid for migration
ianthpun marked this conversation as resolved.
Show resolved Hide resolved

// record command usage
wg := sync.WaitGroup{}
go UsageMetrics(c.Cmd, &wg)
Expand Down
122 changes: 80 additions & 42 deletions internal/migrate/is_validated.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"io"
"path"
"regexp"
"slices"
"strings"
"time"

Expand Down Expand Up @@ -111,6 +112,85 @@ func newValidator(repoService GitHubRepositoriesService, network config.Network,
}
}

func (v *validator) getContractUpdateStatuses(contractNames ...string) ([]contractUpdateStatus, *time.Time, error) {
var contractUpdateStatuses []contractUpdateStatus
err := checkNetwork(v.network)
if err != nil {
return nil, nil, err
}

v.logger.StartProgress("Checking if contracts has been validated")
ianthpun marked this conversation as resolved.
Show resolved Hide resolved
defer v.logger.StopProgress()

addressToContractName := make(map[string]string)
for _, contractName := range contractNames {
addr, err := getAddressByContractName(v.state, contractName, v.network)
if err != nil {
return nil, nil, err
}
addressToContractName[addr.HexWithPrefix()] = contractName
}

// Get last migration report
report, ts, err := v.getLatestMigrationReport(v.network)
if err != nil {
return nil, nil, err
}

// Get all the contract statuses from the report
statuses, err := v.fetchAndParseReport(report.GetPath())
if err != nil {
return nil, nil, err
}

// Get the validation result related to the contract
var foundAddresses []string
for _, s := range statuses {
if addressToContractName[s.AccountAddress] == s.ContractName {
contractUpdateStatuses = append(contractUpdateStatuses, s)
foundAddresses = append(foundAddresses, s.AccountAddress)
}
}

for addr, contractName := range addressToContractName {
var missingContractErr error
if !slices.Contains(foundAddresses, addr) {
builder := strings.Builder{}
builder.WriteString("some contracts do not appear to have been a part of any emulated migrations yet, please ensure that it has been staged & wait for the next emulated migration (last migration report was at ")
Copy link
Member

Choose a reason for hiding this comment

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

Capitalize "Some"

builder.WriteString(ts.Format(time.RFC3339))
builder.WriteString(")\n\n")
builder.WriteString(" - Account: ")
builder.WriteString(addr)
builder.WriteString("\n - Contract: ")
builder.WriteString(contractName)
builder.WriteString("\n - Network: ")
builder.WriteString(v.network.Name)

missingContractErr = fmt.Errorf(builder.String())
}

if missingContractErr != nil {
return nil, nil, missingContractErr
}
}

return contractUpdateStatuses, ts, nil
}

func (v *validator) getContractValidationStatus(network config.Network, address string, contractName string) (contractUpdateStatus, *time.Time, error) {
status, ts, err := v.getContractUpdateStatuses(contractName)
if err != nil {
return contractUpdateStatus{}, nil, err
}

if len(status) != 1 {
return contractUpdateStatus{}, nil, fmt.Errorf("failed to find contract in last migration report")
}

return status[0], ts, nil

}

func (v *validator) validate(contractName string) (validationResult, error) {
err := checkNetwork(v.network)
if err != nil {
Expand Down Expand Up @@ -144,48 +224,6 @@ func (v *validator) validate(contractName string) (validationResult, error) {
}, nil
}

func (v *validator) getContractValidationStatus(network config.Network, address string, contractName string) (contractUpdateStatus, *time.Time, error) {
// Get last migration report
report, timestamp, err := v.getLatestMigrationReport(network)
if err != nil {
return contractUpdateStatus{}, nil, err
}

// Get all the contract statuses from the report
statuses, err := v.fetchAndParseReport(report.GetPath())
if err != nil {
return contractUpdateStatus{}, nil, err
}

// Get the validation result related to the contract
var status *contractUpdateStatus
for _, s := range statuses {
if s.ContractName == contractName && s.AccountAddress == address {
status = &s
break
}
}

// Throw error if contract was not part of the last migration
if status == nil {
builder := strings.Builder{}
builder.WriteString("the contract does not appear to have been a part of any emulated migrations yet, please ensure that it has been staged & wait for the next emulated migration (last migration report was at ")
builder.WriteString(timestamp.Format(time.RFC3339))
builder.WriteString(")\n\n")

builder.WriteString(" - Account: ")
builder.WriteString(address)
builder.WriteString("\n - Contract: ")
builder.WriteString(contractName)
builder.WriteString("\n - Network: ")
builder.WriteString(network.Name)

return contractUpdateStatus{}, nil, fmt.Errorf(builder.String())
}

return *status, timestamp, nil
}

func (v *validator) getLatestMigrationReport(network config.Network) (*github.RepositoryContent, *time.Time, error) {
// Get the content of the migration reports folder
_, folderContent, _, err := v.repoService.GetContents(
Expand Down
Loading