2021-07-04 11:00:56 +00:00
|
|
|
package alphabet
|
2020-10-27 12:14:06 +00:00
|
|
|
|
|
|
|
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"
|
2020-10-27 12:14:06 +00:00
|
|
|
)
|
|
|
|
|
2021-07-04 13:32:13 +00:00
|
|
|
// OnNEP17Payment is a callback for NEP-17 compatible native GAS and NEO
|
|
|
|
// contracts.
|
2021-02-08 15:14:03 +00:00
|
|
|
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-11-29 10:51:57 +00:00
|
|
|
panic("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-11-29 16:34:11 +00:00
|
|
|
args := data.(struct {
|
|
|
|
notaryDisabled bool
|
|
|
|
addrNetmap interop.Hash160
|
|
|
|
addrProxy interop.Hash160
|
|
|
|
name string
|
|
|
|
index int
|
|
|
|
total int
|
|
|
|
})
|
2021-05-12 08:31:07 +00:00
|
|
|
|
2021-03-09 19:15:58 +00:00
|
|
|
ctx := storage.GetContext()
|
|
|
|
|
2021-11-29 16:34:11 +00:00
|
|
|
if len(args.addrNetmap) != 20 || !args.notaryDisabled && len(args.addrProxy) != 20 {
|
2020-10-27 12:14:06 +00:00
|
|
|
panic("incorrect length of contract script hash")
|
|
|
|
}
|
|
|
|
|
2021-11-29 16:34:11 +00:00
|
|
|
storage.Put(ctx, netmapKey, args.addrNetmap)
|
|
|
|
storage.Put(ctx, proxyKey, args.addrProxy)
|
|
|
|
storage.Put(ctx, nameKey, args.name)
|
|
|
|
storage.Put(ctx, indexKey, args.index)
|
|
|
|
storage.Put(ctx, totalKey, args.total)
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2021-04-27 10:48:40 +00:00
|
|
|
// initialize the way to collect signatures
|
2021-11-29 16:34:11 +00:00
|
|
|
storage.Put(ctx, notaryDisabledKey, args.notaryDisabled)
|
|
|
|
if args.notaryDisabled {
|
2021-04-27 10:48:40 +00:00
|
|
|
common.InitVote(ctx)
|
2021-11-29 16:34:11 +00:00
|
|
|
runtime.Log(args.name + " notary disabled")
|
2021-04-27 10:48:40 +00:00
|
|
|
}
|
|
|
|
|
2021-11-29 16:34:11 +00:00
|
|
|
runtime.Log(args.name + " contract initialized")
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2021-09-21 08:02:24 +00:00
|
|
|
// Update method updates contract source code and manifest. Can be invoked
|
2021-09-20 15:41:46 +00:00
|
|
|
// only by committee.
|
2021-09-21 12:58:37 +00:00
|
|
|
func Update(script []byte, manifest []byte, data interface{}) {
|
2021-09-20 15:41:46 +00:00
|
|
|
if !common.HasUpdateAccess() {
|
|
|
|
panic("only committee can update contract")
|
2021-02-11 15:55:32 +00:00
|
|
|
}
|
|
|
|
|
2021-11-09 10:55:21 +00:00
|
|
|
contract.Call(interop.Hash160(management.Hash), "update",
|
|
|
|
contract.All, script, manifest, common.AppendVersion(data))
|
2021-02-11 15:55:32 +00:00
|
|
|
runtime.Log("alphabet contract updated")
|
|
|
|
}
|
|
|
|
|
2021-07-04 13:32:13 +00:00
|
|
|
// GAS returns amount of side chain GAS stored in contract account.
|
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
|
|
|
}
|
|
|
|
|
2021-07-04 13:32:13 +00:00
|
|
|
// NEO returns amount of side chain NEO stored in contract account.
|
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-07-04 13:32:13 +00:00
|
|
|
// Emit method produces side chain GAS and distributes it among Inner Ring nodes
|
|
|
|
// and proxy contract. Can be invoked only by Alphabet node of the Inner Ring.
|
|
|
|
//
|
|
|
|
// To produce GAS, alphabet contract transfers all available NEO from contract
|
|
|
|
// account to itself. If notary enabled, then 50% of the GAS in the contract account
|
|
|
|
// transferred to proxy contract. 43.75% of the GAS are equally distributed
|
|
|
|
// among all Inner Ring nodes. Remaining 6.25% of the GAS stays in the contract.
|
|
|
|
//
|
|
|
|
// If notary disabled, then 87.5% of the GAS are equally distributed among all
|
|
|
|
// Inner Ring nodes. Remaining 12.5% of the GAS stays in the contract.
|
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-07-04 13:32:13 +00:00
|
|
|
// Vote method votes for side chain committee. Requires multisignature from
|
|
|
|
// Alphabet nodes of the Inner Ring.
|
|
|
|
//
|
|
|
|
// This method is used when governance changes list of Alphabet nodes of the
|
|
|
|
// Inner Ring. Alphabet nodes share keys with side chain validators, therefore
|
|
|
|
// it is required to change them as well. To do that NEO holders, which are
|
|
|
|
// alphabet contracts, should vote for new committee.
|
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()
|
2021-11-29 12:21:40 +00:00
|
|
|
common.CheckAlphabetWitness(multiaddr)
|
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)
|
|
|
|
}
|
|
|
|
|
2021-07-04 13:32:13 +00:00
|
|
|
// Name returns Glagolitic name of the contract.
|
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-07-04 13:32:13 +00:00
|
|
|
// Version returns version of the contract.
|
2021-02-19 15:04:33 +00:00
|
|
|
func Version() int {
|
2021-07-29 11:44:53 +00:00
|
|
|
return common.Version
|
2020-12-04 14:54:40 +00:00
|
|
|
}
|