From 79b4f2cc6434824e2b6ba41255f616308c818178 Mon Sep 17 00:00:00 2001 From: Alex Vanin Date: Thu, 3 Jun 2021 11:25:05 +0300 Subject: [PATCH] [#88] container: Migrate container and eACL structures In v0.8.0: - containers were stored as stable marshaled binary, In v0.9.0: - containers are stored the same way as eACL, - eACL structure has new `token []byte field`. Signed-off-by: Alex Vanin --- container/container_contract.go | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/container/container_contract.go b/container/container_contract.go index d11408c..ec93703 100644 --- a/container/container_contract.go +++ b/container/container_contract.go @@ -64,7 +64,10 @@ var ( ) func _deploy(data interface{}, isUpdate bool) { + ctx := storage.GetContext() + if isUpdate { + migrateContainerAndEACLStructures(ctx) // from v0.8.0 to v0.9.0 return } @@ -75,8 +78,6 @@ func _deploy(data interface{}, isUpdate bool) { addrBalance := args[3].(interop.Hash160) addrID := args[4].(interop.Hash160) - ctx := storage.GetContext() - if !common.HasUpdateAccess(ctx) { panic("only owner can reinitialize contract") } @@ -100,6 +101,34 @@ func _deploy(data interface{}, isUpdate bool) { runtime.Log("container contract initialized") } +func migrateContainerAndEACLStructures(ctx storage.Context) { + eACLKeyLength := containerIDSize + len(eACLPrefix) + + it := storage.Find(ctx, []byte{}, storage.None) + for iterator.Next(it) { + pair := iterator.Value(it).([]interface{}) + key := pair[0].([]byte) + + switch len(key) { + case containerIDSize: // migrate containers + val := pair[1].([]byte) + newContainer := Container{value: val} + + common.SetSerialized(ctx, key, newContainer) + case eACLKeyLength: // migrate eACLs + val := pair[1].([]byte) + eacl := std.Deserialize(val).(ExtendedACL) + newEACL := ExtendedACL{ + value: eacl.value, + sig: eacl.sig, + pub: eacl.pub, + } + + common.SetSerialized(ctx, key, newEACL) + } + } +} + func Migrate(script []byte, manifest []byte, data interface{}) bool { ctx := storage.GetReadOnlyContext()