Skip to content

Commit

Permalink
Merge pull request onflow#6341 from onflow/supun/type-requirement-rem…
Browse files Browse the repository at this point in the history
…oval

Allow using interface-converted type-requirement in fields
  • Loading branch information
SupunS authored Aug 21, 2024
2 parents 122fdb6 + aa2515d commit 5ffe36f
Show file tree
Hide file tree
Showing 9 changed files with 1,159 additions and 123 deletions.
95 changes: 79 additions & 16 deletions cmd/util/ledger/migrations/cadence.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ func NewInterfaceTypeConversionRules(chainID flow.ChainID) StaticTypeMigrationRu
}
}

func NewCompositeTypeConversionRules(chainID flow.ChainID) StaticTypeMigrationRules {
func NewCompositeTypeConversionRules(
chainID flow.ChainID,
legacyTypeRequirements *LegacyTypeRequirements,
) StaticTypeMigrationRules {
systemContracts := systemcontracts.SystemContractsForChain(chainID)

oldFungibleTokenVaultCompositeType, newFungibleTokenVaultType :=
Expand All @@ -51,21 +54,42 @@ func NewCompositeTypeConversionRules(chainID flow.ChainID) StaticTypeMigrationRu
oldNonFungibleTokenCollectionCompositeType, newNonFungibleTokenCollectionType :=
nonFungibleTokenCompositeToInterfaceRule(systemContracts, "Collection")

return StaticTypeMigrationRules{
rules := StaticTypeMigrationRules{
oldFungibleTokenVaultCompositeType.ID(): newFungibleTokenVaultType,
oldNonFungibleTokenNFTCompositeType.ID(): newNonFungibleTokenNFTType,
oldNonFungibleTokenCollectionCompositeType.ID(): newNonFungibleTokenCollectionType,
}

for _, typeRequirement := range legacyTypeRequirements.typeRequirements {
oldType, newType := compositeToInterfaceRule(
typeRequirement.Address,
typeRequirement.ContractName,
typeRequirement.TypeName,
)

rules[oldType.ID()] = newType
}

return rules
}

func NewCadence1InterfaceStaticTypeConverter(chainID flow.ChainID) statictypes.InterfaceTypeConverterFunc {
rules := NewInterfaceTypeConversionRules(chainID)
return NewStaticTypeMigration[*interpreter.InterfaceStaticType](rules)
return NewStaticTypeMigration[*interpreter.InterfaceStaticType](
func() StaticTypeMigrationRules {
return NewInterfaceTypeConversionRules(chainID)
},
)
}

func NewCadence1CompositeStaticTypeConverter(chainID flow.ChainID) statictypes.CompositeTypeConverterFunc {
rules := NewCompositeTypeConversionRules(chainID)
return NewStaticTypeMigration[*interpreter.CompositeStaticType](rules)
func NewCadence1CompositeStaticTypeConverter(
chainID flow.ChainID,
legacyTypeRequirements *LegacyTypeRequirements,
) statictypes.CompositeTypeConverterFunc {
return NewStaticTypeMigration[*interpreter.CompositeStaticType](
func() StaticTypeMigrationRules {
return NewCompositeTypeConversionRules(chainID, legacyTypeRequirements)
},
)
}

func nonFungibleTokenCompositeToInterfaceRule(
Expand All @@ -77,11 +101,26 @@ func nonFungibleTokenCompositeToInterfaceRule(
) {
contract := systemContracts.NonFungibleToken

qualifiedIdentifier := fmt.Sprintf("%s.%s", contract.Name, identifier)
return compositeToInterfaceRule(
common.Address(contract.Address),
contract.Name,
identifier,
)
}

func compositeToInterfaceRule(
address common.Address,
contractName string,
typeName string,
) (
*interpreter.CompositeStaticType,
*interpreter.IntersectionStaticType,
) {
qualifiedIdentifier := fmt.Sprintf("%s.%s", contractName, typeName)

location := common.AddressLocation{
Address: common.Address(contract.Address),
Name: contract.Name,
Address: address,
Name: contractName,
}

nftTypeID := location.TypeID(nil, qualifiedIdentifier)
Expand Down Expand Up @@ -388,6 +427,7 @@ func NewCadence1ValueMigrations(
log zerolog.Logger,
rwf reporters.ReportWriterFactory,
importantLocations map[common.AddressLocation]struct{},
legacyTypeRequirements *LegacyTypeRequirements,
opts Options,
) (migs []NamedMigration) {

Expand Down Expand Up @@ -444,7 +484,7 @@ func NewCadence1ValueMigrations(
rwf,
errorMessageHandler,
programs,
NewCadence1CompositeStaticTypeConverter(opts.ChainID),
NewCadence1CompositeStaticTypeConverter(opts.ChainID, legacyTypeRequirements),
NewCadence1InterfaceStaticTypeConverter(opts.ChainID),
storageDomainCapabilities,
opts,
Expand Down Expand Up @@ -535,10 +575,11 @@ const stagedContractUpdateMigrationName = "staged-contracts-update-migration"
func NewCadence1ContractsMigrations(
log zerolog.Logger,
rwf reporters.ReportWriterFactory,
importantLocations map[common.AddressLocation]struct{},
legacyTypeRequirements *LegacyTypeRequirements,
opts Options,
) (
migs []NamedMigration,
importantLocations map[common.AddressLocation]struct{},
) {

stagedContractsMigrationOptions := StagedContractsMigrationOptions{
Expand All @@ -552,10 +593,10 @@ func NewCadence1ContractsMigrations(
Burner: opts.BurnerContractChange,
}

var systemContractsMigration *StagedContractsMigration
systemContractsMigration, importantLocations = NewSystemContractsMigration(
systemContractsMigration := NewSystemContractsMigration(
log,
rwf,
importantLocations,
systemContractsMigrationOptions,
)

Expand All @@ -564,6 +605,7 @@ func NewCadence1ContractsMigrations(
"staged-contracts-migration",
log,
rwf,
legacyTypeRequirements,
stagedContractsMigrationOptions,
).WithContractUpdateValidation().
WithStagedContractUpdates(opts.StagedContracts)
Expand Down Expand Up @@ -621,7 +663,7 @@ func NewCadence1ContractsMigrations(
},
)

return migs, importantLocations
return migs
}

var testnetAccountsWithBrokenSlabReferences = func() map[common.Address]struct{} {
Expand Down Expand Up @@ -748,9 +790,29 @@ func NewCadence1Migrations(
}
}

cadence1ContractsMigrations, importantLocations := NewCadence1ContractsMigrations(
importantLocations := make(map[common.AddressLocation]struct{})
legacyTypeRequirements := &LegacyTypeRequirements{}

cadenceTypeRequirementsExtractor := NewTypeRequirementsExtractingMigration(
log,
rwf,
importantLocations,
legacyTypeRequirements,
)

migs = append(
migs,
NamedMigration{
Name: "extract-type-requirements",
Migrate: cadenceTypeRequirementsExtractor,
},
)

cadence1ContractsMigrations := NewCadence1ContractsMigrations(
log,
rwf,
importantLocations,
legacyTypeRequirements,
opts,
)

Expand All @@ -765,6 +827,7 @@ func NewCadence1Migrations(
log,
rwf,
importantLocations,
legacyTypeRequirements,
opts,
)...,
)
Expand Down
Loading

0 comments on commit 5ffe36f

Please sign in to comment.