2021-07-04 11:00:56 +00:00
|
|
|
package alphabet
|
2020-10-27 12:14:06 +00:00
|
|
|
|
|
|
|
import (
|
2023-03-07 11:06:21 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-contract/common"
|
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"
|
|
|
|
)
|
|
|
|
|
|
|
|
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-30 09:44:05 +00:00
|
|
|
common.AbortWithMessage("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-12-27 07:15:36 +00:00
|
|
|
ctx := storage.GetContext()
|
2023-03-07 08:21:11 +00:00
|
|
|
|
|
|
|
common.RmAndCheckNotaryDisabledKey(data, notaryDisabledKey)
|
|
|
|
|
2021-06-03 07:49:07 +00:00
|
|
|
if isUpdate {
|
2021-12-27 08:49:30 +00:00
|
|
|
args := data.([]interface{})
|
|
|
|
common.CheckVersion(args[len(args)-1].(int))
|
2021-06-03 07:49:07 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-29 16:34:11 +00:00
|
|
|
args := data.(struct {
|
2023-03-07 08:21:11 +00:00
|
|
|
//TODO(@acid-ant): #9 remove notaryDisabled in future version
|
2021-11-29 16:34:11 +00:00
|
|
|
notaryDisabled bool
|
|
|
|
addrNetmap interop.Hash160
|
|
|
|
addrProxy interop.Hash160
|
|
|
|
name string
|
|
|
|
index int
|
|
|
|
total int
|
|
|
|
})
|
2021-05-12 08:31:07 +00:00
|
|
|
|
2023-03-07 08:21:11 +00:00
|
|
|
if len(args.addrNetmap) != interop.Hash160Len || len(args.addrProxy) != interop.Hash160Len {
|
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-11-29 16:34:11 +00:00
|
|
|
runtime.Log(args.name + " contract initialized")
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2022-04-14 11:56:51 +00:00
|
|
|
// Update method updates contract source code and manifest. It 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")
|
|
|
|
}
|
|
|
|
|
2022-04-14 11:56:51 +00:00
|
|
|
// GAS returns the amount of the sidechain GAS stored in the 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
|
|
|
}
|
|
|
|
|
2022-04-14 11:56:51 +00:00
|
|
|
// NEO returns the amount of sidechain NEO stored in the 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)
|
|
|
|
}
|
|
|
|
|
2022-03-21 11:01:45 +00:00
|
|
|
func checkPermission(ir []interop.PublicKey) 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]
|
2022-03-21 11:01:45 +00:00
|
|
|
return runtime.CheckWitness(node)
|
2020-12-04 14:54:40 +00:00
|
|
|
}
|
|
|
|
|
2022-04-14 11:56:51 +00:00
|
|
|
// Emit method produces sidechain GAS and distributes it among Inner Ring nodes
|
|
|
|
// and proxy contract. It can be invoked only by an Alphabet node of the Inner Ring.
|
2021-07-04 13:32:13 +00:00
|
|
|
//
|
2022-04-14 11:56:51 +00:00
|
|
|
// To produce GAS, an alphabet contract transfers all available NEO from the contract
|
2023-03-07 08:21:11 +00:00
|
|
|
// account to itself. 50% of the GAS in the contract account
|
2022-04-14 11:56:51 +00:00
|
|
|
// are transferred to proxy contract. 43.75% of the GAS are equally distributed
|
|
|
|
// among all Inner Ring nodes. Remaining 6.25% of the GAS stay 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-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-11-30 13:21:14 +00:00
|
|
|
if !neo.Transfer(contractHash, contractHash, neo.BalanceOf(contractHash), nil) {
|
|
|
|
panic("failed to transfer funds, aborting")
|
|
|
|
}
|
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
|
|
|
|
2023-03-07 08:21:11 +00:00
|
|
|
proxyAddr := storage.Get(ctx, proxyKey).(interop.Hash160)
|
2021-11-30 13:21:14 +00:00
|
|
|
|
2023-03-07 08:21:11 +00:00
|
|
|
proxyGas := gasBalance / 2
|
|
|
|
if proxyGas == 0 {
|
|
|
|
panic("no gas to emit")
|
|
|
|
}
|
2021-07-21 10:44:59 +00:00
|
|
|
|
2023-03-07 08:21:11 +00:00
|
|
|
if !gas.Transfer(contractHash, proxyAddr, proxyGas, nil) {
|
|
|
|
runtime.Log("could not transfer GAS to proxy contract")
|
2021-07-21 10:44:59 +00:00
|
|
|
}
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2023-03-07 08:21:11 +00:00
|
|
|
gasBalance -= proxyGas
|
2021-04-29 13:06:34 +00:00
|
|
|
|
2023-03-07 08:21:11 +00:00
|
|
|
runtime.Log("utility token has been emitted to proxy contract")
|
|
|
|
|
|
|
|
innerRing := common.InnerRingNodes()
|
2021-04-29 13:06:34 +00:00
|
|
|
|
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 {
|
2022-03-21 11:01:45 +00:00
|
|
|
address := contract.CreateStandardAccount(node)
|
2021-11-30 13:21:14 +00:00
|
|
|
if !gas.Transfer(contractHash, address, gasPerNode, nil) {
|
|
|
|
runtime.Log("could not transfer GAS to one of IR node")
|
|
|
|
}
|
2021-02-19 15:04:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
runtime.Log("utility token has been emitted to inner ring nodes")
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-14 11:56:51 +00:00
|
|
|
// Vote method votes for the sidechain committee. It requires multisignature from
|
2021-07-04 13:32:13 +00:00
|
|
|
// Alphabet nodes of the Inner Ring.
|
|
|
|
//
|
2022-04-14 11:56:51 +00:00
|
|
|
// This method is used when governance changes the list of Alphabet nodes of the
|
|
|
|
// Inner Ring. Alphabet nodes share keys with sidechain validators, therefore
|
|
|
|
// it is required to change them as well. To do that, NEO holders (which are
|
|
|
|
// alphabet contracts) should vote for a 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()
|
2021-03-09 19:15:58 +00:00
|
|
|
index := index(ctx)
|
|
|
|
name := name(ctx)
|
2020-12-04 14:54:40 +00:00
|
|
|
|
2023-02-10 15:07:44 +00:00
|
|
|
common.CheckAlphabetWitness()
|
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-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
|
|
|
}
|
|
|
|
|
2022-04-14 11:56:51 +00:00
|
|
|
// Name returns the 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
|
|
|
|
2022-04-14 11:56:51 +00:00
|
|
|
// Version returns the 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
|
|
|
}
|