Update "InnerRingUpdate" method

This commit is contained in:
alexvanin 2020-07-16 17:23:52 +03:00
parent abdbe6b9dc
commit 7a806bd550

View file

@ -37,6 +37,7 @@ const (
cashedChequesKey = "cheques" cashedChequesKey = "cheques"
blockDiff = 20 // change base on performance evaluation blockDiff = 20 // change base on performance evaluation
publicKeySize = 33 publicKeySize = 33
minInnerRingSize = 3
) )
func Main(op string, args []interface{}) interface{} { func Main(op string, args []interface{}) interface{} {
@ -107,7 +108,7 @@ func Main(op string, args []interface{}) interface{} {
panic("irCandidateRemove: you should be the owner of the public key") panic("irCandidateRemove: you should be the owner of the public key")
} }
var nodes = []node{} // it is explicit declaration of empty slice, not nil nodes := []node{} // it is explicit declaration of empty slice, not nil
candidates := getInnerRingNodes(ctx, candidatesKey) candidates := getInnerRingNodes(ctx, candidatesKey)
for i := range candidates { for i := range candidates {
@ -277,17 +278,12 @@ func Main(op string, args []interface{}) interface{} {
return true return true
case "InnerRingUpdate": case "InnerRingUpdate":
data := args[0].([]byte) if len(args) < 1+minInnerRingSize {
// cheque id + inner ring public keys
panic("irUpdate: bad arguments")
}
id := data[:8] irList := getInnerRingNodes(ctx, innerRingKey)
var ln interface{} = data[8:10]
listItemCount := ln.(int)
listSize := listItemCount * 33
offset := 8 + 2 + listSize
irList := getSerialized(ctx, "InnerRingList").([]node)
usedList := getSerialized(ctx, "UsedVerifCheckList").([]cheque)
threshold := len(irList)/3*2 + 1 threshold := len(irList)/3*2 + 1
irKey := innerRingInvoker(irList) irKey := innerRingInvoker(irList)
@ -295,56 +291,61 @@ func Main(op string, args []interface{}) interface{} {
panic("innerRingUpdate: invoked by non inner ring node") panic("innerRingUpdate: invoked by non inner ring node")
} }
id := args[0].([]byte)
c := cheque{id: id} c := cheque{id: id}
if containsCheck(usedList, c) {
panic("innerRingUpdate: cheque has non unique id") cashedCheques := getCashedCheques(ctx)
chequesList, ok := addCheque(cashedCheques, c)
if !ok {
panic("irUpdate: non unique id")
} }
chequeHash := crypto.SHA256(data) oldNodes := 0
candidates := getInnerRingNodes(ctx, candidatesKey)
n := vote(ctx, chequeHash, irKey)
if n >= threshold {
removeVotes(ctx, chequeHash)
candidates := getSerialized(ctx, "InnerRingCandidates").([]node)
offset = 10
newIR := []node{} newIR := []node{}
loop: loop:
for i := 0; i < listItemCount; i, offset = i+1, offset+33 { for i := 1; i < len(args); i++ {
pub := data[offset : offset+33] key := args[i].([]byte)
if len(key) != publicKeySize {
panic("irUpdate: invalid public key in inner ring list")
}
// find key in actual inner ring list
for j := 0; j < len(irList); j++ { for j := 0; j < len(irList); j++ {
n := irList[j] n := irList[j]
if util.Equals(n.pub, pub) { if bytesEqual(n.pub, key) {
newIR = append(newIR, n) newIR = append(newIR, n)
oldNodes++
continue loop continue loop
} }
} }
for j := 0; j < len(candidates); j++ { // find key in candidates list
n := candidates[j] candidates, newIR, ok = rmNodeByKey(candidates, newIR, key)
if util.Equals(n.pub, pub) { if !ok {
newIR = append(newIR, n) panic("irUpdate: unknown public key in inner ring list")
continue loop
}
} }
} }
if len(newIR) != listItemCount { if oldNodes < len(newIR)*2/3+1 {
panic("new inner ring wasn't processed correctly") panic("irUpdate: inner ring change rate must not be more than 1/3 ")
} }
for i := 0; i < len(newIR); i++ { hashID := crypto.SHA256(id)
n := newIR[i]
delSerializedIR(ctx, "InnerRingCandidates", n.pub)
}
newIRData := binary.Serialize(newIR) n := vote(ctx, hashID, irKey)
storage.Put(ctx, "InnerRingList", newIRData) if n >= threshold {
putSerialized(ctx, "UsedVerifCheckList", c) removeVotes(ctx, hashID)
runtime.Notify("InnerRingUpdate", c.id, newIRData) setSerialized(ctx, candidatesKey, candidates)
setSerialized(ctx, innerRingKey, newIR)
setSerialized(ctx, cashedChequesKey, chequesList)
runtime.Notify("InnerRingUpdate", c.id, newIR)
runtime.Log("irUpdate: inner ring list has been updated")
} }
return true return true
@ -623,6 +624,27 @@ func addNode(lst []node, n node) ([]node, bool) {
return lst, true return lst, true
} }
// rmNodeByKey returns slice of nodes without node with key 'k',
// slices of nodes 'add' with node with key 'k' and bool flag,
// that set to false if node with a key 'k' does not exists in the slice 'lst'.
func rmNodeByKey(lst, add []node, k []byte) ([]node, []node, bool) {
var (
flag bool
newLst = []node{} // it is explicit declaration of empty slice, not nil
)
for i := 0; i < len(lst); i++ {
if bytesEqual(k, lst[i].pub) {
add = append(add, lst[i])
flag = true
} else {
newLst = append(newLst, lst[i])
}
}
return newLst, add, flag
}
// bytesEqual compares two slice of bytes by wrapping them into strings, // bytesEqual compares two slice of bytes by wrapping them into strings,
// which is necessary with new util.Equal interop behaviour, see neo-go#1176. // which is necessary with new util.Equal interop behaviour, see neo-go#1176.
func bytesEqual(a []byte, b []byte) bool { func bytesEqual(a []byte, b []byte) bool {