forked from TrueCloudLab/frostfs-contract
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.
This commit is contained in:
parent
f5d2e96063
commit
abdbe6b9dc
1 changed files with 57 additions and 21 deletions
|
@ -3,6 +3,7 @@ package smart_contract
|
||||||
import (
|
import (
|
||||||
"github.com/nspcc-dev/neo-go/pkg/interop/binary"
|
"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/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/crypto"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/interop/engine"
|
"github.com/nspcc-dev/neo-go/pkg/interop/engine"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/interop/runtime"
|
"github.com/nspcc-dev/neo-go/pkg/interop/runtime"
|
||||||
|
@ -28,7 +29,7 @@ type (
|
||||||
|
|
||||||
const (
|
const (
|
||||||
tokenHash = "\x3b\x7d\x37\x11\xc6\xf0\xcc\xf9\xb1\xdc\xa9\x03\xd1\xbf\xa1\xd8\x96\xf1\x23\x8c"
|
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
|
version = 2
|
||||||
innerRingKey = "innerring"
|
innerRingKey = "innerring"
|
||||||
voteKey = "ballots"
|
voteKey = "ballots"
|
||||||
|
@ -93,41 +94,63 @@ func Main(op string, args []interface{}) interface{} {
|
||||||
|
|
||||||
return true
|
return true
|
||||||
case "InnerRingList":
|
case "InnerRingList":
|
||||||
return getInnerRingNodes(ctx)
|
return getInnerRingNodes(ctx, innerRingKey)
|
||||||
|
case "InnerRingCandidates":
|
||||||
|
return getInnerRingNodes(ctx, candidatesKey)
|
||||||
case "InnerRingCandidateRemove":
|
case "InnerRingCandidateRemove":
|
||||||
data := args[0].([]byte) // public key
|
if len(args) != 1 {
|
||||||
if !runtime.CheckWitness(data) {
|
panic("irCandidateRemove: bad arguments")
|
||||||
panic("you should be the owner of the public key")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
return true
|
||||||
case "InnerRingCandidateAdd":
|
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) {
|
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)
|
c := node{pub: key}
|
||||||
if containsPub(candidates, key) {
|
candidates := getInnerRingNodes(ctx, candidatesKey)
|
||||||
panic("is already in list")
|
|
||||||
|
list, ok := addNode(candidates, c)
|
||||||
|
if !ok {
|
||||||
|
panic("irCandidateAdd: candidate already in the list")
|
||||||
}
|
}
|
||||||
|
|
||||||
from := pubToScriptHash(key)
|
from := contract.CreateStandardAccount(key)
|
||||||
to := runtime.GetExecutingScriptHash()
|
to := runtime.GetExecutingScriptHash()
|
||||||
params := []interface{}{from, to, innerRingCandidateFee}
|
params := []interface{}{from, to, innerRingCandidateFee}
|
||||||
|
|
||||||
transferred := engine.AppCall([]byte(tokenHash), "transfer", params).(bool)
|
transferred := engine.AppCall([]byte(tokenHash), "transfer", params).(bool)
|
||||||
if !transferred {
|
if !transferred {
|
||||||
panic("failed to transfer funds, aborting")
|
panic("irCandidateAdd: failed to transfer funds, aborting")
|
||||||
}
|
}
|
||||||
|
|
||||||
candidate := node{pub: key}
|
runtime.Log("irCandidateAdd: candidate has been added")
|
||||||
if !putSerialized(ctx, "InnerRingCandidates", candidate) {
|
setSerialized(ctx, candidatesKey, list)
|
||||||
panic("failed to put candidate into the queue")
|
|
||||||
}
|
|
||||||
|
|
||||||
return true
|
return true
|
||||||
case "Deposit":
|
case "Deposit":
|
||||||
|
@ -193,7 +216,7 @@ func Main(op string, args []interface{}) interface{} {
|
||||||
amount := args[2].(int) // amount of GAS
|
amount := args[2].(int) // amount of GAS
|
||||||
lockAcc := args[3].([]byte) // lock account from internal balance contract
|
lockAcc := args[3].([]byte) // lock account from internal balance contract
|
||||||
|
|
||||||
irList := getInnerRingNodes(ctx)
|
irList := getInnerRingNodes(ctx, innerRingKey)
|
||||||
threshold := len(irList)/3*2 + 1
|
threshold := len(irList)/3*2 + 1
|
||||||
|
|
||||||
cashedCheques := getCashedCheques(ctx)
|
cashedCheques := getCashedCheques(ctx)
|
||||||
|
@ -335,7 +358,7 @@ func Main(op string, args []interface{}) interface{} {
|
||||||
panic("isInnerRing: incorrect public key")
|
panic("isInnerRing: incorrect public key")
|
||||||
}
|
}
|
||||||
|
|
||||||
irList := getInnerRingNodes(ctx)
|
irList := getInnerRingNodes(ctx, innerRingKey)
|
||||||
for i := range irList {
|
for i := range irList {
|
||||||
node := irList[i]
|
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.
|
// getInnerRingNodes returns deserialized slice of inner ring nodes from storage.
|
||||||
func getInnerRingNodes(ctx storage.Context) []node {
|
func getInnerRingNodes(ctx storage.Context, key string) []node {
|
||||||
data := storage.Get(ctx, innerRingKey)
|
data := storage.Get(ctx, key)
|
||||||
if data != nil {
|
if data != nil {
|
||||||
return binary.Deserialize(data.([]byte)).([]node)
|
return binary.Deserialize(data.([]byte)).([]node)
|
||||||
}
|
}
|
||||||
|
@ -587,6 +610,19 @@ func addCheque(lst []cheque, c cheque) ([]cheque, bool) {
|
||||||
return lst, true
|
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,
|
// 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 {
|
||||||
|
|
Loading…
Reference in a new issue