From c368eac796faae33a012d5c15f03153a678dcd8e Mon Sep 17 00:00:00 2001 From: Alex Vanin Date: Tue, 29 Jun 2021 23:30:42 +0300 Subject: [PATCH] [#100] netmap: Migrate netmap candidates storage Signed-off-by: Alex Vanin --- netmap/netmap_contract.go | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/netmap/netmap_contract.go b/netmap/netmap_contract.go index a6ccf9d..afb4a85 100644 --- a/netmap/netmap_contract.go +++ b/netmap/netmap_contract.go @@ -33,7 +33,6 @@ type ( const ( version = 1 - netmapKey = "netmap" configuredKey = "initconfig" notaryDisabledKey = "notary" innerRingKey = "innerring" @@ -61,7 +60,10 @@ var ( // _deploy function sets up initial list of inner ring public keys. func _deploy(data interface{}, isUpdate bool) { + ctx := storage.GetContext() + if isUpdate { + migrateNetmapCandidates(ctx) // from v0.9.1 to v0.9.2 return } @@ -72,8 +74,6 @@ func _deploy(data interface{}, isUpdate bool) { addrContainer := args[3].(interop.Hash160) keys := args[4].([]interop.PublicKey) - ctx := storage.GetContext() - if !common.HasUpdateAccess(ctx) { panic("only owner can reinitialize contract") } @@ -87,8 +87,6 @@ func _deploy(data interface{}, isUpdate bool) { // epoch number is a little endian int, it doesn't need to be serialized storage.Put(ctx, snapshotEpoch, 0) - // simplified: this used for const sysfee in AddPeer method - common.SetSerialized(ctx, netmapKey, []netmapNode{}) common.SetSerialized(ctx, snapshot0Key, []netmapNode{}) common.SetSerialized(ctx, snapshot1Key, []netmapNode{}) @@ -113,6 +111,24 @@ func _deploy(data interface{}, isUpdate bool) { runtime.Log("netmap contract initialized") } +func migrateNetmapCandidates(ctx storage.Context) { + const netmapKey = "netmap" + + data := storage.Get(ctx, netmapKey) + if data == nil { + return + } + + candidates := std.Deserialize(data.([]byte)).([]netmapNode) + + for i := range candidates { + candidate := candidates[i] + addToNetmap(ctx, candidate.node) + } + + storage.Delete(ctx, netmapKey) +} + func Migrate(script []byte, manifest []byte, data interface{}) bool { ctx := storage.GetReadOnlyContext()