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-10-27 12:14:06 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/contract"
|
2021-02-09 11:55:58 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/native/gas"
|
2021-02-11 15:55:32 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/native/management"
|
2021-02-09 11:55:58 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/native/neo"
|
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 (
|
2020-12-15 12:19:13 +00:00
|
|
|
netmapKey = "netmapScriptHash"
|
2021-02-19 15:04:33 +00:00
|
|
|
proxyKey = "proxyScriptHash"
|
|
|
|
|
|
|
|
indexKey = "index"
|
|
|
|
totalKey = "threshold"
|
|
|
|
nameKey = "name"
|
|
|
|
|
|
|
|
version = 1
|
2020-10-27 12:14:06 +00:00
|
|
|
)
|
|
|
|
|
2021-02-11 16:20:38 +00:00
|
|
|
var ctx storage.Context
|
2021-02-11 16:13:49 +00:00
|
|
|
|
2020-10-27 12:14:06 +00:00
|
|
|
func init() {
|
2021-02-11 16:13:49 +00:00
|
|
|
ctx = storage.GetContext()
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
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-09 11:55:58 +00:00
|
|
|
if !common.BytesEqual(caller, []byte(gas.Hash)) && !common.BytesEqual(caller, []byte(neo.Hash)) {
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-19 15:04:33 +00:00
|
|
|
func Init(owner interop.Hash160, addrNetmap, addrProxy interop.Hash160, name string, index, total int) {
|
2021-02-11 15:55:32 +00:00
|
|
|
if !common.HasUpdateAccess(ctx) {
|
|
|
|
panic("only owner can reinitialize contract")
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2021-02-19 15:04:33 +00:00
|
|
|
if len(addrNetmap) != 20 || len(addrProxy) != 20 {
|
2020-10-27 12:14:06 +00:00
|
|
|
panic("incorrect length of contract script hash")
|
|
|
|
}
|
|
|
|
|
2021-02-11 15:55:32 +00:00
|
|
|
storage.Put(ctx, common.OwnerKey, owner)
|
2020-12-15 12:19:13 +00:00
|
|
|
storage.Put(ctx, netmapKey, addrNetmap)
|
2021-02-19 15:04:33 +00:00
|
|
|
storage.Put(ctx, proxyKey, addrProxy)
|
2020-12-15 12:19:13 +00:00
|
|
|
storage.Put(ctx, nameKey, name)
|
|
|
|
storage.Put(ctx, indexKey, index)
|
|
|
|
storage.Put(ctx, totalKey, total)
|
2020-10-27 12:14:06 +00:00
|
|
|
|
|
|
|
runtime.Log(name + " contract initialized")
|
|
|
|
}
|
|
|
|
|
2021-02-11 15:55:32 +00:00
|
|
|
func Migrate(script []byte, manifest []byte) bool {
|
|
|
|
if !common.HasUpdateAccess(ctx) {
|
|
|
|
runtime.Log("only owner can update contract")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
management.Update(script, manifest)
|
|
|
|
runtime.Log("alphabet contract updated")
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-10-27 12:14:06 +00:00
|
|
|
func Gas() int {
|
2021-02-09 11:55:58 +00:00
|
|
|
return gas.BalanceOf(runtime.GetExecutingScriptHash())
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Neo() int {
|
2021-02-09 11:55:58 +00:00
|
|
|
return neo.BalanceOf(runtime.GetExecutingScriptHash())
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
|
2021-02-19 15:04:33 +00:00
|
|
|
neo.Transfer(contractHash, contractHash, neo.BalanceOf(contractHash), nil)
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2021-02-09 11:55:58 +00:00
|
|
|
gasBalance := gas.BalanceOf(contractHash)
|
2021-02-19 15:04:33 +00:00
|
|
|
proxyAddr := storage.Get(ctx, proxyKey).(interop.Hash160)
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2021-02-19 15:04:33 +00:00
|
|
|
proxyGas := gasBalance / 2
|
|
|
|
if proxyGas == 0 {
|
2020-10-27 12:14:06 +00:00
|
|
|
runtime.Log("no gas to emit")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-02-19 15:04:33 +00:00
|
|
|
gas.Transfer(contractHash, proxyAddr, proxyGas, nil)
|
|
|
|
runtime.Log("utility token has been emitted to proxy contract")
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2021-02-19 15:04:33 +00:00
|
|
|
gasPerNode := gasBalance / 2 * 7 / 8 / len(innerRingKeys)
|
|
|
|
|
|
|
|
if gasPerNode != 0 {
|
|
|
|
for _, node := range innerRingKeys {
|
|
|
|
address := contract.CreateStandardAccount(node.PublicKey)
|
|
|
|
gas.Transfer(contractHash, address, gasPerNode, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
runtime.Log("utility token has been emitted to inner ring nodes")
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-12-04 14:54:40 +00:00
|
|
|
func Vote(epoch int, candidates [][]byte) {
|
2020-12-15 12:19:13 +00:00
|
|
|
index := index()
|
|
|
|
name := name()
|
2020-12-04 14:54:40 +00:00
|
|
|
|
2021-02-19 15:04:33 +00:00
|
|
|
multiaddr := common.InnerRingMultiAddressViaStorage(ctx, netmapKey)
|
|
|
|
if !runtime.CheckWitness(multiaddr) {
|
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")
|
|
|
|
}
|
|
|
|
|
2021-02-19 15:04:33 +00:00
|
|
|
candidate := candidates[index%len(candidates)]
|
|
|
|
address := runtime.GetExecutingScriptHash()
|
2020-11-12 13:52:14 +00:00
|
|
|
|
2021-02-19 15:04:33 +00:00
|
|
|
ok := neo.Vote(address, candidate)
|
|
|
|
if ok {
|
|
|
|
runtime.Log(name + ": successfully voted for validator")
|
2020-11-12 14:20:49 +00:00
|
|
|
} else {
|
2021-02-19 15:04:33 +00:00
|
|
|
runtime.Log(name + ": vote has been failed")
|
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
|
|
|
|
2021-02-19 15:04:33 +00:00
|
|
|
func Version() int {
|
|
|
|
return version
|
2020-12-04 14:54:40 +00:00
|
|
|
}
|