[#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)
hashIRList := crypto.SHA256(rawIRList)
n := common.Vote(ctx, hashIRList, irKey)
if n >= threshold {
runtime.Log("updateInnerRing: inner ring list updated") runtime.Log("updateInnerRing: inner ring list updated")
common.SetSerialized(ctx, innerRingKey, irList) 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,23 +139,15 @@ 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 n >= threshold {
if nm == nil { if nm == nil {
runtime.Log("addPeer: storage node already in the netmap") runtime.Log("addPeer: storage node already in the netmap")
} else { } else {
common.SetSerialized(ctx, netmapKey, nm) common.SetSerialized(ctx, netmapKey, nm)
runtime.Log("addPeer: add storage node to the network map") runtime.Log("addPeer: add storage node to the network map")
} }
common.RemoveVotes(ctx, hashCandidate)
} else {
runtime.Log("addPeer: processed invoke from inner ring")
}
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)
hashID := common.InvokeID([]interface{}{publicKey}, []byte("delete"))
n := common.Vote(ctx, hashID, irKey)
if n >= threshold {
runtime.Log("updateState: remove storage node from the network map") runtime.Log("updateState: remove storage node from the network map")
common.SetSerialized(ctx, netmapKey, newNetmap) 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,10 +194,6 @@ 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"))
n := common.Vote(ctx, hashID, irKey)
if n >= threshold {
runtime.Log("newEpoch: process new epoch") runtime.Log("newEpoch: process new epoch")
// todo: check if provided epoch number is bigger than current // todo: check if provided epoch number is bigger than current
@ -245,11 +205,7 @@ func NewEpoch(epochNum int) bool {
// put netmap into actual snapshot // put netmap into actual snapshot
common.SetSerialized(ctx, snapshot0Key, dataOnlineState) common.SetSerialized(ctx, snapshot0Key, dataOnlineState)
common.RemoveVotes(ctx, hashID)
runtime.Notify("NewEpoch", epochNum) 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
hashID := common.InvokeID([]interface{}{id, key, val}, []byte("config"))
n := common.Vote(ctx, hashID, irKey)
if n >= threshold {
common.RemoveVotes(ctx, hashID)
setConfig(ctx, key, val) setConfig(ctx, key, val)
runtime.Log("setConfig: configuration has been updated") runtime.Log("setConfig: configuration has been updated")
}
return true return true
} }