Skip to content

Commit

Permalink
Add reporting to issue storage capcon
Browse files Browse the repository at this point in the history
  • Loading branch information
SupunS committed Aug 8, 2024
1 parent 8c28fee commit 97bc1d9
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 8 deletions.
56 changes: 48 additions & 8 deletions cmd/util/ledger/migrations/cadence.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,11 +221,32 @@ type NamedMigration struct {
}

type IssueStorageCapConMigration struct {
name string
chainID flow.ChainID
accountsCapabilities *capcons.AccountsCapabilities
interpreterMigrationRuntimeConfig InterpreterMigrationRuntimeConfig
programs map[runtime.Location]*interpreter.Program
mapping *capcons.CapabilityMapping
reporter reporters.ReportWriter
}

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

func NewIssueStorageCapConMigration(
rwf reporters.ReportWriterFactory,
chainID flow.ChainID,
storageDomainCapabilities *capcons.AccountsCapabilities,
programs map[runtime.Location]*interpreter.Program,
capabilityMapping *capcons.CapabilityMapping,
) *IssueStorageCapConMigration {
return &IssueStorageCapConMigration{
name: "cadence_storage_cap_con_issue_migration",
reporter: rwf.ReportWriter(issueStorageCapConMigrationReporterName),
chainID: chainID,
accountsCapabilities: storageDomainCapabilities,
programs: programs,
mapping: capabilityMapping,
}
}

func (m *IssueStorageCapConMigration) InitMigration(
Expand Down Expand Up @@ -296,10 +317,27 @@ func (m *IssueStorageCapConMigration) MigrateAccount(
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
}

func (*IssueStorageCapConMigration) Close() error {
func (m *IssueStorageCapConMigration) Close() error {
m.reporter.Close()
return nil
}

Expand Down Expand Up @@ -369,13 +407,15 @@ func NewCadence1ValueMigrations(
return migration.name, migration
},
func(opts Options) (string, AccountBasedMigration) {
return "cadence_storage_cap_con_issue_migration",
&IssueStorageCapConMigration{
chainID: opts.ChainID,
accountsCapabilities: storageDomainCapabilities,
programs: programs,
mapping: capabilityMapping,
}
migration := NewIssueStorageCapConMigration(
rwf,
opts.ChainID,
storageDomainCapabilities,
programs,
capabilityMapping,
)
return migration.name, migration

},
func(opts Options) (string, AccountBasedMigration) {
migration := NewCadence1LinkValueMigration(
Expand Down
33 changes: 33 additions & 0 deletions cmd/util/ledger/migrations/cadence_values_migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -800,3 +800,36 @@ func (e dictionaryKeyConflictEntry) MarshalJSON() ([]byte, error) {
Path: e.AddressPath.Path.String(),
})
}

// storageCapconIssuedEntry

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

var _ valueMigrationReportEntry = storageCapconIssuedEntry{}

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

var _ json.Marshaler = storageCapconIssuedEntry{}

func (e storageCapconIssuedEntry) MarshalJSON() ([]byte, error) {
return json.Marshal(struct {
Kind string `json:"kind"`
AccountAddress string `json:"account_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(),
BorrowType: string(e.BorrowType.ID()),
CapabilityID: e.CapabilityID.String(),
})
}
27 changes: 27 additions & 0 deletions cmd/util/ledger/migrations/cadence_values_migration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2276,4 +2276,31 @@ func TestCapabilityMigration(t *testing.T) {
},
reporter.entries,
)

issueStorageCapConReporter := rwf.reportWriters[issueStorageCapConMigrationReporterName]
require.NotNil(t, issueStorageCapConReporter)
require.Len(t, issueStorageCapConReporter.entries, 1)

entry := issueStorageCapConReporter.entries[0]

require.IsType(t, storageCapconIssuedEntry{}, entry)
storageCapconIssued := entry.(storageCapconIssuedEntry)

actual, err := storageCapconIssued.MarshalJSON()
require.NoError(t, err)

require.JSONEq(t,
//language=JSON
fmt.Sprintf(
`{
"kind": "storage-capcon-issued",
"account_address": "%s",
"path": "/storage/foo",
"borrow_type": "&AnyStruct",
"capability_id": "3"
}`,
addressB.HexWithPrefix(),
),
string(actual),
)
}

0 comments on commit 97bc1d9

Please sign in to comment.