From abdbe6b9dc4f60b92758d9a5d5828367482c1553 Mon Sep 17 00:00:00 2001 From: alexvanin Date: Thu, 16 Jul 2020 15:06:49 +0300 Subject: [PATCH 1/2] Update methods for inner ring candidate managing This commit: - increases default candidate fee, - adds function to list inner ring candidates, - updates add and remove candidate methods. --- neofs_contract.go | 78 ++++++++++++++++++++++++++++++++++------------- 1 file changed, 57 insertions(+), 21 deletions(-) diff --git a/neofs_contract.go b/neofs_contract.go index 58f6c10..2c2a953 100644 --- a/neofs_contract.go +++ b/neofs_contract.go @@ -3,6 +3,7 @@ package smart_contract import ( "github.com/nspcc-dev/neo-go/pkg/interop/binary" "github.com/nspcc-dev/neo-go/pkg/interop/blockchain" + "github.com/nspcc-dev/neo-go/pkg/interop/contract" "github.com/nspcc-dev/neo-go/pkg/interop/crypto" "github.com/nspcc-dev/neo-go/pkg/interop/engine" "github.com/nspcc-dev/neo-go/pkg/interop/runtime" @@ -28,7 +29,7 @@ type ( const ( tokenHash = "\x3b\x7d\x37\x11\xc6\xf0\xcc\xf9\xb1\xdc\xa9\x03\xd1\xbf\xa1\xd8\x96\xf1\x23\x8c" - innerRingCandidateFee = 100 * 1000 * 1000 // 10^8 + innerRingCandidateFee = 100 * 1_0000_0000 // 100 Fixed8 Gas version = 2 innerRingKey = "innerring" voteKey = "ballots" @@ -93,41 +94,63 @@ func Main(op string, args []interface{}) interface{} { return true case "InnerRingList": - return getInnerRingNodes(ctx) + return getInnerRingNodes(ctx, innerRingKey) + case "InnerRingCandidates": + return getInnerRingNodes(ctx, candidatesKey) case "InnerRingCandidateRemove": - data := args[0].([]byte) // public key - if !runtime.CheckWitness(data) { - panic("you should be the owner of the public key") + if len(args) != 1 { + panic("irCandidateRemove: bad arguments") } - delSerializedIR(ctx, "InnerRingCandidates", data) + key := args[0].([]byte) // inner ring candidate public key + if !runtime.CheckWitness(key) { + panic("irCandidateRemove: you should be the owner of the public key") + } + + var nodes = []node{} // it is explicit declaration of empty slice, not nil + candidates := getInnerRingNodes(ctx, candidatesKey) + + for i := range candidates { + c := candidates[i] + if !bytesEqual(c.pub, key) { + nodes = append(nodes, c) + } else { + runtime.Log("irCandidateRemove: candidate has been removed") + } + } + + setSerialized(ctx, candidatesKey, nodes) return true case "InnerRingCandidateAdd": - key := args[0].([]byte) // public key + if len(args) != 1 { + panic("irCandidateAdd: bad arguments") + } + key := args[0].([]byte) // inner ring candidate public key if !runtime.CheckWitness(key) { - panic("you should be the owner of the public key") + panic("irCandidateAdd: you should be the owner of the public key") } - candidates := getSerialized(ctx, "InnerRingCandidates").([]node) - if containsPub(candidates, key) { - panic("is already in list") + c := node{pub: key} + candidates := getInnerRingNodes(ctx, candidatesKey) + + list, ok := addNode(candidates, c) + if !ok { + panic("irCandidateAdd: candidate already in the list") } - from := pubToScriptHash(key) + from := contract.CreateStandardAccount(key) to := runtime.GetExecutingScriptHash() params := []interface{}{from, to, innerRingCandidateFee} transferred := engine.AppCall([]byte(tokenHash), "transfer", params).(bool) if !transferred { - panic("failed to transfer funds, aborting") + panic("irCandidateAdd: failed to transfer funds, aborting") } - candidate := node{pub: key} - if !putSerialized(ctx, "InnerRingCandidates", candidate) { - panic("failed to put candidate into the queue") - } + runtime.Log("irCandidateAdd: candidate has been added") + setSerialized(ctx, candidatesKey, list) return true case "Deposit": @@ -193,7 +216,7 @@ func Main(op string, args []interface{}) interface{} { amount := args[2].(int) // amount of GAS lockAcc := args[3].([]byte) // lock account from internal balance contract - irList := getInnerRingNodes(ctx) + irList := getInnerRingNodes(ctx, innerRingKey) threshold := len(irList)/3*2 + 1 cashedCheques := getCashedCheques(ctx) @@ -335,7 +358,7 @@ func Main(op string, args []interface{}) interface{} { panic("isInnerRing: incorrect public key") } - irList := getInnerRingNodes(ctx) + irList := getInnerRingNodes(ctx, innerRingKey) for i := range irList { node := irList[i] @@ -545,8 +568,8 @@ func setSerialized(ctx storage.Context, key interface{}, value interface{}) { } // getInnerRingNodes returns deserialized slice of inner ring nodes from storage. -func getInnerRingNodes(ctx storage.Context) []node { - data := storage.Get(ctx, innerRingKey) +func getInnerRingNodes(ctx storage.Context, key string) []node { + data := storage.Get(ctx, key) if data != nil { return binary.Deserialize(data.([]byte)).([]node) } @@ -587,6 +610,19 @@ func addCheque(lst []cheque, c cheque) ([]cheque, bool) { return lst, true } +// addNode returns slice of nodes with appended node 'n' and bool flag +// that set to false if node 'n' is already presented in the slice 'lst'. +func addNode(lst []node, n node) ([]node, bool) { + for i := 0; i < len(lst); i++ { + if bytesEqual(n.pub, lst[i].pub) { + return nil, false + } + } + + lst = append(lst, n) + return lst, true +} + // bytesEqual compares two slice of bytes by wrapping them into strings, // which is necessary with new util.Equal interop behaviour, see neo-go#1176. func bytesEqual(a []byte, b []byte) bool { From 7a806bd550a9858f0f6faded40edd25a0e30698c Mon Sep 17 00:00:00 2001 From: alexvanin Date: Thu, 16 Jul 2020 17:23:52 +0300 Subject: [PATCH 2/2] Update "InnerRingUpdate" method --- neofs_contract.go | 130 +++++++++++++++++++++++++++------------------- 1 file changed, 76 insertions(+), 54 deletions(-) diff --git a/neofs_contract.go b/neofs_contract.go index 2c2a953..7d36587 100644 --- a/neofs_contract.go +++ b/neofs_contract.go @@ -37,6 +37,7 @@ const ( cashedChequesKey = "cheques" blockDiff = 20 // change base on performance evaluation publicKeySize = 33 + minInnerRingSize = 3 ) 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") } - 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) for i := range candidates { @@ -277,17 +278,12 @@ func Main(op string, args []interface{}) interface{} { return true case "InnerRingUpdate": - data := args[0].([]byte) + if len(args) < 1+minInnerRingSize { + // cheque id + inner ring public keys + panic("irUpdate: bad arguments") + } - id := data[:8] - 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) + irList := getInnerRingNodes(ctx, innerRingKey) threshold := len(irList)/3*2 + 1 irKey := innerRingInvoker(irList) @@ -295,56 +291,61 @@ func Main(op string, args []interface{}) interface{} { panic("innerRingUpdate: invoked by non inner ring node") } + id := args[0].([]byte) 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) + newIR := []node{} - n := vote(ctx, chequeHash, irKey) + loop: + for i := 1; i < len(args); i++ { + 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++ { + n := irList[j] + if bytesEqual(n.pub, key) { + newIR = append(newIR, n) + oldNodes++ + + continue loop + } + } + + // find key in candidates list + candidates, newIR, ok = rmNodeByKey(candidates, newIR, key) + if !ok { + panic("irUpdate: unknown public key in inner ring list") + } + } + + if oldNodes < len(newIR)*2/3+1 { + panic("irUpdate: inner ring change rate must not be more than 1/3 ") + } + + hashID := crypto.SHA256(id) + + n := vote(ctx, hashID, irKey) if n >= threshold { - removeVotes(ctx, chequeHash) + removeVotes(ctx, hashID) - candidates := getSerialized(ctx, "InnerRingCandidates").([]node) - offset = 10 - newIR := []node{} + setSerialized(ctx, candidatesKey, candidates) + setSerialized(ctx, innerRingKey, newIR) + setSerialized(ctx, cashedChequesKey, chequesList) - loop: - for i := 0; i < listItemCount; i, offset = i+1, offset+33 { - pub := data[offset : offset+33] - - for j := 0; j < len(irList); j++ { - n := irList[j] - if util.Equals(n.pub, pub) { - newIR = append(newIR, n) - continue loop - } - } - - for j := 0; j < len(candidates); j++ { - n := candidates[j] - if util.Equals(n.pub, pub) { - newIR = append(newIR, n) - continue loop - } - } - } - - if len(newIR) != listItemCount { - panic("new inner ring wasn't processed correctly") - } - - for i := 0; i < len(newIR); i++ { - n := newIR[i] - delSerializedIR(ctx, "InnerRingCandidates", n.pub) - } - - newIRData := binary.Serialize(newIR) - storage.Put(ctx, "InnerRingList", newIRData) - putSerialized(ctx, "UsedVerifCheckList", c) - - runtime.Notify("InnerRingUpdate", c.id, newIRData) + runtime.Notify("InnerRingUpdate", c.id, newIR) + runtime.Log("irUpdate: inner ring list has been updated") } return true @@ -623,6 +624,27 @@ func addNode(lst []node, n node) ([]node, bool) { 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, // which is necessary with new util.Equal interop behaviour, see neo-go#1176. func bytesEqual(a []byte, b []byte) bool {