Merge pull request #12 from nspcc-dev/neo3/ir-candidates

Update inner ring candidate methods
This commit is contained in:
Alex Vanin 2020-07-20 14:49:26 +03:00 committed by GitHub
commit 058e39dcf1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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"
@ -36,6 +37,7 @@ const (
cashedChequesKey = "cheques"
blockDiff = 20 // change base on performance evaluation
publicKeySize = 33
minInnerRingSize = 3
)
func Main(op string, args []interface{}) interface{} {
@ -93,41 +95,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")
}
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 +217,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)
@ -254,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)
@ -272,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)
n := vote(ctx, chequeHash, irKey)
if n >= threshold {
removeVotes(ctx, chequeHash)
candidates := getSerialized(ctx, "InnerRingCandidates").([]node)
offset = 10
oldNodes := 0
candidates := getInnerRingNodes(ctx, candidatesKey)
newIR := []node{}
loop:
for i := 0; i < listItemCount; i, offset = i+1, offset+33 {
pub := data[offset : offset+33]
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 util.Equals(n.pub, pub) {
if bytesEqual(n.pub, key) {
newIR = append(newIR, n)
oldNodes++
continue loop
}
}
for j := 0; j < len(candidates); j++ {
n := candidates[j]
if util.Equals(n.pub, pub) {
newIR = append(newIR, n)
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 len(newIR) != listItemCount {
panic("new inner ring wasn't processed correctly")
if oldNodes < len(newIR)*2/3+1 {
panic("irUpdate: inner ring change rate must not be more than 1/3 ")
}
for i := 0; i < len(newIR); i++ {
n := newIR[i]
delSerializedIR(ctx, "InnerRingCandidates", n.pub)
}
hashID := crypto.SHA256(id)
newIRData := binary.Serialize(newIR)
storage.Put(ctx, "InnerRingList", newIRData)
putSerialized(ctx, "UsedVerifCheckList", c)
n := vote(ctx, hashID, irKey)
if n >= threshold {
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
@ -335,7 +359,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 +569,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 +611,40 @@ 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
}
// 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 {