2021-07-04 14:00:56 +03:00
|
|
|
package alphabet
|
2020-10-27 15:14:06 +03:00
|
|
|
|
|
|
|
import (
|
2023-03-07 14:06:21 +03:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-contract/common"
|
2020-12-10 13:41:16 +03:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop"
|
2020-10-27 15:14:06 +03:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/contract"
|
2021-02-09 14:55:58 +03:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/native/gas"
|
2021-02-11 18:55:32 +03:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/native/management"
|
2021-02-09 14:55:58 +03:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/native/neo"
|
2020-10-27 15:14:06 +03:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/runtime"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/storage"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2020-12-15 15:19:13 +03:00
|
|
|
netmapKey = "netmapScriptHash"
|
2021-02-19 18:04:33 +03:00
|
|
|
proxyKey = "proxyScriptHash"
|
|
|
|
|
|
|
|
indexKey = "index"
|
|
|
|
totalKey = "threshold"
|
|
|
|
nameKey = "name"
|
2020-10-27 15:14:06 +03:00
|
|
|
)
|
|
|
|
|
2021-07-04 16:32:13 +03:00
|
|
|
// OnNEP17Payment is a callback for NEP-17 compatible native GAS and NEO
|
|
|
|
// contracts.
|
2023-11-07 15:18:48 +03:00
|
|
|
func OnNEP17Payment(from interop.Hash160, amount int, data any) {
|
2020-12-10 13:41:16 +03:00
|
|
|
caller := runtime.GetCallingScriptHash()
|
2021-02-09 14:55:58 +03:00
|
|
|
if !common.BytesEqual(caller, []byte(gas.Hash)) && !common.BytesEqual(caller, []byte(neo.Hash)) {
|
2021-11-30 12:44:05 +03:00
|
|
|
common.AbortWithMessage("alphabet contract accepts GAS and NEO only")
|
2020-12-10 13:41:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-07 15:18:48 +03:00
|
|
|
func _deploy(data any, isUpdate bool) {
|
2021-12-27 10:15:36 +03:00
|
|
|
ctx := storage.GetContext()
|
2023-03-07 11:21:11 +03:00
|
|
|
|
2021-06-03 10:49:07 +03:00
|
|
|
if isUpdate {
|
2023-11-07 15:18:48 +03:00
|
|
|
args := data.([]any)
|
2021-12-27 11:49:30 +03:00
|
|
|
common.CheckVersion(args[len(args)-1].(int))
|
2021-06-03 10:49:07 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-29 19:34:11 +03:00
|
|
|
args := data.(struct {
|
2023-11-07 14:55:02 +03:00
|
|
|
addrNetmap interop.Hash160
|
|
|
|
addrProxy interop.Hash160
|
|
|
|
name string
|
|
|
|
index int
|
|
|
|
total int
|
2021-11-29 19:34:11 +03:00
|
|
|
})
|
2021-05-12 11:31:07 +03:00
|
|
|
|
2023-03-07 11:21:11 +03:00
|
|
|
if len(args.addrNetmap) != interop.Hash160Len || len(args.addrProxy) != interop.Hash160Len {
|
2020-10-27 15:14:06 +03:00
|
|
|
panic("incorrect length of contract script hash")
|
|
|
|
}
|
|
|
|
|
2021-11-29 19:34:11 +03: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 15:14:06 +03:00
|
|
|
|
2021-11-29 19:34:11 +03:00
|
|
|
runtime.Log(args.name + " contract initialized")
|
2020-10-27 15:14:06 +03:00
|
|
|
}
|
|
|
|
|
2022-04-14 14:56:51 +03:00
|
|
|
// Update method updates contract source code and manifest. It can be invoked
|
2021-09-20 18:41:46 +03:00
|
|
|
// only by committee.
|
2023-11-07 15:18:48 +03:00
|
|
|
func Update(script []byte, manifest []byte, data any) {
|
2021-09-20 18:41:46 +03:00
|
|
|
if !common.HasUpdateAccess() {
|
|
|
|
panic("only committee can update contract")
|
2021-02-11 18:55:32 +03:00
|
|
|
}
|
|
|
|
|
2023-06-19 11:17:51 +03:00
|
|
|
management.UpdateWithData(script, manifest, common.AppendVersion(data))
|
2021-02-11 18:55:32 +03:00
|
|
|
runtime.Log("alphabet contract updated")
|
|
|
|
}
|
|
|
|
|
2022-04-14 14:56:51 +03:00
|
|
|
// GAS returns the amount of the sidechain GAS stored in the contract account.
|
2020-10-27 15:14:06 +03:00
|
|
|
func Gas() int {
|
2021-02-09 14:55:58 +03:00
|
|
|
return gas.BalanceOf(runtime.GetExecutingScriptHash())
|
2020-10-27 15:14:06 +03:00
|
|
|
}
|
|
|
|
|
2022-04-14 14:56:51 +03:00
|
|
|
// NEO returns the amount of sidechain NEO stored in the contract account.
|
2020-10-27 15:14:06 +03:00
|
|
|
func Neo() int {
|
2021-02-09 14:55:58 +03:00
|
|
|
return neo.BalanceOf(runtime.GetExecutingScriptHash())
|
2020-10-27 15:14:06 +03:00
|
|
|
}
|
|
|
|
|
2021-03-09 22:15:58 +03:00
|
|
|
func currentEpoch(ctx storage.Context) int {
|
2021-03-04 22:21:49 +03:00
|
|
|
netmapContractAddr := storage.Get(ctx, netmapKey).(interop.Hash160)
|
2021-02-08 18:32:38 +03:00
|
|
|
return contract.Call(netmapContractAddr, "epoch", contract.ReadOnly).(int)
|
2020-12-07 15:53:11 +03:00
|
|
|
}
|
|
|
|
|
2021-03-09 22:15:58 +03:00
|
|
|
func name(ctx storage.Context) string {
|
2020-12-15 15:19:13 +03:00
|
|
|
return storage.Get(ctx, nameKey).(string)
|
|
|
|
}
|
|
|
|
|
2021-03-09 22:15:58 +03:00
|
|
|
func index(ctx storage.Context) int {
|
2020-12-15 15:19:13 +03:00
|
|
|
return storage.Get(ctx, indexKey).(int)
|
|
|
|
}
|
|
|
|
|
2022-03-21 14:01:45 +03:00
|
|
|
func checkPermission(ir []interop.PublicKey) bool {
|
2021-03-09 22:15:58 +03:00
|
|
|
ctx := storage.GetReadOnlyContext()
|
|
|
|
index := index(ctx) // read from contract memory
|
2020-12-15 15:19:13 +03:00
|
|
|
|
2020-10-27 15:14:06 +03:00
|
|
|
if len(ir) <= index {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
node := ir[index]
|
2022-03-21 14:01:45 +03:00
|
|
|
return runtime.CheckWitness(node)
|
2020-12-04 17:54:40 +03:00
|
|
|
}
|
|
|
|
|
2022-04-14 14:56:51 +03: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 16:32:13 +03:00
|
|
|
//
|
2022-04-14 14:56:51 +03:00
|
|
|
// To produce GAS, an alphabet contract transfers all available NEO from the contract
|
2023-03-07 11:21:11 +03:00
|
|
|
// account to itself. 50% of the GAS in the contract account
|
2022-04-14 14:56:51 +03: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 14:37:31 +03:00
|
|
|
func Emit() {
|
2021-03-09 22:15:58 +03:00
|
|
|
ctx := storage.GetReadOnlyContext()
|
|
|
|
|
2021-03-24 11:05:40 +03:00
|
|
|
alphabet := common.AlphabetNodes()
|
|
|
|
if !checkPermission(alphabet) {
|
2020-10-27 15:14:06 +03:00
|
|
|
panic("invalid invoker")
|
|
|
|
}
|
|
|
|
|
|
|
|
contractHash := runtime.GetExecutingScriptHash()
|
|
|
|
|
2021-11-30 16:21:14 +03:00
|
|
|
if !neo.Transfer(contractHash, contractHash, neo.BalanceOf(contractHash), nil) {
|
|
|
|
panic("failed to transfer funds, aborting")
|
|
|
|
}
|
2020-10-27 15:14:06 +03:00
|
|
|
|
2021-02-09 14:55:58 +03:00
|
|
|
gasBalance := gas.BalanceOf(contractHash)
|
2020-10-27 15:14:06 +03:00
|
|
|
|
2023-03-07 11:21:11 +03:00
|
|
|
proxyAddr := storage.Get(ctx, proxyKey).(interop.Hash160)
|
2021-11-30 16:21:14 +03:00
|
|
|
|
2023-03-07 11:21:11 +03:00
|
|
|
proxyGas := gasBalance / 2
|
|
|
|
if proxyGas == 0 {
|
|
|
|
panic("no gas to emit")
|
|
|
|
}
|
2021-07-21 13:44:59 +03:00
|
|
|
|
2023-03-07 11:21:11 +03:00
|
|
|
if !gas.Transfer(contractHash, proxyAddr, proxyGas, nil) {
|
|
|
|
runtime.Log("could not transfer GAS to proxy contract")
|
2021-07-21 13:44:59 +03:00
|
|
|
}
|
2020-10-27 15:14:06 +03:00
|
|
|
|
2023-03-07 11:21:11 +03:00
|
|
|
gasBalance -= proxyGas
|
2021-04-29 16:06:34 +03:00
|
|
|
|
2023-03-07 11:21:11 +03:00
|
|
|
runtime.Log("utility token has been emitted to proxy contract")
|
|
|
|
|
|
|
|
innerRing := common.InnerRingNodes()
|
2021-04-29 16:06:34 +03:00
|
|
|
|
2021-07-21 13:44:59 +03:00
|
|
|
gasPerNode := gasBalance * 7 / 8 / len(innerRing)
|
2021-02-19 18:04:33 +03:00
|
|
|
|
|
|
|
if gasPerNode != 0 {
|
2021-03-24 11:05:40 +03:00
|
|
|
for _, node := range innerRing {
|
2022-03-21 14:01:45 +03:00
|
|
|
address := contract.CreateStandardAccount(node)
|
2021-11-30 16:21:14 +03:00
|
|
|
if !gas.Transfer(contractHash, address, gasPerNode, nil) {
|
|
|
|
runtime.Log("could not transfer GAS to one of IR node")
|
|
|
|
}
|
2021-02-19 18:04:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
runtime.Log("utility token has been emitted to inner ring nodes")
|
2020-10-27 15:14:06 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-14 14:56:51 +03:00
|
|
|
// Vote method votes for the sidechain committee. It requires multisignature from
|
2021-07-04 16:32:13 +03:00
|
|
|
// Alphabet nodes of the Inner Ring.
|
|
|
|
//
|
2022-04-14 14:56:51 +03: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 22:21:49 +03:00
|
|
|
func Vote(epoch int, candidates []interop.PublicKey) {
|
2021-04-29 16:06:34 +03:00
|
|
|
ctx := storage.GetContext()
|
2021-03-09 22:15:58 +03:00
|
|
|
index := index(ctx)
|
|
|
|
name := name(ctx)
|
2020-12-04 17:54:40 +03:00
|
|
|
|
2023-02-10 18:07:44 +03:00
|
|
|
common.CheckAlphabetWitness()
|
2020-11-12 16:52:14 +03:00
|
|
|
|
2021-03-09 22:15:58 +03:00
|
|
|
curEpoch := currentEpoch(ctx)
|
2020-12-07 15:53:11 +03:00
|
|
|
if epoch != curEpoch {
|
|
|
|
panic("invalid epoch")
|
|
|
|
}
|
|
|
|
|
2021-02-19 18:04:33 +03:00
|
|
|
candidate := candidates[index%len(candidates)]
|
|
|
|
address := runtime.GetExecutingScriptHash()
|
2020-11-12 16:52:14 +03:00
|
|
|
|
2021-02-19 18:04:33 +03:00
|
|
|
ok := neo.Vote(address, candidate)
|
|
|
|
if ok {
|
|
|
|
runtime.Log(name + ": successfully voted for validator")
|
2020-11-12 17:20:49 +03:00
|
|
|
} else {
|
2021-02-19 18:04:33 +03:00
|
|
|
runtime.Log(name + ": vote has been failed")
|
2020-11-12 17:20:49 +03:00
|
|
|
}
|
2020-11-12 16:52:14 +03:00
|
|
|
}
|
|
|
|
|
2022-04-14 14:56:51 +03:00
|
|
|
// Name returns the Glagolitic name of the contract.
|
2020-10-27 15:14:06 +03:00
|
|
|
func Name() string {
|
2021-03-09 22:15:58 +03:00
|
|
|
ctx := storage.GetReadOnlyContext()
|
|
|
|
return name(ctx)
|
2020-10-27 15:14:06 +03:00
|
|
|
}
|
2020-12-04 17:54:40 +03:00
|
|
|
|
2022-04-14 14:56:51 +03:00
|
|
|
// Version returns the version of the contract.
|
2021-02-19 18:04:33 +03:00
|
|
|
func Version() int {
|
2021-07-29 14:44:53 +03:00
|
|
|
return common.Version
|
2020-12-04 17:54:40 +03:00
|
|
|
}
|