You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Since one account can now have multiple contracts, they are now saved differently. The register key was previously just "code" but will now be "code.{contract_name}".
Because of this all of the old registers need to be moved.
However, because previously the code could contain multiple contract interfaces and one contract, but can now only contain one contract interface or one contract, the code needs to split up during migration.
below is some pseudocode of how the migration might go:
defmigrate(register):
ifnotneeds_migration(register):
# every account has a code register, even if it is empty# the number of registers that need migration is equal to the number of accountsreturn# whatever we do, we need to delete the old register firstdelete_register(register)
ifregister_is_empty(register):
# most accounts won't have any code deployed# we can simply ignore those (we already deleted them)return# load codetry:
parsed_programs=parse_program(register.code)
except:
# program is not parsable! should not happen!raiseException()
iflen(parsed_programs) ==1:
# only one interface or contract (easy case). Should be most common# just save the new contractsave_contract(register, parsed_programs[0])
return# this case is much harder. We have one or more interfaces and one contract# they need to be saved separately, so multiple registers will be created.# However they might depend on each other, so we potentially need to add some imports to each of the pieces# In the worst case scenario the interfaces depend on each other. In which case I dont know what to do# (there needs to be more than one interface for this case)whilelen(parsed_programs) >0:
# there should be at least one that can be parsed without the others (otherwise there is circular dependencies)first_ok_program=next(pforpinparsed_programsifp.is_valid)
# save itnew_register=save_contract(register, first_ok_program)
# remove it from the listparsed_programs.remove(first_ok_program)
# add imports to the rest# this is an aggressive approach there will mostly be more imports than neededforpinparsed_programs:
add_contract_import(p, first_ok_program, new_register)
# if there is a circular dependency we can just report an error and solve that case manually.# there should only be a handful of cases like that, if anydefneeds_migration(register) ->bool:
"""Only need migration if key is 'code' ('636f6465' in hex)"""returnregister.key=='636f6465'defregister_is_empty(register):
returnregister.value==''defdelete_register(register):
passclassParsedProgram:
def__init__(self) ->None:
self.name=''# the interface or contract nameself.code=''self.is_valid=True# if the contract can be parsed as isdefsave_contract(register, program: ParsedProgram):
new_register=copy(register)
new_register.key=hex('code.'+program.name)
new_register.code=hex(program.code)
save_register(new_register)
returnnew_registerdefsave_register(register):
passdefparse_program(code) ->List[ParsedProgram]:
# parse the program and split it so there is only one interface or contract per program# duplicate all the original imports on all programsreturn []
defadd_contract_import(program: ParsedProgram, dependency_program: ParsedProgram, dependency_register):
# prepend importprogram.code=f'import {dependency_program.name} from {dependency_register.owner}\n'+program.codeprogram.is_valid=# check it is valid now
The text was updated successfully, but these errors were encountered:
janezpodhostnik
changed the title
Multiple Contracts per Account - Register Migration
Multiple Contracts per Account - Code Register Migration
Oct 7, 2020
ref: https://github.com/dapperlabs/flow-go/issues/4806
Since one account can now have multiple contracts, they are now saved differently. The register key was previously just "code" but will now be "code.{contract_name}".
Because of this all of the old registers need to be moved.
However, because previously the code could contain multiple contract interfaces and one contract, but can now only contain one contract interface or one contract, the code needs to split up during migration.
below is some pseudocode of how the migration might go:
The text was updated successfully, but these errors were encountered: