2020-03-30 13:59:54 +00:00
|
|
|
package smart_contract
|
|
|
|
|
|
|
|
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-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"
|
|
|
|
"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-05-27 12:23:44 +00:00
|
|
|
check struct {
|
2020-05-28 14:04:32 +00:00
|
|
|
id []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 (
|
|
|
|
// GAS NEP-5 HASH
|
|
|
|
tokenHash = "\x77\xea\x59\x6b\x7a\xdf\x7e\x4d\xd1\x40\x76\x97\x31\xb7\xd2\xf0\xe0\x6b\xcd\x9b"
|
|
|
|
innerRingCandidateFee = 100 * 1000 * 1000 // 10^8
|
|
|
|
version = 1
|
|
|
|
voteKey = "ballots"
|
2020-06-15 15:54:24 +00:00
|
|
|
blockDiff = 20 // change base on performance evaluation
|
2020-05-27 12:23:44 +00:00
|
|
|
)
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2020-05-22 08:06:12 +00:00
|
|
|
Utility operations - they will be changed in production:
|
|
|
|
- Deploy(params: address, pubKey, ... ) - setup initial inner ring state
|
|
|
|
|
|
|
|
User operations:
|
|
|
|
- InnerRingList() - get list of inner ring nodes addresses and public keys
|
|
|
|
- InnerRingCandidateRemove(params: pubKey) - remove node with given public key from the inner ring candidate queue
|
2020-05-26 14:48:56 +00:00
|
|
|
- InnerRingCandidateAdd(params: pubKey) - add node to the inner ring candidate queue
|
2020-05-22 08:06:12 +00:00
|
|
|
- Deposit(params: pubKey, amount) - deposit GAS to the NeoFS account
|
|
|
|
- Withdraw(params: withdrawCheque) - withdraw GAS from the NeoFS account
|
|
|
|
- InnerRingUpdate(params: irCheque) - change list of inner ring nodes
|
|
|
|
- IsInnerRing(params: pubKey) - returns true if pubKey presented in inner ring list
|
|
|
|
- Version() - get version of the NeoFS smart-contract
|
|
|
|
|
|
|
|
Params:
|
|
|
|
- address - string of the valid multiaddress (github.com/multiformats/multiaddr)
|
|
|
|
- pubKey - 33 byte public key
|
|
|
|
- withdrawCheque - serialized structure, that confirms GAS transfer;
|
|
|
|
contains inner ring signatures
|
|
|
|
- irCheque - serialized structure, that confirms new inner ring node list;
|
|
|
|
contains inner ring signatures
|
2020-03-30 13:59:54 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
ctx := storage.GetContext()
|
|
|
|
switch op {
|
|
|
|
case "Deploy":
|
|
|
|
irList := getSerialized(ctx, "InnerRingList").([]node)
|
|
|
|
if len(irList) >= 3 {
|
|
|
|
panic("contract already deployed")
|
|
|
|
}
|
|
|
|
|
|
|
|
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-09 13:38:53 +00:00
|
|
|
data := binary.Serialize(irList)
|
2020-03-30 13:59:54 +00:00
|
|
|
storage.Put(ctx, "InnerRingList", data)
|
|
|
|
|
2020-07-09 13:38:53 +00:00
|
|
|
data = binary.Serialize([]interface{}{})
|
2020-03-30 13:59:54 +00:00
|
|
|
storage.Put(ctx, "UsedVerifCheckList", data)
|
|
|
|
storage.Put(ctx, "InnerRingCandidates", data)
|
|
|
|
|
2020-07-09 13:38:53 +00:00
|
|
|
data = binary.Serialize([]ballot{})
|
2020-05-27 12:23:44 +00:00
|
|
|
storage.Put(ctx, voteKey, data)
|
|
|
|
|
2020-03-30 13:59:54 +00:00
|
|
|
return true
|
|
|
|
case "InnerRingList":
|
|
|
|
irList := getSerialized(ctx, "InnerRingList").([]node)
|
|
|
|
|
|
|
|
return irList
|
|
|
|
case "InnerRingCandidateRemove":
|
|
|
|
data := args[0].([]byte) // public key
|
|
|
|
if !runtime.CheckWitness(data) {
|
|
|
|
panic("you should be the owner of the public key")
|
|
|
|
}
|
|
|
|
|
|
|
|
delSerializedIR(ctx, "InnerRingCandidates", data)
|
|
|
|
|
|
|
|
return true
|
|
|
|
case "InnerRingCandidateAdd":
|
2020-05-26 14:48:56 +00:00
|
|
|
key := args[0].([]byte) // public key
|
2020-03-30 13:59:54 +00:00
|
|
|
|
2020-05-26 14:48:56 +00:00
|
|
|
if !runtime.CheckWitness(key) {
|
2020-03-30 13:59:54 +00:00
|
|
|
panic("you should be the owner of the public key")
|
|
|
|
}
|
|
|
|
|
|
|
|
candidates := getSerialized(ctx, "InnerRingCandidates").([]node)
|
2020-05-26 14:48:56 +00:00
|
|
|
if containsPub(candidates, key) {
|
2020-03-30 13:59:54 +00:00
|
|
|
panic("is already in list")
|
|
|
|
}
|
|
|
|
|
2020-05-26 14:48:56 +00:00
|
|
|
from := pubToScriptHash(key)
|
2020-07-09 13:38:53 +00:00
|
|
|
to := runtime.GetExecutingScriptHash()
|
2020-03-30 13:59:54 +00:00
|
|
|
params := []interface{}{from, to, innerRingCandidateFee}
|
|
|
|
|
|
|
|
transferred := engine.AppCall([]byte(tokenHash), "transfer", params).(bool)
|
|
|
|
if !transferred {
|
|
|
|
panic("failed to transfer funds, aborting")
|
|
|
|
}
|
|
|
|
|
2020-05-26 14:48:56 +00:00
|
|
|
candidate := node{pub: key}
|
2020-03-30 13:59:54 +00:00
|
|
|
if !putSerialized(ctx, "InnerRingCandidates", candidate) {
|
|
|
|
panic("failed to put candidate into the queue")
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
case "Deposit":
|
|
|
|
pk := args[0].([]byte)
|
|
|
|
if !runtime.CheckWitness(pk) {
|
|
|
|
panic("you should be the owner of the public key")
|
|
|
|
}
|
|
|
|
|
|
|
|
amount := args[1].(int)
|
|
|
|
if amount > 0 {
|
|
|
|
amount = amount * 100000000
|
|
|
|
}
|
|
|
|
|
|
|
|
from := pubToScriptHash(pk)
|
2020-07-09 13:38:53 +00:00
|
|
|
to := runtime.GetExecutingScriptHash()
|
2020-03-30 13:59:54 +00:00
|
|
|
params := []interface{}{from, to, amount}
|
|
|
|
transferred := engine.AppCall([]byte(tokenHash), "transfer", params).(bool)
|
|
|
|
if !transferred {
|
|
|
|
panic("failed to transfer funds, aborting")
|
|
|
|
}
|
|
|
|
|
|
|
|
runtime.Log("funds have been transferred")
|
|
|
|
|
2020-05-28 14:04:32 +00:00
|
|
|
var rcv = []byte{}
|
|
|
|
if len(args) == 3 {
|
|
|
|
rcv = args[2].([]byte) // todo: check if rcv value is valid
|
|
|
|
}
|
|
|
|
|
2020-07-09 13:38:53 +00:00
|
|
|
txHash := runtime.GetScriptContainer().Hash
|
2020-05-29 08:44:15 +00:00
|
|
|
|
|
|
|
runtime.Notify("Deposit", pk, amount, rcv, txHash)
|
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) {
|
|
|
|
// todo: consider something different with neoID
|
|
|
|
panic("withdraw: you should be the owner of the account")
|
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-09 13:38:53 +00:00
|
|
|
txHash := runtime.GetScriptContainer().Hash
|
2020-05-29 08:44:15 +00:00
|
|
|
|
|
|
|
runtime.Notify("Withdraw", user, amount, txHash)
|
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
|
|
|
|
lockAcc := args[3].([]byte) // lock account from internal banking that must be cashed out
|
|
|
|
|
|
|
|
ctx := storage.GetContext()
|
|
|
|
|
2020-07-09 13:38:53 +00:00
|
|
|
hashID := crypto.SHA256(id)
|
2020-03-30 13:59:54 +00:00
|
|
|
irList := getSerialized(ctx, "InnerRingList").([]node)
|
2020-05-28 14:04:32 +00:00
|
|
|
usedList := getSerialized(ctx, "UsedVerifCheckList").([]check)
|
|
|
|
threshold := len(irList)/3*2 + 1
|
|
|
|
|
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-05-28 14:04:32 +00:00
|
|
|
c := check{id: id} // todo: use different cheque id for inner ring update and withdraw
|
|
|
|
if containsCheck(usedList, c) {
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
|
|
|
putSerialized(ctx, "UsedVerifCheckList", c)
|
|
|
|
runtime.Notify("Cheque", id, user, amount, lockAcc)
|
|
|
|
}
|
2020-03-30 13:59:54 +00:00
|
|
|
|
|
|
|
return true
|
|
|
|
case "InnerRingUpdate":
|
|
|
|
data := args[0].([]byte)
|
|
|
|
|
|
|
|
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").([]check)
|
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
|
|
|
|
|
|
|
c := check{id: id}
|
2020-03-30 13:59:54 +00:00
|
|
|
if containsCheck(usedList, c) {
|
2020-05-27 12:23:44 +00:00
|
|
|
panic("innerRingUpdate: cheque has non unique id")
|
2020-03-30 13:59:54 +00:00
|
|
|
}
|
|
|
|
|
2020-07-09 13:38:53 +00:00
|
|
|
chequeHash := crypto.SHA256(data)
|
2020-05-27 12:23:44 +00:00
|
|
|
|
2020-06-15 15:54:24 +00:00
|
|
|
n := vote(ctx, chequeHash, irKey)
|
2020-05-27 12:23:44 +00:00
|
|
|
if n >= threshold {
|
|
|
|
removeVotes(ctx, chequeHash)
|
|
|
|
|
|
|
|
candidates := getSerialized(ctx, "InnerRingCandidates").([]node)
|
|
|
|
offset = 10
|
|
|
|
newIR := []node{}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2020-03-30 13:59:54 +00:00
|
|
|
}
|
|
|
|
|
2020-05-27 12:23:44 +00:00
|
|
|
for j := 0; j < len(candidates); j++ {
|
|
|
|
n := candidates[j]
|
|
|
|
if util.Equals(n.pub, pub) {
|
|
|
|
newIR = append(newIR, n)
|
|
|
|
continue loop
|
|
|
|
}
|
2020-03-30 13:59:54 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-20 09:38:11 +00:00
|
|
|
|
2020-05-27 12:23:44 +00:00
|
|
|
if len(newIR) != listItemCount {
|
|
|
|
panic("new inner ring wasn't processed correctly")
|
|
|
|
}
|
2020-03-30 13:59:54 +00:00
|
|
|
|
2020-05-27 12:23:44 +00:00
|
|
|
for i := 0; i < len(newIR); i++ {
|
|
|
|
n := newIR[i]
|
|
|
|
delSerializedIR(ctx, "InnerRingCandidates", n.pub)
|
|
|
|
}
|
2020-03-30 13:59:54 +00:00
|
|
|
|
2020-07-09 13:38:53 +00:00
|
|
|
newIRData := binary.Serialize(newIR)
|
2020-05-27 12:23:44 +00:00
|
|
|
storage.Put(ctx, "InnerRingList", newIRData)
|
|
|
|
putSerialized(ctx, "UsedVerifCheckList", c)
|
|
|
|
|
|
|
|
runtime.Notify("InnerRingUpdate", c.id, newIRData)
|
|
|
|
}
|
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)
|
|
|
|
if len(key) != 33 {
|
|
|
|
panic("isInnerRing: incorrect public key")
|
|
|
|
}
|
|
|
|
|
|
|
|
irList := getSerialized(ctx, "InnerRingList").([]node)
|
|
|
|
for i := range irList {
|
|
|
|
node := irList[i]
|
|
|
|
|
|
|
|
if util.Equals(node.pub, key) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
2020-03-30 13:59:54 +00:00
|
|
|
case "Version":
|
|
|
|
return version
|
|
|
|
}
|
|
|
|
|
|
|
|
panic("unknown operation")
|
|
|
|
}
|
|
|
|
|
2020-07-09 13:38:53 +00:00
|
|
|
// fixme: use strict type deserialization wrappers
|
2020-03-30 13:59:54 +00:00
|
|
|
func getSerialized(ctx storage.Context, key string) interface{} {
|
|
|
|
data := storage.Get(ctx, key).([]byte)
|
|
|
|
if len(data) != 0 {
|
2020-07-09 13:38:53 +00:00
|
|
|
return binary.Deserialize(data)
|
2020-03-30 13:59:54 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func delSerialized(ctx storage.Context, key string, value []byte) bool {
|
|
|
|
data := storage.Get(ctx, key).([]byte)
|
|
|
|
deleted := false
|
|
|
|
|
|
|
|
var newList [][]byte
|
|
|
|
if len(data) != 0 {
|
2020-07-09 13:38:53 +00:00
|
|
|
lst := binary.Deserialize(data).([][]byte)
|
2020-03-30 13:59:54 +00:00
|
|
|
for i := 0; i < len(lst); i++ {
|
|
|
|
if util.Equals(value, lst[i]) {
|
|
|
|
deleted = true
|
|
|
|
} else {
|
|
|
|
newList = append(newList, lst[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if deleted {
|
|
|
|
if len(newList) != 0 {
|
2020-07-09 13:38:53 +00:00
|
|
|
data := binary.Serialize(newList)
|
2020-03-30 13:59:54 +00:00
|
|
|
storage.Put(ctx, key, data)
|
|
|
|
} else {
|
|
|
|
storage.Delete(ctx, key)
|
|
|
|
}
|
|
|
|
runtime.Log("target element has been removed")
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
runtime.Log("target element has not been removed")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func putSerialized(ctx storage.Context, key string, value interface{}) bool {
|
|
|
|
data := storage.Get(ctx, key).([]byte)
|
|
|
|
|
|
|
|
var lst []interface{}
|
|
|
|
if len(data) != 0 {
|
2020-07-09 13:38:53 +00:00
|
|
|
lst = binary.Deserialize(data).([]interface{})
|
2020-03-30 13:59:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
lst = append(lst, value)
|
2020-07-09 13:38:53 +00:00
|
|
|
data = binary.Serialize(lst)
|
2020-03-30 13:59:54 +00:00
|
|
|
storage.Put(ctx, key, data)
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func pubToScriptHash(pkey []byte) []byte {
|
2020-07-09 13:38:53 +00:00
|
|
|
// pre := []byte{0x21}
|
|
|
|
// buf := append(pre, pkey...)
|
|
|
|
// buf = append(buf, 0xac)
|
|
|
|
// h := crypto.Hash160(buf)
|
|
|
|
//
|
|
|
|
// return h
|
|
|
|
|
|
|
|
// fixme: someday ripemd syscall will appear
|
|
|
|
// or simply store script-hashes along with public key
|
|
|
|
return []byte{0x0F, 0xED}
|
2020-03-30 13:59:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func containsCheck(lst []check, c check) bool {
|
|
|
|
for i := 0; i < len(lst); i++ {
|
|
|
|
if util.Equals(c, lst[i]) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
func containsPub(lst []node, elem []byte) bool {
|
|
|
|
for i := 0; i < len(lst); i++ {
|
|
|
|
e := lst[i]
|
|
|
|
if util.Equals(elem, e.pub) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func delSerializedIR(ctx storage.Context, key string, value []byte) bool {
|
|
|
|
data := storage.Get(ctx, key).([]byte)
|
|
|
|
deleted := false
|
|
|
|
|
|
|
|
newList := []node{}
|
|
|
|
if len(data) != 0 {
|
2020-07-09 13:38:53 +00:00
|
|
|
lst := binary.Deserialize(data).([]node)
|
2020-03-30 13:59:54 +00:00
|
|
|
for i := 0; i < len(lst); i++ {
|
|
|
|
n := lst[i]
|
|
|
|
if util.Equals(value, n.pub) {
|
|
|
|
deleted = true
|
|
|
|
} else {
|
|
|
|
newList = append(newList, n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if deleted {
|
2020-07-09 13:38:53 +00:00
|
|
|
data := binary.Serialize(newList)
|
2020-03-30 13:59:54 +00:00
|
|
|
storage.Put(ctx, key, data)
|
|
|
|
runtime.Log("target element has been removed")
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
runtime.Log("target element has not been removed")
|
|
|
|
return false
|
|
|
|
}
|
2020-05-27 12:23:44 +00:00
|
|
|
|
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-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-06-15 15:54:24 +00:00
|
|
|
newCandidates []ballot
|
2020-05-27 12:23:44 +00:00
|
|
|
candidates = getSerialized(ctx, voteKey).([]ballot)
|
|
|
|
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]
|
|
|
|
if util.Equals(cnd.id, id) {
|
2020-06-15 15:54:24 +00:00
|
|
|
voters := cnd.n
|
|
|
|
|
|
|
|
for j := range voters {
|
|
|
|
if util.Equals(voters[j], from) {
|
|
|
|
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-06-15 15:54:24 +00:00
|
|
|
voters := [][]byte{from}
|
|
|
|
newCandidates = append(newCandidates, ballot{
|
|
|
|
id: id,
|
|
|
|
n: voters,
|
|
|
|
block: blockHeight})
|
2020-05-27 12:23:44 +00:00
|
|
|
found = 1
|
|
|
|
}
|
|
|
|
|
2020-07-09 13:38:53 +00:00
|
|
|
data := binary.Serialize(newCandidates)
|
2020-05-27 12:23:44 +00:00
|
|
|
storage.Put(ctx, voteKey, data)
|
|
|
|
|
|
|
|
return found
|
|
|
|
}
|
|
|
|
|
|
|
|
func removeVotes(ctx storage.Context, id []byte) {
|
|
|
|
var (
|
|
|
|
newCandidates = []ballot{}
|
|
|
|
candidates = getSerialized(ctx, voteKey).([]ballot)
|
|
|
|
)
|
|
|
|
|
|
|
|
for i := 0; i < len(candidates); i++ {
|
|
|
|
cnd := candidates[i]
|
|
|
|
if !util.Equals(cnd.id, id) {
|
|
|
|
newCandidates = append(newCandidates, cnd)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-09 13:38:53 +00:00
|
|
|
data := binary.Serialize(newCandidates)
|
2020-05-27 12:23:44 +00:00
|
|
|
storage.Put(ctx, voteKey, data)
|
|
|
|
}
|