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-04-29 13:06:34 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/native/crypto"
|
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"
|
|
|
|
|
2021-04-27 10:48:40 +00:00
|
|
|
notaryDisabledKey = "notary"
|
|
|
|
|
2021-02-19 15:04:33 +00:00
|
|
|
version = 1
|
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-05-12 08:31:07 +00:00
|
|
|
func _deploy(data interface{}, isUpdate bool) {
|
2021-06-03 07:49:07 +00:00
|
|
|
if isUpdate {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-05-12 08:31:07 +00:00
|
|
|
args := data.([]interface{})
|
|
|
|
notaryDisabled := args[0].(bool)
|
|
|
|
owner := args[1].(interop.Hash160)
|
|
|
|
addrNetmap := args[2].(interop.Hash160)
|
|
|
|
addrProxy := args[3].(interop.Hash160)
|
|
|
|
name := args[4].(string)
|
|
|
|
index := args[5].(int)
|
|
|
|
total := args[6].(int)
|
|
|
|
|
2021-03-09 19:15:58 +00:00
|
|
|
ctx := storage.GetContext()
|
|
|
|
|
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-07-21 12:05:58 +00:00
|
|
|
if len(addrNetmap) != 20 || !notaryDisabled && 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
|
|
|
|
2021-04-27 10:48:40 +00:00
|
|
|
// initialize the way to collect signatures
|
|
|
|
storage.Put(ctx, notaryDisabledKey, notaryDisabled)
|
|
|
|
if notaryDisabled {
|
|
|
|
common.InitVote(ctx)
|
2021-05-04 14:02:05 +00:00
|
|
|
runtime.Log(name + " notary disabled")
|
2021-04-27 10:48:40 +00:00
|
|
|
}
|
|
|
|
|
2020-10-27 12:14:06 +00:00
|
|
|
runtime.Log(name + " contract initialized")
|
|
|
|
}
|
|
|
|
|
2021-06-03 07:49:07 +00:00
|
|
|
func Migrate(script []byte, manifest []byte, data interface{}) bool {
|
2021-03-09 19:15:58 +00:00
|
|
|
ctx := storage.GetReadOnlyContext()
|
|
|
|
|
2021-02-11 15:55:32 +00:00
|
|
|
if !common.HasUpdateAccess(ctx) {
|
|
|
|
runtime.Log("only owner can update contract")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-06-30 10:59:32 +00:00
|
|
|
contract.Call(interop.Hash160(management.Hash), "update", contract.All, script, manifest, data)
|
2021-02-11 15:55:32 +00:00
|
|
|
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-03-09 19:15:58 +00:00
|
|
|
func currentEpoch(ctx storage.Context) int {
|
2021-03-04 19:21:49 +00:00
|
|
|
netmapContractAddr := storage.Get(ctx, netmapKey).(interop.Hash160)
|
2021-02-08 15:32:38 +00:00
|
|
|
return contract.Call(netmapContractAddr, "epoch", contract.ReadOnly).(int)
|
2020-12-07 12:53:11 +00:00
|
|
|
}
|
|
|
|
|
2021-03-09 19:15:58 +00:00
|
|
|
func name(ctx storage.Context) string {
|
2020-12-15 12:19:13 +00:00
|
|
|
return storage.Get(ctx, nameKey).(string)
|
|
|
|
}
|
|
|
|
|
2021-03-09 19:15:58 +00:00
|
|
|
func index(ctx storage.Context) int {
|
2020-12-15 12:19:13 +00:00
|
|
|
return storage.Get(ctx, indexKey).(int)
|
|
|
|
}
|
|
|
|
|
2021-02-02 18:26:34 +00:00
|
|
|
func checkPermission(ir []common.IRNode) bool {
|
2021-03-09 19:15:58 +00:00
|
|
|
ctx := storage.GetReadOnlyContext()
|
|
|
|
index := index(ctx) // read from contract memory
|
2020-12-15 12:19:13 +00:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-05-21 11:37:31 +00:00
|
|
|
func Emit() {
|
2021-03-09 19:15:58 +00:00
|
|
|
ctx := storage.GetReadOnlyContext()
|
2021-04-29 13:06:34 +00:00
|
|
|
notaryDisabled := storage.Get(ctx, notaryDisabledKey).(bool)
|
2021-03-09 19:15:58 +00:00
|
|
|
|
2021-03-24 08:05:40 +00:00
|
|
|
alphabet := common.AlphabetNodes()
|
|
|
|
if !checkPermission(alphabet) {
|
2020-10-27 12:14:06 +00:00
|
|
|
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)
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2021-07-21 10:44:59 +00:00
|
|
|
if !notaryDisabled {
|
|
|
|
proxyAddr := storage.Get(ctx, proxyKey).(interop.Hash160)
|
|
|
|
|
|
|
|
proxyGas := gasBalance / 2
|
|
|
|
if proxyGas == 0 {
|
|
|
|
panic("no gas to emit")
|
|
|
|
}
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2021-07-21 10:44:59 +00:00
|
|
|
gas.Transfer(contractHash, proxyAddr, proxyGas, nil)
|
|
|
|
gasBalance -= proxyGas
|
|
|
|
|
|
|
|
runtime.Log("utility token has been emitted to proxy contract")
|
|
|
|
}
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2021-04-29 13:06:34 +00:00
|
|
|
var innerRing []common.IRNode
|
|
|
|
|
|
|
|
if notaryDisabled {
|
|
|
|
netmapContract := storage.Get(ctx, netmapKey).(interop.Hash160)
|
|
|
|
innerRing = common.InnerRingNodesFromNetmap(netmapContract)
|
|
|
|
} else {
|
|
|
|
innerRing = common.InnerRingNodes()
|
|
|
|
}
|
|
|
|
|
2021-07-21 10:44:59 +00:00
|
|
|
gasPerNode := gasBalance * 7 / 8 / len(innerRing)
|
2021-02-19 15:04:33 +00:00
|
|
|
|
|
|
|
if gasPerNode != 0 {
|
2021-03-24 08:05:40 +00:00
|
|
|
for _, node := range innerRing {
|
2021-02-19 15:04:33 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-04 19:21:49 +00:00
|
|
|
func Vote(epoch int, candidates []interop.PublicKey) {
|
2021-04-29 13:06:34 +00:00
|
|
|
ctx := storage.GetContext()
|
|
|
|
notaryDisabled := storage.Get(ctx, notaryDisabledKey).(bool)
|
2021-03-09 19:15:58 +00:00
|
|
|
index := index(ctx)
|
|
|
|
name := name(ctx)
|
2020-12-04 14:54:40 +00:00
|
|
|
|
2021-04-29 13:06:34 +00:00
|
|
|
var ( // for invocation collection without notary
|
|
|
|
alphabet []common.IRNode
|
|
|
|
nodeKey []byte
|
|
|
|
)
|
|
|
|
|
|
|
|
if notaryDisabled {
|
|
|
|
alphabet = common.AlphabetNodes()
|
|
|
|
nodeKey = common.InnerRingInvoker(alphabet)
|
|
|
|
if len(nodeKey) == 0 {
|
|
|
|
panic("invalid invoker")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
multiaddr := common.AlphabetAddress()
|
|
|
|
if !runtime.CheckWitness(multiaddr) {
|
|
|
|
panic("invalid invoker")
|
|
|
|
}
|
2020-11-12 13:52:14 +00:00
|
|
|
}
|
|
|
|
|
2021-03-09 19:15:58 +00:00
|
|
|
curEpoch := currentEpoch(ctx)
|
2020-12-07 12:53:11 +00:00
|
|
|
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-04-29 13:06:34 +00:00
|
|
|
if notaryDisabled {
|
|
|
|
threshold := len(alphabet)*2/3 + 1
|
|
|
|
id := voteID(epoch, candidates)
|
|
|
|
|
|
|
|
n := common.Vote(ctx, id, nodeKey)
|
|
|
|
if n < threshold {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
common.RemoveVotes(ctx, id)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-04-29 13:06:34 +00:00
|
|
|
func voteID(epoch interface{}, args []interop.PublicKey) []byte {
|
|
|
|
var (
|
|
|
|
result []byte
|
|
|
|
epochBytes = epoch.([]byte)
|
|
|
|
)
|
|
|
|
|
|
|
|
result = append(result, epochBytes...)
|
|
|
|
|
|
|
|
for i := range args {
|
|
|
|
result = append(result, args[i]...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return crypto.Sha256(result)
|
|
|
|
}
|
|
|
|
|
2020-10-27 12:14:06 +00:00
|
|
|
func Name() string {
|
2021-03-09 19:15:58 +00:00
|
|
|
ctx := storage.GetReadOnlyContext()
|
|
|
|
return name(ctx)
|
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
|
|
|
}
|