2020-10-27 12:14:06 +00:00
|
|
|
package netmapcontract
|
|
|
|
|
|
|
|
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-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-02-02 17:36:20 +00:00
|
|
|
version = 1
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2021-04-27 10:48:40 +00:00
|
|
|
netmapKey = "netmap"
|
|
|
|
configuredKey = "initconfig"
|
|
|
|
notaryDisabledKey = "notary"
|
2021-04-27 10:56:02 +00:00
|
|
|
innerRingKey = "innerring"
|
2020-10-27 12:14:06 +00:00
|
|
|
|
|
|
|
snapshot0Key = "snapshotCurrent"
|
|
|
|
snapshot1Key = "snapshotPrevious"
|
|
|
|
snapshotEpoch = "snapshotEpoch"
|
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 (
|
|
|
|
_ nodeState = iota
|
|
|
|
onlineState
|
|
|
|
offlineState
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
configPrefix = []byte("config")
|
|
|
|
)
|
|
|
|
|
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) {
|
|
|
|
args := data.([]interface{})
|
|
|
|
notaryDisabled := args[0].(bool)
|
|
|
|
owner := args[1].(interop.Hash160)
|
|
|
|
addrBalance := args[2].(interop.Hash160)
|
|
|
|
addrContainer := args[3].(interop.Hash160)
|
|
|
|
keys := args[4].([]interop.PublicKey)
|
|
|
|
|
2021-03-09 19:15:58 +00:00
|
|
|
ctx := storage.GetContext()
|
|
|
|
|
2021-02-11 15:55:32 +00:00
|
|
|
if !common.HasUpdateAccess(ctx) {
|
|
|
|
panic("only owner can reinitialize contract")
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2021-03-17 13:58:56 +00:00
|
|
|
if len(addrBalance) != 20 || len(addrContainer) != 20 {
|
|
|
|
panic("init: incorrect length of contract script hash")
|
|
|
|
}
|
|
|
|
|
2021-02-11 15:55:32 +00:00
|
|
|
storage.Put(ctx, common.OwnerKey, owner)
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
// simplified: this used for const sysfee in AddPeer method
|
2021-02-02 17:36:20 +00:00
|
|
|
common.SetSerialized(ctx, netmapKey, []netmapNode{})
|
|
|
|
common.SetSerialized(ctx, snapshot0Key, []netmapNode{})
|
|
|
|
common.SetSerialized(ctx, snapshot1Key, []netmapNode{})
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2021-03-17 13:58:56 +00:00
|
|
|
storage.Put(ctx, balanceContractKey, addrBalance)
|
|
|
|
storage.Put(ctx, containerContractKey, addrContainer)
|
|
|
|
|
2021-04-27 10:48:40 +00:00
|
|
|
// initialize the way to collect signatures
|
|
|
|
storage.Put(ctx, notaryDisabledKey, notaryDisabled)
|
|
|
|
if notaryDisabled {
|
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})
|
|
|
|
}
|
|
|
|
|
|
|
|
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-02-11 15:55:32 +00:00
|
|
|
func Migrate(script []byte, manifest []byte) bool {
|
2021-03-09 19:15:58 +00:00
|
|
|
ctx := storage.GetReadOnlyContext()
|
|
|
|
|
2021-02-11 15:55:32 +00:00
|
|
|
if !common.HasUpdateAccess(ctx) {
|
|
|
|
runtime.Log("only owner can update contract")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
management.Update(script, manifest)
|
|
|
|
runtime.Log("netmap contract updated")
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-04-27 10:56:02 +00:00
|
|
|
func InnerRingList() []common.IRNode {
|
|
|
|
ctx := storage.GetReadOnlyContext()
|
|
|
|
return getIRNodes(ctx)
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
panic("updateInnerRing: this method must be invoked by alphabet nodes")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
multiaddr := common.AlphabetAddress()
|
|
|
|
if !runtime.CheckWitness(multiaddr) {
|
|
|
|
panic("updateInnerRing: this method must be invoked by alphabet nodes")
|
|
|
|
}
|
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-04-27 10:56:02 +00:00
|
|
|
runtime.Log("updateInnerRing: inner ring list updated")
|
|
|
|
common.SetSerialized(ctx, innerRingKey, irList)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
alphabet []common.IRNode
|
|
|
|
nodeKey []byte
|
|
|
|
alphabetCall bool
|
|
|
|
)
|
|
|
|
|
|
|
|
if notaryDisabled {
|
|
|
|
alphabet = common.AlphabetNodes()
|
|
|
|
nodeKey = common.InnerRingInvoker(alphabet)
|
|
|
|
alphabetCall = len(nodeKey) != 0
|
|
|
|
} else {
|
|
|
|
multiaddr := common.AlphabetAddress()
|
|
|
|
alphabetCall = runtime.CheckWitness(multiaddr)
|
|
|
|
}
|
2021-03-09 19:15:58 +00:00
|
|
|
|
2021-04-29 13:20:00 +00:00
|
|
|
if !alphabetCall {
|
2020-10-27 12:14:06 +00:00
|
|
|
publicKey := nodeInfo[2:35] // offset:2, len:33
|
|
|
|
if !runtime.CheckWitness(publicKey) {
|
|
|
|
panic("addPeer: witness check failed")
|
|
|
|
}
|
|
|
|
runtime.Notify("AddPeer", nodeInfo)
|
2021-05-21 11:37:31 +00:00
|
|
|
return
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
candidate := storageNode{
|
|
|
|
info: nodeInfo,
|
|
|
|
}
|
|
|
|
|
|
|
|
nm := addToNetmap(ctx, candidate)
|
|
|
|
|
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-02-19 15:52:27 +00:00
|
|
|
if nm == nil {
|
|
|
|
runtime.Log("addPeer: storage node already in the netmap")
|
2020-10-27 12:14:06 +00:00
|
|
|
} else {
|
2021-02-19 15:52:27 +00:00
|
|
|
common.SetSerialized(ctx, netmapKey, nm)
|
|
|
|
runtime.Log("addPeer: add storage node to the network map")
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-21 11:37:31 +00:00
|
|
|
func UpdateState(state int, publicKey interop.PublicKey) {
|
2020-10-27 12:14:06 +00:00
|
|
|
if len(publicKey) != 33 {
|
|
|
|
panic("updateState: incorrect public key")
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
alphabetCall bool
|
|
|
|
)
|
2021-03-09 19:15:58 +00:00
|
|
|
|
2021-04-29 13:20:00 +00:00
|
|
|
if notaryDisabled {
|
|
|
|
alphabet = common.AlphabetNodes()
|
|
|
|
nodeKey = common.InnerRingInvoker(alphabet)
|
|
|
|
alphabetCall = len(nodeKey) != 0
|
|
|
|
} else {
|
|
|
|
multiaddr := common.AlphabetAddress()
|
|
|
|
alphabetCall = runtime.CheckWitness(multiaddr)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !alphabetCall {
|
2020-10-27 12:14:06 +00:00
|
|
|
if !runtime.CheckWitness(publicKey) {
|
|
|
|
panic("updateState: witness check failed")
|
|
|
|
}
|
2021-02-19 15:52:27 +00:00
|
|
|
|
2020-10-27 12:14:06 +00:00
|
|
|
runtime.Notify("UpdateState", state, publicKey)
|
2021-02-19 15:52:27 +00:00
|
|
|
|
2021-05-21 11:37:31 +00:00
|
|
|
return
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2021-04-29 13:20:00 +00:00
|
|
|
if notaryDisabled {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2020-10-27 12:14:06 +00:00
|
|
|
switch nodeState(state) {
|
|
|
|
case offlineState:
|
|
|
|
newNetmap := removeFromNetmap(ctx, publicKey)
|
2021-02-19 15:52:27 +00:00
|
|
|
runtime.Log("updateState: remove storage node from the network map")
|
|
|
|
common.SetSerialized(ctx, netmapKey, newNetmap)
|
2020-10-27 12:14:06 +00:00
|
|
|
default:
|
|
|
|
panic("updateState: unsupported state")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
panic("newEpoch: this method must be invoked by inner ring nodes")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
multiaddr := common.AlphabetAddress()
|
|
|
|
if !runtime.CheckWitness(multiaddr) {
|
|
|
|
panic("newEpoch: this method must be invoked by inner ring nodes")
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2020-10-27 12:14:06 +00:00
|
|
|
data0snapshot := getSnapshot(ctx, snapshot0Key)
|
|
|
|
dataOnlineState := filterNetmap(ctx, onlineState)
|
|
|
|
|
2021-02-19 15:52:27 +00:00
|
|
|
runtime.Log("newEpoch: 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)
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2021-02-19 15:52:27 +00:00
|
|
|
// put actual snapshot into previous snapshot
|
|
|
|
common.SetSerialized(ctx, snapshot1Key, data0snapshot)
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2021-02-19 15:52:27 +00:00
|
|
|
// put netmap into actual snapshot
|
|
|
|
common.SetSerialized(ctx, snapshot0Key, 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
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Netmap() []storageNode {
|
2021-03-09 19:15:58 +00:00
|
|
|
ctx := storage.GetReadOnlyContext()
|
2020-10-27 12:14:06 +00:00
|
|
|
return getSnapshot(ctx, snapshot0Key)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Snapshot(diff int) []storageNode {
|
|
|
|
var key string
|
2021-03-09 19:15:58 +00:00
|
|
|
|
2020-10-27 12:14:06 +00:00
|
|
|
switch diff {
|
|
|
|
case 0:
|
|
|
|
key = snapshot0Key
|
|
|
|
case 1:
|
|
|
|
key = snapshot1Key
|
|
|
|
default:
|
|
|
|
panic("snapshot: incorrect diff")
|
|
|
|
}
|
|
|
|
|
2021-03-09 19:15:58 +00:00
|
|
|
ctx := storage.GetReadOnlyContext()
|
2020-10-27 12:14:06 +00:00
|
|
|
return getSnapshot(ctx, key)
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
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-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 {
|
|
|
|
panic("setConfig: invoked by non inner ring node")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
multiaddr := common.AlphabetAddress()
|
|
|
|
if !runtime.CheckWitness(multiaddr) {
|
|
|
|
panic("setConfig: invoked by non inner ring node")
|
|
|
|
}
|
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-02-19 15:52:27 +00:00
|
|
|
runtime.Log("setConfig: configuration has been updated")
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2021-05-21 11:37:31 +00:00
|
|
|
func InitConfig(args [][]byte) {
|
2021-03-09 19:15:58 +00:00
|
|
|
ctx := storage.GetContext()
|
|
|
|
|
2020-10-27 12:14:06 +00:00
|
|
|
if storage.Get(ctx, configuredKey) != nil {
|
|
|
|
panic("netmap: configuration already installed")
|
|
|
|
}
|
|
|
|
|
|
|
|
ln := len(args)
|
|
|
|
if ln%2 != 0 {
|
|
|
|
panic("initConfig: bad arguments")
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < ln/2; i++ {
|
|
|
|
key := args[i*2]
|
|
|
|
val := args[i*2+1]
|
|
|
|
|
|
|
|
setConfig(ctx, key, val)
|
|
|
|
}
|
|
|
|
|
|
|
|
storage.Put(ctx, configuredKey, true)
|
|
|
|
runtime.Log("netmap: config has been installed")
|
|
|
|
}
|
|
|
|
|
|
|
|
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-02-08 15:23:08 +00:00
|
|
|
pair := iterator.Value(it).([]interface{})
|
|
|
|
key := pair[0].([]byte)
|
|
|
|
val := pair[1].([]byte)
|
2020-10-27 12:14:06 +00:00
|
|
|
r := record{key: key[len(configPrefix):], val: val}
|
|
|
|
|
|
|
|
config = append(config, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
return config
|
|
|
|
}
|
|
|
|
|
|
|
|
func Version() int {
|
|
|
|
return version
|
|
|
|
}
|
|
|
|
|
|
|
|
func addToNetmap(ctx storage.Context, n storageNode) []netmapNode {
|
|
|
|
var (
|
|
|
|
newNode = n.info
|
|
|
|
newNodeKey = newNode[2:35]
|
|
|
|
|
|
|
|
netmap = getNetmapNodes(ctx)
|
|
|
|
node = netmapNode{
|
|
|
|
node: n,
|
|
|
|
state: onlineState,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
for i := range netmap {
|
|
|
|
netmapNode := netmap[i].node.info
|
|
|
|
netmapNodeKey := netmapNode[2:35]
|
|
|
|
|
2021-02-02 17:36:20 +00:00
|
|
|
if common.BytesEqual(newNodeKey, netmapNodeKey) {
|
2020-10-27 12:14:06 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
netmap = append(netmap, node)
|
|
|
|
|
|
|
|
return netmap
|
|
|
|
}
|
|
|
|
|
2021-03-04 19:21:49 +00:00
|
|
|
func removeFromNetmap(ctx storage.Context, key interop.PublicKey) []netmapNode {
|
2020-10-27 12:14:06 +00:00
|
|
|
var (
|
|
|
|
netmap = getNetmapNodes(ctx)
|
|
|
|
newNetmap = []netmapNode{}
|
|
|
|
)
|
|
|
|
|
|
|
|
for i := 0; i < len(netmap); i++ {
|
|
|
|
item := netmap[i]
|
|
|
|
node := item.node.info
|
|
|
|
publicKey := node[2:35] // offset:2, len:33
|
|
|
|
|
2021-02-02 17:36:20 +00:00
|
|
|
if !common.BytesEqual(publicKey, key) {
|
2020-10-27 12:14:06 +00:00
|
|
|
newNetmap = append(newNetmap, item)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return newNetmap
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
data := storage.Get(ctx, netmapKey)
|
|
|
|
if data != nil {
|
2021-03-12 12:16:36 +00:00
|
|
|
return std.Deserialize(data.([]byte)).([]netmapNode)
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return []netmapNode{}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|