Skip to content

Commit

Permalink
Merge pull request #6312 from onflow/supun/fix-storage-cap-migration
Browse files Browse the repository at this point in the history
Report and skip storage caps with no borrow type
  • Loading branch information
SupunS authored Aug 9, 2024
2 parents 2158798 + 69c5da9 commit a53b9b8
Show file tree
Hide file tree
Showing 9 changed files with 213 additions and 56 deletions.
42 changes: 24 additions & 18 deletions cmd/util/ledger/migrations/cadence.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import (
_ "embed"
"fmt"

"github.com/rs/zerolog"

"github.com/onflow/cadence/migrations/capcons"
"github.com/onflow/cadence/migrations/statictypes"
"github.com/onflow/cadence/runtime"
"github.com/onflow/cadence/runtime/common"
"github.com/onflow/cadence/runtime/interpreter"
"github.com/rs/zerolog"

"github.com/onflow/flow-go/cmd/util/ledger/reporters"
"github.com/onflow/flow-go/cmd/util/ledger/util"
Expand Down Expand Up @@ -228,16 +229,22 @@ type IssueStorageCapConMigration struct {
programs map[runtime.Location]*interpreter.Program
mapping *capcons.CapabilityMapping
reporter reporters.ReportWriter
logVerboseDiff bool
verboseErrorOutput bool
errorMessageHandler *errorMessageHandler
log zerolog.Logger
}

const issueStorageCapConMigrationReporterName = "cadence-storage-capcon-issue-migration"

func NewIssueStorageCapConMigration(
rwf reporters.ReportWriterFactory,
errorMessageHandler *errorMessageHandler,
chainID flow.ChainID,
storageDomainCapabilities *capcons.AccountsCapabilities,
programs map[runtime.Location]*interpreter.Program,
capabilityMapping *capcons.CapabilityMapping,
opts Options,
) *IssueStorageCapConMigration {
return &IssueStorageCapConMigration{
name: "cadence_storage_cap_con_issue_migration",
Expand All @@ -246,14 +253,19 @@ func NewIssueStorageCapConMigration(
accountsCapabilities: storageDomainCapabilities,
programs: programs,
mapping: capabilityMapping,
logVerboseDiff: opts.LogVerboseDiff,
verboseErrorOutput: opts.VerboseErrorOutput,
errorMessageHandler: errorMessageHandler,
}
}

func (m *IssueStorageCapConMigration) InitMigration(
_ zerolog.Logger,
log zerolog.Logger,
_ *registers.ByAccount,
_ int,
) error {
m.log = log.With().Str("migration", m.name).Logger()

// During the migration, we only provide already checked programs,
// no parsing/checking of contracts is expected.

Expand Down Expand Up @@ -309,30 +321,22 @@ func (m *IssueStorageCapConMigration) MigrateAccount(
idGenerator: idGenerator,
}

reporter := newValueMigrationReporter(
m.reporter,
m.log,
m.errorMessageHandler,
m.verboseErrorOutput,
)

capcons.IssueAccountCapabilities(
migrationRuntime.Interpreter,
reporter,
address,
accountCapabilities,
handler,
m.mapping,
)

// It would be ideal to do the reporting inside `IssueAccountCapabilities` function above.
// However, that doesn't have the access to the reporter. So doing it here.
for _, capability := range accountCapabilities.Capabilities {
id, _, _ := m.mapping.Get(interpreter.AddressPath{
Address: address,
Path: capability.Path,
})

m.reporter.Write(storageCapconIssuedEntry{
AccountAddress: address,
Path: capability.Path,
BorrowType: capability.BorrowType,
CapabilityID: id,
})
}

return nil
}

Expand Down Expand Up @@ -409,10 +413,12 @@ func NewCadence1ValueMigrations(
func(opts Options) (string, AccountBasedMigration) {
migration := NewIssueStorageCapConMigration(
rwf,
errorMessageHandler,
opts.ChainID,
storageDomainCapabilities,
programs,
capabilityMapping,
opts,
)
return migration.name, migration

Expand Down
72 changes: 64 additions & 8 deletions cmd/util/ledger/migrations/cadence_values_migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,7 @@ type cadenceValueMigrationReporter struct {

var _ capcons.LinkMigrationReporter = &cadenceValueMigrationReporter{}
var _ capcons.CapabilityMigrationReporter = &cadenceValueMigrationReporter{}
var _ capcons.StorageCapabilityMigrationReporter = &cadenceValueMigrationReporter{}
var _ migrations.Reporter = &cadenceValueMigrationReporter{}

func newValueMigrationReporter(
Expand Down Expand Up @@ -529,6 +530,30 @@ func (t *cadenceValueMigrationReporter) MissingCapabilityID(
})
}

func (t *cadenceValueMigrationReporter) MissingBorrowType(
accountAddress common.Address,
addressPath interpreter.AddressPath,
) {
t.reportWriter.Write(storageCapConsMissingBorrowTypeEntry{
AccountAddress: accountAddress,
AddressPath: addressPath,
})
}

func (t *cadenceValueMigrationReporter) IssuedStorageCapabilityController(
accountAddress common.Address,
addressPath interpreter.AddressPath,
borrowType *interpreter.ReferenceStaticType,
capabilityID interpreter.UInt64Value,
) {
t.reportWriter.Write(storageCapConIssuedEntry{
AccountAddress: accountAddress,
AddressPath: addressPath,
BorrowType: borrowType,
CapabilityID: capabilityID,
})
}

func (t *cadenceValueMigrationReporter) MigratedLink(
accountAddressPath interpreter.AddressPath,
capabilityID interpreter.UInt64Value,
Expand Down Expand Up @@ -801,35 +826,66 @@ func (e dictionaryKeyConflictEntry) MarshalJSON() ([]byte, error) {
})
}

// storageCapconIssuedEntry
// storageCapConIssuedEntry

type storageCapconIssuedEntry struct {
type storageCapConIssuedEntry struct {
AccountAddress common.Address
Path interpreter.PathValue
AddressPath interpreter.AddressPath
BorrowType interpreter.StaticType
CapabilityID interpreter.UInt64Value
}

var _ valueMigrationReportEntry = storageCapconIssuedEntry{}
var _ valueMigrationReportEntry = storageCapConIssuedEntry{}

func (e storageCapconIssuedEntry) accountAddress() common.Address {
func (e storageCapConIssuedEntry) accountAddress() common.Address {
return e.AccountAddress
}

var _ json.Marshaler = storageCapconIssuedEntry{}
var _ json.Marshaler = storageCapConIssuedEntry{}

func (e storageCapconIssuedEntry) MarshalJSON() ([]byte, error) {
func (e storageCapConIssuedEntry) MarshalJSON() ([]byte, error) {
return json.Marshal(struct {
Kind string `json:"kind"`
AccountAddress string `json:"account_address"`
Address string `json:"address"`
Path string `json:"path"`
BorrowType string `json:"borrow_type"`
CapabilityID string `json:"capability_id"`
}{
Kind: "storage-capcon-issued",
AccountAddress: e.AccountAddress.HexWithPrefix(),
Path: e.Path.String(),
Address: e.AddressPath.Address.HexWithPrefix(),
Path: e.AddressPath.Path.String(),
BorrowType: string(e.BorrowType.ID()),
CapabilityID: e.CapabilityID.String(),
})
}

// StorageCapConMissingBorrowType

type storageCapConsMissingBorrowTypeEntry struct {
AccountAddress common.Address
AddressPath interpreter.AddressPath
}

var _ valueMigrationReportEntry = storageCapConsMissingBorrowTypeEntry{}

func (e storageCapConsMissingBorrowTypeEntry) accountAddress() common.Address {
return e.AccountAddress
}

var _ json.Marshaler = storageCapConsMissingBorrowTypeEntry{}

func (e storageCapConsMissingBorrowTypeEntry) MarshalJSON() ([]byte, error) {
return json.Marshal(struct {
Kind string `json:"kind"`
AccountAddress string `json:"account_address"`
Address string `json:"address"`
Path string `json:"path"`
}{
Kind: "storage-capcon-missing-borrow-type",
AccountAddress: e.AccountAddress.HexWithPrefix(),
Address: e.AddressPath.Address.HexWithPrefix(),
Path: e.AddressPath.Path.String(),
})
}
Loading

0 comments on commit a53b9b8

Please sign in to comment.