2020-10-27 12:14:06 +00:00
|
|
|
package netmapcontract
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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/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
|
|
|
|
|
|
|
netmapKey = "netmap"
|
|
|
|
innerRingKey = "innerring"
|
|
|
|
configuredKey = "initconfig"
|
|
|
|
|
|
|
|
snapshot0Key = "snapshotCurrent"
|
|
|
|
snapshot1Key = "snapshotPrevious"
|
|
|
|
snapshotEpoch = "snapshotEpoch"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
_ nodeState = iota
|
|
|
|
onlineState
|
|
|
|
offlineState
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
configPrefix = []byte("config")
|
|
|
|
|
|
|
|
ctx storage.Context
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
ctx = storage.GetContext()
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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")
|
|
|
|
}
|
|
|
|
|
2021-02-02 18:26:34 +00:00
|
|
|
var irList []common.IRNode
|
2020-10-27 12:14:06 +00:00
|
|
|
|
|
|
|
for i := 0; i < len(keys); i++ {
|
|
|
|
key := keys[i]
|
2021-02-02 18:26:34 +00:00
|
|
|
irList = append(irList, common.IRNode{PublicKey: key})
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2021-02-02 17:36:20 +00:00
|
|
|
common.SetSerialized(ctx, innerRingKey, irList)
|
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{})
|
|
|
|
common.InitVote(ctx)
|
2020-10-27 12:14:06 +00:00
|
|
|
|
|
|
|
runtime.Log("netmap contract initialized")
|
|
|
|
}
|
|
|
|
|
2021-02-02 18:26:34 +00:00
|
|
|
func InnerRingList() []common.IRNode {
|
2020-10-27 12:14:06 +00:00
|
|
|
return getIRNodes(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func UpdateInnerRing(keys [][]byte) bool {
|
|
|
|
innerRing := getIRNodes(ctx)
|
|
|
|
threshold := len(innerRing)/3*2 + 1
|
|
|
|
|
2021-02-02 18:26:34 +00:00
|
|
|
irKey := common.InnerRingInvoker(innerRing)
|
2020-10-27 12:14:06 +00:00
|
|
|
if len(irKey) == 0 {
|
|
|
|
panic("updateInnerRing: this method must be invoked by inner ring nodes")
|
|
|
|
}
|
|
|
|
|
2021-02-02 18:26:34 +00:00
|
|
|
var irList []common.IRNode
|
2020-10-27 12:14:06 +00:00
|
|
|
|
|
|
|
for i := 0; i < len(keys); i++ {
|
|
|
|
key := keys[i]
|
2021-02-02 18:26:34 +00:00
|
|
|
irList = append(irList, common.IRNode{PublicKey: key})
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rawIRList := binary.Serialize(irList)
|
|
|
|
hashIRList := crypto.SHA256(rawIRList)
|
|
|
|
|
2021-02-02 17:36:20 +00:00
|
|
|
n := common.Vote(ctx, hashIRList, irKey)
|
2020-10-27 12:14:06 +00:00
|
|
|
if n >= threshold {
|
|
|
|
runtime.Log("updateInnerRing: inner ring list updated")
|
2021-02-02 17:36:20 +00:00
|
|
|
common.SetSerialized(ctx, innerRingKey, irList)
|
|
|
|
common.RemoveVotes(ctx, hashIRList)
|
2020-10-27 12:14:06 +00:00
|
|
|
} else {
|
|
|
|
runtime.Log("updateInnerRing: processed invoke from inner ring")
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func AddPeer(nodeInfo []byte) bool {
|
|
|
|
innerRing := getIRNodes(ctx)
|
|
|
|
threshold := len(innerRing)/3*2 + 1
|
|
|
|
|
2021-02-02 18:26:34 +00:00
|
|
|
irKey := common.InnerRingInvoker(innerRing)
|
2020-10-27 12:14:06 +00:00
|
|
|
if len(irKey) == 0 {
|
|
|
|
publicKey := nodeInfo[2:35] // offset:2, len:33
|
|
|
|
if !runtime.CheckWitness(publicKey) {
|
|
|
|
panic("addPeer: witness check failed")
|
|
|
|
}
|
|
|
|
runtime.Notify("AddPeer", nodeInfo)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
candidate := storageNode{
|
|
|
|
info: nodeInfo,
|
|
|
|
}
|
|
|
|
rawCandidate := binary.Serialize(candidate)
|
|
|
|
hashCandidate := crypto.SHA256(rawCandidate)
|
|
|
|
|
|
|
|
nm := addToNetmap(ctx, candidate)
|
|
|
|
|
2021-02-02 17:36:20 +00:00
|
|
|
n := common.Vote(ctx, hashCandidate, irKey)
|
2020-10-27 12:14:06 +00:00
|
|
|
if n >= threshold {
|
|
|
|
if nm == nil {
|
|
|
|
runtime.Log("addPeer: storage node already in the netmap")
|
|
|
|
} else {
|
2021-02-02 17:36:20 +00:00
|
|
|
common.SetSerialized(ctx, netmapKey, nm)
|
2020-10-27 12:14:06 +00:00
|
|
|
runtime.Log("addPeer: add storage node to the network map")
|
|
|
|
}
|
2021-02-02 17:36:20 +00:00
|
|
|
common.RemoveVotes(ctx, hashCandidate)
|
2020-10-27 12:14:06 +00:00
|
|
|
} else {
|
|
|
|
runtime.Log("addPeer: processed invoke from inner ring")
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func UpdateState(state int, publicKey []byte) bool {
|
|
|
|
if len(publicKey) != 33 {
|
|
|
|
panic("updateState: incorrect public key")
|
|
|
|
}
|
|
|
|
|
|
|
|
innerRing := getIRNodes(ctx)
|
|
|
|
threshold := len(innerRing)/3*2 + 1
|
|
|
|
|
2021-02-02 18:26:34 +00:00
|
|
|
irKey := common.InnerRingInvoker(innerRing)
|
2020-10-27 12:14:06 +00:00
|
|
|
if len(irKey) == 0 {
|
|
|
|
if !runtime.CheckWitness(publicKey) {
|
|
|
|
panic("updateState: witness check failed")
|
|
|
|
}
|
|
|
|
runtime.Notify("UpdateState", state, publicKey)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
switch nodeState(state) {
|
|
|
|
case offlineState:
|
|
|
|
newNetmap := removeFromNetmap(ctx, publicKey)
|
|
|
|
|
2021-02-02 17:42:05 +00:00
|
|
|
hashID := common.InvokeID([]interface{}{publicKey}, []byte("delete"))
|
2021-02-02 17:36:20 +00:00
|
|
|
n := common.Vote(ctx, hashID, irKey)
|
2020-10-27 12:14:06 +00:00
|
|
|
if n >= threshold {
|
|
|
|
runtime.Log("updateState: remove storage node from the network map")
|
2021-02-02 17:36:20 +00:00
|
|
|
common.SetSerialized(ctx, netmapKey, newNetmap)
|
|
|
|
common.RemoveVotes(ctx, hashID)
|
2020-10-27 12:14:06 +00:00
|
|
|
} else {
|
|
|
|
runtime.Log("updateState: processed invoke from inner ring")
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
panic("updateState: unsupported state")
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewEpoch(epochNum int) bool {
|
|
|
|
innerRing := getIRNodes(ctx)
|
|
|
|
threshold := len(innerRing)/3*2 + 1
|
|
|
|
|
2021-02-02 18:26:34 +00:00
|
|
|
irKey := common.InnerRingInvoker(innerRing)
|
2020-10-27 12:14:06 +00:00
|
|
|
if len(irKey) == 0 {
|
|
|
|
panic("newEpoch: this method must be invoked by inner ring nodes")
|
|
|
|
}
|
|
|
|
|
2020-12-25 11:45:24 +00:00
|
|
|
currentEpoch := storage.Get(ctx, snapshotEpoch).(int)
|
|
|
|
if epochNum <= currentEpoch {
|
|
|
|
return false // ignore invocations with invalid epoch
|
|
|
|
}
|
|
|
|
|
2020-10-27 12:14:06 +00:00
|
|
|
data0snapshot := getSnapshot(ctx, snapshot0Key)
|
|
|
|
dataOnlineState := filterNetmap(ctx, onlineState)
|
|
|
|
|
2021-02-02 17:42:05 +00:00
|
|
|
hashID := common.InvokeID([]interface{}{epochNum}, []byte("epoch"))
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2021-02-02 17:36:20 +00:00
|
|
|
n := common.Vote(ctx, hashID, irKey)
|
2020-10-27 12:14:06 +00:00
|
|
|
if n >= threshold {
|
|
|
|
runtime.Log("newEpoch: process new epoch")
|
|
|
|
|
|
|
|
// todo: check if provided epoch number is bigger than current
|
|
|
|
storage.Put(ctx, snapshotEpoch, epochNum)
|
|
|
|
|
|
|
|
// put actual snapshot into previous snapshot
|
2021-02-02 17:36:20 +00:00
|
|
|
common.SetSerialized(ctx, snapshot1Key, data0snapshot)
|
2020-10-27 12:14:06 +00:00
|
|
|
|
|
|
|
// put netmap into actual snapshot
|
2021-02-02 17:36:20 +00:00
|
|
|
common.SetSerialized(ctx, snapshot0Key, dataOnlineState)
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2021-02-02 17:36:20 +00:00
|
|
|
common.RemoveVotes(ctx, hashID)
|
2020-10-27 12:14:06 +00:00
|
|
|
runtime.Notify("NewEpoch", epochNum)
|
|
|
|
} else {
|
|
|
|
runtime.Log("newEpoch: processed invoke from inner ring")
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func Epoch() int {
|
|
|
|
return storage.Get(ctx, snapshotEpoch).(int)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Netmap() []storageNode {
|
|
|
|
return getSnapshot(ctx, snapshot0Key)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Snapshot(diff int) []storageNode {
|
|
|
|
var key string
|
|
|
|
switch diff {
|
|
|
|
case 0:
|
|
|
|
key = snapshot0Key
|
|
|
|
case 1:
|
|
|
|
key = snapshot1Key
|
|
|
|
default:
|
|
|
|
panic("snapshot: incorrect diff")
|
|
|
|
}
|
|
|
|
|
|
|
|
return getSnapshot(ctx, key)
|
|
|
|
}
|
|
|
|
|
2021-01-12 08:09:30 +00:00
|
|
|
func SnapshotByEpoch(epoch int) []storageNode {
|
|
|
|
currentEpoch := storage.Get(ctx, snapshotEpoch).(int)
|
|
|
|
|
|
|
|
return Snapshot(currentEpoch - epoch)
|
|
|
|
}
|
|
|
|
|
2020-10-27 12:14:06 +00:00
|
|
|
func Config(key []byte) interface{} {
|
|
|
|
return getConfig(ctx, key)
|
|
|
|
}
|
|
|
|
|
|
|
|
func SetConfig(id, key, val []byte) bool {
|
|
|
|
// check if it is inner ring invocation
|
|
|
|
innerRing := getIRNodes(ctx)
|
|
|
|
threshold := len(innerRing)/3*2 + 1
|
|
|
|
|
2021-02-02 18:26:34 +00:00
|
|
|
irKey := common.InnerRingInvoker(innerRing)
|
2020-10-27 12:14:06 +00:00
|
|
|
if len(irKey) == 0 {
|
|
|
|
panic("setConfig: invoked by non inner ring node")
|
|
|
|
}
|
|
|
|
|
|
|
|
// check unique id of the operation
|
2021-02-02 17:42:05 +00:00
|
|
|
hashID := common.InvokeID([]interface{}{id, key, val}, []byte("config"))
|
2021-02-02 17:36:20 +00:00
|
|
|
n := common.Vote(ctx, hashID, irKey)
|
2020-10-27 12:14:06 +00:00
|
|
|
|
|
|
|
if n >= threshold {
|
2021-02-02 17:36:20 +00:00
|
|
|
common.RemoveVotes(ctx, hashID)
|
2020-10-27 12:14:06 +00:00
|
|
|
setConfig(ctx, key, val)
|
|
|
|
|
|
|
|
runtime.Log("setConfig: configuration has been updated")
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func InitConfig(args [][]byte) bool {
|
|
|
|
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")
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func ListConfig() []record {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func removeFromNetmap(ctx storage.Context, key []byte) []netmapNode {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-02-02 18:26:34 +00:00
|
|
|
func getIRNodes(ctx storage.Context) []common.IRNode {
|
2020-10-27 12:14:06 +00:00
|
|
|
data := storage.Get(ctx, innerRingKey)
|
|
|
|
if data != nil {
|
2021-02-02 18:26:34 +00:00
|
|
|
return binary.Deserialize(data.([]byte)).([]common.IRNode)
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2021-02-02 18:26:34 +00:00
|
|
|
return []common.IRNode{}
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func getNetmapNodes(ctx storage.Context) []netmapNode {
|
|
|
|
data := storage.Get(ctx, netmapKey)
|
|
|
|
if data != nil {
|
|
|
|
return binary.Deserialize(data.([]byte)).([]netmapNode)
|
|
|
|
}
|
|
|
|
|
|
|
|
return []netmapNode{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getSnapshot(ctx storage.Context, key string) []storageNode {
|
|
|
|
data := storage.Get(ctx, key)
|
|
|
|
if data != nil {
|
|
|
|
return binary.Deserialize(data.([]byte)).([]storageNode)
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|