Skip to content

Commit a53b9b8

Browse files
authored
Merge pull request #6312 from onflow/supun/fix-storage-cap-migration
Report and skip storage caps with no borrow type
2 parents 2158798 + 69c5da9 commit a53b9b8

File tree

9 files changed

+213
-56
lines changed

9 files changed

+213
-56
lines changed

cmd/util/ledger/migrations/cadence.go

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ import (
55
_ "embed"
66
"fmt"
77

8+
"github.com/rs/zerolog"
9+
810
"github.com/onflow/cadence/migrations/capcons"
911
"github.com/onflow/cadence/migrations/statictypes"
1012
"github.com/onflow/cadence/runtime"
1113
"github.com/onflow/cadence/runtime/common"
1214
"github.com/onflow/cadence/runtime/interpreter"
13-
"github.com/rs/zerolog"
1415

1516
"github.com/onflow/flow-go/cmd/util/ledger/reporters"
1617
"github.com/onflow/flow-go/cmd/util/ledger/util"
@@ -228,16 +229,22 @@ type IssueStorageCapConMigration struct {
228229
programs map[runtime.Location]*interpreter.Program
229230
mapping *capcons.CapabilityMapping
230231
reporter reporters.ReportWriter
232+
logVerboseDiff bool
233+
verboseErrorOutput bool
234+
errorMessageHandler *errorMessageHandler
235+
log zerolog.Logger
231236
}
232237

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

235240
func NewIssueStorageCapConMigration(
236241
rwf reporters.ReportWriterFactory,
242+
errorMessageHandler *errorMessageHandler,
237243
chainID flow.ChainID,
238244
storageDomainCapabilities *capcons.AccountsCapabilities,
239245
programs map[runtime.Location]*interpreter.Program,
240246
capabilityMapping *capcons.CapabilityMapping,
247+
opts Options,
241248
) *IssueStorageCapConMigration {
242249
return &IssueStorageCapConMigration{
243250
name: "cadence_storage_cap_con_issue_migration",
@@ -246,14 +253,19 @@ func NewIssueStorageCapConMigration(
246253
accountsCapabilities: storageDomainCapabilities,
247254
programs: programs,
248255
mapping: capabilityMapping,
256+
logVerboseDiff: opts.LogVerboseDiff,
257+
verboseErrorOutput: opts.VerboseErrorOutput,
258+
errorMessageHandler: errorMessageHandler,
249259
}
250260
}
251261

252262
func (m *IssueStorageCapConMigration) InitMigration(
253-
_ zerolog.Logger,
263+
log zerolog.Logger,
254264
_ *registers.ByAccount,
255265
_ int,
256266
) error {
267+
m.log = log.With().Str("migration", m.name).Logger()
268+
257269
// During the migration, we only provide already checked programs,
258270
// no parsing/checking of contracts is expected.
259271

@@ -309,30 +321,22 @@ func (m *IssueStorageCapConMigration) MigrateAccount(
309321
idGenerator: idGenerator,
310322
}
311323

324+
reporter := newValueMigrationReporter(
325+
m.reporter,
326+
m.log,
327+
m.errorMessageHandler,
328+
m.verboseErrorOutput,
329+
)
330+
312331
capcons.IssueAccountCapabilities(
313332
migrationRuntime.Interpreter,
333+
reporter,
314334
address,
315335
accountCapabilities,
316336
handler,
317337
m.mapping,
318338
)
319339

320-
// It would be ideal to do the reporting inside `IssueAccountCapabilities` function above.
321-
// However, that doesn't have the access to the reporter. So doing it here.
322-
for _, capability := range accountCapabilities.Capabilities {
323-
id, _, _ := m.mapping.Get(interpreter.AddressPath{
324-
Address: address,
325-
Path: capability.Path,
326-
})
327-
328-
m.reporter.Write(storageCapconIssuedEntry{
329-
AccountAddress: address,
330-
Path: capability.Path,
331-
BorrowType: capability.BorrowType,
332-
CapabilityID: id,
333-
})
334-
}
335-
336340
return nil
337341
}
338342

@@ -409,10 +413,12 @@ func NewCadence1ValueMigrations(
409413
func(opts Options) (string, AccountBasedMigration) {
410414
migration := NewIssueStorageCapConMigration(
411415
rwf,
416+
errorMessageHandler,
412417
opts.ChainID,
413418
storageDomainCapabilities,
414419
programs,
415420
capabilityMapping,
421+
opts,
416422
)
417423
return migration.name, migration
418424

cmd/util/ledger/migrations/cadence_values_migration.go

Lines changed: 64 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,7 @@ type cadenceValueMigrationReporter struct {
449449

450450
var _ capcons.LinkMigrationReporter = &cadenceValueMigrationReporter{}
451451
var _ capcons.CapabilityMigrationReporter = &cadenceValueMigrationReporter{}
452+
var _ capcons.StorageCapabilityMigrationReporter = &cadenceValueMigrationReporter{}
452453
var _ migrations.Reporter = &cadenceValueMigrationReporter{}
453454

454455
func newValueMigrationReporter(
@@ -529,6 +530,30 @@ func (t *cadenceValueMigrationReporter) MissingCapabilityID(
529530
})
530531
}
531532

533+
func (t *cadenceValueMigrationReporter) MissingBorrowType(
534+
accountAddress common.Address,
535+
addressPath interpreter.AddressPath,
536+
) {
537+
t.reportWriter.Write(storageCapConsMissingBorrowTypeEntry{
538+
AccountAddress: accountAddress,
539+
AddressPath: addressPath,
540+
})
541+
}
542+
543+
func (t *cadenceValueMigrationReporter) IssuedStorageCapabilityController(
544+
accountAddress common.Address,
545+
addressPath interpreter.AddressPath,
546+
borrowType *interpreter.ReferenceStaticType,
547+
capabilityID interpreter.UInt64Value,
548+
) {
549+
t.reportWriter.Write(storageCapConIssuedEntry{
550+
AccountAddress: accountAddress,
551+
AddressPath: addressPath,
552+
BorrowType: borrowType,
553+
CapabilityID: capabilityID,
554+
})
555+
}
556+
532557
func (t *cadenceValueMigrationReporter) MigratedLink(
533558
accountAddressPath interpreter.AddressPath,
534559
capabilityID interpreter.UInt64Value,
@@ -801,35 +826,66 @@ func (e dictionaryKeyConflictEntry) MarshalJSON() ([]byte, error) {
801826
})
802827
}
803828

804-
// storageCapconIssuedEntry
829+
// storageCapConIssuedEntry
805830

806-
type storageCapconIssuedEntry struct {
831+
type storageCapConIssuedEntry struct {
807832
AccountAddress common.Address
808-
Path interpreter.PathValue
833+
AddressPath interpreter.AddressPath
809834
BorrowType interpreter.StaticType
810835
CapabilityID interpreter.UInt64Value
811836
}
812837

813-
var _ valueMigrationReportEntry = storageCapconIssuedEntry{}
838+
var _ valueMigrationReportEntry = storageCapConIssuedEntry{}
814839

815-
func (e storageCapconIssuedEntry) accountAddress() common.Address {
840+
func (e storageCapConIssuedEntry) accountAddress() common.Address {
816841
return e.AccountAddress
817842
}
818843

819-
var _ json.Marshaler = storageCapconIssuedEntry{}
844+
var _ json.Marshaler = storageCapConIssuedEntry{}
820845

821-
func (e storageCapconIssuedEntry) MarshalJSON() ([]byte, error) {
846+
func (e storageCapConIssuedEntry) MarshalJSON() ([]byte, error) {
822847
return json.Marshal(struct {
823848
Kind string `json:"kind"`
824849
AccountAddress string `json:"account_address"`
850+
Address string `json:"address"`
825851
Path string `json:"path"`
826852
BorrowType string `json:"borrow_type"`
827853
CapabilityID string `json:"capability_id"`
828854
}{
829855
Kind: "storage-capcon-issued",
830856
AccountAddress: e.AccountAddress.HexWithPrefix(),
831-
Path: e.Path.String(),
857+
Address: e.AddressPath.Address.HexWithPrefix(),
858+
Path: e.AddressPath.Path.String(),
832859
BorrowType: string(e.BorrowType.ID()),
833860
CapabilityID: e.CapabilityID.String(),
834861
})
835862
}
863+
864+
// StorageCapConMissingBorrowType
865+
866+
type storageCapConsMissingBorrowTypeEntry struct {
867+
AccountAddress common.Address
868+
AddressPath interpreter.AddressPath
869+
}
870+
871+
var _ valueMigrationReportEntry = storageCapConsMissingBorrowTypeEntry{}
872+
873+
func (e storageCapConsMissingBorrowTypeEntry) accountAddress() common.Address {
874+
return e.AccountAddress
875+
}
876+
877+
var _ json.Marshaler = storageCapConsMissingBorrowTypeEntry{}
878+
879+
func (e storageCapConsMissingBorrowTypeEntry) MarshalJSON() ([]byte, error) {
880+
return json.Marshal(struct {
881+
Kind string `json:"kind"`
882+
AccountAddress string `json:"account_address"`
883+
Address string `json:"address"`
884+
Path string `json:"path"`
885+
}{
886+
Kind: "storage-capcon-missing-borrow-type",
887+
AccountAddress: e.AccountAddress.HexWithPrefix(),
888+
Address: e.AddressPath.Address.HexWithPrefix(),
889+
Path: e.AddressPath.Path.String(),
890+
})
891+
}

0 commit comments

Comments
 (0)