[#51] netmap: Support notary contract

Signed-off-by: Alex Vanin <alexey@nspcc.ru>
This commit is contained in:
Alex Vanin 2021-02-19 18:52:27 +03:00 committed by Alex Vanin
parent b17337b8e5
commit 28a9e2842f

View file

@ -82,7 +82,6 @@ func Init(owner interop.Hash160, keys [][]byte) {
common.SetSerialized(ctx, netmapKey, []netmapNode{}) common.SetSerialized(ctx, netmapKey, []netmapNode{})
common.SetSerialized(ctx, snapshot0Key, []netmapNode{}) common.SetSerialized(ctx, snapshot0Key, []netmapNode{})
common.SetSerialized(ctx, snapshot1Key, []netmapNode{}) common.SetSerialized(ctx, snapshot1Key, []netmapNode{})
common.InitVote(ctx)
runtime.Log("netmap contract initialized") runtime.Log("netmap contract initialized")
} }
@ -108,11 +107,8 @@ func Multiaddress() []byte {
} }
func UpdateInnerRing(keys [][]byte) bool { func UpdateInnerRing(keys [][]byte) bool {
innerRing := getIRNodes(ctx) multiaddr := Multiaddress()
threshold := len(innerRing)/3*2 + 1 if !runtime.CheckWitness(multiaddr) {
irKey := common.InnerRingInvoker(innerRing)
if len(irKey) == 0 {
panic("updateInnerRing: this method must be invoked by inner ring nodes") panic("updateInnerRing: this method must be invoked by inner ring nodes")
} }
@ -123,27 +119,15 @@ func UpdateInnerRing(keys [][]byte) bool {
irList = append(irList, common.IRNode{PublicKey: key}) irList = append(irList, common.IRNode{PublicKey: key})
} }
rawIRList := binary.Serialize(irList) runtime.Log("updateInnerRing: inner ring list updated")
hashIRList := crypto.SHA256(rawIRList) common.SetSerialized(ctx, innerRingKey, irList)
n := common.Vote(ctx, hashIRList, irKey)
if n >= threshold {
runtime.Log("updateInnerRing: inner ring list updated")
common.SetSerialized(ctx, innerRingKey, irList)
common.RemoveVotes(ctx, hashIRList)
} else {
runtime.Log("updateInnerRing: processed invoke from inner ring")
}
return true return true
} }
func AddPeer(nodeInfo []byte) bool { func AddPeer(nodeInfo []byte) bool {
innerRing := getIRNodes(ctx) multiaddr := Multiaddress()
threshold := len(innerRing)/3*2 + 1 if !runtime.CheckWitness(multiaddr) {
irKey := common.InnerRingInvoker(innerRing)
if len(irKey) == 0 {
publicKey := nodeInfo[2:35] // offset:2, len:33 publicKey := nodeInfo[2:35] // offset:2, len:33
if !runtime.CheckWitness(publicKey) { if !runtime.CheckWitness(publicKey) {
panic("addPeer: witness check failed") panic("addPeer: witness check failed")
@ -155,22 +139,14 @@ func AddPeer(nodeInfo []byte) bool {
candidate := storageNode{ candidate := storageNode{
info: nodeInfo, info: nodeInfo,
} }
rawCandidate := binary.Serialize(candidate)
hashCandidate := crypto.SHA256(rawCandidate)
nm := addToNetmap(ctx, candidate) nm := addToNetmap(ctx, candidate)
n := common.Vote(ctx, hashCandidate, irKey) if nm == nil {
if n >= threshold { runtime.Log("addPeer: storage node already in the netmap")
if nm == nil {
runtime.Log("addPeer: storage node already in the netmap")
} else {
common.SetSerialized(ctx, netmapKey, nm)
runtime.Log("addPeer: add storage node to the network map")
}
common.RemoveVotes(ctx, hashCandidate)
} else { } else {
runtime.Log("addPeer: processed invoke from inner ring") common.SetSerialized(ctx, netmapKey, nm)
runtime.Log("addPeer: add storage node to the network map")
} }
return true return true
@ -181,31 +157,22 @@ func UpdateState(state int, publicKey []byte) bool {
panic("updateState: incorrect public key") panic("updateState: incorrect public key")
} }
innerRing := getIRNodes(ctx) multiaddr := Multiaddress()
threshold := len(innerRing)/3*2 + 1 if !runtime.CheckWitness(multiaddr) {
irKey := common.InnerRingInvoker(innerRing)
if len(irKey) == 0 {
if !runtime.CheckWitness(publicKey) { if !runtime.CheckWitness(publicKey) {
panic("updateState: witness check failed") panic("updateState: witness check failed")
} }
runtime.Notify("UpdateState", state, publicKey) runtime.Notify("UpdateState", state, publicKey)
return true return true
} }
switch nodeState(state) { switch nodeState(state) {
case offlineState: case offlineState:
newNetmap := removeFromNetmap(ctx, publicKey) newNetmap := removeFromNetmap(ctx, publicKey)
runtime.Log("updateState: remove storage node from the network map")
hashID := common.InvokeID([]interface{}{publicKey}, []byte("delete")) common.SetSerialized(ctx, netmapKey, newNetmap)
n := common.Vote(ctx, hashID, irKey)
if n >= threshold {
runtime.Log("updateState: remove storage node from the network map")
common.SetSerialized(ctx, netmapKey, newNetmap)
common.RemoveVotes(ctx, hashID)
} else {
runtime.Log("updateState: processed invoke from inner ring")
}
default: default:
panic("updateState: unsupported state") panic("updateState: unsupported state")
} }
@ -214,11 +181,8 @@ func UpdateState(state int, publicKey []byte) bool {
} }
func NewEpoch(epochNum int) bool { func NewEpoch(epochNum int) bool {
innerRing := getIRNodes(ctx) multiaddr := Multiaddress()
threshold := len(innerRing)/3*2 + 1 if !runtime.CheckWitness(multiaddr) {
irKey := common.InnerRingInvoker(innerRing)
if len(irKey) == 0 {
panic("newEpoch: this method must be invoked by inner ring nodes") panic("newEpoch: this method must be invoked by inner ring nodes")
} }
@ -230,26 +194,18 @@ func NewEpoch(epochNum int) bool {
data0snapshot := getSnapshot(ctx, snapshot0Key) data0snapshot := getSnapshot(ctx, snapshot0Key)
dataOnlineState := filterNetmap(ctx, onlineState) dataOnlineState := filterNetmap(ctx, onlineState)
hashID := common.InvokeID([]interface{}{epochNum}, []byte("epoch")) runtime.Log("newEpoch: process new epoch")
n := common.Vote(ctx, hashID, irKey) // todo: check if provided epoch number is bigger than current
if n >= threshold { storage.Put(ctx, snapshotEpoch, epochNum)
runtime.Log("newEpoch: process new epoch")
// todo: check if provided epoch number is bigger than current // put actual snapshot into previous snapshot
storage.Put(ctx, snapshotEpoch, epochNum) common.SetSerialized(ctx, snapshot1Key, data0snapshot)
// put actual snapshot into previous snapshot // put netmap into actual snapshot
common.SetSerialized(ctx, snapshot1Key, data0snapshot) common.SetSerialized(ctx, snapshot0Key, dataOnlineState)
// put netmap into actual snapshot runtime.Notify("NewEpoch", epochNum)
common.SetSerialized(ctx, snapshot0Key, dataOnlineState)
common.RemoveVotes(ctx, hashID)
runtime.Notify("NewEpoch", epochNum)
} else {
runtime.Log("newEpoch: processed invoke from inner ring")
}
return true return true
} }
@ -287,25 +243,14 @@ func Config(key []byte) interface{} {
} }
func SetConfig(id, key, val []byte) bool { func SetConfig(id, key, val []byte) bool {
// check if it is inner ring invocation multiaddr := Multiaddress()
innerRing := getIRNodes(ctx) if !runtime.CheckWitness(multiaddr) {
threshold := len(innerRing)/3*2 + 1
irKey := common.InnerRingInvoker(innerRing)
if len(irKey) == 0 {
panic("setConfig: invoked by non inner ring node") panic("setConfig: invoked by non inner ring node")
} }
// check unique id of the operation setConfig(ctx, key, val)
hashID := common.InvokeID([]interface{}{id, key, val}, []byte("config"))
n := common.Vote(ctx, hashID, irKey)
if n >= threshold { runtime.Log("setConfig: configuration has been updated")
common.RemoveVotes(ctx, hashID)
setConfig(ctx, key, val)
runtime.Log("setConfig: configuration has been updated")
}
return true return true
} }