2021-07-04 11:00:56 +00:00
|
|
|
package netmap
|
2020-10-27 12:14:06 +00:00
|
|
|
|
|
|
|
import (
|
2021-02-11 15:55:32 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop"
|
2021-03-17 08:10:23 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/contract"
|
2020-10-27 12:14:06 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/iterator"
|
2021-04-29 13:20:00 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/native/crypto"
|
2021-07-21 11:45:32 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/native/ledger"
|
2021-02-11 15:55:32 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/native/management"
|
2021-03-12 12:16:36 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/native/std"
|
2020-10-27 12:14:06 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/runtime"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/storage"
|
2021-02-02 16:39:16 +00:00
|
|
|
"github.com/nspcc-dev/neofs-contract/common"
|
2020-10-27 12:14:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
storageNode struct {
|
|
|
|
info []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
netmapNode struct {
|
|
|
|
node storageNode
|
|
|
|
state nodeState
|
|
|
|
}
|
|
|
|
|
|
|
|
nodeState int
|
|
|
|
|
|
|
|
record struct {
|
|
|
|
key []byte
|
|
|
|
val []byte
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2021-04-27 10:48:40 +00:00
|
|
|
notaryDisabledKey = "notary"
|
2021-04-27 10:56:02 +00:00
|
|
|
innerRingKey = "innerring"
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2022-03-10 10:53:36 +00:00
|
|
|
// SnapshotCount contains the number of previous snapshots stored by this contract.
|
|
|
|
// Must be less than 255.
|
2022-03-10 11:30:04 +00:00
|
|
|
SnapshotCount = 10
|
2022-03-10 10:53:36 +00:00
|
|
|
snapshotKeyPrefix = "snapshot_"
|
|
|
|
snapshotCurrentIDKey = "snapshotCurrent"
|
|
|
|
snapshotEpoch = "snapshotEpoch"
|
|
|
|
snapshotBlockKey = "snapshotBlock"
|
2021-03-17 13:58:56 +00:00
|
|
|
|
|
|
|
containerContractKey = "containerScriptHash"
|
|
|
|
balanceContractKey = "balanceScriptHash"
|
2021-04-27 10:48:40 +00:00
|
|
|
|
|
|
|
cleanupEpochMethod = "newEpoch"
|
2020-10-27 12:14:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2021-11-29 17:58:27 +00:00
|
|
|
// V2 format
|
2020-10-27 12:14:06 +00:00
|
|
|
_ nodeState = iota
|
2022-03-10 11:29:24 +00:00
|
|
|
OnlineState
|
|
|
|
OfflineState
|
2020-10-27 12:14:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2021-06-29 18:15:53 +00:00
|
|
|
configPrefix = []byte("config")
|
|
|
|
candidatePrefix = []byte("candidate")
|
2020-10-27 12:14:06 +00:00
|
|
|
)
|
|
|
|
|
2021-05-12 08:31:07 +00:00
|
|
|
// _deploy function sets up initial list of inner ring public keys.
|
|
|
|
func _deploy(data interface{}, isUpdate bool) {
|
2021-06-29 20:30:42 +00:00
|
|
|
ctx := storage.GetContext()
|
|
|
|
|
2021-11-29 16:34:11 +00:00
|
|
|
var args = data.(struct {
|
|
|
|
notaryDisabled bool
|
|
|
|
addrBalance interop.Hash160
|
|
|
|
addrContainer interop.Hash160
|
|
|
|
keys []interop.PublicKey
|
|
|
|
config [][]byte
|
2021-12-27 08:49:30 +00:00
|
|
|
version int
|
2021-11-29 16:34:11 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
ln := len(args.config)
|
2021-10-19 10:04:10 +00:00
|
|
|
if ln%2 != 0 {
|
2021-11-29 10:51:57 +00:00
|
|
|
panic("bad configuration")
|
2021-10-19 10:04:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < ln/2; i++ {
|
2021-11-29 16:34:11 +00:00
|
|
|
key := args.config[i*2]
|
|
|
|
val := args.config[i*2+1]
|
2021-10-19 10:04:10 +00:00
|
|
|
|
|
|
|
setConfig(ctx, key, val)
|
|
|
|
}
|
|
|
|
|
|
|
|
if isUpdate {
|
2021-12-27 08:49:30 +00:00
|
|
|
common.CheckVersion(args.version)
|
2022-03-10 10:53:36 +00:00
|
|
|
|
|
|
|
data = storage.Get(ctx, "snapshotPrevious")
|
|
|
|
storage.Put(ctx, snapshotKeyPrefix+"0", data)
|
|
|
|
|
|
|
|
data := storage.Get(ctx, "snapshotCurrent")
|
|
|
|
storage.Put(ctx, snapshotKeyPrefix+"1", data)
|
|
|
|
|
|
|
|
storage.Put(ctx, snapshotCurrentIDKey, 1)
|
|
|
|
|
2021-10-19 10:04:10 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-29 16:43:01 +00:00
|
|
|
if len(args.addrBalance) != interop.Hash160Len || len(args.addrContainer) != interop.Hash160Len {
|
2021-11-29 10:51:57 +00:00
|
|
|
panic("incorrect length of contract script hash")
|
2021-03-17 13:58:56 +00:00
|
|
|
}
|
|
|
|
|
2020-10-27 12:14:06 +00:00
|
|
|
// epoch number is a little endian int, it doesn't need to be serialized
|
|
|
|
storage.Put(ctx, snapshotEpoch, 0)
|
2021-07-21 11:45:32 +00:00
|
|
|
storage.Put(ctx, snapshotBlockKey, 0)
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2022-03-10 10:53:36 +00:00
|
|
|
prefix := []byte(snapshotKeyPrefix)
|
|
|
|
for i := 0; i < SnapshotCount; i++ {
|
|
|
|
common.SetSerialized(ctx, append(prefix, byte(i)), []storageNode{})
|
|
|
|
}
|
|
|
|
common.SetSerialized(ctx, snapshotCurrentIDKey, 0)
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2021-11-29 16:34:11 +00:00
|
|
|
storage.Put(ctx, balanceContractKey, args.addrBalance)
|
|
|
|
storage.Put(ctx, containerContractKey, args.addrContainer)
|
2021-03-17 13:58:56 +00:00
|
|
|
|
2021-04-27 10:48:40 +00:00
|
|
|
// initialize the way to collect signatures
|
2021-11-29 16:34:11 +00:00
|
|
|
storage.Put(ctx, notaryDisabledKey, args.notaryDisabled)
|
|
|
|
if args.notaryDisabled {
|
2021-04-27 10:56:02 +00:00
|
|
|
var irList []common.IRNode
|
|
|
|
|
2021-11-29 16:34:11 +00:00
|
|
|
for i := 0; i < len(args.keys); i++ {
|
|
|
|
key := args.keys[i]
|
2021-04-27 10:56:02 +00:00
|
|
|
irList = append(irList, common.IRNode{PublicKey: key})
|
|
|
|
}
|
|
|
|
|
|
|
|
common.SetSerialized(ctx, innerRingKey, irList)
|
2021-04-27 10:48:40 +00:00
|
|
|
common.InitVote(ctx)
|
2021-05-04 14:02:05 +00:00
|
|
|
runtime.Log("netmap contract notary disabled")
|
2021-04-27 10:48:40 +00:00
|
|
|
}
|
|
|
|
|
2020-10-27 12:14:06 +00:00
|
|
|
runtime.Log("netmap contract initialized")
|
|
|
|
}
|
|
|
|
|
2021-09-21 08:02:24 +00:00
|
|
|
// Update method updates contract source code and manifest. Can be invoked
|
2021-09-20 15:41:46 +00:00
|
|
|
// only by committee.
|
2021-09-21 12:58:37 +00:00
|
|
|
func Update(script []byte, manifest []byte, data interface{}) {
|
2021-09-20 15:41:46 +00:00
|
|
|
if !common.HasUpdateAccess() {
|
|
|
|
panic("only committee can update contract")
|
2021-02-11 15:55:32 +00:00
|
|
|
}
|
|
|
|
|
2021-11-09 10:55:21 +00:00
|
|
|
contract.Call(interop.Hash160(management.Hash), "update",
|
|
|
|
contract.All, script, manifest, common.AppendVersion(data))
|
2021-02-11 15:55:32 +00:00
|
|
|
runtime.Log("netmap contract updated")
|
|
|
|
}
|
|
|
|
|
2021-07-22 11:37:52 +00:00
|
|
|
// InnerRingList method returns slice of structures that contains public key of
|
|
|
|
// Inner Ring node. Should be used only in notary disabled environment.
|
|
|
|
//
|
|
|
|
// If notary enabled, then look to NeoFSAlphabet role in native RoleManagement
|
|
|
|
// contract of the side chain.
|
2021-04-27 10:56:02 +00:00
|
|
|
func InnerRingList() []common.IRNode {
|
|
|
|
ctx := storage.GetReadOnlyContext()
|
|
|
|
return getIRNodes(ctx)
|
|
|
|
}
|
|
|
|
|
2021-07-22 11:37:52 +00:00
|
|
|
// UpdateInnerRing method updates list of Inner Ring node keys. Should be used
|
|
|
|
// only in notary disabled environment. Can be invoked only by Alphabet nodes.
|
|
|
|
//
|
|
|
|
// If notary enabled, then update NeoFSAlphabet role in native RoleManagement
|
|
|
|
// contract of the side chain. Use notary service to collect multi signature.
|
2021-05-21 11:37:31 +00:00
|
|
|
func UpdateInnerRing(keys []interop.PublicKey) {
|
2021-04-27 10:56:02 +00:00
|
|
|
ctx := storage.GetContext()
|
2021-04-29 13:20:00 +00:00
|
|
|
notaryDisabled := storage.Get(ctx, notaryDisabledKey).(bool)
|
2021-04-27 10:56:02 +00:00
|
|
|
|
2021-04-29 13:20:00 +00:00
|
|
|
var ( // for invocation collection without notary
|
|
|
|
alphabet []common.IRNode
|
|
|
|
nodeKey []byte
|
|
|
|
)
|
|
|
|
|
|
|
|
if notaryDisabled {
|
|
|
|
alphabet = common.AlphabetNodes()
|
|
|
|
nodeKey = common.InnerRingInvoker(alphabet)
|
|
|
|
if len(nodeKey) == 0 {
|
2021-11-29 10:51:57 +00:00
|
|
|
panic("this method must be invoked by alphabet nodes")
|
2021-04-29 13:20:00 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
multiaddr := common.AlphabetAddress()
|
2021-11-29 12:21:40 +00:00
|
|
|
common.CheckAlphabetWitness(multiaddr)
|
2021-04-27 10:56:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var irList []common.IRNode
|
|
|
|
|
|
|
|
for i := 0; i < len(keys); i++ {
|
|
|
|
key := keys[i]
|
|
|
|
irList = append(irList, common.IRNode{PublicKey: key})
|
|
|
|
}
|
|
|
|
|
2021-04-29 13:20:00 +00:00
|
|
|
if notaryDisabled {
|
|
|
|
threshold := len(alphabet)*2/3 + 1
|
|
|
|
id := keysID(keys, []byte("updateIR"))
|
|
|
|
|
|
|
|
n := common.Vote(ctx, id, nodeKey)
|
|
|
|
if n < threshold {
|
2021-05-21 11:37:31 +00:00
|
|
|
return
|
2021-04-29 13:20:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
common.RemoveVotes(ctx, id)
|
|
|
|
}
|
|
|
|
|
2021-11-30 09:02:59 +00:00
|
|
|
runtime.Log("inner ring list updated")
|
2021-04-27 10:56:02 +00:00
|
|
|
common.SetSerialized(ctx, innerRingKey, irList)
|
|
|
|
}
|
|
|
|
|
2022-03-11 09:19:35 +00:00
|
|
|
// AddPeerIR method tries to add new candidate to the network map.
|
|
|
|
// Should only be invoked in notary-enabled environment by the alphabet.
|
|
|
|
func AddPeerIR(nodeInfo []byte) {
|
2021-12-02 15:59:29 +00:00
|
|
|
ctx := storage.GetContext()
|
|
|
|
notaryDisabled := storage.Get(ctx, notaryDisabledKey).(bool)
|
|
|
|
if notaryDisabled {
|
2022-03-11 09:19:35 +00:00
|
|
|
panic("AddPeerIR should only be called in notary-enabled environment")
|
2021-12-02 15:59:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
common.CheckAlphabetWitness(common.AlphabetAddress())
|
|
|
|
|
|
|
|
addToNetmap(ctx, storageNode{info: nodeInfo})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-07-22 11:37:52 +00:00
|
|
|
// AddPeer method adds new candidate to the next network map if it was invoked
|
|
|
|
// by Alphabet node. If it was invoked by node candidate, it produces AddPeer
|
|
|
|
// notification. Otherwise method throws panic.
|
|
|
|
//
|
2021-10-16 09:57:45 +00:00
|
|
|
// If the candidate already exists, it's info is updated.
|
2021-07-22 11:37:52 +00:00
|
|
|
// NodeInfo argument contains stable marshaled version of netmap.NodeInfo
|
|
|
|
// structure.
|
2021-05-21 11:37:31 +00:00
|
|
|
func AddPeer(nodeInfo []byte) {
|
2021-03-09 19:15:58 +00:00
|
|
|
ctx := storage.GetContext()
|
2021-04-29 13:20:00 +00:00
|
|
|
notaryDisabled := storage.Get(ctx, notaryDisabledKey).(bool)
|
|
|
|
|
|
|
|
var ( // for invocation collection without notary
|
2021-12-02 15:59:29 +00:00
|
|
|
alphabet []common.IRNode
|
|
|
|
nodeKey []byte
|
2021-04-29 13:20:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if notaryDisabled {
|
|
|
|
alphabet = common.AlphabetNodes()
|
|
|
|
nodeKey = common.InnerRingInvoker(alphabet)
|
2021-10-19 13:47:03 +00:00
|
|
|
}
|
|
|
|
|
2021-12-02 15:59:29 +00:00
|
|
|
// If notary is enabled or caller is not an alphabet node,
|
|
|
|
// just emit the notification for alphabet.
|
|
|
|
if !notaryDisabled || len(nodeKey) == 0 {
|
2021-11-29 17:58:27 +00:00
|
|
|
// V2 format
|
2021-10-19 13:47:03 +00:00
|
|
|
publicKey := nodeInfo[2:35] // offset:2, len:33
|
2021-11-29 12:21:40 +00:00
|
|
|
|
2021-12-02 15:59:29 +00:00
|
|
|
common.CheckWitness(publicKey)
|
2022-03-17 13:02:56 +00:00
|
|
|
if notaryDisabled {
|
|
|
|
runtime.Notify("AddPeer", nodeInfo)
|
|
|
|
}
|
2021-10-19 13:47:03 +00:00
|
|
|
return
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
candidate := storageNode{
|
|
|
|
info: nodeInfo,
|
|
|
|
}
|
|
|
|
|
2021-04-29 13:20:00 +00:00
|
|
|
if notaryDisabled {
|
|
|
|
threshold := len(alphabet)*2/3 + 1
|
|
|
|
rawCandidate := std.Serialize(candidate)
|
|
|
|
id := crypto.Sha256(rawCandidate)
|
|
|
|
|
|
|
|
n := common.Vote(ctx, id, nodeKey)
|
|
|
|
if n < threshold {
|
2021-05-21 11:37:31 +00:00
|
|
|
return
|
2021-04-29 13:20:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
common.RemoveVotes(ctx, id)
|
|
|
|
}
|
|
|
|
|
2021-06-29 18:15:53 +00:00
|
|
|
addToNetmap(ctx, candidate)
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2022-03-22 07:50:36 +00:00
|
|
|
// UpdateState method updates state of node from the network map candidate list.
|
|
|
|
// For notary-ENABLED environment tx must be signed by both storage node and the alphabet.
|
|
|
|
// To force update without storage node signature, see `UpdateStateIR`.
|
2021-07-22 11:37:52 +00:00
|
|
|
//
|
2022-03-22 07:50:36 +00:00
|
|
|
// For notary-DISABLED environment the behaviour depends on who signed the transaction:
|
|
|
|
// 1. If it was signed by alphabet, go into voting.
|
|
|
|
// 2. If it was signed by a storage node, emit `UpdateState` notification.
|
|
|
|
// 2. Fail in any other case.
|
|
|
|
//
|
|
|
|
// The behaviour can be summarized in the following table:
|
|
|
|
// | notary \ Signer | Storage node | Alphabet | Both |
|
|
|
|
// | ENABLED | FAIL | FAIL | OK |
|
|
|
|
// | DISABLED | NOTIFICATION | OK | OK (same as alphabet) |
|
2021-07-22 11:37:52 +00:00
|
|
|
// State argument defines node state. The only supported state now is (2) --
|
|
|
|
// offline state. Node is removed from network map candidate list.
|
|
|
|
//
|
|
|
|
// Method panics when invoked with unsupported states.
|
2021-05-21 11:37:31 +00:00
|
|
|
func UpdateState(state int, publicKey interop.PublicKey) {
|
2021-11-29 16:43:01 +00:00
|
|
|
if len(publicKey) != interop.PublicKeyCompressedLen {
|
2021-11-29 10:51:57 +00:00
|
|
|
panic("incorrect public key")
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2021-03-09 19:15:58 +00:00
|
|
|
ctx := storage.GetContext()
|
2021-04-29 13:20:00 +00:00
|
|
|
notaryDisabled := storage.Get(ctx, notaryDisabledKey).(bool)
|
|
|
|
|
|
|
|
if notaryDisabled {
|
2022-03-22 07:50:36 +00:00
|
|
|
alphabet := common.AlphabetNodes()
|
|
|
|
nodeKey := common.InnerRingInvoker(alphabet)
|
2021-11-29 12:21:40 +00:00
|
|
|
|
2022-03-22 07:50:36 +00:00
|
|
|
// If caller is not an alphabet node,
|
|
|
|
// just emit the notification for alphabet.
|
|
|
|
if len(nodeKey) == 0 {
|
|
|
|
common.CheckWitness(publicKey)
|
2022-03-17 13:02:56 +00:00
|
|
|
runtime.Notify("UpdateState", state, publicKey)
|
2022-03-22 07:50:36 +00:00
|
|
|
return
|
2022-03-17 13:02:56 +00:00
|
|
|
}
|
2021-02-19 15:52:27 +00:00
|
|
|
|
2021-04-29 13:20:00 +00:00
|
|
|
threshold := len(alphabet)*2/3 + 1
|
|
|
|
id := common.InvokeID([]interface{}{state, publicKey}, []byte("update"))
|
|
|
|
|
|
|
|
n := common.Vote(ctx, id, nodeKey)
|
|
|
|
if n < threshold {
|
2021-05-21 11:37:31 +00:00
|
|
|
return
|
2021-04-29 13:20:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
common.RemoveVotes(ctx, id)
|
2022-03-22 07:50:36 +00:00
|
|
|
} else {
|
|
|
|
common.CheckWitness(publicKey)
|
|
|
|
common.CheckAlphabetWitness(common.AlphabetAddress())
|
2021-04-29 13:20:00 +00:00
|
|
|
}
|
|
|
|
|
2020-10-27 12:14:06 +00:00
|
|
|
switch nodeState(state) {
|
2022-03-10 11:29:24 +00:00
|
|
|
case OfflineState:
|
2021-06-29 18:15:53 +00:00
|
|
|
removeFromNetmap(ctx, publicKey)
|
2021-11-30 09:02:59 +00:00
|
|
|
runtime.Log("remove storage node from the network map")
|
2020-10-27 12:14:06 +00:00
|
|
|
default:
|
2021-11-29 10:51:57 +00:00
|
|
|
panic("unsupported state")
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-10 12:01:10 +00:00
|
|
|
// UpdateStateIR method tries to change node state in the network map.
|
|
|
|
// Should only be invoked in notary-enabled environment by the alphabet.
|
|
|
|
func UpdateStateIR(state nodeState, publicKey interop.PublicKey) {
|
|
|
|
ctx := storage.GetContext()
|
|
|
|
notaryDisabled := storage.Get(ctx, notaryDisabledKey).(bool)
|
|
|
|
if notaryDisabled {
|
|
|
|
panic("UpdateStateIR should only be called in notary-enabled environment")
|
|
|
|
}
|
|
|
|
|
|
|
|
common.CheckAlphabetWitness(common.AlphabetAddress())
|
|
|
|
|
|
|
|
switch state {
|
2022-03-10 11:29:24 +00:00
|
|
|
case OfflineState:
|
2022-03-10 12:01:10 +00:00
|
|
|
removeFromNetmap(ctx, publicKey)
|
|
|
|
default:
|
|
|
|
panic("unsupported state")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-22 11:37:52 +00:00
|
|
|
// NewEpoch method changes epoch number up to provided epochNum argument. Can
|
|
|
|
// be invoked only by Alphabet nodes. If provided epoch number is less or equal
|
|
|
|
// current epoch number, method throws panic.
|
|
|
|
//
|
|
|
|
// When epoch number updated, contract sets storage node candidates as current
|
|
|
|
// network map. Also contract invokes NewEpoch method on Balance and Container
|
|
|
|
// contracts.
|
|
|
|
//
|
|
|
|
// Produces NewEpoch notification.
|
2021-05-21 11:37:31 +00:00
|
|
|
func NewEpoch(epochNum int) {
|
2021-03-09 19:15:58 +00:00
|
|
|
ctx := storage.GetContext()
|
2021-04-29 13:20:00 +00:00
|
|
|
notaryDisabled := storage.Get(ctx, notaryDisabledKey).(bool)
|
|
|
|
|
|
|
|
var ( // for invocation collection without notary
|
|
|
|
alphabet []common.IRNode
|
|
|
|
nodeKey []byte
|
|
|
|
)
|
|
|
|
|
|
|
|
if notaryDisabled {
|
|
|
|
alphabet = common.AlphabetNodes()
|
|
|
|
nodeKey = common.InnerRingInvoker(alphabet)
|
|
|
|
if len(nodeKey) == 0 {
|
2021-11-29 10:51:57 +00:00
|
|
|
panic("this method must be invoked by inner ring nodes")
|
2021-04-29 13:20:00 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
multiaddr := common.AlphabetAddress()
|
2021-11-29 12:21:40 +00:00
|
|
|
common.CheckAlphabetWitness(multiaddr)
|
2021-04-29 13:20:00 +00:00
|
|
|
}
|
2021-03-09 19:15:58 +00:00
|
|
|
|
2021-04-29 13:20:00 +00:00
|
|
|
if notaryDisabled {
|
|
|
|
threshold := len(alphabet)*2/3 + 1
|
|
|
|
id := common.InvokeID([]interface{}{epochNum}, []byte("epoch"))
|
|
|
|
|
|
|
|
n := common.Vote(ctx, id, nodeKey)
|
|
|
|
if n < threshold {
|
2021-05-21 11:37:31 +00:00
|
|
|
return
|
2021-04-29 13:20:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
common.RemoveVotes(ctx, id)
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2020-12-25 11:45:24 +00:00
|
|
|
currentEpoch := storage.Get(ctx, snapshotEpoch).(int)
|
|
|
|
if epochNum <= currentEpoch {
|
2021-05-21 11:37:31 +00:00
|
|
|
panic("invalid epoch") // ignore invocations with invalid epoch
|
2020-12-25 11:45:24 +00:00
|
|
|
}
|
|
|
|
|
2022-03-10 11:29:24 +00:00
|
|
|
dataOnlineState := filterNetmap(ctx, OnlineState)
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2021-11-30 09:02:59 +00:00
|
|
|
runtime.Log("process new epoch")
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2021-02-19 15:52:27 +00:00
|
|
|
// todo: check if provided epoch number is bigger than current
|
|
|
|
storage.Put(ctx, snapshotEpoch, epochNum)
|
2021-07-21 11:45:32 +00:00
|
|
|
storage.Put(ctx, snapshotBlockKey, ledger.CurrentIndex())
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2022-03-10 10:53:36 +00:00
|
|
|
id := storage.Get(ctx, snapshotCurrentIDKey).(int)
|
|
|
|
id = (id + 1) % SnapshotCount
|
|
|
|
storage.Put(ctx, snapshotCurrentIDKey, id)
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2021-02-19 15:52:27 +00:00
|
|
|
// put netmap into actual snapshot
|
2022-03-10 10:53:36 +00:00
|
|
|
common.SetSerialized(ctx, snapshotKeyPrefix+string([]byte{byte(id)}), dataOnlineState)
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2021-03-17 13:58:56 +00:00
|
|
|
// make clean up routines in other contracts
|
|
|
|
cleanup(ctx, epochNum)
|
|
|
|
|
2021-02-19 15:52:27 +00:00
|
|
|
runtime.Notify("NewEpoch", epochNum)
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2021-07-22 11:37:52 +00:00
|
|
|
// Epoch method returns current epoch number.
|
2020-10-27 12:14:06 +00:00
|
|
|
func Epoch() int {
|
2021-03-09 19:15:58 +00:00
|
|
|
ctx := storage.GetReadOnlyContext()
|
2020-10-27 12:14:06 +00:00
|
|
|
return storage.Get(ctx, snapshotEpoch).(int)
|
|
|
|
}
|
|
|
|
|
2021-07-22 11:37:52 +00:00
|
|
|
// LastEpochBlock method returns block number when current epoch was applied.
|
2021-07-21 11:45:32 +00:00
|
|
|
func LastEpochBlock() int {
|
|
|
|
ctx := storage.GetReadOnlyContext()
|
|
|
|
return storage.Get(ctx, snapshotBlockKey).(int)
|
|
|
|
}
|
|
|
|
|
2021-07-22 11:37:52 +00:00
|
|
|
// Netmap method returns list of structures that contain byte array of stable
|
|
|
|
// marshalled netmap.NodeInfo structure. These structure contain Storage nodes
|
|
|
|
// of current epoch.
|
2020-10-27 12:14:06 +00:00
|
|
|
func Netmap() []storageNode {
|
2021-03-09 19:15:58 +00:00
|
|
|
ctx := storage.GetReadOnlyContext()
|
2022-03-10 10:53:36 +00:00
|
|
|
id := storage.Get(ctx, snapshotCurrentIDKey).(int)
|
|
|
|
return getSnapshot(ctx, snapshotKeyPrefix+string([]byte{byte(id)}))
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2022-03-10 10:53:36 +00:00
|
|
|
// NetmapCandidates method returns list of structures that contain node state
|
2021-07-22 11:37:52 +00:00
|
|
|
// and byte array of stable marshalled netmap.NodeInfo structure.
|
|
|
|
// These structure contain Storage node candidates for next epoch.
|
2021-06-02 10:39:00 +00:00
|
|
|
func NetmapCandidates() []netmapNode {
|
|
|
|
ctx := storage.GetReadOnlyContext()
|
|
|
|
return getNetmapNodes(ctx)
|
|
|
|
}
|
|
|
|
|
2021-07-22 11:37:52 +00:00
|
|
|
// Snapshot method returns list of structures that contain node state
|
|
|
|
// (online: 1) and byte array of stable marshalled netmap.NodeInfo structure.
|
|
|
|
// These structure contain Storage nodes of specified epoch.
|
|
|
|
//
|
|
|
|
// Netmap contract contains only two recent network map snapshot: current and
|
|
|
|
// previous epoch. For diff bigger than 1 or less than 0 method throws panic.
|
2020-10-27 12:14:06 +00:00
|
|
|
func Snapshot(diff int) []storageNode {
|
2022-03-10 10:53:36 +00:00
|
|
|
if diff < 0 || SnapshotCount <= diff {
|
2021-11-29 10:51:57 +00:00
|
|
|
panic("incorrect diff")
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2021-03-09 19:15:58 +00:00
|
|
|
ctx := storage.GetReadOnlyContext()
|
2022-03-10 10:53:36 +00:00
|
|
|
id := storage.Get(ctx, snapshotCurrentIDKey).(int)
|
|
|
|
needID := (id - diff + SnapshotCount) % SnapshotCount
|
|
|
|
key := snapshotKeyPrefix + string([]byte{byte(needID)})
|
2020-10-27 12:14:06 +00:00
|
|
|
return getSnapshot(ctx, key)
|
|
|
|
}
|
|
|
|
|
2021-07-22 11:37:52 +00:00
|
|
|
// SnapshotByEpoch method returns list of structures that contain node state
|
|
|
|
// (online: 1) and byte array of stable marshalled netmap.NodeInfo structure.
|
|
|
|
// These structure contain Storage nodes of specified epoch.
|
|
|
|
//
|
|
|
|
// Netmap contract contains only two recent network map snapshot: current and
|
|
|
|
// previous epoch. For all others epoch method throws panic.
|
2021-01-12 08:09:30 +00:00
|
|
|
func SnapshotByEpoch(epoch int) []storageNode {
|
2021-03-09 19:15:58 +00:00
|
|
|
ctx := storage.GetReadOnlyContext()
|
2021-01-12 08:09:30 +00:00
|
|
|
currentEpoch := storage.Get(ctx, snapshotEpoch).(int)
|
|
|
|
|
|
|
|
return Snapshot(currentEpoch - epoch)
|
|
|
|
}
|
|
|
|
|
2021-07-22 11:37:52 +00:00
|
|
|
// Config returns configuration value of NeoFS configuration. If key does
|
|
|
|
// not exists, returns nil.
|
2020-10-27 12:14:06 +00:00
|
|
|
func Config(key []byte) interface{} {
|
2021-03-09 19:15:58 +00:00
|
|
|
ctx := storage.GetReadOnlyContext()
|
2020-10-27 12:14:06 +00:00
|
|
|
return getConfig(ctx, key)
|
|
|
|
}
|
|
|
|
|
2021-07-22 11:37:52 +00:00
|
|
|
// SetConfig key-value pair as a NeoFS runtime configuration value. Can be invoked
|
|
|
|
// only by Alphabet nodes.
|
2021-05-21 11:37:31 +00:00
|
|
|
func SetConfig(id, key, val []byte) {
|
2021-04-29 13:20:00 +00:00
|
|
|
ctx := storage.GetContext()
|
|
|
|
notaryDisabled := storage.Get(ctx, notaryDisabledKey).(bool)
|
|
|
|
|
|
|
|
var ( // for invocation collection without notary
|
|
|
|
alphabet []common.IRNode
|
|
|
|
nodeKey []byte
|
|
|
|
)
|
|
|
|
|
|
|
|
if notaryDisabled {
|
|
|
|
alphabet = common.AlphabetNodes()
|
|
|
|
nodeKey = common.InnerRingInvoker(alphabet)
|
|
|
|
if len(nodeKey) == 0 {
|
2021-11-29 10:51:57 +00:00
|
|
|
panic("invoked by non inner ring node")
|
2021-04-29 13:20:00 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
multiaddr := common.AlphabetAddress()
|
2021-11-29 12:21:40 +00:00
|
|
|
common.CheckAlphabetWitness(multiaddr)
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2021-04-29 13:20:00 +00:00
|
|
|
if notaryDisabled {
|
|
|
|
threshold := len(alphabet)*2/3 + 1
|
|
|
|
|
|
|
|
n := common.Vote(ctx, id, nodeKey)
|
|
|
|
if n < threshold {
|
2021-05-21 11:37:31 +00:00
|
|
|
return
|
2021-04-29 13:20:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
common.RemoveVotes(ctx, id)
|
|
|
|
}
|
2021-03-09 19:15:58 +00:00
|
|
|
|
2021-02-19 15:52:27 +00:00
|
|
|
setConfig(ctx, key, val)
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2021-11-30 09:02:59 +00:00
|
|
|
runtime.Log("configuration has been updated")
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2021-07-22 11:37:52 +00:00
|
|
|
// ListConfig returns array of structures that contain key and value of all
|
|
|
|
// NeoFS configuration records. Key and value are both byte arrays.
|
2020-10-27 12:14:06 +00:00
|
|
|
func ListConfig() []record {
|
2021-03-09 19:15:58 +00:00
|
|
|
ctx := storage.GetReadOnlyContext()
|
|
|
|
|
2020-10-27 12:14:06 +00:00
|
|
|
var config []record
|
|
|
|
|
2021-02-08 15:23:08 +00:00
|
|
|
it := storage.Find(ctx, configPrefix, storage.None)
|
2020-10-27 12:14:06 +00:00
|
|
|
for iterator.Next(it) {
|
2021-12-01 17:28:58 +00:00
|
|
|
pair := iterator.Value(it).(struct {
|
|
|
|
key []byte
|
|
|
|
val []byte
|
|
|
|
})
|
|
|
|
r := record{key: pair.key[len(configPrefix):], val: pair.val}
|
2020-10-27 12:14:06 +00:00
|
|
|
|
|
|
|
config = append(config, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
return config
|
|
|
|
}
|
|
|
|
|
2021-07-22 11:37:52 +00:00
|
|
|
// Version returns version of the contract.
|
2020-10-27 12:14:06 +00:00
|
|
|
func Version() int {
|
2021-07-29 11:44:53 +00:00
|
|
|
return common.Version
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2021-06-29 18:15:53 +00:00
|
|
|
func addToNetmap(ctx storage.Context, n storageNode) {
|
2020-10-27 12:14:06 +00:00
|
|
|
var (
|
|
|
|
newNode = n.info
|
|
|
|
newNodeKey = newNode[2:35]
|
2021-06-29 18:15:53 +00:00
|
|
|
storageKey = append(candidatePrefix, newNodeKey...)
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2021-06-29 18:15:53 +00:00
|
|
|
node = netmapNode{
|
2020-10-27 12:14:06 +00:00
|
|
|
node: n,
|
2022-03-10 11:29:24 +00:00
|
|
|
state: OnlineState,
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2021-06-29 18:15:53 +00:00
|
|
|
storage.Put(ctx, storageKey, std.Serialize(node))
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2021-06-29 18:15:53 +00:00
|
|
|
func removeFromNetmap(ctx storage.Context, key interop.PublicKey) {
|
|
|
|
storageKey := append(candidatePrefix, key...)
|
|
|
|
storage.Delete(ctx, storageKey)
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func filterNetmap(ctx storage.Context, st nodeState) []storageNode {
|
|
|
|
var (
|
|
|
|
netmap = getNetmapNodes(ctx)
|
|
|
|
result = []storageNode{}
|
|
|
|
)
|
|
|
|
|
|
|
|
for i := 0; i < len(netmap); i++ {
|
|
|
|
item := netmap[i]
|
|
|
|
if item.state == st {
|
|
|
|
result = append(result, item.node)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func getNetmapNodes(ctx storage.Context) []netmapNode {
|
2021-06-29 18:15:53 +00:00
|
|
|
result := []netmapNode{}
|
|
|
|
|
2021-11-29 16:56:19 +00:00
|
|
|
it := storage.Find(ctx, candidatePrefix, storage.ValuesOnly|storage.DeserializeValues)
|
2021-06-29 18:15:53 +00:00
|
|
|
for iterator.Next(it) {
|
2021-11-29 16:56:19 +00:00
|
|
|
node := iterator.Value(it).(netmapNode)
|
2021-06-29 18:15:53 +00:00
|
|
|
result = append(result, node)
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2021-06-29 18:15:53 +00:00
|
|
|
return result
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func getSnapshot(ctx storage.Context, key string) []storageNode {
|
|
|
|
data := storage.Get(ctx, key)
|
|
|
|
if data != nil {
|
2021-03-12 12:16:36 +00:00
|
|
|
return std.Deserialize(data.([]byte)).([]storageNode)
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return []storageNode{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getConfig(ctx storage.Context, key interface{}) interface{} {
|
|
|
|
postfix := key.([]byte)
|
|
|
|
storageKey := append(configPrefix, postfix...)
|
|
|
|
|
|
|
|
return storage.Get(ctx, storageKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
func setConfig(ctx storage.Context, key, val interface{}) {
|
|
|
|
postfix := key.([]byte)
|
|
|
|
storageKey := append(configPrefix, postfix...)
|
|
|
|
|
|
|
|
storage.Put(ctx, storageKey, val)
|
|
|
|
}
|
2021-02-19 14:24:03 +00:00
|
|
|
|
2021-03-17 13:58:56 +00:00
|
|
|
func cleanup(ctx storage.Context, epoch int) {
|
|
|
|
balanceContractAddr := storage.Get(ctx, balanceContractKey).(interop.Hash160)
|
|
|
|
contract.Call(balanceContractAddr, cleanupEpochMethod, contract.All, epoch)
|
|
|
|
|
|
|
|
containerContractAddr := storage.Get(ctx, containerContractKey).(interop.Hash160)
|
|
|
|
contract.Call(containerContractAddr, cleanupEpochMethod, contract.All, epoch)
|
|
|
|
}
|
2021-04-27 10:56:02 +00:00
|
|
|
|
|
|
|
func getIRNodes(ctx storage.Context) []common.IRNode {
|
|
|
|
data := storage.Get(ctx, innerRingKey)
|
|
|
|
if data != nil {
|
|
|
|
return std.Deserialize(data.([]byte)).([]common.IRNode)
|
|
|
|
}
|
|
|
|
|
|
|
|
return []common.IRNode{}
|
|
|
|
}
|
2021-04-29 13:20:00 +00:00
|
|
|
|
|
|
|
func keysID(args []interop.PublicKey, prefix []byte) []byte {
|
|
|
|
var (
|
|
|
|
result []byte
|
|
|
|
)
|
|
|
|
|
|
|
|
result = append(result, prefix...)
|
|
|
|
|
|
|
|
for i := range args {
|
|
|
|
result = append(result, args[i]...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return crypto.Sha256(result)
|
|
|
|
}
|