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
|
2021-02-08 15:45:31 +00:00
|
|
|
gasHash = "\x28\xb3\xad\xab\x72\x69\xf9\xc2\x18\x1d\xb3\xcb\x74\x1e\xbf\x55\x19\x30\xe2\x70"
|
2020-10-27 12:14:06 +00:00
|
|
|
|
|
|
|
// native neo token script hash
|
2021-02-08 15:45:31 +00:00
|
|
|
neoHash = "\x83\xab\x06\x79\xad\x55\xc0\x50\xa1\x3a\xd4\x3f\x59\x36\xea\x73\xf5\xeb\x1e\xf6"
|
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
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
if runtime.GetTrigger() != runtime.Application {
|
|
|
|
panic("contract has not been called in application node")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
2021-02-09 10:20:55 +00:00
|
|
|
ctx := storage.GetContext()
|
|
|
|
|
2020-12-15 12:19:13 +00:00
|
|
|
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-09 10:20:55 +00:00
|
|
|
ctx := storage.GetContext()
|
|
|
|
|
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 {
|
2021-02-09 10:20:55 +00:00
|
|
|
ctx := storage.GetContext()
|
|
|
|
|
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 {
|
2021-02-09 10:20:55 +00:00
|
|
|
ctx := storage.GetContext()
|
|
|
|
|
2020-12-15 12:19:13 +00:00
|
|
|
return storage.Get(ctx, nameKey).(string)
|
|
|
|
}
|
|
|
|
|
|
|
|
func index() int {
|
2021-02-09 10:20:55 +00:00
|
|
|
ctx := storage.GetContext()
|
|
|
|
|
2020-12-15 12:19:13 +00:00
|
|
|
return storage.Get(ctx, indexKey).(int)
|
|
|
|
}
|
|
|
|
|
|
|
|
func total() int {
|
2021-02-09 10:20:55 +00:00
|
|
|
ctx := storage.GetContext()
|
|
|
|
|
2020-12-15 12:19:13 +00:00
|
|
|
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) {
|
2021-02-09 10:20:55 +00:00
|
|
|
ctx := storage.GetContext()
|
|
|
|
|
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 {
|
2021-02-09 10:20:55 +00:00
|
|
|
ctx = storage.GetContext()
|
|
|
|
|
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) {
|
2021-02-09 10:20:55 +00:00
|
|
|
ctx = storage.GetContext()
|
|
|
|
|
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)
|
|
|
|
)
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|