2020-10-27 12:14:06 +00:00
|
|
|
package alphabetcontract
|
|
|
|
|
|
|
|
import (
|
2020-12-10 10:41:16 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop"
|
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"
|
2021-02-02 16:39:16 +00:00
|
|
|
"github.com/nspcc-dev/neofs-contract/common"
|
2020-10-27 12:14:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// native gas token script hash
|
2020-12-28 14:45:33 +00:00
|
|
|
gasHash = "\xfb\xed\xfe\x2e\xd2\x22\x65\x92\xb6\x48\xc4\xda\x97\xb9\xc9\xcd\x5d\xc1\xa6\xa6"
|
2020-10-27 12:14:06 +00:00
|
|
|
|
|
|
|
// native neo token script hash
|
2020-12-28 14:45:33 +00:00
|
|
|
neoHash = "\xb6\x72\x0f\xef\x7e\x7e\xb7\x3f\x25\xaf\xb4\x70\xf5\x87\x99\x7c\xe3\xe2\x46\x0a"
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2020-12-15 12:19:13 +00:00
|
|
|
netmapKey = "netmapScriptHash"
|
|
|
|
indexKey = "index"
|
|
|
|
totalKey = "threshold"
|
|
|
|
nameKey = "name"
|
2020-12-04 14:54:40 +00:00
|
|
|
voteKey = "ballots"
|
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()
|
|
|
|
}
|
|
|
|
|
2021-02-08 15:14:03 +00:00
|
|
|
// OnNEP17Payment is a callback for NEP-17 compatible native GAS and NEO contracts.
|
|
|
|
func OnNEP17Payment(from interop.Hash160, amount int, data interface{}) {
|
2020-12-10 10:41:16 +00:00
|
|
|
caller := runtime.GetCallingScriptHash()
|
2021-02-02 17:36:20 +00:00
|
|
|
if !common.BytesEqual(caller, []byte(gasHash)) && !common.BytesEqual(caller, []byte(neoHash)) {
|
2021-02-08 15:14:03 +00:00
|
|
|
panic("onNEP17Payment: alphabet contract accepts GAS and NEO only")
|
2020-12-10 10:41:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-15 12:19:13 +00:00
|
|
|
func Init(addrNetmap []byte, name string, index, total int) {
|
|
|
|
if storage.Get(ctx, netmapKey) != nil {
|
2020-10-27 12:14:06 +00:00
|
|
|
panic("contract already deployed")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(addrNetmap) != 20 {
|
|
|
|
panic("incorrect length of contract script hash")
|
|
|
|
}
|
|
|
|
|
2020-12-15 12:19:13 +00:00
|
|
|
storage.Put(ctx, netmapKey, addrNetmap)
|
|
|
|
storage.Put(ctx, nameKey, name)
|
|
|
|
storage.Put(ctx, indexKey, index)
|
|
|
|
storage.Put(ctx, totalKey, total)
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2021-02-02 17:36:20 +00:00
|
|
|
common.SetSerialized(ctx, voteKey, []common.Ballot{})
|
2020-12-04 14:54:40 +00:00
|
|
|
|
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 {
|
2021-02-08 15:32:38 +00:00
|
|
|
balance := contract.Call([]byte(hash), "balanceOf", contract.ReadOnly, addr)
|
2020-10-27 12:14:06 +00:00
|
|
|
return balance.(int)
|
|
|
|
}
|
|
|
|
|
2021-02-02 18:26:34 +00:00
|
|
|
func irList() []common.IRNode {
|
2021-02-02 19:20:31 +00:00
|
|
|
return common.InnerRingListViaStorage(ctx, netmapKey)
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2020-12-07 12:53:11 +00:00
|
|
|
func currentEpoch() int {
|
2020-12-15 12:19:13 +00:00
|
|
|
netmapContractAddr := storage.Get(ctx, netmapKey).([]byte)
|
2021-02-08 15:32:38 +00:00
|
|
|
return contract.Call(netmapContractAddr, "epoch", contract.ReadOnly).(int)
|
2020-12-07 12:53:11 +00:00
|
|
|
}
|
|
|
|
|
2020-12-15 12:19:13 +00:00
|
|
|
func name() string {
|
|
|
|
return storage.Get(ctx, nameKey).(string)
|
|
|
|
}
|
|
|
|
|
|
|
|
func index() int {
|
|
|
|
return storage.Get(ctx, indexKey).(int)
|
|
|
|
}
|
|
|
|
|
|
|
|
func total() int {
|
|
|
|
return storage.Get(ctx, totalKey).(int)
|
|
|
|
}
|
|
|
|
|
2021-02-02 18:26:34 +00:00
|
|
|
func checkPermission(ir []common.IRNode) bool {
|
2020-12-15 12:19:13 +00:00
|
|
|
index := index() // read from contract memory
|
|
|
|
|
2020-10-27 12:14:06 +00:00
|
|
|
if len(ir) <= index {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
node := ir[index]
|
2021-02-02 18:26:34 +00:00
|
|
|
return runtime.CheckWitness(node.PublicKey)
|
2020-12-04 14:54:40 +00:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
|
2021-02-08 15:32:38 +00:00
|
|
|
_ = contract.Call([]byte(neoHash), "transfer", contract.All, 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]
|
2021-02-02 18:26:34 +00:00
|
|
|
address := contract.CreateStandardAccount(node.PublicKey)
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2021-02-08 15:32:38 +00:00
|
|
|
_ = contract.Call([]byte(gasHash), "transfer", contract.All, 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-15 12:19:13 +00:00
|
|
|
threshold := total()/3*2 + 1
|
|
|
|
index := index()
|
|
|
|
name := name()
|
2020-12-04 14:54:40 +00:00
|
|
|
|
2021-02-02 18:26:34 +00:00
|
|
|
key := common.InnerRingInvoker(innerRingKeys)
|
2020-12-04 14:54:40 +00:00
|
|
|
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()
|
|
|
|
|
2021-02-08 15:32:38 +00:00
|
|
|
ok := contract.Call([]byte(neoHash), "vote", contract.All, 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 {
|
2020-12-15 12:19:13 +00:00
|
|
|
return name()
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
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 (
|
2021-02-02 16:39:16 +00:00
|
|
|
newCandidates []common.Ballot
|
2020-12-04 14:54:40 +00:00
|
|
|
candidates = getBallots(ctx)
|
|
|
|
found = -1
|
|
|
|
)
|
|
|
|
|
|
|
|
for i := 0; i < len(candidates); i++ {
|
|
|
|
cnd := candidates[i]
|
2021-02-02 17:36:20 +00:00
|
|
|
if common.BytesEqual(cnd.ID, id) {
|
2021-02-02 16:39:16 +00:00
|
|
|
voters := cnd.Voters
|
2020-12-04 14:54:40 +00:00
|
|
|
|
|
|
|
for j := range voters {
|
2021-02-02 17:36:20 +00:00
|
|
|
if common.BytesEqual(voters[j], from) {
|
2020-12-04 14:54:40 +00:00
|
|
|
return len(voters)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
voters = append(voters, from)
|
2021-02-02 16:39:16 +00:00
|
|
|
cnd = common.Ballot{ID: id, Voters: 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
|
2021-02-02 16:39:16 +00:00
|
|
|
if cnd.Height == epoch {
|
2020-12-04 14:54:40 +00:00
|
|
|
newCandidates = append(newCandidates, cnd)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if found < 0 {
|
|
|
|
voters := [][]byte{from}
|
2021-02-02 16:39:16 +00:00
|
|
|
newCandidates = append(newCandidates, common.Ballot{
|
|
|
|
ID: id,
|
|
|
|
Voters: voters,
|
|
|
|
Height: epoch})
|
2020-12-04 14:54:40 +00:00
|
|
|
found = 1
|
|
|
|
}
|
|
|
|
|
2021-02-02 17:36:20 +00:00
|
|
|
common.SetSerialized(ctx, voteKey, newCandidates)
|
2020-12-04 14:54:40 +00:00
|
|
|
|
|
|
|
return found
|
|
|
|
}
|
|
|
|
|
|
|
|
func removeVotes(ctx storage.Context, id []byte) {
|
|
|
|
var (
|
2021-02-02 16:39:16 +00:00
|
|
|
newCandidates []common.Ballot
|
2020-12-04 14:54:40 +00:00
|
|
|
candidates = getBallots(ctx)
|
|
|
|
)
|
|
|
|
|
|
|
|
for i := 0; i < len(candidates); i++ {
|
|
|
|
cnd := candidates[i]
|
2021-02-02 17:36:20 +00:00
|
|
|
if !common.BytesEqual(cnd.ID, id) {
|
2020-12-04 14:54:40 +00:00
|
|
|
newCandidates = append(newCandidates, cnd)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-02 17:36:20 +00:00
|
|
|
common.SetSerialized(ctx, voteKey, newCandidates)
|
2020-12-04 14:54:40 +00:00
|
|
|
}
|
|
|
|
|
2021-02-02 16:39:16 +00:00
|
|
|
func getBallots(ctx storage.Context) []common.Ballot {
|
2020-12-04 14:54:40 +00:00
|
|
|
data := storage.Get(ctx, voteKey)
|
|
|
|
if data != nil {
|
2021-02-02 16:39:16 +00:00
|
|
|
return binary.Deserialize(data.([]byte)).([]common.Ballot)
|
2020-12-04 14:54:40 +00:00
|
|
|
}
|
|
|
|
|
2021-02-02 16:39:16 +00:00
|
|
|
return []common.Ballot{}
|
2020-12-04 14:54:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func voteID(epoch interface{}, args [][]byte) []byte {
|
|
|
|
var (
|
2020-12-10 10:41:16 +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)
|
|
|
|
}
|