From f47058cda74933468c0b65148fe48e43b2079ac6 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Mon, 13 Feb 2023 19:29:05 +0400 Subject: [PATCH] Perform `switchToNotary` actions only for versions earlier than v0.17.0 Signed-off-by: Leonard Lyubich --- alphabet/alphabet_contract.go | 12 +++++- audit/audit_contract.go | 16 ++++++-- balance/balance_contract.go | 16 ++++++-- container/container_contract.go | 15 +++++-- neofsid/neofsid_contract.go | 16 ++++++-- netmap/netmap_contract.go | 66 +++++++++++++++++-------------- reputation/reputation_contract.go | 16 ++++++-- subnet/subnet_contract.go | 16 ++++++-- 8 files changed, 117 insertions(+), 56 deletions(-) diff --git a/alphabet/alphabet_contract.go b/alphabet/alphabet_contract.go index 585ab74b..7261dd33 100644 --- a/alphabet/alphabet_contract.go +++ b/alphabet/alphabet_contract.go @@ -35,9 +35,17 @@ func _deploy(data interface{}, isUpdate bool) { ctx := storage.GetContext() if isUpdate { args := data.([]interface{}) - common.CheckVersion(args[len(args)-1].(int)) + version := args[len(args)-1].(int) - switchToNotary(ctx, args) + common.CheckVersion(version) + + // switch to notary mode if version of the current contract deployment is + // earlier than v0.17.0 (initial version when non-notary mode was taken out of + // use) + // TODO: avoid number magic, add function for version comparison to common package + if version < 17_000 { + switchToNotary(ctx, args) + } return } diff --git a/audit/audit_contract.go b/audit/audit_contract.go index 7fe6fc53..a9cdbe35 100644 --- a/audit/audit_contract.go +++ b/audit/audit_contract.go @@ -41,9 +41,17 @@ func _deploy(data interface{}, isUpdate bool) { ctx := storage.GetContext() if isUpdate { args := data.([]interface{}) - common.CheckVersion(args[len(args)-1].(int)) + version := args[len(args)-1].(int) - switchToNotary(ctx, args) + common.CheckVersion(version) + + // switch to notary mode if version of the current contract deployment is + // earlier than v0.17.0 (initial version when non-notary mode was taken out of + // use) + // TODO: avoid number magic, add function for version comparison to common package + if version < 17_000 { + switchToNotary(ctx) + } return } @@ -62,10 +70,10 @@ func _deploy(data interface{}, isUpdate bool) { // re-initializes contract from non-notary to notary mode. Does nothing if // action has already been done. The function is called on contract update with -// storage.Context and parameters from _deploy. +// storage.Context from _deploy. // // switchToNotary removes values stored by 'netmapScriptHash' and 'notary' keys. -func switchToNotary(ctx storage.Context, args []interface{}) { +func switchToNotary(ctx storage.Context) { const notaryDisabledKey = "notary" // non-notary legacy notaryVal := storage.Get(ctx, notaryDisabledKey) diff --git a/balance/balance_contract.go b/balance/balance_contract.go index 6f28b0d0..81b64f5c 100644 --- a/balance/balance_contract.go +++ b/balance/balance_contract.go @@ -59,9 +59,17 @@ func _deploy(data interface{}, isUpdate bool) { ctx := storage.GetContext() if isUpdate { args := data.([]interface{}) - common.CheckVersion(args[len(args)-1].(int)) + version := args[len(args)-1].(int) - switchToNotary(ctx, args) + common.CheckVersion(version) + + // switch to notary mode if version of the current contract deployment is + // earlier than v0.17.0 (initial version when non-notary mode was taken out of + // use) + // TODO: avoid number magic, add function for version comparison to common package + if version < 17_000 { + switchToNotary(ctx) + } return } @@ -80,14 +88,14 @@ func _deploy(data interface{}, isUpdate bool) { // re-initializes contract from non-notary to notary mode. Does nothing if // action has already been done. The function is called on contract update with -// storage.Context and parameters from _deploy. +// storage.Context from _deploy. // // If contract stores non-empty value by 'ballots' key, switchToNotary panics. // Otherwise, existing value is removed. // // switchToNotary removes values stored by 'netmapScriptHash', // 'containerScriptHash' and 'notary' keys. -func switchToNotary(ctx storage.Context, args []interface{}) { +func switchToNotary(ctx storage.Context) { const notaryDisabledKey = "notary" // non-notary legacy notaryVal := storage.Get(ctx, notaryDisabledKey) diff --git a/container/container_contract.go b/container/container_contract.go index 64ceccf1..4fc47a5e 100644 --- a/container/container_contract.go +++ b/container/container_contract.go @@ -101,7 +101,8 @@ func _deploy(data interface{}, isUpdate bool) { ctx := storage.GetContext() if isUpdate { args := data.([]interface{}) - common.CheckVersion(args[len(args)-1].(int)) + version := args[len(args)-1].(int) + common.CheckVersion(version) it := storage.Find(ctx, []byte{}, storage.None) for iterator.Next(it) { @@ -123,7 +124,13 @@ func _deploy(data interface{}, isUpdate bool) { } } - switchToNotary(ctx, args) + // switch to notary mode if version of the current contract deployment is + // earlier than v0.17.0 (initial version when non-notary mode was taken out of + // use) + // TODO: avoid number magic, add function for version comparison to common package + if version < 17_000 { + switchToNotary(ctx) + } return } @@ -161,13 +168,13 @@ func _deploy(data interface{}, isUpdate bool) { // re-initializes contract from non-notary to notary mode. Does nothing if // action has already been done. The function is called on contract update with -// storage.Context and parameters from _deploy. +// storage.Context from _deploy. // // If contract stores non-empty value by 'ballots' key, switchToNotary panics. // Otherwise, existing value is removed. // // switchToNotary removes value stored by 'notary' key. -func switchToNotary(ctx storage.Context, args []interface{}) { +func switchToNotary(ctx storage.Context) { const notaryDisabledKey = "notary" // non-notary legacy notaryVal := storage.Get(ctx, notaryDisabledKey) diff --git a/neofsid/neofsid_contract.go b/neofsid/neofsid_contract.go index d1736a4c..fa33ac32 100644 --- a/neofsid/neofsid_contract.go +++ b/neofsid/neofsid_contract.go @@ -32,9 +32,17 @@ func _deploy(data interface{}, isUpdate bool) { if isUpdate { args := data.([]interface{}) - common.CheckVersion(args[len(args)-1].(int)) + version := args[len(args)-1].(int) - switchToNotary(ctx, args) + common.CheckVersion(version) + + // switch to notary mode if version of the current contract deployment is + // earlier than v0.17.0 (initial version when non-notary mode was taken out of + // use) + // TODO: avoid number magic, add function for version comparison to common package + if version < 17_000 { + switchToNotary(ctx) + } return } @@ -60,14 +68,14 @@ func _deploy(data interface{}, isUpdate bool) { // re-initializes contract from non-notary to notary mode. Does nothing if // action has already been done. The function is called on contract update with -// storage.Context and parameters from _deploy. +// storage.Context from _deploy. // // If contract stores non-empty value by 'ballots' key, switchToNotary panics. // Otherwise, existing value is removed. // // switchToNotary removes values stored by 'containerScriptHash' and 'notary' // keys. -func switchToNotary(ctx storage.Context, args []interface{}) { +func switchToNotary(ctx storage.Context) { const notaryDisabledKey = "notary" // non-notary legacy notaryVal := storage.Get(ctx, notaryDisabledKey) diff --git a/netmap/netmap_contract.go b/netmap/netmap_contract.go index 625d1e6d..5986151e 100644 --- a/netmap/netmap_contract.go +++ b/netmap/netmap_contract.go @@ -121,39 +121,45 @@ func _deploy(data interface{}, isUpdate bool) { if isUpdate { common.CheckVersion(args.version) - if args.version >= 16*1_000 { // 0.16.0+ already have appropriate format - return - } + if args.version < 16*1_000 { + count := getSnapshotCount(ctx) + prefix := []byte(snapshotKeyPrefix) + for i := 0; i < count; i++ { + key := append(prefix, byte(i)) + data := storage.Get(ctx, key) + if data != nil { + nodes := std.Deserialize(data.([]byte)).([]oldNode) + var newnodes []Node + for j := range nodes { + // Old structure contains only the first field, + // second is implicitly assumed to be Online. + newnodes = append(newnodes, Node{ + BLOB: nodes[j].BLOB, + State: NodeStateOnline, + }) + } + common.SetSerialized(ctx, key, newnodes) + } + } - count := getSnapshotCount(ctx) - prefix := []byte(snapshotKeyPrefix) - for i := 0; i < count; i++ { - key := append(prefix, byte(i)) - data := storage.Get(ctx, key) - if data != nil { - nodes := std.Deserialize(data.([]byte)).([]oldNode) - var newnodes []Node - for j := range nodes { - // Old structure contains only the first field, - // second is implicitly assumed to be Online. - newnodes = append(newnodes, Node{ - BLOB: nodes[j].BLOB, - State: NodeStateOnline, - }) + it := storage.Find(ctx, candidatePrefix, storage.None) + for iterator.Next(it) { + cand := iterator.Value(it).(kv) + oldcan := std.Deserialize(cand.v).(oldCandidate) + newcan := Node{ + BLOB: oldcan.f1.BLOB, + State: oldcan.f2, } - common.SetSerialized(ctx, key, newnodes) + common.SetSerialized(ctx, cand.k, newcan) } } - it := storage.Find(ctx, candidatePrefix, storage.None) - for iterator.Next(it) { - cand := iterator.Value(it).(kv) - oldcan := std.Deserialize(cand.v).(oldCandidate) - newcan := Node{ - BLOB: oldcan.f1.BLOB, - State: oldcan.f2, - } - common.SetSerialized(ctx, cand.k, newcan) + // switch to notary mode if version of the current contract deployment is + // earlier than v0.17.0 (initial version when non-notary mode was taken out of + // use) + // TODO: avoid number magic, add function for version comparison to common package + if args.version < 17_000 { + switchToNotary(ctx) } return @@ -182,13 +188,13 @@ func _deploy(data interface{}, isUpdate bool) { // re-initializes contract from non-notary to notary mode. Does nothing if // action has already been done. The function is called on contract update with -// storage.Context and parameters from _deploy. +// storage.Context from _deploy. // // If contract stores non-empty value by 'ballots' key, switchToNotary panics. // Otherwise, existing value is removed. // // switchToNotary removes values stored by 'innerring' and 'notary' keys. -func switchToNotary(ctx storage.Context, args []interface{}) { +func switchToNotary(ctx storage.Context) { const notaryDisabledKey = "notary" // non-notary legacy notaryVal := storage.Get(ctx, notaryDisabledKey) diff --git a/reputation/reputation_contract.go b/reputation/reputation_contract.go index adc25f5d..a293a117 100644 --- a/reputation/reputation_contract.go +++ b/reputation/reputation_contract.go @@ -22,9 +22,17 @@ func _deploy(data interface{}, isUpdate bool) { if isUpdate { args := data.([]interface{}) - common.CheckVersion(args[len(args)-1].(int)) + version := args[len(args)-1].(int) - switchToNotary(ctx, args) + common.CheckVersion(version) + + // switch to notary mode if version of the current contract deployment is + // earlier than v0.17.0 (initial version when non-notary mode was taken out of + // use) + // TODO: avoid number magic, add function for version comparison to common package + if version < 17_000 { + switchToNotary(ctx) + } return } @@ -42,13 +50,13 @@ func _deploy(data interface{}, isUpdate bool) { // re-initializes contract from non-notary to notary mode. Does nothing if // action has already been done. The function is called on contract update with -// storage.Context and parameters from _deploy. +// storage.Context from _deploy. // // If contract stores non-empty value by 'ballots' key, switchToNotary panics. // Otherwise, existing value is removed. // // switchToNotary removes value stored by 'notary' key. -func switchToNotary(ctx storage.Context, args []interface{}) { +func switchToNotary(ctx storage.Context) { const notaryDisabledKey = "notary" // non-notary legacy notaryVal := storage.Get(ctx, notaryDisabledKey) diff --git a/subnet/subnet_contract.go b/subnet/subnet_contract.go index 7dec100f..4fb695a9 100644 --- a/subnet/subnet_contract.go +++ b/subnet/subnet_contract.go @@ -59,9 +59,17 @@ const ( func _deploy(data interface{}, isUpdate bool) { if isUpdate { args := data.([]interface{}) - common.CheckVersion(args[len(args)-1].(int)) + version := args[len(args)-1].(int) - switchToNotary(storage.GetContext(), args) + common.CheckVersion(version) + + // switch to notary mode if version of the current contract deployment is + // earlier than v0.17.0 (initial version when non-notary mode was taken out of + // use) + // TODO: avoid number magic, add function for version comparison to common package + if version < 17_000 { + switchToNotary(storage.GetContext()) + } return } @@ -77,13 +85,13 @@ func _deploy(data interface{}, isUpdate bool) { // re-initializes contract from non-notary to notary mode. Does nothing if // action has already been done. The function is called on contract update with -// storage.Context and parameters from _deploy. +// storage.Context from _deploy. // // If contract stores non-empty value by 'ballots' key, switchToNotary panics. // Otherwise, existing value is removed. // // switchToNotary removes value stored by 'notary' key. -func switchToNotary(ctx storage.Context, args []interface{}) { +func switchToNotary(ctx storage.Context) { const notaryDisabledKey = "notary" // non-notary legacy notaryVal := storage.Get(ctx, notaryDisabledKey)