From 0130123e94224711b1adeb1af0b49560f9100682 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastian=20M=C3=BCller?= Date: Mon, 24 Jun 2024 09:52:40 -0700 Subject: [PATCH 01/12] improve error message for invalid state --- .../migrations/migration_metrics_collector.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/cmd/util/ledger/migrations/migration_metrics_collector.go b/cmd/util/ledger/migrations/migration_metrics_collector.go index cb558538bed..b6b62841203 100644 --- a/cmd/util/ledger/migrations/migration_metrics_collector.go +++ b/cmd/util/ledger/migrations/migration_metrics_collector.go @@ -67,7 +67,7 @@ func (m *MetricsCollectingMigration) InitMigration( // If the program is available, that means the associated contracts is compatible with Cadence 1.0. // i.e: the contract is either migrated to be compatible with 1.0 or existing contract already compatible. - for _, program := range m.programs { + for location, program := range m.programs { var nestedDecls *ast.Members contract := program.Program.SoleContractDeclaration() @@ -80,7 +80,16 @@ func (m *MetricsCollectingMigration) InitMigration( } else { contractInterface := program.Program.SoleContractInterfaceDeclaration() if contractInterface == nil { - panic(errors.NewUnreachableError()) + declarations := program.Program.Declarations() + declarationKinds := make([]common.DeclarationKind, 0, len(declarations)) + for _, declaration := range declarations { + declarationKinds = append(declarationKinds, declaration.DeclarationKind()) + } + panic(errors.NewUnexpectedError( + "invalid program %s: expected a sole contract or contract interface, got %s", + location, + declarationKinds, + )) } nestedDecls = contractInterface.Members From db596e428c59232d6234d83df75bf8dfb9b59218 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastian=20M=C3=BCller?= Date: Mon, 24 Jun 2024 09:55:46 -0700 Subject: [PATCH 02/12] ignore empty contracts when checking all contracts --- cmd/util/ledger/migrations/cadence_test.go | 69 +++++++++++++++++++ .../migrations/contract_checking_migration.go | 5 ++ 2 files changed, 74 insertions(+) create mode 100644 cmd/util/ledger/migrations/cadence_test.go diff --git a/cmd/util/ledger/migrations/cadence_test.go b/cmd/util/ledger/migrations/cadence_test.go new file mode 100644 index 00000000000..7c6e02dccb7 --- /dev/null +++ b/cmd/util/ledger/migrations/cadence_test.go @@ -0,0 +1,69 @@ +package migrations + +import ( + "testing" + + "github.com/onflow/cadence/runtime/common" + "github.com/onflow/cadence/runtime/interpreter" + "github.com/rs/zerolog" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "github.com/onflow/flow-go/cmd/util/ledger/util/registers" + "github.com/onflow/flow-go/fvm/environment" + "github.com/onflow/flow-go/model/flow" +) + +func TestMigrateCadence1EmptyContract(t *testing.T) { + + t.Parallel() + + const chainID = flow.Testnet + + address, err := common.HexToAddress("0x4184b8bdf78db9eb") + require.NoError(t, err) + + const contractName = "FungibleToken" + + registersByAccount := registers.NewByAccount() + + err = registersByAccount.Set( + string(address[:]), + flow.ContractKey(contractName), + // some whitespace for testing purposes + []byte(" \t \n "), + ) + require.NoError(t, err) + + encodedContractNames, err := environment.EncodeContractNames([]string{contractName}) + require.NoError(t, err) + + err = registersByAccount.Set( + string(address[:]), + flow.ContractNamesKey, + encodedContractNames, + ) + require.NoError(t, err) + + programs := map[common.Location]*interpreter.Program{} + + rwf := &testReportWriterFactory{} + + // Run contract checking migration + + log := zerolog.Nop() + checkingMigration := NewContractCheckingMigration(log, rwf, chainID, false, programs) + + err = checkingMigration(registersByAccount) + require.NoError(t, err) + + reporter := rwf.reportWriters[contractCheckingReporterName] + assert.Empty(t, reporter.entries) + + // Initialize metrics collecting migration (used to run into unexpected error) + + metricsCollectingMigration := NewMetricsCollectingMigration(log, chainID, rwf, programs) + + err = metricsCollectingMigration.InitMigration(log, registersByAccount, 1) + require.NoError(t, err) +} diff --git a/cmd/util/ledger/migrations/contract_checking_migration.go b/cmd/util/ledger/migrations/contract_checking_migration.go index 3080d4bcb53..1442bdfb6c7 100644 --- a/cmd/util/ledger/migrations/contract_checking_migration.go +++ b/cmd/util/ledger/migrations/contract_checking_migration.go @@ -1,6 +1,7 @@ package migrations import ( + "bytes" "encoding/json" "fmt" "sort" @@ -78,6 +79,10 @@ func NewContractCheckingMigration( return err } + if len(bytes.TrimSpace(code)) == 0 { + continue + } + address := common.Address([]byte(owner)) location := common.AddressLocation{ Address: address, From d630d1b85b6c9b7fc7a3c5939aa345c26c3ea441 Mon Sep 17 00:00:00 2001 From: sjonpaulbrown Date: Mon, 24 Jun 2024 15:58:01 -0600 Subject: [PATCH 03/12] Update Flaky Test workflow to use new project, tables, and Workload Identity --- .github/workflows/flaky-test-monitor.yml | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/.github/workflows/flaky-test-monitor.yml b/.github/workflows/flaky-test-monitor.yml index e9d55f75164..0df5c5d75fb 100644 --- a/.github/workflows/flaky-test-monitor.yml +++ b/.github/workflows/flaky-test-monitor.yml @@ -8,9 +8,12 @@ on: push: paths: - '.github/workflows/flaky-test-monitor.yml' +permissions: + id-token: write + contents: read env: - BIGQUERY_DATASET: production_src_flow_test_metrics + BIGQUERY_DATASET: dev_src_flow_test_metrics BIGQUERY_TABLE: skipped_tests BIGQUERY_TABLE2: test_results GO_VERSION: "1.22" @@ -73,7 +76,8 @@ jobs: TEST_CATEGORY: unit uses: ./.github/workflows/actions/test-monitor-process-results with: - gcp_sa_key: ${{ secrets.GCP_SA_KEY }} + service_account: ${{ secrets.FLAKY_TEST_SERVICE_ACCOUNT }} + workload_identity_provider: ${{ secrets.FLAKY_TEST_WORKLOAD_IDENTITY_PROVIDER }} unit-test-modules: name: Unit Tests (Modules) @@ -115,7 +119,8 @@ jobs: TEST_CATEGORY: ${{ matrix.test_category }} uses: ./.github/workflows/actions/test-monitor-process-results with: - gcp_sa_key: ${{ secrets.GCP_SA_KEY }} + service_account: ${{ secrets.FLAKY_TEST_SERVICE_ACCOUNT }} + workload_identity_provider: ${{ secrets.FLAKY_TEST_WORKLOAD_IDENTITY_PROVIDER }} integration-test: name: Integration Tests @@ -175,4 +180,6 @@ jobs: TEST_CATEGORY: ${{ matrix.test_category }} uses: ./.github/workflows/actions/test-monitor-process-results with: - gcp_sa_key: ${{ secrets.GCP_SA_KEY }} + service_account: ${{ secrets.FLAKY_TEST_SERVICE_ACCOUNT }} + workload_identity_provider: ${{ secrets.FLAKY_TEST_WORKLOAD_IDENTITY_PROVIDER }} + From 627ed93c84d7976c8716a3628eb51c190e4d7ba0 Mon Sep 17 00:00:00 2001 From: sjonpaulbrown Date: Mon, 24 Jun 2024 15:58:23 -0600 Subject: [PATCH 04/12] Update composite action to enable uploads using Workload Identity --- .../test-monitor-process-results/action.yml | 23 +++++++++++++++---- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/.github/workflows/actions/test-monitor-process-results/action.yml b/.github/workflows/actions/test-monitor-process-results/action.yml index 08f1c16ab82..ea62e2f9c20 100644 --- a/.github/workflows/actions/test-monitor-process-results/action.yml +++ b/.github/workflows/actions/test-monitor-process-results/action.yml @@ -3,8 +3,11 @@ name: Test Monitor - Process Results description: Custom action that's used in multiple Flaky Test Monitor jobs to process test results and upload them to BigQuery inputs: - gcp_sa_key: - description: 'The GCP service account key for uploading to BigQuery' + service_account: + description: 'The GCP Service Account' + required: true + workload_identity_provider: + description: 'The GCP Workload Identity Provider' required: true runs: @@ -14,20 +17,30 @@ runs: id: commit_date run: echo "::set-output name=date::$(git show --no-patch --no-notes --pretty='%cI' $COMMIT_SHA)" shell: bash + - name: Get job run date id: job_run_date run: echo "::set-output name=date::$(TZ=":UTC" date -Iseconds)" shell: bash + - name: Process test results run: cat test-output | go run tools/test_monitor/level1/process_summary1_results.go env: JOB_STARTED: ${{ steps.job_run_date.outputs.date }} COMMIT_DATE: ${{ steps.commit_date.outputs.date }} shell: bash - - name: Set up Cloud SDK - uses: google-github-actions/setup-gcloud@v0 + + - name: Google auth + id: auth + uses: google-github-actions/auth@v2 with: - service_account_key: ${{ inputs.gcp_sa_key }} + service_account: ${{ inputs.service_account }} + token_format: 'access_token' + workload_identity_provider: ${{ inputs.workload_identity_provider }} + + - name: 'Set up Cloud SDK' + uses: 'google-github-actions/setup-gcloud@v2' + - name: Upload results to BigQuery (skipped tests) uses: nick-fields/retry@v2 with: From 16a2886fe6ebc397c4b30c468611358f51984422 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastian=20M=C3=BCller?= Date: Mon, 24 Jun 2024 15:00:35 -0700 Subject: [PATCH 05/12] add migration which cleans up contract names and contract code --- cmd/util/ledger/migrations/cadence.go | 10 + .../migrations/contract_cleanup_migration.go | 180 ++++++++++++++++++ .../contract_cleanup_migration_test.go | 117 ++++++++++++ 3 files changed, 307 insertions(+) create mode 100644 cmd/util/ledger/migrations/contract_cleanup_migration.go create mode 100644 cmd/util/ledger/migrations/contract_cleanup_migration_test.go diff --git a/cmd/util/ledger/migrations/cadence.go b/cmd/util/ledger/migrations/cadence.go index 80e3725a1ad..01262efa74a 100644 --- a/cmd/util/ledger/migrations/cadence.go +++ b/cmd/util/ledger/migrations/cadence.go @@ -236,6 +236,16 @@ func NewCadence1ValueMigrations( programs := make(map[common.Location]*interpreter.Program, 1000) migs = []NamedMigration{ + { + Name: "cleanup-contracts", + Migrate: NewAccountBasedMigration( + log, + opts.NWorker, + []AccountBasedMigration{ + NewContractCleanupMigration(), + }, + ), + }, { Name: "check-contracts", Migrate: NewContractCheckingMigration( diff --git a/cmd/util/ledger/migrations/contract_cleanup_migration.go b/cmd/util/ledger/migrations/contract_cleanup_migration.go new file mode 100644 index 00000000000..1173f70792e --- /dev/null +++ b/cmd/util/ledger/migrations/contract_cleanup_migration.go @@ -0,0 +1,180 @@ +package migrations + +import ( + "bytes" + "context" + "fmt" + "sort" + + "github.com/rs/zerolog" + + "github.com/onflow/cadence/runtime/common" + + "github.com/onflow/flow-go/cmd/util/ledger/util/registers" + "github.com/onflow/flow-go/fvm/environment" + "github.com/onflow/flow-go/model/flow" +) + +// ContractCleanupMigration normalized account's contract names and removes empty contracts. +type ContractCleanupMigration struct { + log zerolog.Logger +} + +var _ AccountBasedMigration = &ContractCleanupMigration{} + +func NewContractCleanupMigration() *ContractCleanupMigration { + return &ContractCleanupMigration{} +} + +func (d *ContractCleanupMigration) InitMigration( + log zerolog.Logger, + _ *registers.ByAccount, + _ int, +) error { + d.log = log. + With(). + Str("migration", "ContractCleanupMigration"). + Logger() + + return nil +} + +func (d *ContractCleanupMigration) MigrateAccount( + _ context.Context, + address common.Address, + accountRegisters *registers.AccountRegisters, +) error { + + // Normalize the account's contract names. + // This will deduplicate and sort the contract names. + + contractNames, err := d.normalizeContractNames(accountRegisters) + if err != nil { + return fmt.Errorf( + "failed to normalize contract names for %s: %w", + address.HexWithPrefix(), + err, + ) + } + + // Cleanup the code for each contract. + // If the contract code is empty, it will be removed. + + for _, contractName := range contractNames { + err = d.cleanupContractCode(accountRegisters, contractName) + if err != nil { + return fmt.Errorf( + "failed to cleanup contract code for %s: %w", + address.HexWithPrefix(), + err, + ) + } + } + + return nil +} + +// normalizeContractNames deduplicates and sorts the account's contract names. +func (d *ContractCleanupMigration) normalizeContractNames( + accountRegisters *registers.AccountRegisters, +) ( + []string, + error, +) { + owner := accountRegisters.Owner() + + encodedContractNames, err := accountRegisters.Get(owner, flow.ContractNamesKey) + if err != nil { + return nil, fmt.Errorf( + "failed to get contract names: %w", + err, + ) + } + + if len(encodedContractNames) == 0 { + return nil, nil + } + + contractNames, err := environment.DecodeContractNames(encodedContractNames) + if err != nil { + return nil, fmt.Errorf( + "failed to decode contract names: %w", + err, + ) + } + + contractNames = normalizeContractNames(contractNames) + + newEncodedContractNames, err := environment.EncodeContractNames(contractNames) + if err != nil { + return nil, fmt.Errorf( + "failed to encode contract names: %w", + err, + ) + } + + err = accountRegisters.Set(owner, flow.ContractNamesKey, newEncodedContractNames) + if err != nil { + return nil, fmt.Errorf( + "failed to set new contract names: %w", + err, + ) + } + + return contractNames, nil +} + +// normalizeContractNames deduplicates and sorts the contract names. +func normalizeContractNames(names []string) []string { + seen := make(map[string]struct{}, len(names)) + deduplicated := make([]string, 0, len(names)) + + for _, name := range names { + if _, ok := seen[name]; ok { + continue + } + + seen[name] = struct{}{} + deduplicated = append(deduplicated, name) + } + + sort.Strings(deduplicated) + + return deduplicated +} + +// cleanupContractCode removes the code for the contract if it is empty. +func (d *ContractCleanupMigration) cleanupContractCode( + accountRegisters *registers.AccountRegisters, + contractName string, +) error { + owner := accountRegisters.Owner() + + contractKey := flow.ContractKey(contractName) + + code, err := accountRegisters.Get(owner, contractKey) + if err != nil { + return fmt.Errorf( + "failed to get contract code for %s: %w", + contractName, + err, + ) + } + + if len(bytes.TrimSpace(code)) == 0 { + err = accountRegisters.Set(owner, contractKey, nil) + if err != nil { + return fmt.Errorf( + "failed to clear contract code for %s: %w", + contractName, + err, + ) + } + } + + return nil +} + +func (d *ContractCleanupMigration) Close() error { + return nil +} diff --git a/cmd/util/ledger/migrations/contract_cleanup_migration_test.go b/cmd/util/ledger/migrations/contract_cleanup_migration_test.go new file mode 100644 index 00000000000..8adf8d027af --- /dev/null +++ b/cmd/util/ledger/migrations/contract_cleanup_migration_test.go @@ -0,0 +1,117 @@ +package migrations + +import ( + "context" + "testing" + + "github.com/onflow/cadence/runtime/common" + "github.com/rs/zerolog" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "github.com/onflow/flow-go/cmd/util/ledger/util/registers" + "github.com/onflow/flow-go/fvm/environment" + "github.com/onflow/flow-go/model/flow" +) + +func TestContractCleanupMigration(t *testing.T) { + + t.Parallel() + + // Arrange + + address, err := common.HexToAddress("0x4184b8bdf78db9eb") + require.NoError(t, err) + + flowAddress := flow.ConvertAddress(address) + owner := flow.AddressToRegisterOwner(flowAddress) + + const contractNameEmpty = "Foo" + const contractNameNonEmpty = "Bar" + + registersByAccount := registers.NewByAccount() + + err = registersByAccount.Set( + owner, + flow.ContractKey(contractNameEmpty), + // Some whitespace for testing purposes + []byte(" \t \n "), + ) + require.NoError(t, err) + + err = registersByAccount.Set( + owner, + flow.ContractKey(contractNameNonEmpty), + []byte(" \n \t access(all) contract Bar {} \n \n"), + ) + require.NoError(t, err) + + encodedContractNames, err := environment.EncodeContractNames([]string{ + // Unsorted and duplicates for testing purposes + contractNameEmpty, + contractNameNonEmpty, + contractNameEmpty, + contractNameNonEmpty, + contractNameEmpty, + contractNameEmpty, + contractNameNonEmpty, + }) + require.NoError(t, err) + + err = registersByAccount.Set( + owner, + flow.ContractNamesKey, + encodedContractNames, + ) + require.NoError(t, err) + + // Act + + checkingMigration := NewContractCleanupMigration() + + log := zerolog.Nop() + + err = checkingMigration.InitMigration(log, registersByAccount, 1) + require.NoError(t, err) + + accountRegisters := registersByAccount.AccountRegisters(owner) + + err = checkingMigration.MigrateAccount( + context.Background(), + address, + accountRegisters, + ) + require.NoError(t, err) + + // Assert + + encodedContractNames, err = registersByAccount.Get( + owner, + flow.ContractNamesKey, + ) + require.NoError(t, err) + + contractNames, err := environment.DecodeContractNames(encodedContractNames) + require.NoError(t, err) + assert.Equal(t, + []string{ + contractNameNonEmpty, + contractNameEmpty, + }, + contractNames, + ) + + contractEmpty, err := registersByAccount.Get( + owner, + flow.ContractKey(contractNameEmpty), + ) + require.NoError(t, err) + assert.Nil(t, contractEmpty) + + contractNonEmpty, err := registersByAccount.Get( + owner, + flow.ContractKey(contractNameNonEmpty), + ) + require.NoError(t, err) + assert.NotEmpty(t, contractNonEmpty) +} From 301063c68e53b55d1d3b8d6434522c4aa210d163 Mon Sep 17 00:00:00 2001 From: sjonpaulbrown Date: Mon, 24 Jun 2024 16:30:35 -0600 Subject: [PATCH 06/12] Update TPS monitor automation to write to new BigQuery table --- integration/benchmark/cmd/ci/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/integration/benchmark/cmd/ci/main.go b/integration/benchmark/cmd/ci/main.go index 105ca023d2d..1f213cf74ce 100644 --- a/integration/benchmark/cmd/ci/main.go +++ b/integration/benchmark/cmd/ci/main.go @@ -64,7 +64,7 @@ func main() { gitRepoURLFlag := flag.String("git-repo-url", "https://github.com/onflow/flow-go.git", "git repo URL") bigQueryUpload := flag.Bool("bigquery-upload", true, "whether to upload results to BigQuery (true / false)") pushgateway := flag.String("pushgateway", "disabled", "host:port for pushgateway") - bigQueryProjectFlag := flag.String("bigquery-project", "dapperlabs-data", "project name for the bigquery uploader") + bigQueryProjectFlag := flag.String("bigquery-project", "ff-data-platform", "project name for the bigquery uploader") bigQueryDatasetFlag := flag.String("bigquery-dataset", "dev_src_flow_tps_metrics", "dataset name for the bigquery uploader") bigQueryRawTableFlag := flag.String("bigquery-raw-table", "rawResults", "table name for the bigquery raw results") flag.Parse() From e406eb35714aa2e966e61987b0e6747878052142 Mon Sep 17 00:00:00 2001 From: Supun Setunga Date: Tue, 25 Jun 2024 09:06:05 -0700 Subject: [PATCH 07/12] Fix invalidCadenceTypeError creation --- model/convert/service_event.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/model/convert/service_event.go b/model/convert/service_event.go index 292fb62be11..386356fff8d 100644 --- a/model/convert/service_event.go +++ b/model/convert/service_event.go @@ -682,11 +682,14 @@ func invalidCadenceTypeError( fieldName string, actualType, expectedType cadence.Value, ) error { + // NOTE: This error is reported if the Go-types are different (not if Cadence types are different). + // Therefore, print the Go-type instead of cadence type. + // Cadence type can be `nil`, since the `expectedType` is always the zero-value of the Go type. return fmt.Errorf( - "invalid Cadence type for field %s (got=%s, expected=%s)", + "invalid Cadence type for field %s (got=%T, expected=%T)", fieldName, - actualType.Type().ID(), - expectedType.Type().ID(), + actualType, + expectedType, ) } From 29b1a5bcdedb59b8f54f17e883118a9c85fe2f9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastian=20M=C3=BCller?= Date: Tue, 25 Jun 2024 11:08:53 -0700 Subject: [PATCH 08/12] Update to Cadence v1.0.0-preview.35 --- go.mod | 6 +++--- go.sum | 11 ++++++----- insecure/go.mod | 6 +++--- insecure/go.sum | 11 ++++++----- integration/go.mod | 6 +++--- integration/go.sum | 11 ++++++----- 6 files changed, 27 insertions(+), 24 deletions(-) diff --git a/go.mod b/go.mod index 4478289e694..8ba431f0315 100644 --- a/go.mod +++ b/go.mod @@ -47,12 +47,12 @@ require ( github.com/multiformats/go-multiaddr-dns v0.3.1 github.com/multiformats/go-multihash v0.2.3 github.com/onflow/atree v0.7.0-rc.2 - github.com/onflow/cadence v1.0.0-preview.34 + github.com/onflow/cadence v1.0.0-preview.35 github.com/onflow/crypto v0.25.1 github.com/onflow/flow v0.3.4 github.com/onflow/flow-core-contracts/lib/go/contracts v1.3.0 github.com/onflow/flow-core-contracts/lib/go/templates v1.3.0 - github.com/onflow/flow-go-sdk v1.0.0-preview.36 + github.com/onflow/flow-go-sdk v1.0.0-preview.37 github.com/onflow/flow/protobuf/go/flow v0.4.4 github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58 github.com/pierrec/lz4 v2.6.1+incompatible @@ -258,7 +258,7 @@ require ( github.com/onflow/flow-ft/lib/go/templates v1.0.0 // indirect github.com/onflow/flow-nft/lib/go/contracts v1.2.1 // indirect github.com/onflow/flow-nft/lib/go/templates v1.2.0 // indirect - github.com/onflow/sdks v0.5.1-0.20230912225508-b35402f12bba // indirect + github.com/onflow/sdks v0.6.0-preview.1 // indirect github.com/onsi/ginkgo/v2 v2.13.2 // indirect github.com/opencontainers/runtime-spec v1.1.0 // indirect github.com/opentracing/opentracing-go v1.2.0 // indirect diff --git a/go.sum b/go.sum index 7beb1e4191c..04e5310738d 100644 --- a/go.sum +++ b/go.sum @@ -2171,8 +2171,8 @@ github.com/onflow/atree v0.7.0-rc.2/go.mod h1:xvP61FoOs95K7IYdIYRnNcYQGf4nbF/uuJ github.com/onflow/boxo v0.0.0-20240201202436-f2477b92f483 h1:LpiQhTAfM9CAmNVEs0n//cBBgCg+vJSiIxTHYUklZ84= github.com/onflow/boxo v0.0.0-20240201202436-f2477b92f483/go.mod h1:pIZgTWdm3k3pLF9Uq6MB8JEcW07UDwNJjlXW1HELW80= github.com/onflow/cadence v1.0.0-M3/go.mod h1:odXGZZ/wGNA5mwT8bC9v8u8EXACHllB2ABSZK65TGL8= -github.com/onflow/cadence v1.0.0-preview.34 h1:MJSli75W6LJVUqSx/tq4MQe64H1+EcQBD/sNgpOO4jE= -github.com/onflow/cadence v1.0.0-preview.34/go.mod h1:jOwvPSSLTr9TvaKMs7KKiBYMmpdpNNAFxBsjMlrqVD0= +github.com/onflow/cadence v1.0.0-preview.35 h1:HZgt/9Foa6sCSH9SNaIFUSXK6q2ZxETg0ivsZbf+hhU= +github.com/onflow/cadence v1.0.0-preview.35/go.mod h1:jOwvPSSLTr9TvaKMs7KKiBYMmpdpNNAFxBsjMlrqVD0= github.com/onflow/crypto v0.25.0/go.mod h1:C8FbaX0x8y+FxWjbkHy0Q4EASCDR9bSPWZqlpCLYyVI= github.com/onflow/crypto v0.25.1 h1:0txy2PKPMM873JbpxQNbJmuOJtD56bfs48RQfm0ts5A= github.com/onflow/crypto v0.25.1/go.mod h1:C8FbaX0x8y+FxWjbkHy0Q4EASCDR9bSPWZqlpCLYyVI= @@ -2187,8 +2187,8 @@ github.com/onflow/flow-ft/lib/go/contracts v1.0.0/go.mod h1:PwsL8fC81cjnUnTfmyL/ github.com/onflow/flow-ft/lib/go/templates v1.0.0 h1:6cMS/lUJJ17HjKBfMO/eh0GGvnpElPgBXx7h5aoWJhs= github.com/onflow/flow-ft/lib/go/templates v1.0.0/go.mod h1:uQ8XFqmMK2jxyBSVrmyuwdWjTEb+6zGjRYotfDJ5pAE= github.com/onflow/flow-go-sdk v1.0.0-M1/go.mod h1:TDW0MNuCs4SvqYRUzkbRnRmHQL1h4X8wURsCw9P9beo= -github.com/onflow/flow-go-sdk v1.0.0-preview.36 h1:3g72MjmZPEEVAbtDATbjwqKoNSB7yHLWswUHSAB5zwQ= -github.com/onflow/flow-go-sdk v1.0.0-preview.36/go.mod h1:mjkXIluC+kseYyd8Z1aTq73IiffAUeoY5fuX/C2Z+1w= +github.com/onflow/flow-go-sdk v1.0.0-preview.37 h1:ujeIQheD+skzFt9+eOT9nXcc1rFxVLSCzwisEI3+2DA= +github.com/onflow/flow-go-sdk v1.0.0-preview.37/go.mod h1:2aSN7RdKzxWoCtCyOJz9W/ZNqkLgQDQS3hKLYwPlvGw= github.com/onflow/flow-nft/lib/go/contracts v1.2.1 h1:woAAS5z651sDpi7ihAHll8NvRS9uFXIXkL6xR+bKFZY= github.com/onflow/flow-nft/lib/go/contracts v1.2.1/go.mod h1:2gpbza+uzs1k7x31hkpBPlggIRkI53Suo0n2AyA2HcE= github.com/onflow/flow-nft/lib/go/templates v1.2.0 h1:JSQyh9rg0RC+D1930BiRXN8lrtMs+ubVMK6aQPon6Yc= @@ -2200,8 +2200,9 @@ github.com/onflow/go-ethereum v1.13.4 h1:iNO86fm8RbBbhZ87ZulblInqCdHnAQVY8okBrNs github.com/onflow/go-ethereum v1.13.4/go.mod h1:cE/gEUkAffhwbVmMJYz+t1dAfVNHNwZCgc3BWtZxBGY= github.com/onflow/nft-storefront/lib/go/contracts v1.0.0 h1:sxyWLqGm/p4EKT6DUlQESDG1ZNMN9GjPCm1gTq7NGfc= github.com/onflow/nft-storefront/lib/go/contracts v1.0.0/go.mod h1:kMeq9zUwCrgrSojEbTUTTJpZ4WwacVm2pA7LVFr+glk= -github.com/onflow/sdks v0.5.1-0.20230912225508-b35402f12bba h1:rIehuhO6bj4FkwE4VzwEjX7MoAlOhUJENBJLqDqVxAo= github.com/onflow/sdks v0.5.1-0.20230912225508-b35402f12bba/go.mod h1:F0dj0EyHC55kknLkeD10js4mo14yTdMotnWMslPirrU= +github.com/onflow/sdks v0.6.0-preview.1 h1:mb/cUezuqWEP1gFZNAgUI4boBltudv4nlfxke1KBp9k= +github.com/onflow/sdks v0.6.0-preview.1/go.mod h1:F0dj0EyHC55kknLkeD10js4mo14yTdMotnWMslPirrU= github.com/onflow/wal v1.0.2 h1:5bgsJVf2O3cfMNK12fiiTyYZ8cOrUiELt3heBJfHOhc= github.com/onflow/wal v1.0.2/go.mod h1:iMC8gkLqu4nkbkAla5HkSBb+FGyQOZiWz3DYm2wSXCk= github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= diff --git a/insecure/go.mod b/insecure/go.mod index 653b5dddc0f..847f760e01c 100644 --- a/insecure/go.mod +++ b/insecure/go.mod @@ -199,17 +199,17 @@ require ( github.com/multiformats/go-varint v0.0.7 // indirect github.com/olekukonko/tablewriter v0.0.5 // indirect github.com/onflow/atree v0.7.0-rc.2 // indirect - github.com/onflow/cadence v1.0.0-preview.34 // indirect + github.com/onflow/cadence v1.0.0-preview.35 // indirect github.com/onflow/flow-core-contracts/lib/go/contracts v1.3.0 // indirect github.com/onflow/flow-core-contracts/lib/go/templates v1.3.0 // indirect github.com/onflow/flow-ft/lib/go/contracts v1.0.0 // indirect github.com/onflow/flow-ft/lib/go/templates v1.0.0 // indirect - github.com/onflow/flow-go-sdk v1.0.0-preview.36 // indirect + github.com/onflow/flow-go-sdk v1.0.0-preview.37 // indirect github.com/onflow/flow-nft/lib/go/contracts v1.2.1 // indirect github.com/onflow/flow-nft/lib/go/templates v1.2.0 // indirect github.com/onflow/flow/protobuf/go/flow v0.4.4 // indirect github.com/onflow/go-ethereum v1.13.4 // indirect - github.com/onflow/sdks v0.5.1-0.20230912225508-b35402f12bba // indirect + github.com/onflow/sdks v0.6.0-preview.1 // indirect github.com/onflow/wal v1.0.2 // indirect github.com/onsi/ginkgo/v2 v2.13.2 // indirect github.com/opencontainers/runtime-spec v1.1.0 // indirect diff --git a/insecure/go.sum b/insecure/go.sum index 030ffa3f4cb..78e57537f8f 100644 --- a/insecure/go.sum +++ b/insecure/go.sum @@ -2160,8 +2160,8 @@ github.com/onflow/atree v0.6.1-0.20230711151834-86040b30171f/go.mod h1:xvP61FoOs github.com/onflow/atree v0.7.0-rc.2 h1:mZmVrl/zPlfI44EjV3FdR2QwIqT8nz1sCONUBFcML/U= github.com/onflow/atree v0.7.0-rc.2/go.mod h1:xvP61FoOs95K7IYdIYRnNcYQGf4nbF/uuJ0tHf4DRuM= github.com/onflow/cadence v1.0.0-M3/go.mod h1:odXGZZ/wGNA5mwT8bC9v8u8EXACHllB2ABSZK65TGL8= -github.com/onflow/cadence v1.0.0-preview.34 h1:MJSli75W6LJVUqSx/tq4MQe64H1+EcQBD/sNgpOO4jE= -github.com/onflow/cadence v1.0.0-preview.34/go.mod h1:jOwvPSSLTr9TvaKMs7KKiBYMmpdpNNAFxBsjMlrqVD0= +github.com/onflow/cadence v1.0.0-preview.35 h1:HZgt/9Foa6sCSH9SNaIFUSXK6q2ZxETg0ivsZbf+hhU= +github.com/onflow/cadence v1.0.0-preview.35/go.mod h1:jOwvPSSLTr9TvaKMs7KKiBYMmpdpNNAFxBsjMlrqVD0= github.com/onflow/crypto v0.25.0/go.mod h1:C8FbaX0x8y+FxWjbkHy0Q4EASCDR9bSPWZqlpCLYyVI= github.com/onflow/crypto v0.25.1 h1:0txy2PKPMM873JbpxQNbJmuOJtD56bfs48RQfm0ts5A= github.com/onflow/crypto v0.25.1/go.mod h1:C8FbaX0x8y+FxWjbkHy0Q4EASCDR9bSPWZqlpCLYyVI= @@ -2174,8 +2174,8 @@ github.com/onflow/flow-ft/lib/go/contracts v1.0.0/go.mod h1:PwsL8fC81cjnUnTfmyL/ github.com/onflow/flow-ft/lib/go/templates v1.0.0 h1:6cMS/lUJJ17HjKBfMO/eh0GGvnpElPgBXx7h5aoWJhs= github.com/onflow/flow-ft/lib/go/templates v1.0.0/go.mod h1:uQ8XFqmMK2jxyBSVrmyuwdWjTEb+6zGjRYotfDJ5pAE= github.com/onflow/flow-go-sdk v1.0.0-M1/go.mod h1:TDW0MNuCs4SvqYRUzkbRnRmHQL1h4X8wURsCw9P9beo= -github.com/onflow/flow-go-sdk v1.0.0-preview.36 h1:3g72MjmZPEEVAbtDATbjwqKoNSB7yHLWswUHSAB5zwQ= -github.com/onflow/flow-go-sdk v1.0.0-preview.36/go.mod h1:mjkXIluC+kseYyd8Z1aTq73IiffAUeoY5fuX/C2Z+1w= +github.com/onflow/flow-go-sdk v1.0.0-preview.37 h1:ujeIQheD+skzFt9+eOT9nXcc1rFxVLSCzwisEI3+2DA= +github.com/onflow/flow-go-sdk v1.0.0-preview.37/go.mod h1:2aSN7RdKzxWoCtCyOJz9W/ZNqkLgQDQS3hKLYwPlvGw= github.com/onflow/flow-nft/lib/go/contracts v1.2.1 h1:woAAS5z651sDpi7ihAHll8NvRS9uFXIXkL6xR+bKFZY= github.com/onflow/flow-nft/lib/go/contracts v1.2.1/go.mod h1:2gpbza+uzs1k7x31hkpBPlggIRkI53Suo0n2AyA2HcE= github.com/onflow/flow-nft/lib/go/templates v1.2.0 h1:JSQyh9rg0RC+D1930BiRXN8lrtMs+ubVMK6aQPon6Yc= @@ -2185,8 +2185,9 @@ github.com/onflow/flow/protobuf/go/flow v0.4.4 h1:lD1owoZGFgLcvdLZDmP0Kc4GOuQeSU github.com/onflow/flow/protobuf/go/flow v0.4.4/go.mod h1:NA2pX2nw8zuaxfKphhKsk00kWLwfd+tv8mS23YXO4Sk= github.com/onflow/go-ethereum v1.13.4 h1:iNO86fm8RbBbhZ87ZulblInqCdHnAQVY8okBrNsTevc= github.com/onflow/go-ethereum v1.13.4/go.mod h1:cE/gEUkAffhwbVmMJYz+t1dAfVNHNwZCgc3BWtZxBGY= -github.com/onflow/sdks v0.5.1-0.20230912225508-b35402f12bba h1:rIehuhO6bj4FkwE4VzwEjX7MoAlOhUJENBJLqDqVxAo= github.com/onflow/sdks v0.5.1-0.20230912225508-b35402f12bba/go.mod h1:F0dj0EyHC55kknLkeD10js4mo14yTdMotnWMslPirrU= +github.com/onflow/sdks v0.6.0-preview.1 h1:mb/cUezuqWEP1gFZNAgUI4boBltudv4nlfxke1KBp9k= +github.com/onflow/sdks v0.6.0-preview.1/go.mod h1:F0dj0EyHC55kknLkeD10js4mo14yTdMotnWMslPirrU= github.com/onflow/wal v1.0.2 h1:5bgsJVf2O3cfMNK12fiiTyYZ8cOrUiELt3heBJfHOhc= github.com/onflow/wal v1.0.2/go.mod h1:iMC8gkLqu4nkbkAla5HkSBb+FGyQOZiWz3DYm2wSXCk= github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= diff --git a/integration/go.mod b/integration/go.mod index ce66485b5a2..0f0c0531fe9 100644 --- a/integration/go.mod +++ b/integration/go.mod @@ -19,13 +19,13 @@ require ( github.com/ipfs/go-datastore v0.6.0 github.com/ipfs/go-ds-badger2 v0.1.3 github.com/libp2p/go-libp2p v0.32.2 - github.com/onflow/cadence v1.0.0-preview.34 + github.com/onflow/cadence v1.0.0-preview.35 github.com/onflow/crypto v0.25.1 github.com/onflow/flow-core-contracts/lib/go/contracts v1.3.0 github.com/onflow/flow-core-contracts/lib/go/templates v1.3.0 github.com/onflow/flow-emulator v1.0.0-preview.24 github.com/onflow/flow-go v0.35.5-0.20240517202625-55f862b45dfd - github.com/onflow/flow-go-sdk v1.0.0-preview.36 + github.com/onflow/flow-go-sdk v1.0.0-preview.37 github.com/onflow/flow-go/insecure v0.0.0-00010101000000-000000000000 github.com/onflow/flow/protobuf/go/flow v0.4.4 github.com/onflow/go-ethereum v1.13.4 @@ -247,7 +247,7 @@ require ( github.com/onflow/flow-ft/lib/go/templates v1.0.0 // indirect github.com/onflow/flow-nft/lib/go/contracts v1.2.1 // indirect github.com/onflow/flow-nft/lib/go/templates v1.2.0 // indirect - github.com/onflow/sdks v0.5.1-0.20230912225508-b35402f12bba // indirect + github.com/onflow/sdks v0.6.0-preview.1 // indirect github.com/onflow/wal v1.0.2 // indirect github.com/onsi/ginkgo/v2 v2.13.2 // indirect github.com/opencontainers/go-digest v1.0.0 // indirect diff --git a/integration/go.sum b/integration/go.sum index 821a5c27fd9..047b51801e7 100644 --- a/integration/go.sum +++ b/integration/go.sum @@ -2150,8 +2150,8 @@ github.com/onflow/atree v0.6.1-0.20230711151834-86040b30171f/go.mod h1:xvP61FoOs github.com/onflow/atree v0.7.0-rc.2 h1:mZmVrl/zPlfI44EjV3FdR2QwIqT8nz1sCONUBFcML/U= github.com/onflow/atree v0.7.0-rc.2/go.mod h1:xvP61FoOs95K7IYdIYRnNcYQGf4nbF/uuJ0tHf4DRuM= github.com/onflow/cadence v1.0.0-M3/go.mod h1:odXGZZ/wGNA5mwT8bC9v8u8EXACHllB2ABSZK65TGL8= -github.com/onflow/cadence v1.0.0-preview.34 h1:MJSli75W6LJVUqSx/tq4MQe64H1+EcQBD/sNgpOO4jE= -github.com/onflow/cadence v1.0.0-preview.34/go.mod h1:jOwvPSSLTr9TvaKMs7KKiBYMmpdpNNAFxBsjMlrqVD0= +github.com/onflow/cadence v1.0.0-preview.35 h1:HZgt/9Foa6sCSH9SNaIFUSXK6q2ZxETg0ivsZbf+hhU= +github.com/onflow/cadence v1.0.0-preview.35/go.mod h1:jOwvPSSLTr9TvaKMs7KKiBYMmpdpNNAFxBsjMlrqVD0= github.com/onflow/crypto v0.25.0/go.mod h1:C8FbaX0x8y+FxWjbkHy0Q4EASCDR9bSPWZqlpCLYyVI= github.com/onflow/crypto v0.25.1 h1:0txy2PKPMM873JbpxQNbJmuOJtD56bfs48RQfm0ts5A= github.com/onflow/crypto v0.25.1/go.mod h1:C8FbaX0x8y+FxWjbkHy0Q4EASCDR9bSPWZqlpCLYyVI= @@ -2166,8 +2166,8 @@ github.com/onflow/flow-ft/lib/go/contracts v1.0.0/go.mod h1:PwsL8fC81cjnUnTfmyL/ github.com/onflow/flow-ft/lib/go/templates v1.0.0 h1:6cMS/lUJJ17HjKBfMO/eh0GGvnpElPgBXx7h5aoWJhs= github.com/onflow/flow-ft/lib/go/templates v1.0.0/go.mod h1:uQ8XFqmMK2jxyBSVrmyuwdWjTEb+6zGjRYotfDJ5pAE= github.com/onflow/flow-go-sdk v1.0.0-M1/go.mod h1:TDW0MNuCs4SvqYRUzkbRnRmHQL1h4X8wURsCw9P9beo= -github.com/onflow/flow-go-sdk v1.0.0-preview.36 h1:3g72MjmZPEEVAbtDATbjwqKoNSB7yHLWswUHSAB5zwQ= -github.com/onflow/flow-go-sdk v1.0.0-preview.36/go.mod h1:mjkXIluC+kseYyd8Z1aTq73IiffAUeoY5fuX/C2Z+1w= +github.com/onflow/flow-go-sdk v1.0.0-preview.37 h1:ujeIQheD+skzFt9+eOT9nXcc1rFxVLSCzwisEI3+2DA= +github.com/onflow/flow-go-sdk v1.0.0-preview.37/go.mod h1:2aSN7RdKzxWoCtCyOJz9W/ZNqkLgQDQS3hKLYwPlvGw= github.com/onflow/flow-nft/lib/go/contracts v1.2.1 h1:woAAS5z651sDpi7ihAHll8NvRS9uFXIXkL6xR+bKFZY= github.com/onflow/flow-nft/lib/go/contracts v1.2.1/go.mod h1:2gpbza+uzs1k7x31hkpBPlggIRkI53Suo0n2AyA2HcE= github.com/onflow/flow-nft/lib/go/templates v1.2.0 h1:JSQyh9rg0RC+D1930BiRXN8lrtMs+ubVMK6aQPon6Yc= @@ -2177,8 +2177,9 @@ github.com/onflow/flow/protobuf/go/flow v0.4.4 h1:lD1owoZGFgLcvdLZDmP0Kc4GOuQeSU github.com/onflow/flow/protobuf/go/flow v0.4.4/go.mod h1:NA2pX2nw8zuaxfKphhKsk00kWLwfd+tv8mS23YXO4Sk= github.com/onflow/go-ethereum v1.13.4 h1:iNO86fm8RbBbhZ87ZulblInqCdHnAQVY8okBrNsTevc= github.com/onflow/go-ethereum v1.13.4/go.mod h1:cE/gEUkAffhwbVmMJYz+t1dAfVNHNwZCgc3BWtZxBGY= -github.com/onflow/sdks v0.5.1-0.20230912225508-b35402f12bba h1:rIehuhO6bj4FkwE4VzwEjX7MoAlOhUJENBJLqDqVxAo= github.com/onflow/sdks v0.5.1-0.20230912225508-b35402f12bba/go.mod h1:F0dj0EyHC55kknLkeD10js4mo14yTdMotnWMslPirrU= +github.com/onflow/sdks v0.6.0-preview.1 h1:mb/cUezuqWEP1gFZNAgUI4boBltudv4nlfxke1KBp9k= +github.com/onflow/sdks v0.6.0-preview.1/go.mod h1:F0dj0EyHC55kknLkeD10js4mo14yTdMotnWMslPirrU= github.com/onflow/wal v1.0.2 h1:5bgsJVf2O3cfMNK12fiiTyYZ8cOrUiELt3heBJfHOhc= github.com/onflow/wal v1.0.2/go.mod h1:iMC8gkLqu4nkbkAla5HkSBb+FGyQOZiWz3DYm2wSXCk= github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= From ce3120e493dfc01a7eb9c46b893ba2a27d96d678 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastian=20M=C3=BCller?= Date: Tue, 25 Jun 2024 11:31:31 -0700 Subject: [PATCH 09/12] adjust to API changes in cadence package --- fvm/fvm_signature_test.go | 56 +++------ fvm/transactionPayerBalanceChecker.go | 17 --- fvm/transactionPayerBalanceChecker_test.go | 26 ++++- .../indexer/util_test.go | 11 +- utils/unittest/generator/events.go | 67 ++++++----- utils/unittest/service_events_fixtures.go | 110 ++++++++++-------- 6 files changed, 141 insertions(+), 146 deletions(-) diff --git a/fvm/fvm_signature_test.go b/fvm/fvm_signature_test.go index 870794001e9..ae2cbe5498a 100644 --- a/fvm/fvm_signature_test.go +++ b/fvm/fvm_signature_test.go @@ -27,6 +27,12 @@ var createMessage = func(m string) (signableMessage []byte, message cadence.Arra return signableMessage, message } +var uint8ArrayArrayType = cadence.NewVariableSizedArrayType( + cadence.NewVariableSizedArrayType( + cadence.UInt8Type, + ), +) + func TestKeyListSignature(t *testing.T) { t.Parallel() @@ -552,12 +558,8 @@ func TestBLSMultiSignature(t *testing.T) { script := fvm.Script(code).WithArguments( jsoncdc.MustEncode(cadence.Array{ - Values: signatures, - ArrayType: &cadence.VariableSizedArrayType{ - ElementType: &cadence.VariableSizedArrayType{ - ElementType: cadence.UInt8Type, - }, - }, + Values: signatures, + ArrayType: uint8ArrayArrayType, }), ) @@ -585,11 +587,7 @@ func TestBLSMultiSignature(t *testing.T) { script := fvm.Script(code).WithArguments( jsoncdc.MustEncode(cadence.Array{ Values: signatures, - ArrayType: &cadence.VariableSizedArrayType{ - ElementType: &cadence.VariableSizedArrayType{ - ElementType: cadence.UInt8Type, - }, - }, + ArrayType: uint8ArrayArrayType, }), ) @@ -608,11 +606,7 @@ func TestBLSMultiSignature(t *testing.T) { script := fvm.Script(code).WithArguments( jsoncdc.MustEncode(cadence.Array{ Values: signatures, - ArrayType: &cadence.VariableSizedArrayType{ - ElementType: &cadence.VariableSizedArrayType{ - ElementType: cadence.UInt8Type, - }, - }, + ArrayType: uint8ArrayArrayType, }), ) @@ -678,11 +672,7 @@ func TestBLSMultiSignature(t *testing.T) { script := fvm.Script(code(BLSSignatureAlgorithm)).WithArguments( jsoncdc.MustEncode(cadence.Array{ Values: publicKeys, - ArrayType: &cadence.VariableSizedArrayType{ - ElementType: &cadence.VariableSizedArrayType{ - ElementType: cadence.UInt8Type, - }, - }, + ArrayType: uint8ArrayArrayType, }), ) @@ -712,11 +702,7 @@ func TestBLSMultiSignature(t *testing.T) { script := fvm.Script(code(signatureAlgorithm)).WithArguments( jsoncdc.MustEncode(cadence.Array{ Values: publicKeys, - ArrayType: &cadence.VariableSizedArrayType{ - ElementType: &cadence.VariableSizedArrayType{ - ElementType: cadence.UInt8Type, - }, - }, + ArrayType: uint8ArrayArrayType, }), ) @@ -732,11 +718,7 @@ func TestBLSMultiSignature(t *testing.T) { script := fvm.Script(code(BLSSignatureAlgorithm)).WithArguments( jsoncdc.MustEncode(cadence.Array{ Values: publicKeys, - ArrayType: &cadence.VariableSizedArrayType{ - ElementType: &cadence.VariableSizedArrayType{ - ElementType: cadence.UInt8Type, - }, - }, + ArrayType: uint8ArrayArrayType, }), ) @@ -812,19 +794,11 @@ func TestBLSMultiSignature(t *testing.T) { script := fvm.Script(code).WithArguments( jsoncdc.MustEncode(cadence.Array{ // keys Values: publicKeys, - ArrayType: &cadence.VariableSizedArrayType{ - ElementType: &cadence.VariableSizedArrayType{ - ElementType: cadence.UInt8Type, - }, - }, + ArrayType: uint8ArrayArrayType, }), jsoncdc.MustEncode(cadence.Array{ // signatures Values: signatures, - ArrayType: &cadence.VariableSizedArrayType{ - ElementType: &cadence.VariableSizedArrayType{ - ElementType: cadence.UInt8Type, - }, - }, + ArrayType: uint8ArrayArrayType, }), jsoncdc.MustEncode(cadenceMessage), jsoncdc.MustEncode(cadence.String(tag)), diff --git a/fvm/transactionPayerBalanceChecker.go b/fvm/transactionPayerBalanceChecker.go index 280cf5e2ada..79edfff5804 100644 --- a/fvm/transactionPayerBalanceChecker.go +++ b/fvm/transactionPayerBalanceChecker.go @@ -16,23 +16,6 @@ const VerifyPayerBalanceResultTypeCanExecuteTransactionFieldName = "canExecuteTr const VerifyPayerBalanceResultTypeRequiredBalanceFieldName = "requiredBalance" const VerifyPayerBalanceResultTypeMaximumTransactionFeesFieldName = "maximumTransactionFees" -var VerifyPayerBalanceResultType = &cadence.StructType{ - Fields: []cadence.Field{ - { - Identifier: VerifyPayerBalanceResultTypeCanExecuteTransactionFieldName, - Type: cadence.BoolType, - }, - { - Identifier: VerifyPayerBalanceResultTypeRequiredBalanceFieldName, - Type: cadence.UFix64Type, - }, - { - Identifier: VerifyPayerBalanceResultTypeMaximumTransactionFeesFieldName, - Type: cadence.UFix64Type, - }, - }, -} - // decodeVerifyPayerBalanceResult decodes the VerifyPayerBalanceResult struct // https://github.com/onflow/flow-core-contracts/blob/7c70c6a1d33c2879b60c78e363fa68fc6fce13b9/contracts/FlowFees.cdc#L75 func decodeVerifyPayerBalanceResult(resultValue cadence.Value) ( diff --git a/fvm/transactionPayerBalanceChecker_test.go b/fvm/transactionPayerBalanceChecker_test.go index f03b03758d8..2dc3ca677b5 100644 --- a/fvm/transactionPayerBalanceChecker_test.go +++ b/fvm/transactionPayerBalanceChecker_test.go @@ -16,6 +16,28 @@ import ( "github.com/onflow/flow-go/model/flow" ) +var verifyPayerBalanceResultType = cadence.NewStructType( + // TODO: location + nil, + // TODO: qualified identifier + "", + []cadence.Field{ + { + Identifier: fvm.VerifyPayerBalanceResultTypeCanExecuteTransactionFieldName, + Type: cadence.BoolType, + }, + { + Identifier: fvm.VerifyPayerBalanceResultTypeRequiredBalanceFieldName, + Type: cadence.UFix64Type, + }, + { + Identifier: fvm.VerifyPayerBalanceResultTypeMaximumTransactionFeesFieldName, + Type: cadence.UFix64Type, + }, + }, + nil, +) + func TestTransactionPayerBalanceChecker(t *testing.T) { payer := flow.HexToAddress("1") t.Run("TransactionFeesEnabled == false disables the balance check", func(t *testing.T) { @@ -90,7 +112,7 @@ func TestTransactionPayerBalanceChecker(t *testing.T) { cadence.NewBool(true), cadence.UFix64(100), cadence.UFix64(100), - }).WithType(fvm.VerifyPayerBalanceResultType), + }).WithType(verifyPayerBalanceResultType), nil, ) @@ -119,7 +141,7 @@ func TestTransactionPayerBalanceChecker(t *testing.T) { cadence.NewBool(false), cadence.UFix64(100), cadence.UFix64(101), - }).WithType(fvm.VerifyPayerBalanceResultType), + }).WithType(verifyPayerBalanceResultType), nil, ) diff --git a/module/state_synchronization/indexer/util_test.go b/module/state_synchronization/indexer/util_test.go index c7c66970e84..612e858e6ed 100644 --- a/module/state_synchronization/indexer/util_test.go +++ b/module/state_synchronization/indexer/util_test.go @@ -58,10 +58,10 @@ func TestFindContractUpdates(t *testing.T) { } func contractUpdatedFixture(t *testing.T, address common.Address, contractName string) flow.Event { - contractUpdateEventType := &cadence.EventType{ - Location: stdlib.AccountContractAddedEventType.Location, - QualifiedIdentifier: stdlib.AccountContractAddedEventType.QualifiedIdentifier(), - Fields: []cadence.Field{ + contractUpdateEventType := cadence.NewEventType( + stdlib.AccountContractAddedEventType.Location, + stdlib.AccountContractAddedEventType.QualifiedIdentifier(), + []cadence.Field{ { Identifier: "address", Type: cadence.AddressType, @@ -75,7 +75,8 @@ func contractUpdatedFixture(t *testing.T, address common.Address, contractName s Type: cadence.StringType, }, }, - } + nil, + ) contractString, err := cadence.NewString(contractName) require.NoError(t, err) diff --git a/utils/unittest/generator/events.go b/utils/unittest/generator/events.go index 63a0904fb72..8ea969c1240 100644 --- a/utils/unittest/generator/events.go +++ b/utils/unittest/generator/events.go @@ -55,10 +55,10 @@ func (g *Events) New() flow.Event { identifier := fmt.Sprintf("TestContract.FooEvent%d", g.count) typeID := location.TypeID(nil, identifier) - testEventType := &cadence.EventType{ - Location: location, - QualifiedIdentifier: identifier, - Fields: []cadence.Field{ + testEventType := cadence.NewEventType( + location, + identifier, + []cadence.Field{ { Identifier: "a", Type: cadence.IntType, @@ -68,7 +68,8 @@ func (g *Events) New() flow.Event { Type: cadence.StringType, }, }, - } + nil, + ) fooString, err := cadence.NewString("foo") if err != nil { @@ -123,16 +124,18 @@ func GenerateAccountCreateEvent(t *testing.T, address flow.Address) flow.Event { cadenceEvent := cadence.NewEvent( []cadence.Value{ cadence.NewAddress(address), - }).WithType(&cadence.EventType{ - Location: stdlib.FlowLocation{}, - QualifiedIdentifier: "AccountCreated", - Fields: []cadence.Field{ - { - Identifier: "address", - Type: cadence.AddressType, + }). + WithType(cadence.NewEventType( + stdlib.FlowLocation{}, + "AccountCreated", + []cadence.Field{ + { + Identifier: "address", + Type: cadence.AddressType, + }, }, - }, - }) + nil, + )) payload, err := ccf.Encode(cadenceEvent) require.NoError(t, err) @@ -162,24 +165,26 @@ func GenerateAccountContractEvent(t *testing.T, qualifiedIdentifier string, addr testutils.ConvertToCadence([]byte{111, 43, 164, 202, 220, 174, 148, 17, 253, 161, 9, 124, 237, 83, 227, 75, 115, 149, 141, 83, 129, 145, 252, 68, 122, 137, 80, 155, 89, 233, 136, 213}), ).WithType(cadence.NewConstantSizedArrayType(32, cadence.UInt8Type)), contractName, - }).WithType(&cadence.EventType{ - Location: stdlib.FlowLocation{}, - QualifiedIdentifier: qualifiedIdentifier, - Fields: []cadence.Field{ - { - Identifier: "address", - Type: cadence.AddressType, + }). + WithType(cadence.NewEventType( + stdlib.FlowLocation{}, + qualifiedIdentifier, + []cadence.Field{ + { + Identifier: "address", + Type: cadence.AddressType, + }, + { + Identifier: "codeHash", + Type: cadence.NewConstantSizedArrayType(32, cadence.UInt8Type), + }, + { + Identifier: "contract", + Type: cadence.StringType, + }, }, - { - Identifier: "codeHash", - Type: cadence.NewConstantSizedArrayType(32, cadence.UInt8Type), - }, - { - Identifier: "contract", - Type: cadence.StringType, - }, - }, - }) + nil, + )) payload, err := ccf.Encode(cadenceEvent) require.NoError(t, err) diff --git a/utils/unittest/service_events_fixtures.go b/utils/unittest/service_events_fixtures.go index f92923f6f18..11088635809 100644 --- a/utils/unittest/service_events_fixtures.go +++ b/utils/unittest/service_events_fixtures.go @@ -736,10 +736,10 @@ func newFlowClusterQCVoteStructType() cadence.Type { address, _ := common.HexToAddress("01cf0e2f2f715450") location := common.NewAddressLocation(nil, address, "FlowClusterQC") - return &cadence.StructType{ - Location: location, - QualifiedIdentifier: "FlowClusterQC.Vote", - Fields: []cadence.Field{ + return cadence.NewStructType( + location, + "FlowClusterQC.Vote", + []cadence.Field{ { Identifier: "nodeID", Type: cadence.StringType, @@ -761,7 +761,8 @@ func newFlowClusterQCVoteStructType() cadence.Type { Type: cadence.UInt64Type, }, }, - } + nil, + ) } func newFlowIDTableStakingNodeInfoStructType() *cadence.StructType { @@ -771,10 +772,10 @@ func newFlowIDTableStakingNodeInfoStructType() *cadence.StructType { address, _ := common.HexToAddress("01cf0e2f2f715450") location := common.NewAddressLocation(nil, address, "FlowIDTableStaking") - return &cadence.StructType{ - Location: location, - QualifiedIdentifier: "FlowIDTableStaking.NodeInfo", - Fields: []cadence.Field{ + return cadence.NewStructType( + location, + "FlowIDTableStaking.NodeInfo", + []cadence.Field{ { Identifier: "id", Type: cadence.StringType, @@ -832,7 +833,8 @@ func newFlowIDTableStakingNodeInfoStructType() *cadence.StructType { Type: cadence.UInt64Type, }, }, - } + nil, + ) } func newFlowEpochEpochSetupEventType() *cadence.EventType { @@ -842,10 +844,10 @@ func newFlowEpochEpochSetupEventType() *cadence.EventType { address, _ := common.HexToAddress("01cf0e2f2f715450") location := common.NewAddressLocation(nil, address, "FlowEpoch") - return &cadence.EventType{ - Location: location, - QualifiedIdentifier: "FlowEpoch.EpochSetup", - Fields: []cadence.Field{ + return cadence.NewEventType( + location, + "FlowEpoch.EpochSetup", + []cadence.Field{ { Identifier: "counter", Type: cadence.UInt64Type, @@ -891,7 +893,8 @@ func newFlowEpochEpochSetupEventType() *cadence.EventType { Type: cadence.UInt64Type, }, }, - } + nil, + ) } func newFlowEpochEpochCommitEventType() *cadence.EventType { @@ -901,10 +904,10 @@ func newFlowEpochEpochCommitEventType() *cadence.EventType { address, _ := common.HexToAddress("01cf0e2f2f715450") location := common.NewAddressLocation(nil, address, "FlowEpoch") - return &cadence.EventType{ - Location: location, - QualifiedIdentifier: "FlowEpoch.EpochCommitted", - Fields: []cadence.Field{ + return cadence.NewEventType( + location, + "FlowEpoch.EpochCommitted", + []cadence.Field{ { Identifier: "counter", Type: cadence.UInt64Type, @@ -918,7 +921,8 @@ func newFlowEpochEpochCommitEventType() *cadence.EventType { Type: cadence.NewVariableSizedArrayType(cadence.StringType), }, }, - } + nil, + ) } func newFlowClusterQCClusterQCStructType() *cadence.StructType { @@ -928,10 +932,10 @@ func newFlowClusterQCClusterQCStructType() *cadence.StructType { address, _ := common.HexToAddress("01cf0e2f2f715450") location := common.NewAddressLocation(nil, address, "FlowClusterQC") - return &cadence.StructType{ - Location: location, - QualifiedIdentifier: "FlowClusterQC.ClusterQC", - Fields: []cadence.Field{ + return cadence.NewStructType( + location, + "FlowClusterQC.ClusterQC", + []cadence.Field{ { Identifier: "index", Type: cadence.UInt16Type, @@ -949,7 +953,8 @@ func newFlowClusterQCClusterQCStructType() *cadence.StructType { Type: cadence.NewVariableSizedArrayType(cadence.StringType), }, }, - } + nil, + ) } func NewNodeVersionBeaconVersionBeaconEventType() *cadence.EventType { @@ -959,10 +964,10 @@ func NewNodeVersionBeaconVersionBeaconEventType() *cadence.EventType { address, _ := common.HexToAddress("01cf0e2f2f715450") location := common.NewAddressLocation(nil, address, "NodeVersionBeacon") - return &cadence.EventType{ - Location: location, - QualifiedIdentifier: "NodeVersionBeacon.VersionBeacon", - Fields: []cadence.Field{ + return cadence.NewEventType( + location, + "NodeVersionBeacon.VersionBeacon", + []cadence.Field{ { Identifier: "versionBoundaries", Type: cadence.NewVariableSizedArrayType(NewNodeVersionBeaconVersionBoundaryStructType()), @@ -972,7 +977,8 @@ func NewNodeVersionBeaconVersionBeaconEventType() *cadence.EventType { Type: cadence.UInt64Type, }, }, - } + nil, + ) } func NewNodeVersionBeaconVersionBoundaryStructType() *cadence.StructType { @@ -982,10 +988,10 @@ func NewNodeVersionBeaconVersionBoundaryStructType() *cadence.StructType { address, _ := common.HexToAddress("01cf0e2f2f715450") location := common.NewAddressLocation(nil, address, "NodeVersionBeacon") - return &cadence.StructType{ - Location: location, - QualifiedIdentifier: "NodeVersionBeacon.VersionBoundary", - Fields: []cadence.Field{ + return cadence.NewStructType( + location, + "NodeVersionBeacon.VersionBoundary", + []cadence.Field{ { Identifier: "blockHeight", Type: cadence.UInt64Type, @@ -995,7 +1001,8 @@ func NewNodeVersionBeaconVersionBoundaryStructType() *cadence.StructType { Type: NewNodeVersionBeaconSemverStructType(), }, }, - } + nil, + ) } func NewNodeVersionBeaconSemverStructType() *cadence.StructType { @@ -1005,10 +1012,10 @@ func NewNodeVersionBeaconSemverStructType() *cadence.StructType { address, _ := common.HexToAddress("01cf0e2f2f715450") location := common.NewAddressLocation(nil, address, "NodeVersionBeacon") - return &cadence.StructType{ - Location: location, - QualifiedIdentifier: "NodeVersionBeacon.Semver", - Fields: []cadence.Field{ + return cadence.NewStructType( + location, + "NodeVersionBeacon.Semver", + []cadence.Field{ { Identifier: "major", Type: cadence.UInt8Type, @@ -1026,7 +1033,8 @@ func NewNodeVersionBeaconSemverStructType() *cadence.StructType { Type: cadence.NewOptionalType(cadence.StringType), }, }, - } + nil, + ) } func NewProtocolStateVersionUpgradeEventType() *cadence.EventType { @@ -1036,10 +1044,10 @@ func NewProtocolStateVersionUpgradeEventType() *cadence.EventType { address, _ := common.HexToAddress("01cf0e2f2f715450") location := common.NewAddressLocation(nil, address, "ProtocolStateVersionUpgrade") - return &cadence.EventType{ - Location: location, - QualifiedIdentifier: "NodeVersionBeacon.ProtocolStateVersionUpgrade", - Fields: []cadence.Field{ + return cadence.NewEventType( + location, + "NodeVersionBeacon.ProtocolStateVersionUpgrade", + []cadence.Field{ { Identifier: "newProtocolVersion", Type: cadence.UInt64Type, @@ -1049,7 +1057,8 @@ func NewProtocolStateVersionUpgradeEventType() *cadence.EventType { Type: cadence.UInt64Type, }, }, - } + nil, + ) } func ufix64FromString(s string) cadence.UFix64 { @@ -1152,10 +1161,10 @@ func NewFlowClusterQCClusterStructType() *cadence.StructType { address, _ := common.HexToAddress("01cf0e2f2f715450") location := common.NewAddressLocation(nil, address, "FlowClusterQC") - return &cadence.StructType{ - Location: location, - QualifiedIdentifier: "FlowClusterQC.Cluster", - Fields: []cadence.Field{ + return cadence.NewStructType( + location, + "FlowClusterQC.Cluster", + []cadence.Field{ { Identifier: "index", Type: cadence.UInt16Type, @@ -1177,5 +1186,6 @@ func NewFlowClusterQCClusterStructType() *cadence.StructType { Type: cadence.NewDictionaryType(cadence.StringType, cadence.UInt64Type), }, }, - } + nil, + ) } From be3db22b4c6d23d10ea818ab1a76465b42a63693 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastian=20M=C3=BCller?= Date: Tue, 25 Jun 2024 11:40:05 -0700 Subject: [PATCH 10/12] lint --- fvm/fvm_signature_test.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/fvm/fvm_signature_test.go b/fvm/fvm_signature_test.go index ae2cbe5498a..b92e461e5e8 100644 --- a/fvm/fvm_signature_test.go +++ b/fvm/fvm_signature_test.go @@ -586,7 +586,7 @@ func TestBLSMultiSignature(t *testing.T) { script := fvm.Script(code).WithArguments( jsoncdc.MustEncode(cadence.Array{ - Values: signatures, + Values: signatures, ArrayType: uint8ArrayArrayType, }), ) @@ -605,7 +605,7 @@ func TestBLSMultiSignature(t *testing.T) { signatures := []cadence.Value{} script := fvm.Script(code).WithArguments( jsoncdc.MustEncode(cadence.Array{ - Values: signatures, + Values: signatures, ArrayType: uint8ArrayArrayType, }), ) @@ -671,7 +671,7 @@ func TestBLSMultiSignature(t *testing.T) { script := fvm.Script(code(BLSSignatureAlgorithm)).WithArguments( jsoncdc.MustEncode(cadence.Array{ - Values: publicKeys, + Values: publicKeys, ArrayType: uint8ArrayArrayType, }), ) @@ -701,7 +701,7 @@ func TestBLSMultiSignature(t *testing.T) { script := fvm.Script(code(signatureAlgorithm)).WithArguments( jsoncdc.MustEncode(cadence.Array{ - Values: publicKeys, + Values: publicKeys, ArrayType: uint8ArrayArrayType, }), ) @@ -717,7 +717,7 @@ func TestBLSMultiSignature(t *testing.T) { var publicKeys []cadence.Value script := fvm.Script(code(BLSSignatureAlgorithm)).WithArguments( jsoncdc.MustEncode(cadence.Array{ - Values: publicKeys, + Values: publicKeys, ArrayType: uint8ArrayArrayType, }), ) @@ -793,11 +793,11 @@ func TestBLSMultiSignature(t *testing.T) { script := fvm.Script(code).WithArguments( jsoncdc.MustEncode(cadence.Array{ // keys - Values: publicKeys, + Values: publicKeys, ArrayType: uint8ArrayArrayType, }), jsoncdc.MustEncode(cadence.Array{ // signatures - Values: signatures, + Values: signatures, ArrayType: uint8ArrayArrayType, }), jsoncdc.MustEncode(cadenceMessage), From 0dbc20e60ea3177b14b745bb63dd6fb2d773e90c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastian=20M=C3=BCller?= Date: Tue, 25 Jun 2024 13:45:44 -0700 Subject: [PATCH 11/12] when removing an empty contract, also remove it from the contract names --- .../migrations/contract_cleanup_migration.go | 122 +++++++++++------- .../contract_cleanup_migration_test.go | 96 +++++++++++++- 2 files changed, 170 insertions(+), 48 deletions(-) diff --git a/cmd/util/ledger/migrations/contract_cleanup_migration.go b/cmd/util/ledger/migrations/contract_cleanup_migration.go index 1173f70792e..e77c2d05eac 100644 --- a/cmd/util/ledger/migrations/contract_cleanup_migration.go +++ b/cmd/util/ledger/migrations/contract_cleanup_migration.go @@ -45,23 +45,28 @@ func (d *ContractCleanupMigration) MigrateAccount( accountRegisters *registers.AccountRegisters, ) error { - // Normalize the account's contract names. - // This will deduplicate and sort the contract names. + // Get the set of all contract names for the account. - contractNames, err := d.normalizeContractNames(accountRegisters) + contractNames, err := d.getContractNames(accountRegisters) if err != nil { return fmt.Errorf( - "failed to normalize contract names for %s: %w", + "failed to get contract names for %s: %w", address.HexWithPrefix(), err, ) } - // Cleanup the code for each contract. - // If the contract code is empty, it will be removed. + contractNameSet := make(map[string]struct{}) + for _, contractName := range contractNames { + contractNameSet[contractName] = struct{}{} + } + + // Cleanup the code for each contract in the account. + // If the contract code is empty, the contract code register will be removed, + // and the contract name will be removed from the account's contract names. for _, contractName := range contractNames { - err = d.cleanupContractCode(accountRegisters, contractName) + removed, err := d.cleanupContractCode(accountRegisters, contractName) if err != nil { return fmt.Errorf( "failed to cleanup contract code for %s: %w", @@ -69,18 +74,40 @@ func (d *ContractCleanupMigration) MigrateAccount( err, ) } + + if removed { + delete(contractNameSet, contractName) + } + } + + // Sort the contract names and set them back to the account. + + newContractNames := make([]string, 0, len(contractNameSet)) + for contractName := range contractNameSet { + newContractNames = append(newContractNames, contractName) + } + + sort.Strings(newContractNames) + + // NOTE: Always set the contract names back to the account, + // even if there are no contract names. + // This effectively clears the contract names register. + + err = d.setContractNames(accountRegisters, newContractNames) + if err != nil { + return fmt.Errorf( + "failed to set contract names for %s: %w", + address.HexWithPrefix(), + err, + ) } return nil } -// normalizeContractNames deduplicates and sorts the account's contract names. -func (d *ContractCleanupMigration) normalizeContractNames( +func (d *ContractCleanupMigration) getContractNames( accountRegisters *registers.AccountRegisters, -) ( - []string, - error, -) { +) ([]string, error) { owner := accountRegisters.Owner() encodedContractNames, err := accountRegisters.Get(owner, flow.ContractNamesKey) @@ -103,76 +130,79 @@ func (d *ContractCleanupMigration) normalizeContractNames( ) } - contractNames = normalizeContractNames(contractNames) + return contractNames, nil +} - newEncodedContractNames, err := environment.EncodeContractNames(contractNames) - if err != nil { - return nil, fmt.Errorf( - "failed to encode contract names: %w", - err, - ) +func (d *ContractCleanupMigration) setContractNames( + accountRegisters *registers.AccountRegisters, + contractNames []string, +) error { + owner := accountRegisters.Owner() + + var newEncodedContractNames []byte + var err error + + // Encode the new contract names, if there are any. + + if len(contractNames) > 0 { + newEncodedContractNames, err = environment.EncodeContractNames(contractNames) + if err != nil { + return fmt.Errorf( + "failed to encode contract names: %w", + err, + ) + } } + // NOTE: always set the contract names register, even if there are not contract names. + // This effectively clears the contract names register. + err = accountRegisters.Set(owner, flow.ContractNamesKey, newEncodedContractNames) if err != nil { - return nil, fmt.Errorf( - "failed to set new contract names: %w", + return fmt.Errorf( + "failed to set contract names: %w", err, ) } - return contractNames, nil -} - -// normalizeContractNames deduplicates and sorts the contract names. -func normalizeContractNames(names []string) []string { - seen := make(map[string]struct{}, len(names)) - deduplicated := make([]string, 0, len(names)) - - for _, name := range names { - if _, ok := seen[name]; ok { - continue - } - - seen[name] = struct{}{} - deduplicated = append(deduplicated, name) - } - - sort.Strings(deduplicated) - - return deduplicated + return nil } // cleanupContractCode removes the code for the contract if it is empty. +// Returns true if the contract code was removed. func (d *ContractCleanupMigration) cleanupContractCode( accountRegisters *registers.AccountRegisters, contractName string, -) error { +) (removed bool, err error) { owner := accountRegisters.Owner() contractKey := flow.ContractKey(contractName) code, err := accountRegisters.Get(owner, contractKey) if err != nil { - return fmt.Errorf( + return false, fmt.Errorf( "failed to get contract code for %s: %w", contractName, err, ) } + // If the contract code is empty, remove the contract code register. + if len(bytes.TrimSpace(code)) == 0 { err = accountRegisters.Set(owner, contractKey, nil) if err != nil { - return fmt.Errorf( + return false, fmt.Errorf( "failed to clear contract code for %s: %w", contractName, err, ) } + + removed = true } - return nil + return removed, nil } func (d *ContractCleanupMigration) Close() error { diff --git a/cmd/util/ledger/migrations/contract_cleanup_migration_test.go b/cmd/util/ledger/migrations/contract_cleanup_migration_test.go index 8adf8d027af..d8a239830a0 100644 --- a/cmd/util/ledger/migrations/contract_cleanup_migration_test.go +++ b/cmd/util/ledger/migrations/contract_cleanup_migration_test.go @@ -14,7 +14,7 @@ import ( "github.com/onflow/flow-go/model/flow" ) -func TestContractCleanupMigration(t *testing.T) { +func TestContractCleanupMigration1(t *testing.T) { t.Parallel() @@ -96,7 +96,6 @@ func TestContractCleanupMigration(t *testing.T) { assert.Equal(t, []string{ contractNameNonEmpty, - contractNameEmpty, }, contractNames, ) @@ -115,3 +114,96 @@ func TestContractCleanupMigration(t *testing.T) { require.NoError(t, err) assert.NotEmpty(t, contractNonEmpty) } + +func TestContractCleanupMigration2(t *testing.T) { + + t.Parallel() + + // Arrange + + address, err := common.HexToAddress("0x4184b8bdf78db9eb") + require.NoError(t, err) + + flowAddress := flow.ConvertAddress(address) + owner := flow.AddressToRegisterOwner(flowAddress) + + const contractNameEmpty1 = "Foo" + const contractNameEmpty2 = "Bar" + + registersByAccount := registers.NewByAccount() + + err = registersByAccount.Set( + owner, + flow.ContractKey(contractNameEmpty1), + // Some whitespace for testing purposes + []byte(" \t \n "), + ) + require.NoError(t, err) + + err = registersByAccount.Set( + owner, + flow.ContractKey(contractNameEmpty2), + []byte("\n \t \n \t"), + ) + require.NoError(t, err) + + encodedContractNames, err := environment.EncodeContractNames([]string{ + // Unsorted and duplicates for testing purposes + contractNameEmpty1, + contractNameEmpty2, + contractNameEmpty1, + contractNameEmpty2, + contractNameEmpty1, + contractNameEmpty1, + contractNameEmpty2, + }) + require.NoError(t, err) + + err = registersByAccount.Set( + owner, + flow.ContractNamesKey, + encodedContractNames, + ) + require.NoError(t, err) + + // Act + + checkingMigration := NewContractCleanupMigration() + + log := zerolog.Nop() + + err = checkingMigration.InitMigration(log, registersByAccount, 1) + require.NoError(t, err) + + accountRegisters := registersByAccount.AccountRegisters(owner) + + err = checkingMigration.MigrateAccount( + context.Background(), + address, + accountRegisters, + ) + require.NoError(t, err) + + // Assert + + encodedContractNames, err = registersByAccount.Get( + owner, + flow.ContractNamesKey, + ) + require.NoError(t, err) + assert.Nil(t, encodedContractNames) + + contractEmpty1, err := registersByAccount.Get( + owner, + flow.ContractKey(contractNameEmpty1), + ) + require.NoError(t, err) + assert.Nil(t, contractEmpty1) + + contractEmpty2, err := registersByAccount.Get( + owner, + flow.ContractKey(contractNameEmpty2), + ) + require.NoError(t, err) + assert.Nil(t, contractEmpty2) +} From 82fc1a4a7b4c7d14180ef59b7df919d04774dc77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastian=20M=C3=BCller?= Date: Tue, 25 Jun 2024 14:35:04 -0700 Subject: [PATCH 12/12] report removed empty contracts and contract names changes --- cmd/util/ledger/migrations/cadence.go | 2 +- .../migrations/contract_cleanup_migration.go | 96 +++++++++++++++++-- .../contract_cleanup_migration_test.go | 80 ++++++++++++++-- 3 files changed, 164 insertions(+), 14 deletions(-) diff --git a/cmd/util/ledger/migrations/cadence.go b/cmd/util/ledger/migrations/cadence.go index 01262efa74a..139463e386d 100644 --- a/cmd/util/ledger/migrations/cadence.go +++ b/cmd/util/ledger/migrations/cadence.go @@ -242,7 +242,7 @@ func NewCadence1ValueMigrations( log, opts.NWorker, []AccountBasedMigration{ - NewContractCleanupMigration(), + NewContractCleanupMigration(rwf), }, ), }, diff --git a/cmd/util/ledger/migrations/contract_cleanup_migration.go b/cmd/util/ledger/migrations/contract_cleanup_migration.go index e77c2d05eac..bee3a8e49a4 100644 --- a/cmd/util/ledger/migrations/contract_cleanup_migration.go +++ b/cmd/util/ledger/migrations/contract_cleanup_migration.go @@ -3,6 +3,7 @@ package migrations import ( "bytes" "context" + "encoding/json" "fmt" "sort" @@ -10,6 +11,7 @@ import ( "github.com/onflow/cadence/runtime/common" + "github.com/onflow/flow-go/cmd/util/ledger/reporters" "github.com/onflow/flow-go/cmd/util/ledger/util/registers" "github.com/onflow/flow-go/fvm/environment" "github.com/onflow/flow-go/model/flow" @@ -17,13 +19,18 @@ import ( // ContractCleanupMigration normalized account's contract names and removes empty contracts. type ContractCleanupMigration struct { - log zerolog.Logger + log zerolog.Logger + reporter reporters.ReportWriter } +const contractCleanupReporterName = "contract-cleanup" + var _ AccountBasedMigration = &ContractCleanupMigration{} -func NewContractCleanupMigration() *ContractCleanupMigration { - return &ContractCleanupMigration{} +func NewContractCleanupMigration(rwf reporters.ReportWriterFactory) *ContractCleanupMigration { + return &ContractCleanupMigration{ + reporter: rwf.ReportWriter(contractCleanupReporterName), + } } func (d *ContractCleanupMigration) InitMigration( @@ -47,7 +54,7 @@ func (d *ContractCleanupMigration) MigrateAccount( // Get the set of all contract names for the account. - contractNames, err := d.getContractNames(accountRegisters) + oldContractNames, err := d.getContractNames(accountRegisters) if err != nil { return fmt.Errorf( "failed to get contract names for %s: %w", @@ -57,7 +64,7 @@ func (d *ContractCleanupMigration) MigrateAccount( } contractNameSet := make(map[string]struct{}) - for _, contractName := range contractNames { + for _, contractName := range oldContractNames { contractNameSet[contractName] = struct{}{} } @@ -65,8 +72,12 @@ func (d *ContractCleanupMigration) MigrateAccount( // If the contract code is empty, the contract code register will be removed, // and the contract name will be removed from the account's contract names. - for _, contractName := range contractNames { - removed, err := d.cleanupContractCode(accountRegisters, contractName) + for contractName := range contractNameSet { + removed, err := d.cleanupContractCode( + address, + accountRegisters, + contractName, + ) if err != nil { return fmt.Errorf( "failed to cleanup contract code for %s: %w", @@ -102,6 +113,14 @@ func (d *ContractCleanupMigration) MigrateAccount( ) } + if !stringSlicesEqual(newContractNames, oldContractNames) { + d.reporter.Write(contractNamesChanged{ + AccountAddress: address, + Old: oldContractNames, + New: newContractNames, + }) + } + return nil } @@ -171,6 +190,7 @@ func (d *ContractCleanupMigration) setContractNames( // cleanupContractCode removes the code for the contract if it is empty. // Returns true if the contract code was removed. func (d *ContractCleanupMigration) cleanupContractCode( + address common.Address, accountRegisters *registers.AccountRegisters, contractName string, ) (removed bool, err error) { @@ -199,6 +219,11 @@ func (d *ContractCleanupMigration) cleanupContractCode( ) } + d.reporter.Write(emptyContractRemoved{ + AccountAddress: address, + ContractName: contractName, + }) + removed = true } @@ -206,5 +231,62 @@ func (d *ContractCleanupMigration) cleanupContractCode( } func (d *ContractCleanupMigration) Close() error { + d.reporter.Close() + return nil } + +type emptyContractRemoved struct { + AccountAddress common.Address + ContractName string +} + +var _ json.Marshaler = emptyContractRemoved{} + +func (e emptyContractRemoved) MarshalJSON() ([]byte, error) { + return json.Marshal(struct { + Kind string `json:"kind"` + AccountAddress string `json:"address"` + ContractName string `json:"name"` + }{ + Kind: "empty-contract-removed", + AccountAddress: e.AccountAddress.HexWithPrefix(), + ContractName: e.ContractName, + }) +} + +func stringSlicesEqual(a, b []string) bool { + if len(a) != len(b) { + return false + } + + for i := range a { + if a[i] != b[i] { + return false + } + } + + return true +} + +type contractNamesChanged struct { + AccountAddress common.Address `json:"address"` + Old []string `json:"old"` + New []string `json:"new"` +} + +var _ json.Marshaler = contractNamesChanged{} + +func (e contractNamesChanged) MarshalJSON() ([]byte, error) { + return json.Marshal(struct { + Kind string `json:"kind"` + AccountAddress string `json:"address"` + Old []string `json:"old"` + New []string `json:"new"` + }{ + Kind: "contract-names-changed", + AccountAddress: e.AccountAddress.HexWithPrefix(), + Old: e.Old, + New: e.New, + }) +} diff --git a/cmd/util/ledger/migrations/contract_cleanup_migration_test.go b/cmd/util/ledger/migrations/contract_cleanup_migration_test.go index d8a239830a0..ec57f9bb99e 100644 --- a/cmd/util/ledger/migrations/contract_cleanup_migration_test.go +++ b/cmd/util/ledger/migrations/contract_cleanup_migration_test.go @@ -67,22 +67,27 @@ func TestContractCleanupMigration1(t *testing.T) { // Act - checkingMigration := NewContractCleanupMigration() + rwf := &testReportWriterFactory{} + + cleanupMigration := NewContractCleanupMigration(rwf) log := zerolog.Nop() - err = checkingMigration.InitMigration(log, registersByAccount, 1) + err = cleanupMigration.InitMigration(log, registersByAccount, 1) require.NoError(t, err) accountRegisters := registersByAccount.AccountRegisters(owner) - err = checkingMigration.MigrateAccount( + err = cleanupMigration.MigrateAccount( context.Background(), address, accountRegisters, ) require.NoError(t, err) + err = cleanupMigration.Close() + require.NoError(t, err) + // Assert encodedContractNames, err = registersByAccount.Get( @@ -113,6 +118,34 @@ func TestContractCleanupMigration1(t *testing.T) { ) require.NoError(t, err) assert.NotEmpty(t, contractNonEmpty) + + reporter := rwf.reportWriters[contractCleanupReporterName] + require.NotNil(t, reporter) + + assert.Equal(t, + []any{ + emptyContractRemoved{ + AccountAddress: address, + ContractName: contractNameEmpty, + }, + contractNamesChanged{ + AccountAddress: address, + Old: []string{ + contractNameEmpty, + contractNameNonEmpty, + contractNameEmpty, + contractNameNonEmpty, + contractNameEmpty, + contractNameEmpty, + contractNameNonEmpty, + }, + New: []string{ + contractNameNonEmpty, + }, + }, + }, + reporter.entries, + ) } func TestContractCleanupMigration2(t *testing.T) { @@ -168,22 +201,27 @@ func TestContractCleanupMigration2(t *testing.T) { // Act - checkingMigration := NewContractCleanupMigration() + rwf := &testReportWriterFactory{} + + cleanupMigration := NewContractCleanupMigration(rwf) log := zerolog.Nop() - err = checkingMigration.InitMigration(log, registersByAccount, 1) + err = cleanupMigration.InitMigration(log, registersByAccount, 1) require.NoError(t, err) accountRegisters := registersByAccount.AccountRegisters(owner) - err = checkingMigration.MigrateAccount( + err = cleanupMigration.MigrateAccount( context.Background(), address, accountRegisters, ) require.NoError(t, err) + err = cleanupMigration.Close() + require.NoError(t, err) + // Assert encodedContractNames, err = registersByAccount.Get( @@ -206,4 +244,34 @@ func TestContractCleanupMigration2(t *testing.T) { ) require.NoError(t, err) assert.Nil(t, contractEmpty2) + + reporter := rwf.reportWriters[contractCleanupReporterName] + require.NotNil(t, reporter) + + assert.Equal(t, + []any{ + emptyContractRemoved{ + AccountAddress: address, + ContractName: contractNameEmpty1, + }, + emptyContractRemoved{ + AccountAddress: address, + ContractName: contractNameEmpty2, + }, + contractNamesChanged{ + AccountAddress: address, + Old: []string{ + contractNameEmpty1, + contractNameEmpty2, + contractNameEmpty1, + contractNameEmpty2, + contractNameEmpty1, + contractNameEmpty1, + contractNameEmpty2, + }, + New: []string{}, + }, + }, + reporter.entries, + ) }