[#49] Support contract migration

At initialization contract saves master script hash
that allows to re-initialize or migrate contract.

Signed-off-by: Alex Vanin <alexey@nspcc.ru>
This commit is contained in:
Alex Vanin 2021-02-11 18:55:32 +03:00 committed by Alex Vanin
parent a4a9a49a76
commit 88c738b736
11 changed files with 186 additions and 28 deletions

View file

@ -1,9 +1,11 @@
package netmapcontract
import (
"github.com/nspcc-dev/neo-go/pkg/interop"
"github.com/nspcc-dev/neo-go/pkg/interop/binary"
"github.com/nspcc-dev/neo-go/pkg/interop/crypto"
"github.com/nspcc-dev/neo-go/pkg/interop/iterator"
"github.com/nspcc-dev/neo-go/pkg/interop/native/management"
"github.com/nspcc-dev/neo-go/pkg/interop/runtime"
"github.com/nspcc-dev/neo-go/pkg/interop/storage"
"github.com/nspcc-dev/neofs-contract/common"
@ -57,9 +59,9 @@ func init() {
// Init function sets up initial list of inner ring public keys and should
// be invoked once at neofs infrastructure setup.
func Init(keys [][]byte) {
if storage.Get(ctx, innerRingKey) != nil {
panic("netmap: contract already initialized")
func Init(owner interop.Hash160, keys [][]byte) {
if !common.HasUpdateAccess(ctx) {
panic("only owner can reinitialize contract")
}
var irList []common.IRNode
@ -69,6 +71,8 @@ func Init(keys [][]byte) {
irList = append(irList, common.IRNode{PublicKey: key})
}
storage.Put(ctx, common.OwnerKey, owner)
common.SetSerialized(ctx, innerRingKey, irList)
// epoch number is a little endian int, it doesn't need to be serialized
@ -83,6 +87,18 @@ func Init(keys [][]byte) {
runtime.Log("netmap contract initialized")
}
func Migrate(script []byte, manifest []byte) bool {
if !common.HasUpdateAccess(ctx) {
runtime.Log("only owner can update contract")
return false
}
management.Update(script, manifest)
runtime.Log("netmap contract updated")
return true
}
func InnerRingList() []common.IRNode {
return getIRNodes(ctx)
}