2020-03-30 13:59:54 +00:00
|
|
|
package smart_contract
|
|
|
|
|
2020-07-20 13:09:40 +00:00
|
|
|
/*
|
|
|
|
NeoFS Smart Contract for NEO3.0.
|
|
|
|
|
|
|
|
Utility operations, executed once in deploy stage:
|
|
|
|
- Init(pubKey, ... ) - setup initial inner ring nodes
|
|
|
|
- InitConfig(key, value, key, value...) - setup initial NeoFS configuration
|
|
|
|
|
|
|
|
User operations:
|
|
|
|
- Deposit(script-hash, amount, script-hash(?)) - deposit gas assets to this script-hash address to NeoFS balance
|
|
|
|
- Withdraw(script-hash, amount) - initialize gas asset withdraw from NeoFS balance
|
|
|
|
- Bind(script-hash, pubKeys...) - bind public key with user's account to use it in NeoFS requests
|
|
|
|
- Unbind(script-hash, pubKeys...) - unbind public key from user's account
|
|
|
|
|
|
|
|
Inner ring list operations:
|
|
|
|
- InnerRingList() - returns array of inner ring node keys
|
|
|
|
- InnerRingCandidates() - returns array of inner ring candidate node keys
|
|
|
|
- IsInnerRing(pubKey) - returns 'true' if key is inside of inner ring list
|
|
|
|
- InnerRingCandidateAdd(pubKey) - adds key to the list of inner ring candidates
|
|
|
|
- InnerRingCandidateRemove(pubKey) - removes key from the list of inner ring candidates
|
|
|
|
- InnerRingUpdate(id, pubKeys...) - updates list of inner ring nodes with provided list of public keys
|
|
|
|
|
|
|
|
Config operations:
|
|
|
|
- Config(key) - returns value of NeoFS configuration with key 'key'
|
|
|
|
- ListConfig() - returns array of all key-value pairs of NeoFS configuration
|
|
|
|
- SetConfig(id, key, value) - set key-value pair as a NeoFS runtime configuration value
|
|
|
|
|
|
|
|
Other utility operations:
|
|
|
|
- Version - returns contract version
|
|
|
|
- Cheque(id, script- hash, amount, script-hash) - sends gas assets back to the user if they were successfully
|
|
|
|
locked in NeoFS balance contract
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
- (?) - parameter can be omitted
|
|
|
|
- pubKey - 33 bytes of public key
|
|
|
|
- id - unique byte sequence
|
|
|
|
*/
|
|
|
|
|
2020-03-30 13:59:54 +00:00
|
|
|
import (
|
2020-07-09 13:38:53 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/binary"
|
2020-06-15 15:54:24 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/blockchain"
|
2020-07-16 12:06:49 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/contract"
|
2020-03-30 13:59:54 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/crypto"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/engine"
|
2020-07-16 15:59:49 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/iterator"
|
2020-03-30 13:59:54 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/runtime"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/storage"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/util"
|
|
|
|
)
|
|
|
|
|
2020-05-27 12:23:44 +00:00
|
|
|
type (
|
|
|
|
ballot struct {
|
2020-06-15 15:54:24 +00:00
|
|
|
id []byte // id of the voting decision
|
|
|
|
n [][]byte // already voted inner ring nodes
|
|
|
|
block int // block with the last vote
|
2020-05-27 12:23:44 +00:00
|
|
|
}
|
2020-03-30 13:59:54 +00:00
|
|
|
|
2020-05-27 12:23:44 +00:00
|
|
|
node struct {
|
|
|
|
pub []byte
|
|
|
|
}
|
2020-03-30 13:59:54 +00:00
|
|
|
|
2020-07-15 16:17:34 +00:00
|
|
|
cheque struct {
|
2020-05-28 14:04:32 +00:00
|
|
|
id []byte
|
2020-05-27 12:23:44 +00:00
|
|
|
}
|
2020-07-16 15:59:49 +00:00
|
|
|
|
|
|
|
record struct {
|
|
|
|
key []byte
|
|
|
|
val []byte
|
|
|
|
}
|
2020-05-27 12:23:44 +00:00
|
|
|
)
|
2020-03-30 13:59:54 +00:00
|
|
|
|
2020-05-27 12:23:44 +00:00
|
|
|
const (
|
2020-07-20 13:09:40 +00:00
|
|
|
// native gas token script hash
|
|
|
|
tokenHash = "\x3b\x7d\x37\x11\xc6\xf0\xcc\xf9\xb1\xdc\xa9\x03\xd1\xbf\xa1\xd8\x96\xf1\x23\x8c"
|
|
|
|
|
2020-07-16 15:59:49 +00:00
|
|
|
defaultCandidateFee = 100 * 1_0000_0000 // 100 Fixed8 Gas
|
|
|
|
candidateFeeConfigKey = "InnerRingCandidateFee"
|
2020-07-20 13:09:40 +00:00
|
|
|
|
|
|
|
version = 2
|
|
|
|
|
|
|
|
innerRingKey = "innerring"
|
|
|
|
voteKey = "ballots"
|
|
|
|
candidatesKey = "candidates"
|
|
|
|
cashedChequesKey = "cheques"
|
|
|
|
|
|
|
|
blockDiff = 20 // change base on performance evaluation
|
|
|
|
publicKeySize = 33
|
|
|
|
minInnerRingSize = 3
|
2020-05-27 12:23:44 +00:00
|
|
|
)
|
2020-03-30 13:59:54 +00:00
|
|
|
|
2020-07-16 15:59:49 +00:00
|
|
|
var (
|
|
|
|
configPrefix = []byte("config")
|
|
|
|
)
|
|
|
|
|
2020-03-30 13:59:54 +00:00
|
|
|
func Main(op string, args []interface{}) interface{} {
|
|
|
|
// The trigger determines whether this smart-contract is being
|
|
|
|
// run in 'verification' or 'application' mode.
|
|
|
|
if runtime.GetTrigger() != runtime.Application() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := storage.GetContext()
|
2020-07-15 16:26:24 +00:00
|
|
|
|
2020-03-30 13:59:54 +00:00
|
|
|
switch op {
|
2020-07-15 16:26:24 +00:00
|
|
|
case "Init":
|
|
|
|
if storage.Get(ctx, innerRingKey) != nil {
|
|
|
|
panic("neofs: contract already deployed")
|
2020-03-30 13:59:54 +00:00
|
|
|
}
|
|
|
|
|
2020-07-15 16:26:24 +00:00
|
|
|
var irList []node
|
|
|
|
|
2020-05-26 14:48:56 +00:00
|
|
|
for i := 0; i < len(args); i++ {
|
|
|
|
pub := args[i].([]byte)
|
|
|
|
irList = append(irList, node{pub: pub})
|
2020-03-30 13:59:54 +00:00
|
|
|
}
|
|
|
|
|
2020-07-15 16:26:24 +00:00
|
|
|
// initialize all storage slices
|
|
|
|
setSerialized(ctx, innerRingKey, irList)
|
|
|
|
setSerialized(ctx, voteKey, []ballot{})
|
|
|
|
setSerialized(ctx, candidatesKey, []node{})
|
|
|
|
setSerialized(ctx, cashedChequesKey, []cheque{})
|
2020-03-30 13:59:54 +00:00
|
|
|
|
2020-07-15 16:26:24 +00:00
|
|
|
runtime.Log("neofs: contract initialized")
|
2020-05-27 12:23:44 +00:00
|
|
|
|
2020-03-30 13:59:54 +00:00
|
|
|
return true
|
|
|
|
case "InnerRingList":
|
2020-07-16 12:06:49 +00:00
|
|
|
return getInnerRingNodes(ctx, innerRingKey)
|
|
|
|
case "InnerRingCandidates":
|
|
|
|
return getInnerRingNodes(ctx, candidatesKey)
|
2020-03-30 13:59:54 +00:00
|
|
|
case "InnerRingCandidateRemove":
|
2020-07-16 12:06:49 +00:00
|
|
|
if len(args) != 1 {
|
|
|
|
panic("irCandidateRemove: bad arguments")
|
|
|
|
}
|
|
|
|
|
|
|
|
key := args[0].([]byte) // inner ring candidate public key
|
|
|
|
if !runtime.CheckWitness(key) {
|
|
|
|
panic("irCandidateRemove: you should be the owner of the public key")
|
2020-03-30 13:59:54 +00:00
|
|
|
}
|
|
|
|
|
2020-07-16 14:23:52 +00:00
|
|
|
nodes := []node{} // it is explicit declaration of empty slice, not nil
|
2020-07-16 12:06:49 +00:00
|
|
|
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)
|
2020-03-30 13:59:54 +00:00
|
|
|
|
|
|
|
return true
|
|
|
|
case "InnerRingCandidateAdd":
|
2020-07-16 12:06:49 +00:00
|
|
|
if len(args) != 1 {
|
|
|
|
panic("irCandidateAdd: bad arguments")
|
|
|
|
}
|
2020-03-30 13:59:54 +00:00
|
|
|
|
2020-07-16 12:06:49 +00:00
|
|
|
key := args[0].([]byte) // inner ring candidate public key
|
2020-05-26 14:48:56 +00:00
|
|
|
if !runtime.CheckWitness(key) {
|
2020-07-16 12:06:49 +00:00
|
|
|
panic("irCandidateAdd: you should be the owner of the public key")
|
2020-03-30 13:59:54 +00:00
|
|
|
}
|
|
|
|
|
2020-07-16 12:06:49 +00:00
|
|
|
c := node{pub: key}
|
|
|
|
candidates := getInnerRingNodes(ctx, candidatesKey)
|
|
|
|
|
|
|
|
list, ok := addNode(candidates, c)
|
|
|
|
if !ok {
|
|
|
|
panic("irCandidateAdd: candidate already in the list")
|
2020-03-30 13:59:54 +00:00
|
|
|
}
|
|
|
|
|
2020-07-16 12:06:49 +00:00
|
|
|
from := contract.CreateStandardAccount(key)
|
2020-07-09 13:38:53 +00:00
|
|
|
to := runtime.GetExecutingScriptHash()
|
2020-07-16 15:59:49 +00:00
|
|
|
fee := getConfig(ctx, candidateFeeConfigKey).(int)
|
|
|
|
params := []interface{}{from, to, fee}
|
2020-03-30 13:59:54 +00:00
|
|
|
|
|
|
|
transferred := engine.AppCall([]byte(tokenHash), "transfer", params).(bool)
|
|
|
|
if !transferred {
|
2020-07-16 12:06:49 +00:00
|
|
|
panic("irCandidateAdd: failed to transfer funds, aborting")
|
2020-03-30 13:59:54 +00:00
|
|
|
}
|
|
|
|
|
2020-07-16 12:06:49 +00:00
|
|
|
runtime.Log("irCandidateAdd: candidate has been added")
|
|
|
|
setSerialized(ctx, candidatesKey, list)
|
2020-03-30 13:59:54 +00:00
|
|
|
|
|
|
|
return true
|
|
|
|
case "Deposit":
|
2020-07-15 08:51:49 +00:00
|
|
|
if len(args) < 2 || len(args) > 3 {
|
|
|
|
panic("deposit: bad arguments")
|
|
|
|
}
|
|
|
|
|
|
|
|
from := args[0].([]byte)
|
|
|
|
if !runtime.CheckWitness(from) {
|
|
|
|
panic("deposit: you should be the owner of the wallet")
|
2020-03-30 13:59:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
amount := args[1].(int)
|
|
|
|
if amount > 0 {
|
|
|
|
amount = amount * 100000000
|
|
|
|
}
|
|
|
|
|
2020-07-09 13:38:53 +00:00
|
|
|
to := runtime.GetExecutingScriptHash()
|
2020-03-30 13:59:54 +00:00
|
|
|
params := []interface{}{from, to, amount}
|
2020-07-15 08:51:49 +00:00
|
|
|
|
2020-03-30 13:59:54 +00:00
|
|
|
transferred := engine.AppCall([]byte(tokenHash), "transfer", params).(bool)
|
|
|
|
if !transferred {
|
2020-07-15 08:51:49 +00:00
|
|
|
panic("deposit: failed to transfer funds, aborting")
|
2020-03-30 13:59:54 +00:00
|
|
|
}
|
|
|
|
|
2020-07-15 08:51:49 +00:00
|
|
|
runtime.Log("deposit: funds have been transferred")
|
2020-03-30 13:59:54 +00:00
|
|
|
|
2020-07-15 08:51:49 +00:00
|
|
|
var rcv = from
|
2020-05-28 14:04:32 +00:00
|
|
|
if len(args) == 3 {
|
|
|
|
rcv = args[2].([]byte) // todo: check if rcv value is valid
|
|
|
|
}
|
|
|
|
|
2020-07-15 08:51:49 +00:00
|
|
|
tx := runtime.GetScriptContainer()
|
|
|
|
runtime.Notify("Deposit", from, amount, rcv, tx.Hash)
|
2020-05-28 14:04:32 +00:00
|
|
|
|
2020-03-30 13:59:54 +00:00
|
|
|
return true
|
|
|
|
case "Withdraw":
|
2020-05-28 14:04:32 +00:00
|
|
|
if len(args) != 2 {
|
|
|
|
panic("withdraw: bad arguments")
|
|
|
|
}
|
2020-03-30 13:59:54 +00:00
|
|
|
|
2020-05-28 14:04:32 +00:00
|
|
|
user := args[0].([]byte)
|
|
|
|
if !runtime.CheckWitness(user) {
|
2020-07-15 09:05:03 +00:00
|
|
|
panic("withdraw: you should be the owner of the wallet")
|
2020-03-30 13:59:54 +00:00
|
|
|
}
|
2020-05-28 14:04:32 +00:00
|
|
|
|
|
|
|
amount := args[1].(int)
|
|
|
|
if amount > 0 {
|
|
|
|
amount = amount * 100000000
|
2020-03-30 13:59:54 +00:00
|
|
|
}
|
|
|
|
|
2020-07-15 09:05:03 +00:00
|
|
|
tx := runtime.GetScriptContainer()
|
|
|
|
runtime.Notify("Withdraw", user, amount, tx.Hash)
|
2020-05-28 14:04:32 +00:00
|
|
|
|
|
|
|
return true
|
|
|
|
case "Cheque":
|
|
|
|
if len(args) != 4 {
|
|
|
|
panic("cheque: bad arguments")
|
|
|
|
}
|
|
|
|
|
|
|
|
id := args[0].([]byte) // unique cheque id
|
|
|
|
user := args[1].([]byte) // GAS receiver
|
|
|
|
amount := args[2].(int) // amount of GAS
|
2020-07-15 16:31:34 +00:00
|
|
|
lockAcc := args[3].([]byte) // lock account from internal balance contract
|
2020-05-28 14:04:32 +00:00
|
|
|
|
2020-07-16 12:06:49 +00:00
|
|
|
irList := getInnerRingNodes(ctx, innerRingKey)
|
2020-07-15 16:31:34 +00:00
|
|
|
threshold := len(irList)/3*2 + 1
|
2020-05-28 14:04:32 +00:00
|
|
|
|
2020-07-15 16:31:34 +00:00
|
|
|
cashedCheques := getCashedCheques(ctx)
|
2020-07-09 13:38:53 +00:00
|
|
|
hashID := crypto.SHA256(id)
|
2020-05-28 14:04:32 +00:00
|
|
|
|
2020-06-15 15:54:24 +00:00
|
|
|
irKey := innerRingInvoker(irList)
|
|
|
|
if len(irKey) == 0 {
|
2020-05-28 14:04:32 +00:00
|
|
|
panic("cheque: invoked by non inner ring node")
|
2020-03-30 13:59:54 +00:00
|
|
|
}
|
|
|
|
|
2020-07-15 16:31:34 +00:00
|
|
|
c := cheque{id: id}
|
|
|
|
|
|
|
|
list, ok := addCheque(cashedCheques, c)
|
|
|
|
if !ok {
|
2020-05-28 14:04:32 +00:00
|
|
|
panic("cheque: non unique id")
|
2020-03-30 13:59:54 +00:00
|
|
|
}
|
|
|
|
|
2020-06-15 15:54:24 +00:00
|
|
|
n := vote(ctx, hashID, irKey)
|
2020-05-28 14:04:32 +00:00
|
|
|
if n >= threshold {
|
|
|
|
removeVotes(ctx, hashID)
|
|
|
|
|
2020-07-09 13:38:53 +00:00
|
|
|
from := runtime.GetExecutingScriptHash()
|
2020-05-28 14:04:32 +00:00
|
|
|
params := []interface{}{from, user, amount}
|
|
|
|
|
|
|
|
transferred := engine.AppCall([]byte(tokenHash), "transfer", params).(bool)
|
|
|
|
if !transferred {
|
|
|
|
panic("cheque: failed to transfer funds, aborting")
|
|
|
|
}
|
|
|
|
|
2020-07-15 16:31:34 +00:00
|
|
|
runtime.Log("cheque: funds have been transferred")
|
|
|
|
|
|
|
|
setSerialized(ctx, cashedChequesKey, list)
|
2020-05-28 14:04:32 +00:00
|
|
|
runtime.Notify("Cheque", id, user, amount, lockAcc)
|
|
|
|
}
|
2020-03-30 13:59:54 +00:00
|
|
|
|
2020-07-16 08:02:19 +00:00
|
|
|
return true
|
|
|
|
case "Bind", "Unbind":
|
|
|
|
if len(args) < 2 {
|
|
|
|
panic("binding: bad arguments")
|
|
|
|
}
|
|
|
|
|
|
|
|
user := args[0].([]byte)
|
|
|
|
if !runtime.CheckWitness(user) {
|
|
|
|
panic("binding: you should be the owner of the wallet")
|
|
|
|
}
|
|
|
|
|
|
|
|
var keys [][]byte
|
2020-07-20 13:09:40 +00:00
|
|
|
|
2020-07-16 08:02:19 +00:00
|
|
|
for i := 1; i < len(args); i++ {
|
|
|
|
pub := args[i].([]byte)
|
|
|
|
if len(pub) != publicKeySize {
|
|
|
|
panic("binding: incorrect public key size")
|
|
|
|
}
|
|
|
|
|
|
|
|
keys = append(keys, pub)
|
|
|
|
}
|
|
|
|
|
|
|
|
runtime.Notify(op, user, keys)
|
|
|
|
|
2020-03-30 13:59:54 +00:00
|
|
|
return true
|
|
|
|
case "InnerRingUpdate":
|
2020-07-16 14:23:52 +00:00
|
|
|
if len(args) < 1+minInnerRingSize {
|
|
|
|
// cheque id + inner ring public keys
|
|
|
|
panic("irUpdate: bad arguments")
|
|
|
|
}
|
2020-03-30 13:59:54 +00:00
|
|
|
|
2020-07-16 14:23:52 +00:00
|
|
|
irList := getInnerRingNodes(ctx, innerRingKey)
|
2020-05-27 12:23:44 +00:00
|
|
|
threshold := len(irList)/3*2 + 1
|
|
|
|
|
2020-06-15 15:54:24 +00:00
|
|
|
irKey := innerRingInvoker(irList)
|
|
|
|
if len(irKey) == 0 {
|
2020-05-27 12:23:44 +00:00
|
|
|
panic("innerRingUpdate: invoked by non inner ring node")
|
2020-03-30 13:59:54 +00:00
|
|
|
}
|
2020-05-27 12:23:44 +00:00
|
|
|
|
2020-07-16 14:23:52 +00:00
|
|
|
id := args[0].([]byte)
|
2020-07-15 16:17:34 +00:00
|
|
|
c := cheque{id: id}
|
2020-07-16 14:23:52 +00:00
|
|
|
|
|
|
|
cashedCheques := getCashedCheques(ctx)
|
|
|
|
|
|
|
|
chequesList, ok := addCheque(cashedCheques, c)
|
|
|
|
if !ok {
|
|
|
|
panic("irUpdate: non unique id")
|
2020-03-30 13:59:54 +00:00
|
|
|
}
|
|
|
|
|
2020-07-16 14:23:52 +00:00
|
|
|
oldNodes := 0
|
|
|
|
candidates := getInnerRingNodes(ctx, candidatesKey)
|
|
|
|
newIR := []node{}
|
2020-05-27 12:23:44 +00:00
|
|
|
|
2020-07-16 14:23:52 +00:00
|
|
|
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++
|
2020-03-30 13:59:54 +00:00
|
|
|
|
2020-07-16 14:23:52 +00:00
|
|
|
continue loop
|
2020-03-30 13:59:54 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-20 09:38:11 +00:00
|
|
|
|
2020-07-16 14:23:52 +00:00
|
|
|
// find key in candidates list
|
|
|
|
candidates, newIR, ok = rmNodeByKey(candidates, newIR, key)
|
|
|
|
if !ok {
|
|
|
|
panic("irUpdate: unknown public key in inner ring list")
|
2020-05-27 12:23:44 +00:00
|
|
|
}
|
2020-07-16 14:23:52 +00:00
|
|
|
}
|
2020-03-30 13:59:54 +00:00
|
|
|
|
2020-07-16 14:23:52 +00:00
|
|
|
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, hashID)
|
2020-03-30 13:59:54 +00:00
|
|
|
|
2020-07-16 14:23:52 +00:00
|
|
|
setSerialized(ctx, candidatesKey, candidates)
|
|
|
|
setSerialized(ctx, innerRingKey, newIR)
|
|
|
|
setSerialized(ctx, cashedChequesKey, chequesList)
|
2020-05-27 12:23:44 +00:00
|
|
|
|
2020-07-16 14:23:52 +00:00
|
|
|
runtime.Notify("InnerRingUpdate", c.id, newIR)
|
|
|
|
runtime.Log("irUpdate: inner ring list has been updated")
|
2020-05-27 12:23:44 +00:00
|
|
|
}
|
2020-03-30 13:59:54 +00:00
|
|
|
|
|
|
|
return true
|
2020-05-22 08:06:12 +00:00
|
|
|
case "IsInnerRing":
|
|
|
|
if len(args) != 1 {
|
|
|
|
panic("isInnerRing: wrong arguments")
|
|
|
|
}
|
|
|
|
|
|
|
|
key := args[0].([]byte)
|
2020-07-20 13:09:40 +00:00
|
|
|
if len(key) != publicKeySize {
|
2020-05-22 08:06:12 +00:00
|
|
|
panic("isInnerRing: incorrect public key")
|
|
|
|
}
|
|
|
|
|
2020-07-16 12:06:49 +00:00
|
|
|
irList := getInnerRingNodes(ctx, innerRingKey)
|
2020-05-22 08:06:12 +00:00
|
|
|
for i := range irList {
|
|
|
|
node := irList[i]
|
|
|
|
|
2020-07-15 16:26:24 +00:00
|
|
|
if bytesEqual(node.pub, key) {
|
2020-05-22 08:06:12 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
2020-07-16 15:59:49 +00:00
|
|
|
case "Config":
|
|
|
|
if len(args) != 1 {
|
|
|
|
panic("config: bad arguments")
|
|
|
|
}
|
|
|
|
|
|
|
|
key := args[0].([]byte)
|
|
|
|
|
|
|
|
return getConfig(ctx, key)
|
|
|
|
case "SetConfig":
|
|
|
|
if len(args) != 3 {
|
|
|
|
panic("setConfig: bad arguments")
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if it is inner ring invocation
|
|
|
|
irList := getInnerRingNodes(ctx, innerRingKey)
|
|
|
|
threshold := len(irList)/3*2 + 1
|
|
|
|
|
|
|
|
irKey := innerRingInvoker(irList)
|
|
|
|
if len(irKey) == 0 {
|
|
|
|
panic("setConfig: invoked by non inner ring node")
|
|
|
|
}
|
|
|
|
|
|
|
|
// check unique id of the operation
|
|
|
|
id := args[0].([]byte)
|
|
|
|
c := cheque{id: id}
|
|
|
|
cashedCheques := getCashedCheques(ctx)
|
|
|
|
|
|
|
|
chequesList, ok := addCheque(cashedCheques, c)
|
|
|
|
if !ok {
|
|
|
|
panic("setConfig: non unique id")
|
|
|
|
}
|
|
|
|
|
|
|
|
// vote for new configuration value
|
|
|
|
hashID := crypto.SHA256(id)
|
|
|
|
|
|
|
|
n := vote(ctx, hashID, irKey)
|
|
|
|
if n >= threshold {
|
|
|
|
removeVotes(ctx, hashID)
|
|
|
|
|
|
|
|
key := args[1]
|
|
|
|
val := args[2]
|
|
|
|
|
|
|
|
setConfig(ctx, key, val)
|
|
|
|
setSerialized(ctx, cashedChequesKey, chequesList)
|
|
|
|
|
|
|
|
runtime.Notify("SetConfig", id, key, val)
|
|
|
|
runtime.Log("setConfig: configuration has been updated")
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
case "ListConfig":
|
|
|
|
var config []record
|
|
|
|
|
|
|
|
it := storage.Find(ctx, configPrefix)
|
|
|
|
for iterator.Next(it) {
|
|
|
|
key := iterator.Key(it).([]byte)
|
|
|
|
val := iterator.Value(it).([]byte)
|
|
|
|
r := record{key: key[len(configPrefix):], val: val}
|
|
|
|
|
|
|
|
config = append(config, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
return config
|
|
|
|
case "InitConfig":
|
|
|
|
if getConfig(ctx, candidateFeeConfigKey) != nil {
|
|
|
|
panic("neofs: configuration already installed")
|
|
|
|
}
|
|
|
|
|
|
|
|
ln := len(args)
|
|
|
|
if ln%2 != 0 {
|
|
|
|
panic("initConfig: bad arguments")
|
|
|
|
}
|
|
|
|
|
|
|
|
setConfig(ctx, candidateFeeConfigKey, defaultCandidateFee)
|
|
|
|
|
|
|
|
for i := 0; i < ln/2; i++ {
|
|
|
|
key := args[i*2]
|
|
|
|
val := args[i*2+1]
|
|
|
|
|
|
|
|
setConfig(ctx, key, val)
|
|
|
|
}
|
|
|
|
|
|
|
|
runtime.Log("neofs: config has been installed")
|
|
|
|
|
|
|
|
return true
|
2020-03-30 13:59:54 +00:00
|
|
|
case "Version":
|
|
|
|
return version
|
|
|
|
}
|
|
|
|
|
|
|
|
panic("unknown operation")
|
|
|
|
}
|
|
|
|
|
2020-06-17 09:54:41 +00:00
|
|
|
// innerRingInvoker returns public key of inner ring node that invoked contract.
|
2020-06-15 15:54:24 +00:00
|
|
|
func innerRingInvoker(ir []node) []byte {
|
|
|
|
for i := 0; i < len(ir); i++ {
|
|
|
|
node := ir[i]
|
|
|
|
if runtime.CheckWitness(node.pub) {
|
|
|
|
return node.pub
|
2020-05-27 12:23:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-15 15:54:24 +00:00
|
|
|
return nil
|
2020-05-27 12:23:44 +00:00
|
|
|
}
|
|
|
|
|
2020-07-20 13:09:40 +00:00
|
|
|
// vote adds ballot for the decision with specific 'id' and returns amount
|
|
|
|
// on unique voters for that decision.
|
2020-06-15 15:54:24 +00:00
|
|
|
func vote(ctx storage.Context, id, from []byte) int {
|
2020-05-27 12:23:44 +00:00
|
|
|
var (
|
2020-07-20 13:09:40 +00:00
|
|
|
newCandidates = []ballot{} // it is explicit declaration of empty slice, not nil
|
2020-07-15 16:21:51 +00:00
|
|
|
candidates = getBallots(ctx)
|
2020-05-27 12:23:44 +00:00
|
|
|
found = -1
|
2020-06-15 15:54:24 +00:00
|
|
|
blockHeight = blockchain.GetHeight()
|
2020-05-27 12:23:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
for i := 0; i < len(candidates); i++ {
|
|
|
|
cnd := candidates[i]
|
2020-07-15 16:21:51 +00:00
|
|
|
if bytesEqual(cnd.id, id) {
|
2020-06-15 15:54:24 +00:00
|
|
|
voters := cnd.n
|
|
|
|
|
|
|
|
for j := range voters {
|
2020-07-15 16:21:51 +00:00
|
|
|
if bytesEqual(voters[j], from) {
|
2020-06-15 15:54:24 +00:00
|
|
|
return len(voters)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
voters = append(voters, from)
|
|
|
|
cnd = ballot{id: id, n: voters, block: blockHeight}
|
|
|
|
found = len(voters)
|
|
|
|
}
|
|
|
|
|
|
|
|
// do not add old ballots, they are invalid
|
|
|
|
if blockHeight-cnd.block <= blockDiff {
|
|
|
|
newCandidates = append(newCandidates, cnd)
|
2020-05-27 12:23:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if found < 0 {
|
2020-07-15 16:21:51 +00:00
|
|
|
found = 1
|
2020-06-15 15:54:24 +00:00
|
|
|
voters := [][]byte{from}
|
2020-07-15 16:21:51 +00:00
|
|
|
|
2020-06-15 15:54:24 +00:00
|
|
|
newCandidates = append(newCandidates, ballot{
|
|
|
|
id: id,
|
|
|
|
n: voters,
|
|
|
|
block: blockHeight})
|
2020-05-27 12:23:44 +00:00
|
|
|
}
|
|
|
|
|
2020-07-15 16:21:51 +00:00
|
|
|
setSerialized(ctx, voteKey, newCandidates)
|
2020-05-27 12:23:44 +00:00
|
|
|
|
|
|
|
return found
|
|
|
|
}
|
|
|
|
|
2020-07-20 13:09:40 +00:00
|
|
|
// removeVotes clears ballots of the decision that has benn aceepted by
|
|
|
|
// inner ring nodes.
|
2020-05-27 12:23:44 +00:00
|
|
|
func removeVotes(ctx storage.Context, id []byte) {
|
|
|
|
var (
|
2020-07-20 13:09:40 +00:00
|
|
|
newCandidates = []ballot{} // it is explicit declaration of empty slice, not nil
|
2020-07-15 16:21:51 +00:00
|
|
|
candidates = getBallots(ctx)
|
2020-05-27 12:23:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
for i := 0; i < len(candidates); i++ {
|
|
|
|
cnd := candidates[i]
|
2020-07-15 16:21:51 +00:00
|
|
|
if !bytesEqual(cnd.id, id) {
|
2020-05-27 12:23:44 +00:00
|
|
|
newCandidates = append(newCandidates, cnd)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-15 16:21:51 +00:00
|
|
|
setSerialized(ctx, voteKey, newCandidates)
|
|
|
|
}
|
|
|
|
|
|
|
|
// setSerialized serializes data and puts it into contract storage.
|
|
|
|
func setSerialized(ctx storage.Context, key interface{}, value interface{}) {
|
|
|
|
data := binary.Serialize(value)
|
|
|
|
storage.Put(ctx, key, data)
|
|
|
|
}
|
|
|
|
|
2020-07-15 16:26:24 +00:00
|
|
|
// getInnerRingNodes returns deserialized slice of inner ring nodes from storage.
|
2020-07-16 12:06:49 +00:00
|
|
|
func getInnerRingNodes(ctx storage.Context, key string) []node {
|
|
|
|
data := storage.Get(ctx, key)
|
2020-07-15 16:26:24 +00:00
|
|
|
if data != nil {
|
|
|
|
return binary.Deserialize(data.([]byte)).([]node)
|
|
|
|
}
|
|
|
|
|
|
|
|
return []node{}
|
|
|
|
}
|
|
|
|
|
2020-07-15 16:31:34 +00:00
|
|
|
// getInnerRingNodes returns deserialized slice of used cheques.
|
|
|
|
func getCashedCheques(ctx storage.Context) []cheque {
|
|
|
|
data := storage.Get(ctx, cashedChequesKey)
|
|
|
|
if data != nil {
|
|
|
|
return binary.Deserialize(data.([]byte)).([]cheque)
|
|
|
|
}
|
|
|
|
|
|
|
|
return []cheque{}
|
|
|
|
}
|
|
|
|
|
2020-07-15 16:21:51 +00:00
|
|
|
// getInnerRingNodes returns deserialized slice of vote ballots.
|
|
|
|
func getBallots(ctx storage.Context) []ballot {
|
|
|
|
data := storage.Get(ctx, voteKey)
|
|
|
|
if data != nil {
|
|
|
|
return binary.Deserialize(data.([]byte)).([]ballot)
|
|
|
|
}
|
|
|
|
|
|
|
|
return []ballot{}
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:59:49 +00:00
|
|
|
// getConfig returns installed neofs configuration value or nil if it is not set.
|
|
|
|
func getConfig(ctx storage.Context, key interface{}) interface{} {
|
|
|
|
postfix := key.([]byte)
|
|
|
|
storageKey := append(configPrefix, postfix...)
|
|
|
|
|
|
|
|
return storage.Get(ctx, storageKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
// setConfig sets neofs configuration value in the contract storage.
|
|
|
|
func setConfig(ctx storage.Context, key, val interface{}) {
|
|
|
|
postfix := key.([]byte)
|
|
|
|
storageKey := append(configPrefix, postfix...)
|
|
|
|
|
|
|
|
storage.Put(ctx, storageKey, val)
|
|
|
|
}
|
|
|
|
|
2020-07-15 16:31:34 +00:00
|
|
|
// addCheque returns slice of cheques with appended cheque 'c' and bool flag
|
|
|
|
// that set to false if cheque 'c' is already presented in the slice 'lst'.
|
|
|
|
func addCheque(lst []cheque, c cheque) ([]cheque, bool) {
|
|
|
|
for i := 0; i < len(lst); i++ {
|
|
|
|
if bytesEqual(c.id, lst[i].id) {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lst = append(lst, c)
|
2020-07-20 13:09:40 +00:00
|
|
|
|
2020-07-15 16:31:34 +00:00
|
|
|
return lst, true
|
|
|
|
}
|
|
|
|
|
2020-07-16 12:06:49 +00:00
|
|
|
// 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)
|
2020-07-20 13:09:40 +00:00
|
|
|
|
2020-07-16 12:06:49 +00:00
|
|
|
return lst, true
|
|
|
|
}
|
|
|
|
|
2020-07-16 14:23:52 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2020-07-15 16:21:51 +00:00
|
|
|
// 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 {
|
|
|
|
return util.Equals(string(a), string(b))
|
2020-05-27 12:23:44 +00:00
|
|
|
}
|