2020-10-27 12:14:06 +00:00
|
|
|
package alphabetcontract
|
|
|
|
|
|
|
|
import (
|
2020-12-04 14:54:40 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/binary"
|
2020-10-27 12:14:06 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/contract"
|
2020-12-04 14:54:40 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/crypto"
|
2020-10-27 12:14:06 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/runtime"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/storage"
|
2020-12-04 14:54:40 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/util"
|
2020-10-27 12:14:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
irNode struct {
|
|
|
|
key []byte
|
|
|
|
}
|
2020-12-04 14:54:40 +00:00
|
|
|
|
|
|
|
ballot struct {
|
2020-12-07 12:53:11 +00:00
|
|
|
id []byte // hash of validators list
|
|
|
|
n [][]byte // already voted inner ring nodes
|
|
|
|
height int // height is an neofs epoch when ballot was registered
|
2020-12-04 14:54:40 +00:00
|
|
|
}
|
2020-10-27 12:14:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// native gas token script hash
|
|
|
|
gasHash = "\xbc\xaf\x41\xd6\x84\xc7\xd4\xad\x6e\xe0\xd9\x9d\xa9\x70\x7b\x9d\x1f\x0c\x8e\x66"
|
|
|
|
|
|
|
|
// native neo token script hash
|
|
|
|
neoHash = "\x25\x05\x9e\xcb\x48\x78\xd3\xa8\x75\xf9\x1c\x51\xce\xde\xd3\x30\xd4\x57\x5f\xde"
|
|
|
|
|
|
|
|
name = "Dobro"
|
|
|
|
index = 4
|
|
|
|
|
|
|
|
netmapContractKey = "netmapScriptHash"
|
2020-12-04 14:54:40 +00:00
|
|
|
|
2020-12-07 12:53:11 +00:00
|
|
|
threshold = totalAlphabetContracts * 2 / 3 + 1
|
2020-12-04 14:54:40 +00:00
|
|
|
voteKey = "ballots"
|
|
|
|
|
|
|
|
totalAlphabetContracts = 7
|
2020-10-27 12:14:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ctx storage.Context
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
if runtime.GetTrigger() != runtime.Application {
|
|
|
|
panic("contract has not been called in application node")
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx = storage.GetContext()
|
|
|
|
}
|
|
|
|
|
|
|
|
func Init(addrNetmap []byte) {
|
|
|
|
if storage.Get(ctx, netmapContractKey) != nil {
|
|
|
|
panic("contract already deployed")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(addrNetmap) != 20 {
|
|
|
|
panic("incorrect length of contract script hash")
|
|
|
|
}
|
|
|
|
|
|
|
|
storage.Put(ctx, netmapContractKey, addrNetmap)
|
|
|
|
|
2020-12-04 14:54:40 +00:00
|
|
|
setSerialized(ctx, voteKey, []ballot{})
|
|
|
|
|
2020-10-27 12:14:06 +00:00
|
|
|
runtime.Log(name + " contract initialized")
|
|
|
|
}
|
|
|
|
|
|
|
|
func Gas() int {
|
|
|
|
contractHash := runtime.GetExecutingScriptHash()
|
|
|
|
return balance(gasHash, contractHash)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Neo() int {
|
|
|
|
contractHash := runtime.GetExecutingScriptHash()
|
|
|
|
return balance(neoHash, contractHash)
|
|
|
|
}
|
|
|
|
|
|
|
|
func balance(hash string, addr []byte) int {
|
2020-12-10 14:45:45 +00:00
|
|
|
balance := contract.Call([]byte(hash), "balanceOf", addr)
|
2020-10-27 12:14:06 +00:00
|
|
|
return balance.(int)
|
|
|
|
}
|
|
|
|
|
|
|
|
func irList() []irNode {
|
|
|
|
netmapContractAddr := storage.Get(ctx, netmapContractKey).([]byte)
|
2020-12-10 14:45:45 +00:00
|
|
|
return contract.Call(netmapContractAddr, "innerRingList").([]irNode)
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2020-12-07 12:53:11 +00:00
|
|
|
func currentEpoch() int {
|
|
|
|
netmapContractAddr := storage.Get(ctx, netmapContractKey).([]byte)
|
2020-12-10 14:45:45 +00:00
|
|
|
return contract.Call(netmapContractAddr, "epoch").(int)
|
2020-12-07 12:53:11 +00:00
|
|
|
}
|
|
|
|
|
2020-10-27 12:14:06 +00:00
|
|
|
func checkPermission(ir []irNode) bool {
|
|
|
|
if len(ir) <= index {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
node := ir[index]
|
|
|
|
return runtime.CheckWitness(node.key)
|
|
|
|
}
|
|
|
|
|
2020-12-04 14:54:40 +00:00
|
|
|
func innerRingInvoker(ir []irNode) []byte {
|
|
|
|
for i := 0; i < len(ir); i++ {
|
|
|
|
if i >= totalAlphabetContracts {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
node := ir[i]
|
|
|
|
if runtime.CheckWitness(node.key) {
|
|
|
|
return node.key
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-10-27 12:14:06 +00:00
|
|
|
func Emit() bool {
|
|
|
|
innerRingKeys := irList()
|
|
|
|
if !checkPermission(innerRingKeys) {
|
|
|
|
panic("invalid invoker")
|
|
|
|
}
|
|
|
|
|
|
|
|
contractHash := runtime.GetExecutingScriptHash()
|
|
|
|
neo := balance(neoHash, contractHash)
|
|
|
|
|
2020-12-10 15:45:01 +00:00
|
|
|
_ = contract.Call([]byte(neoHash), "transfer", contractHash, contractHash, neo, nil)
|
2020-10-27 12:14:06 +00:00
|
|
|
|
|
|
|
gas := balance(gasHash, contractHash)
|
|
|
|
gasPerNode := gas * 7 / 8 / len(innerRingKeys)
|
|
|
|
|
|
|
|
if gasPerNode == 0 {
|
|
|
|
runtime.Log("no gas to emit")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range innerRingKeys {
|
|
|
|
node := innerRingKeys[i]
|
|
|
|
address := contract.CreateStandardAccount(node.key)
|
|
|
|
|
2020-12-10 15:45:01 +00:00
|
|
|
_ = contract.Call([]byte(gasHash), "transfer", contractHash, address, gasPerNode, nil)
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
runtime.Log("utility token has been emitted to inner ring nodes")
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-12-04 14:54:40 +00:00
|
|
|
func Vote(epoch int, candidates [][]byte) {
|
2020-11-12 13:52:14 +00:00
|
|
|
innerRingKeys := irList()
|
2020-12-04 14:54:40 +00:00
|
|
|
|
|
|
|
key := innerRingInvoker(innerRingKeys)
|
|
|
|
if len(key) == 0 {
|
2020-11-12 13:52:14 +00:00
|
|
|
panic("invalid invoker")
|
|
|
|
}
|
|
|
|
|
2020-12-07 12:53:11 +00:00
|
|
|
curEpoch := currentEpoch()
|
|
|
|
if epoch != curEpoch {
|
|
|
|
panic("invalid epoch")
|
|
|
|
}
|
|
|
|
|
2020-12-04 14:54:40 +00:00
|
|
|
id := voteID(epoch, candidates)
|
2020-12-07 12:53:11 +00:00
|
|
|
n := vote(ctx, curEpoch, id, key)
|
2020-11-12 13:52:14 +00:00
|
|
|
|
2020-12-04 14:54:40 +00:00
|
|
|
if n >= threshold {
|
|
|
|
candidate := candidates[index%len(candidates)]
|
|
|
|
address := runtime.GetExecutingScriptHash()
|
|
|
|
|
2020-12-10 14:45:45 +00:00
|
|
|
ok := contract.Call([]byte(neoHash), "vote", address, candidate).(bool)
|
2020-12-04 14:54:40 +00:00
|
|
|
if ok {
|
|
|
|
runtime.Log(name + ": successfully voted for validator")
|
|
|
|
removeVotes(ctx, id)
|
|
|
|
} else {
|
|
|
|
runtime.Log(name + ": vote has been failed")
|
|
|
|
}
|
2020-11-12 14:20:49 +00:00
|
|
|
} else {
|
2020-12-04 14:54:40 +00:00
|
|
|
runtime.Log(name + ": saved vote for validator")
|
2020-11-12 14:20:49 +00:00
|
|
|
}
|
2020-11-12 13:52:14 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-10-27 12:14:06 +00:00
|
|
|
func Name() string {
|
|
|
|
return name
|
|
|
|
}
|
2020-12-04 14:54:40 +00:00
|
|
|
|
2020-12-07 12:53:11 +00:00
|
|
|
func vote(ctx storage.Context, epoch int, id, from []byte) int {
|
2020-12-04 14:54:40 +00:00
|
|
|
var (
|
|
|
|
newCandidates []ballot
|
|
|
|
candidates = getBallots(ctx)
|
|
|
|
found = -1
|
|
|
|
)
|
|
|
|
|
|
|
|
for i := 0; i < len(candidates); i++ {
|
|
|
|
cnd := candidates[i]
|
|
|
|
if bytesEqual(cnd.id, id) {
|
|
|
|
voters := cnd.n
|
|
|
|
|
|
|
|
for j := range voters {
|
|
|
|
if bytesEqual(voters[j], from) {
|
|
|
|
return len(voters)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
voters = append(voters, from)
|
2020-12-07 12:53:11 +00:00
|
|
|
cnd = ballot{id: id, n: voters, height: epoch}
|
2020-12-04 14:54:40 +00:00
|
|
|
found = len(voters)
|
|
|
|
}
|
|
|
|
|
2020-12-07 12:53:11 +00:00
|
|
|
// add only valid ballots with current epochs
|
|
|
|
if cnd.height == epoch {
|
2020-12-04 14:54:40 +00:00
|
|
|
newCandidates = append(newCandidates, cnd)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if found < 0 {
|
|
|
|
voters := [][]byte{from}
|
|
|
|
newCandidates = append(newCandidates, ballot{
|
2020-12-07 12:53:11 +00:00
|
|
|
id: id,
|
|
|
|
n: voters,
|
|
|
|
height: epoch})
|
2020-12-04 14:54:40 +00:00
|
|
|
found = 1
|
|
|
|
}
|
|
|
|
|
|
|
|
setSerialized(ctx, voteKey, newCandidates)
|
|
|
|
|
|
|
|
return found
|
|
|
|
}
|
|
|
|
|
|
|
|
func removeVotes(ctx storage.Context, id []byte) {
|
|
|
|
var (
|
|
|
|
newCandidates []ballot
|
|
|
|
candidates = getBallots(ctx)
|
|
|
|
)
|
|
|
|
|
|
|
|
for i := 0; i < len(candidates); i++ {
|
|
|
|
cnd := candidates[i]
|
|
|
|
if !bytesEqual(cnd.id, id) {
|
|
|
|
newCandidates = append(newCandidates, cnd)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setSerialized(ctx, voteKey, newCandidates)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getBallots(ctx storage.Context) []ballot {
|
|
|
|
data := storage.Get(ctx, voteKey)
|
|
|
|
if data != nil {
|
|
|
|
return binary.Deserialize(data.([]byte)).([]ballot)
|
|
|
|
}
|
|
|
|
|
|
|
|
return []ballot{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func setSerialized(ctx storage.Context, key interface{}, value interface{}) {
|
|
|
|
data := binary.Serialize(value)
|
|
|
|
storage.Put(ctx, key, data)
|
|
|
|
}
|
|
|
|
|
|
|
|
// neo-go#1176
|
|
|
|
func bytesEqual(a []byte, b []byte) bool {
|
|
|
|
return util.Equals(string(a), string(b))
|
|
|
|
}
|
|
|
|
|
|
|
|
func voteID(epoch interface{}, args [][]byte) []byte {
|
|
|
|
var (
|
2020-12-07 12:53:11 +00:00
|
|
|
result []byte
|
2020-12-04 14:54:40 +00:00
|
|
|
epochBytes = epoch.([]byte)
|
|
|
|
)
|
|
|
|
|
|
|
|
result = append(result, epochBytes...)
|
|
|
|
|
|
|
|
for i := range args {
|
|
|
|
result = append(result, args[i]...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return crypto.SHA256(result)
|
|
|
|
}
|