2020-03-30 13:59:54 +00:00
|
|
|
package smart_contract
|
|
|
|
|
2020-07-20 13:09:40 +00:00
|
|
|
/*
|
|
|
|
NeoFS Smart Contract for NEO3.0.
|
|
|
|
|
2020-08-26 09:28:38 +00:00
|
|
|
Utility methods, executed once in deploy stage:
|
|
|
|
- Init
|
|
|
|
- InitConfig
|
|
|
|
|
|
|
|
User related methods:
|
|
|
|
- Deposit
|
|
|
|
- Withdraw
|
|
|
|
- Bind
|
|
|
|
- Unbind
|
|
|
|
|
|
|
|
Inner ring list related methods:
|
2021-03-24 10:23:18 +00:00
|
|
|
- AlphabetList
|
2021-04-20 14:27:15 +00:00
|
|
|
- AlphabetAddress
|
2020-08-26 09:28:38 +00:00
|
|
|
- InnerRingCandidates
|
|
|
|
- InnerRingCandidateAdd
|
|
|
|
- InnerRingCandidateRemove
|
2021-03-24 10:23:18 +00:00
|
|
|
- AlphabetUpdate
|
2020-08-26 09:28:38 +00:00
|
|
|
|
|
|
|
Config methods:
|
|
|
|
- Config
|
|
|
|
- ListConfig
|
|
|
|
- SetConfig
|
|
|
|
|
|
|
|
Other utility methods:
|
2021-04-20 14:27:15 +00:00
|
|
|
- Migrate
|
2020-08-26 09:28:38 +00:00
|
|
|
- Version
|
|
|
|
- Cheque
|
2020-07-20 13:09:40 +00:00
|
|
|
*/
|
|
|
|
|
2020-03-30 13:59:54 +00:00
|
|
|
import (
|
2020-12-09 13:27:39 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop"
|
2020-07-16 12:06:49 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/contract"
|
2020-07-16 15:59:49 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/iterator"
|
2021-04-29 13:15:29 +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-03-12 12:16:36 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/native/std"
|
2020-03-30 13:59:54 +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-03-30 13:59:54 +00:00
|
|
|
)
|
|
|
|
|
2020-05-27 12:23:44 +00:00
|
|
|
type (
|
2020-07-16 15:59:49 +00:00
|
|
|
record struct {
|
|
|
|
key []byte
|
|
|
|
val []byte
|
|
|
|
}
|
2020-05-27 12:23:44 +00:00
|
|
|
)
|
2020-03-30 13:59:54 +00:00
|
|
|
|
2020-05-27 12:23:44 +00:00
|
|
|
const (
|
2020-07-16 15:59:49 +00:00
|
|
|
candidateFeeConfigKey = "InnerRingCandidateFee"
|
2021-04-20 14:44:36 +00:00
|
|
|
withdrawFeeConfigKey = "WithdrawFee"
|
2020-07-20 13:09:40 +00:00
|
|
|
|
2020-08-26 09:15:07 +00:00
|
|
|
version = 3
|
2020-07-20 13:09:40 +00:00
|
|
|
|
2021-04-27 10:48:40 +00:00
|
|
|
alphabetKey = "alphabet"
|
|
|
|
candidatesKey = "candidates"
|
|
|
|
notaryDisabledKey = "notary"
|
2020-07-20 13:09:40 +00:00
|
|
|
|
2021-04-20 14:44:36 +00:00
|
|
|
processingContractKey = "processingScriptHash"
|
|
|
|
|
2021-03-24 10:23:18 +00:00
|
|
|
publicKeySize = 33
|
2020-10-26 15:26:04 +00:00
|
|
|
|
|
|
|
maxBalanceAmount = 9000 // Max integer of Fixed12 in JSON bound (2**53-1)
|
2020-12-09 13:27:39 +00:00
|
|
|
|
|
|
|
// hardcoded value to ignore deposit notification in onReceive
|
|
|
|
ignoreDepositNotification = "\x57\x0b"
|
2020-05-27 12:23:44 +00:00
|
|
|
)
|
2020-03-30 13:59:54 +00:00
|
|
|
|
2021-02-11 16:13:49 +00:00
|
|
|
var (
|
|
|
|
configPrefix = []byte("config")
|
|
|
|
)
|
2020-07-16 15:59:49 +00:00
|
|
|
|
2021-03-24 10:23:18 +00:00
|
|
|
// Init set up initial alphabet node keys.
|
2021-04-27 10:48:40 +00:00
|
|
|
func Init(notaryDisabled bool, owner, addrProc interop.Hash160, args []interop.PublicKey) bool {
|
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-08-26 09:15:07 +00:00
|
|
|
}
|
2020-07-15 16:26:24 +00:00
|
|
|
|
2021-02-02 18:26:34 +00:00
|
|
|
var irList []common.IRNode
|
2020-03-30 13:59:54 +00:00
|
|
|
|
2021-03-22 13:56:59 +00:00
|
|
|
if len(args) == 0 {
|
2021-03-24 10:23:18 +00:00
|
|
|
panic("neofs: at least one alphabet key must be provided")
|
2020-09-17 08:08:41 +00:00
|
|
|
}
|
|
|
|
|
2021-04-20 14:44:36 +00:00
|
|
|
if len(addrProc) != 20 {
|
|
|
|
panic("neofs: incorrect length of contract script hash")
|
|
|
|
}
|
|
|
|
|
2020-08-26 09:15:07 +00:00
|
|
|
for i := 0; i < len(args); i++ {
|
2020-08-28 06:42:06 +00:00
|
|
|
pub := args[i]
|
2020-09-17 08:08:41 +00:00
|
|
|
if len(pub) != publicKeySize {
|
|
|
|
panic("neofs: incorrect public key length")
|
|
|
|
}
|
2021-02-02 18:26:34 +00:00
|
|
|
irList = append(irList, common.IRNode{PublicKey: pub})
|
2020-08-26 09:15:07 +00:00
|
|
|
}
|
2020-07-16 12:06:49 +00:00
|
|
|
|
2020-08-26 09:15:07 +00:00
|
|
|
// initialize all storage slices
|
2021-03-24 10:23:18 +00:00
|
|
|
common.SetSerialized(ctx, alphabetKey, irList)
|
2021-02-02 18:26:34 +00:00
|
|
|
common.SetSerialized(ctx, candidatesKey, []common.IRNode{})
|
2020-03-30 13:59:54 +00:00
|
|
|
|
2021-02-11 15:55:32 +00:00
|
|
|
storage.Put(ctx, common.OwnerKey, owner)
|
2021-04-20 14:44:36 +00:00
|
|
|
storage.Put(ctx, processingContractKey, addrProc)
|
2021-02-11 15:55:32 +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("neofs contract notary disabled")
|
2021-04-27 10:48:40 +00:00
|
|
|
}
|
|
|
|
|
2020-08-26 09:15:07 +00:00
|
|
|
runtime.Log("neofs: contract initialized")
|
2020-07-16 12:06:49 +00:00
|
|
|
|
2020-08-26 09:15:07 +00:00
|
|
|
return true
|
|
|
|
}
|
2020-07-16 12:06:49 +00:00
|
|
|
|
2021-02-11 15:55:32 +00:00
|
|
|
// Migrate updates smart contract execution script and manifest.
|
|
|
|
func Migrate(script []byte, manifest []byte) 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
|
|
|
|
}
|
|
|
|
|
|
|
|
management.Update(script, manifest)
|
|
|
|
runtime.Log("neofs contract updated")
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-03-24 10:23:18 +00:00
|
|
|
// AlphabetList returns array of alphabet node keys.
|
|
|
|
func AlphabetList() []common.IRNode {
|
2021-03-09 19:15:58 +00:00
|
|
|
ctx := storage.GetReadOnlyContext()
|
2021-03-24 10:23:18 +00:00
|
|
|
return getNodes(ctx, alphabetKey)
|
2020-08-26 09:15:07 +00:00
|
|
|
}
|
2020-03-30 13:59:54 +00:00
|
|
|
|
2021-04-20 14:27:15 +00:00
|
|
|
// AlphabetAddress returns 2\3n+1 multi signature address of alphabet nodes.
|
|
|
|
func AlphabetAddress() interop.Hash160 {
|
|
|
|
ctx := storage.GetReadOnlyContext()
|
|
|
|
return multiaddress(getNodes(ctx, alphabetKey))
|
|
|
|
}
|
|
|
|
|
2020-08-26 09:28:38 +00:00
|
|
|
// InnerRingCandidates returns array of inner ring candidate node keys.
|
2021-02-02 18:26:34 +00:00
|
|
|
func InnerRingCandidates() []common.IRNode {
|
2021-03-09 19:15:58 +00:00
|
|
|
ctx := storage.GetReadOnlyContext()
|
2021-03-24 10:23:18 +00:00
|
|
|
return getNodes(ctx, candidatesKey)
|
2020-08-26 09:15:07 +00:00
|
|
|
}
|
2020-03-30 13:59:54 +00:00
|
|
|
|
2020-08-26 09:28:38 +00:00
|
|
|
// InnerRingCandidateRemove removes key from the list of inner ring candidates.
|
2021-03-04 19:21:49 +00:00
|
|
|
func InnerRingCandidateRemove(key interop.PublicKey) bool {
|
2021-03-09 19:15:58 +00:00
|
|
|
ctx := storage.GetContext()
|
2021-04-29 13:15:29 +00:00
|
|
|
notaryDisabled := storage.Get(ctx, notaryDisabledKey).(bool)
|
|
|
|
|
|
|
|
var ( // for invocation collection without notary
|
|
|
|
alphabet []common.IRNode
|
|
|
|
nodeKey []byte
|
|
|
|
)
|
|
|
|
|
|
|
|
keyOwner := runtime.CheckWitness(key)
|
|
|
|
|
|
|
|
if !keyOwner {
|
|
|
|
if notaryDisabled {
|
|
|
|
alphabet = getNodes(ctx, alphabetKey)
|
|
|
|
nodeKey = common.InnerRingInvoker(alphabet)
|
|
|
|
if len(nodeKey) == 0 {
|
|
|
|
panic("irCandidateRemove: this method must be invoked by candidate or alphabet")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
multiaddr := AlphabetAddress()
|
|
|
|
if !runtime.CheckWitness(multiaddr) {
|
|
|
|
panic("irCandidateRemove: this method must be invoked by candidate or alphabet")
|
|
|
|
}
|
|
|
|
}
|
2020-08-26 09:15:07 +00:00
|
|
|
}
|
2020-03-30 13:59:54 +00:00
|
|
|
|
2021-02-02 18:26:34 +00:00
|
|
|
nodes := []common.IRNode{} // it is explicit declaration of empty slice, not nil
|
2021-03-24 10:23:18 +00:00
|
|
|
candidates := getNodes(ctx, candidatesKey)
|
2020-07-16 12:06:49 +00:00
|
|
|
|
2020-08-26 09:15:07 +00:00
|
|
|
for i := range candidates {
|
|
|
|
c := candidates[i]
|
2021-02-02 18:26:34 +00:00
|
|
|
if !common.BytesEqual(c.PublicKey, key) {
|
2020-08-26 09:15:07 +00:00
|
|
|
nodes = append(nodes, c)
|
|
|
|
} else {
|
|
|
|
runtime.Log("irCandidateRemove: candidate has been removed")
|
2020-03-30 13:59:54 +00:00
|
|
|
}
|
2020-08-26 09:15:07 +00:00
|
|
|
}
|
2020-03-30 13:59:54 +00:00
|
|
|
|
2021-04-29 13:15:29 +00:00
|
|
|
if notaryDisabled && !keyOwner {
|
|
|
|
threshold := len(alphabet)*2/3 + 1
|
|
|
|
id := append(key, []byte("delete")...)
|
|
|
|
hashID := crypto.Sha256(id)
|
|
|
|
|
|
|
|
n := common.Vote(ctx, hashID, nodeKey)
|
|
|
|
if n < threshold {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
common.RemoveVotes(ctx, hashID)
|
|
|
|
}
|
|
|
|
|
2021-02-02 17:36:20 +00:00
|
|
|
common.SetSerialized(ctx, candidatesKey, nodes)
|
2020-03-30 13:59:54 +00:00
|
|
|
|
2020-08-26 09:15:07 +00:00
|
|
|
return true
|
|
|
|
}
|
2020-03-30 13:59:54 +00:00
|
|
|
|
2020-08-26 09:28:38 +00:00
|
|
|
// InnerRingCandidateAdd adds key to the list of inner ring candidates.
|
2021-03-04 19:21:49 +00:00
|
|
|
func InnerRingCandidateAdd(key interop.PublicKey) bool {
|
2021-03-09 19:15:58 +00:00
|
|
|
ctx := storage.GetContext()
|
|
|
|
|
2020-08-26 09:15:07 +00:00
|
|
|
if !runtime.CheckWitness(key) {
|
2021-04-20 14:30:43 +00:00
|
|
|
panic("irCandidateAdd: this method must be invoked by candidate")
|
2020-08-26 09:15:07 +00:00
|
|
|
}
|
2020-03-30 13:59:54 +00:00
|
|
|
|
2021-02-02 18:26:34 +00:00
|
|
|
c := common.IRNode{PublicKey: key}
|
2021-03-24 10:23:18 +00:00
|
|
|
candidates := getNodes(ctx, candidatesKey)
|
2020-07-15 08:51:49 +00:00
|
|
|
|
2020-08-26 09:15:07 +00:00
|
|
|
list, ok := addNode(candidates, c)
|
|
|
|
if !ok {
|
|
|
|
panic("irCandidateAdd: candidate already in the list")
|
|
|
|
}
|
2020-03-30 13:59:54 +00:00
|
|
|
|
2020-08-26 09:15:07 +00:00
|
|
|
from := contract.CreateStandardAccount(key)
|
|
|
|
to := runtime.GetExecutingScriptHash()
|
|
|
|
fee := getConfig(ctx, candidateFeeConfigKey).(int)
|
2020-03-30 13:59:54 +00:00
|
|
|
|
2021-02-09 11:55:58 +00:00
|
|
|
transferred := gas.Transfer(from, to, fee, []byte(ignoreDepositNotification))
|
2020-08-26 09:15:07 +00:00
|
|
|
if !transferred {
|
|
|
|
panic("irCandidateAdd: failed to transfer funds, aborting")
|
|
|
|
}
|
2020-07-15 08:51:49 +00:00
|
|
|
|
2020-08-26 09:15:07 +00:00
|
|
|
runtime.Log("irCandidateAdd: candidate has been added")
|
2021-02-02 17:36:20 +00:00
|
|
|
common.SetSerialized(ctx, candidatesKey, list)
|
2020-03-30 13:59:54 +00:00
|
|
|
|
2020-08-26 09:15:07 +00:00
|
|
|
return true
|
|
|
|
}
|
2020-03-30 13:59:54 +00:00
|
|
|
|
2021-02-08 15:14:03 +00:00
|
|
|
// OnNEP17Payment is a callback for NEP-17 compatible native GAS contract.
|
|
|
|
func OnNEP17Payment(from interop.Hash160, amount int, data interface{}) {
|
2020-12-09 13:27:39 +00:00
|
|
|
rcv := data.(interop.Hash160)
|
2021-02-02 17:36:20 +00:00
|
|
|
if common.BytesEqual(rcv, []byte(ignoreDepositNotification)) {
|
2020-12-09 13:27:39 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
caller := runtime.GetCallingScriptHash()
|
2021-03-04 19:21:49 +00:00
|
|
|
if !common.BytesEqual(caller, interop.Hash160(gas.Hash)) {
|
2021-02-08 15:14:03 +00:00
|
|
|
panic("onNEP17Payment: only GAS can be accepted for deposit")
|
2020-12-09 13:27:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch len(rcv) {
|
|
|
|
case 20:
|
|
|
|
case 0:
|
|
|
|
rcv = from
|
|
|
|
default:
|
2021-02-08 15:14:03 +00:00
|
|
|
panic("onNEP17Payment: invalid data argument, expected Hash160")
|
2020-12-09 13:27:39 +00:00
|
|
|
}
|
|
|
|
|
2021-02-08 15:14:03 +00:00
|
|
|
runtime.Log("onNEP17Payment: funds have been transferred")
|
2020-12-09 13:27:39 +00:00
|
|
|
|
|
|
|
tx := runtime.GetScriptContainer()
|
|
|
|
runtime.Notify("Deposit", from, amount, rcv, tx.Hash)
|
|
|
|
}
|
|
|
|
|
2020-08-26 09:28:38 +00:00
|
|
|
// Deposit gas assets to this script-hash address in NeoFS balance contract.
|
2020-12-09 13:27:39 +00:00
|
|
|
func Deposit(from interop.Hash160, amount int, rcv interop.Hash160) bool {
|
2020-08-26 09:15:07 +00:00
|
|
|
if !runtime.CheckWitness(from) {
|
|
|
|
panic("deposit: you should be the owner of the wallet")
|
|
|
|
}
|
2020-05-28 14:04:32 +00:00
|
|
|
|
2020-10-26 15:26:04 +00:00
|
|
|
if amount > maxBalanceAmount {
|
|
|
|
panic("deposit: out of max amount limit")
|
|
|
|
}
|
|
|
|
|
2020-08-28 06:42:06 +00:00
|
|
|
if amount <= 0 {
|
|
|
|
return false
|
2020-08-26 09:15:07 +00:00
|
|
|
}
|
2020-08-28 06:42:06 +00:00
|
|
|
amount = amount * 100000000
|
2020-03-30 13:59:54 +00:00
|
|
|
|
2020-08-26 09:15:07 +00:00
|
|
|
to := runtime.GetExecutingScriptHash()
|
2020-05-28 14:04:32 +00:00
|
|
|
|
2021-02-09 11:55:58 +00:00
|
|
|
transferred := gas.Transfer(from, to, amount, rcv)
|
2020-08-26 09:15:07 +00:00
|
|
|
if !transferred {
|
|
|
|
panic("deposit: failed to transfer funds, aborting")
|
|
|
|
}
|
2020-03-30 13:59:54 +00:00
|
|
|
|
2020-08-26 09:15:07 +00:00
|
|
|
return true
|
|
|
|
}
|
2020-05-28 14:04:32 +00:00
|
|
|
|
2020-08-26 09:28:38 +00:00
|
|
|
// Withdraw initialize gas asset withdraw from NeoFS balance.
|
2021-04-20 14:44:36 +00:00
|
|
|
func Withdraw(user interop.Hash160, amount int) bool {
|
2020-08-26 09:15:07 +00:00
|
|
|
if !runtime.CheckWitness(user) {
|
|
|
|
panic("withdraw: you should be the owner of the wallet")
|
|
|
|
}
|
2020-05-28 14:04:32 +00:00
|
|
|
|
2020-08-26 09:19:40 +00:00
|
|
|
if amount < 0 {
|
|
|
|
panic("withdraw: non positive amount number")
|
2020-08-26 09:15:07 +00:00
|
|
|
}
|
2020-03-30 13:59:54 +00:00
|
|
|
|
2020-10-26 15:26:04 +00:00
|
|
|
if amount > maxBalanceAmount {
|
|
|
|
panic("withdraw: out of max amount limit")
|
|
|
|
}
|
|
|
|
|
2021-04-20 14:44:36 +00:00
|
|
|
ctx := storage.GetContext()
|
2021-04-29 13:15:29 +00:00
|
|
|
notaryDisabled := storage.Get(ctx, notaryDisabledKey).(bool)
|
|
|
|
|
|
|
|
// transfer fee to proxy contract to pay cheque invocation
|
2021-04-20 14:44:36 +00:00
|
|
|
fee := getConfig(ctx, withdrawFeeConfigKey).(int)
|
2020-08-26 09:19:40 +00:00
|
|
|
|
2021-04-29 13:15:29 +00:00
|
|
|
if notaryDisabled {
|
|
|
|
alphabet := getNodes(ctx, alphabetKey)
|
|
|
|
for _, node := range alphabet {
|
|
|
|
processingAddr := contract.CreateStandardAccount(node.PublicKey)
|
|
|
|
|
|
|
|
transferred := gas.Transfer(user, processingAddr, fee, []byte{})
|
|
|
|
if !transferred {
|
|
|
|
panic("withdraw: failed to transfer withdraw fee, aborting")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
processingAddr := storage.Get(ctx, processingContractKey).(interop.Hash160)
|
|
|
|
|
|
|
|
transferred := gas.Transfer(user, processingAddr, fee, []byte{})
|
|
|
|
if !transferred {
|
|
|
|
panic("withdraw: failed to transfer withdraw fee, aborting")
|
|
|
|
}
|
2021-04-20 14:44:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// notify alphabet nodes
|
|
|
|
amount = amount * 100000000
|
2020-08-26 09:15:07 +00:00
|
|
|
tx := runtime.GetScriptContainer()
|
2021-04-20 14:44:36 +00:00
|
|
|
|
2020-08-26 09:15:07 +00:00
|
|
|
runtime.Notify("Withdraw", user, amount, tx.Hash)
|
2020-07-15 16:31:34 +00:00
|
|
|
|
2020-08-26 09:15:07 +00:00
|
|
|
return true
|
|
|
|
}
|
2020-03-30 13:59:54 +00:00
|
|
|
|
2020-08-26 09:28:38 +00:00
|
|
|
// Cheque sends gas assets back to the user if they were successfully
|
|
|
|
// locked in NeoFS balance contract.
|
2021-03-04 19:21:49 +00:00
|
|
|
func Cheque(id []byte, user interop.Hash160, amount int, lockAcc []byte) bool {
|
2021-04-29 13:15:29 +00:00
|
|
|
ctx := storage.GetContext()
|
|
|
|
notaryDisabled := storage.Get(ctx, notaryDisabledKey).(bool)
|
|
|
|
|
|
|
|
var ( // for invocation collection without notary
|
|
|
|
alphabet []common.IRNode
|
|
|
|
nodeKey []byte
|
|
|
|
)
|
|
|
|
|
|
|
|
if notaryDisabled {
|
|
|
|
alphabet = getNodes(ctx, alphabetKey)
|
|
|
|
nodeKey = common.InnerRingInvoker(alphabet)
|
|
|
|
if len(nodeKey) == 0 {
|
|
|
|
panic("cheque: this method must be invoked by alphabet")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
multiaddr := AlphabetAddress()
|
|
|
|
if !runtime.CheckWitness(multiaddr) {
|
|
|
|
panic("cheque: this method must be invoked by alphabet")
|
|
|
|
}
|
2020-08-26 09:15:07 +00:00
|
|
|
}
|
2020-05-28 14:04:32 +00:00
|
|
|
|
2021-04-20 14:30:43 +00:00
|
|
|
from := runtime.GetExecutingScriptHash()
|
2020-07-16 08:02:19 +00:00
|
|
|
|
2021-04-29 13:15:29 +00:00
|
|
|
if notaryDisabled {
|
|
|
|
threshold := len(alphabet)*2/3 + 1
|
|
|
|
|
|
|
|
n := common.Vote(ctx, id, nodeKey)
|
|
|
|
if n < threshold {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
common.RemoveVotes(ctx, id)
|
|
|
|
}
|
|
|
|
|
2021-04-20 14:30:43 +00:00
|
|
|
transferred := gas.Transfer(from, user, amount, nil)
|
|
|
|
if !transferred {
|
|
|
|
panic("cheque: failed to transfer funds, aborting")
|
2020-08-26 09:15:07 +00:00
|
|
|
}
|
2020-07-16 08:02:19 +00:00
|
|
|
|
2021-04-20 14:30:43 +00:00
|
|
|
runtime.Log("cheque: funds have been transferred")
|
|
|
|
runtime.Notify("Cheque", id, user, amount, lockAcc)
|
|
|
|
|
2020-08-26 09:15:07 +00:00
|
|
|
return true
|
|
|
|
}
|
2020-07-16 08:02:19 +00:00
|
|
|
|
2020-08-26 09:28:38 +00:00
|
|
|
// Bind public key with user's account to use it in NeoFS requests.
|
2021-03-04 19:21:49 +00:00
|
|
|
func Bind(user []byte, keys []interop.PublicKey) bool {
|
2020-08-26 09:15:07 +00:00
|
|
|
if !runtime.CheckWitness(user) {
|
|
|
|
panic("binding: you should be the owner of the wallet")
|
|
|
|
}
|
2020-07-16 08:02:19 +00:00
|
|
|
|
2020-08-26 09:15:07 +00:00
|
|
|
for i := 0; i < len(keys); i++ {
|
2020-08-28 06:42:06 +00:00
|
|
|
pubKey := keys[i]
|
2020-08-26 09:15:07 +00:00
|
|
|
if len(pubKey) != publicKeySize {
|
|
|
|
panic("binding: incorrect public key size")
|
2020-07-16 14:23:52 +00:00
|
|
|
}
|
2020-08-26 09:15:07 +00:00
|
|
|
}
|
2020-03-30 13:59:54 +00:00
|
|
|
|
2020-08-26 09:15:07 +00:00
|
|
|
runtime.Notify("Bind", user, keys)
|
2020-05-27 12:23:44 +00:00
|
|
|
|
2020-08-26 09:15:07 +00:00
|
|
|
return true
|
|
|
|
}
|
2020-07-16 14:23:52 +00:00
|
|
|
|
2020-08-26 09:28:38 +00:00
|
|
|
// Unbind public key from user's account
|
2021-03-04 19:21:49 +00:00
|
|
|
func Unbind(user []byte, keys []interop.PublicKey) bool {
|
2020-08-26 09:15:07 +00:00
|
|
|
if !runtime.CheckWitness(user) {
|
|
|
|
panic("unbinding: you should be the owner of the wallet")
|
|
|
|
}
|
2020-07-16 14:23:52 +00:00
|
|
|
|
2020-08-26 09:15:07 +00:00
|
|
|
for i := 0; i < len(keys); i++ {
|
2020-08-28 06:42:06 +00:00
|
|
|
pubKey := keys[i]
|
2020-08-26 09:15:07 +00:00
|
|
|
if len(pubKey) != publicKeySize {
|
|
|
|
panic("unbinding: incorrect public key size")
|
2020-03-30 13:59:54 +00:00
|
|
|
}
|
2020-08-26 09:15:07 +00:00
|
|
|
}
|
2020-03-30 13:59:54 +00:00
|
|
|
|
2020-08-26 09:15:07 +00:00
|
|
|
runtime.Notify("Unbind", user, keys)
|
2020-03-30 13:59:54 +00:00
|
|
|
|
2020-08-26 09:15:07 +00:00
|
|
|
return true
|
|
|
|
}
|
2020-05-20 09:38:11 +00:00
|
|
|
|
2021-03-24 10:23:18 +00:00
|
|
|
// AlphabetUpdate updates list of alphabet nodes with provided list of
|
2020-08-26 09:28:38 +00:00
|
|
|
// public keys.
|
2021-04-29 13:15:29 +00:00
|
|
|
func AlphabetUpdate(id []byte, args []interop.PublicKey) bool {
|
2021-03-09 19:15:58 +00:00
|
|
|
ctx := storage.GetContext()
|
2021-04-29 13:15:29 +00:00
|
|
|
notaryDisabled := storage.Get(ctx, notaryDisabledKey).(bool)
|
2021-03-09 19:15:58 +00:00
|
|
|
|
2021-03-24 10:23:18 +00:00
|
|
|
if len(args) == 0 {
|
|
|
|
panic("alphabetUpdate: bad arguments")
|
2020-08-26 09:15:07 +00:00
|
|
|
}
|
2020-03-30 13:59:54 +00:00
|
|
|
|
2021-04-29 13:15:29 +00:00
|
|
|
var ( // for invocation collection without notary
|
|
|
|
alphabet []common.IRNode
|
|
|
|
nodeKey []byte
|
|
|
|
)
|
|
|
|
|
|
|
|
if notaryDisabled {
|
|
|
|
alphabet = getNodes(ctx, alphabetKey)
|
|
|
|
nodeKey = common.InnerRingInvoker(alphabet)
|
|
|
|
if len(nodeKey) == 0 {
|
|
|
|
panic("alphabetUpdate: this method must be invoked by alphabet")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
multiaddr := AlphabetAddress()
|
|
|
|
if !runtime.CheckWitness(multiaddr) {
|
|
|
|
panic("alphabetUpdate: this method must be invoked by alphabet")
|
|
|
|
}
|
2020-08-26 09:15:07 +00:00
|
|
|
}
|
2020-07-16 14:23:52 +00:00
|
|
|
|
2021-03-24 10:23:18 +00:00
|
|
|
newAlphabet := []common.IRNode{}
|
2020-05-22 08:06:12 +00:00
|
|
|
|
2020-12-14 07:55:30 +00:00
|
|
|
for i := 0; i < len(args); i++ {
|
2021-03-24 10:23:18 +00:00
|
|
|
pubKey := args[i]
|
|
|
|
if len(pubKey) != publicKeySize {
|
|
|
|
panic("alphabetUpdate: invalid public key in alphabet list")
|
2020-07-16 15:59:49 +00:00
|
|
|
}
|
|
|
|
|
2021-03-24 10:23:18 +00:00
|
|
|
newAlphabet = append(newAlphabet, common.IRNode{
|
|
|
|
PublicKey: pubKey,
|
|
|
|
})
|
2020-08-26 09:15:07 +00:00
|
|
|
}
|
2020-07-16 15:59:49 +00:00
|
|
|
|
2021-04-29 13:15:29 +00:00
|
|
|
if notaryDisabled {
|
|
|
|
threshold := len(alphabet)*2/3 + 1
|
|
|
|
|
|
|
|
n := common.Vote(ctx, id, nodeKey)
|
|
|
|
if n < threshold {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
common.RemoveVotes(ctx, id)
|
|
|
|
}
|
|
|
|
|
2021-04-20 14:30:43 +00:00
|
|
|
common.SetSerialized(ctx, alphabetKey, newAlphabet)
|
2020-07-16 15:59:49 +00:00
|
|
|
|
2021-04-29 13:15:29 +00:00
|
|
|
runtime.Notify("AlphabetUpdate", id, newAlphabet)
|
2021-04-20 14:30:43 +00:00
|
|
|
runtime.Log("alphabetUpdate: alphabet list has been updated")
|
2020-07-16 15:59:49 +00:00
|
|
|
|
2020-08-26 09:15:07 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-08-26 09:28:38 +00:00
|
|
|
// Config returns value of NeoFS configuration with provided key.
|
2020-08-26 09:15:07 +00:00
|
|
|
func Config(key []byte) interface{} {
|
2021-03-09 19:15:58 +00:00
|
|
|
ctx := storage.GetReadOnlyContext()
|
2020-08-26 09:15:07 +00:00
|
|
|
return getConfig(ctx, key)
|
|
|
|
}
|
2020-07-16 15:59:49 +00:00
|
|
|
|
2020-08-26 09:28:38 +00:00
|
|
|
// SetConfig key-value pair as a NeoFS runtime configuration value.
|
2020-08-26 09:15:07 +00:00
|
|
|
func SetConfig(id, key, val []byte) bool {
|
2021-03-09 19:15:58 +00:00
|
|
|
ctx := storage.GetContext()
|
2021-04-29 13:15:29 +00:00
|
|
|
notaryDisabled := storage.Get(ctx, notaryDisabledKey).(bool)
|
|
|
|
|
|
|
|
var ( // for invocation collection without notary
|
|
|
|
alphabet []common.IRNode
|
|
|
|
nodeKey []byte
|
|
|
|
)
|
|
|
|
|
|
|
|
if notaryDisabled {
|
|
|
|
alphabet = getNodes(ctx, alphabetKey)
|
|
|
|
nodeKey = common.InnerRingInvoker(alphabet)
|
|
|
|
if len(key) == 0 {
|
|
|
|
panic("setConfig: this method must be invoked by alphabet")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
multiaddr := AlphabetAddress()
|
|
|
|
if !runtime.CheckWitness(multiaddr) {
|
|
|
|
panic("setConfig: this method must be invoked by alphabet")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if notaryDisabled {
|
|
|
|
threshold := len(alphabet)*2/3 + 1
|
|
|
|
|
|
|
|
n := common.Vote(ctx, id, nodeKey)
|
|
|
|
if n < threshold {
|
|
|
|
return true
|
|
|
|
}
|
2021-03-09 19:15:58 +00:00
|
|
|
|
2021-04-29 13:15:29 +00:00
|
|
|
common.RemoveVotes(ctx, id)
|
2020-08-26 09:15:07 +00:00
|
|
|
}
|
2020-07-16 15:59:49 +00:00
|
|
|
|
2021-04-20 14:30:43 +00:00
|
|
|
setConfig(ctx, key, val)
|
2020-07-16 15:59:49 +00:00
|
|
|
|
2021-04-20 14:30:43 +00:00
|
|
|
runtime.Notify("SetConfig", id, key, val)
|
|
|
|
runtime.Log("setConfig: configuration has been updated")
|
2020-07-16 15:59:49 +00:00
|
|
|
|
2020-08-26 09:15:07 +00:00
|
|
|
return true
|
|
|
|
}
|
2020-07-16 15:59:49 +00:00
|
|
|
|
2020-08-26 09:28:38 +00:00
|
|
|
// ListConfig returns array of all key-value pairs of NeoFS configuration.
|
2020-08-26 09:15:07 +00:00
|
|
|
func ListConfig() []record {
|
2021-03-09 19:15:58 +00:00
|
|
|
ctx := storage.GetReadOnlyContext()
|
|
|
|
|
2020-08-26 09:15:07 +00:00
|
|
|
var config []record
|
2020-07-16 15:59:49 +00:00
|
|
|
|
2021-02-08 15:23:08 +00:00
|
|
|
it := storage.Find(ctx, configPrefix, storage.None)
|
2020-08-26 09:15:07 +00:00
|
|
|
for iterator.Next(it) {
|
2021-02-08 15:23:08 +00:00
|
|
|
pair := iterator.Value(it).([]interface{})
|
|
|
|
key := pair[0].([]byte)
|
|
|
|
val := pair[1].([]byte)
|
2020-08-26 09:15:07 +00:00
|
|
|
r := record{key: key[len(configPrefix):], val: val}
|
|
|
|
|
|
|
|
config = append(config, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
return config
|
|
|
|
}
|
|
|
|
|
2020-08-26 09:28:38 +00:00
|
|
|
// InitConfig set up initial NeoFS key-value configuration.
|
2020-08-28 06:42:06 +00:00
|
|
|
func InitConfig(args [][]byte) bool {
|
2021-03-09 19:15:58 +00:00
|
|
|
ctx := storage.GetContext()
|
|
|
|
|
2020-08-26 09:15:07 +00:00
|
|
|
if getConfig(ctx, candidateFeeConfigKey) != nil {
|
|
|
|
panic("neofs: configuration already installed")
|
|
|
|
}
|
|
|
|
|
|
|
|
ln := len(args)
|
|
|
|
if ln%2 != 0 {
|
|
|
|
panic("initConfig: bad arguments")
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < ln/2; i++ {
|
|
|
|
key := args[i*2]
|
|
|
|
val := args[i*2+1]
|
2020-07-16 15:59:49 +00:00
|
|
|
|
2020-08-26 09:15:07 +00:00
|
|
|
setConfig(ctx, key, val)
|
2020-03-30 13:59:54 +00:00
|
|
|
}
|
|
|
|
|
2020-08-26 09:15:07 +00:00
|
|
|
runtime.Log("neofs: config has been installed")
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-08-26 09:28:38 +00:00
|
|
|
// Version of contract.
|
2020-08-26 09:15:07 +00:00
|
|
|
func Version() int {
|
|
|
|
return version
|
2020-03-30 13:59:54 +00:00
|
|
|
}
|
|
|
|
|
2021-03-24 10:23:18 +00:00
|
|
|
// getNodes returns deserialized slice of nodes from storage.
|
|
|
|
func getNodes(ctx storage.Context, key string) []common.IRNode {
|
2020-07-16 12:06:49 +00:00
|
|
|
data := storage.Get(ctx, key)
|
2020-07-15 16:26:24 +00:00
|
|
|
if data != nil {
|
2021-03-12 12:16:36 +00:00
|
|
|
return std.Deserialize(data.([]byte)).([]common.IRNode)
|
2020-07-15 16:26:24 +00:00
|
|
|
}
|
|
|
|
|
2021-02-02 18:26:34 +00:00
|
|
|
return []common.IRNode{}
|
2020-07-15 16:26:24 +00:00
|
|
|
}
|
|
|
|
|
2020-07-16 15:59:49 +00:00
|
|
|
// getConfig returns installed neofs configuration value or nil if it is not set.
|
|
|
|
func getConfig(ctx storage.Context, key interface{}) interface{} {
|
|
|
|
postfix := key.([]byte)
|
|
|
|
storageKey := append(configPrefix, postfix...)
|
|
|
|
|
|
|
|
return storage.Get(ctx, storageKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
// setConfig sets neofs configuration value in the contract storage.
|
|
|
|
func setConfig(ctx storage.Context, key, val interface{}) {
|
|
|
|
postfix := key.([]byte)
|
|
|
|
storageKey := append(configPrefix, postfix...)
|
|
|
|
|
|
|
|
storage.Put(ctx, storageKey, val)
|
|
|
|
}
|
|
|
|
|
2020-07-16 12:06:49 +00:00
|
|
|
// addNode returns slice of nodes with appended node 'n' and bool flag
|
|
|
|
// that set to false if node 'n' is already presented in the slice 'lst'.
|
2021-02-02 18:26:34 +00:00
|
|
|
func addNode(lst []common.IRNode, n common.IRNode) ([]common.IRNode, bool) {
|
2020-07-16 12:06:49 +00:00
|
|
|
for i := 0; i < len(lst); i++ {
|
2021-02-02 18:26:34 +00:00
|
|
|
if common.BytesEqual(n.PublicKey, lst[i].PublicKey) {
|
2020-07-16 12:06:49 +00:00
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lst = append(lst, n)
|
2020-07-20 13:09:40 +00:00
|
|
|
|
2020-07-16 12:06:49 +00:00
|
|
|
return lst, true
|
|
|
|
}
|
|
|
|
|
2021-04-20 14:27:15 +00:00
|
|
|
// multiaddress returns multi signature address from list of IRNode structures
|
|
|
|
// with m = 2/3n+1.
|
|
|
|
func multiaddress(n []common.IRNode) []byte {
|
|
|
|
threshold := len(n)*2/3 + 1
|
|
|
|
|
|
|
|
keys := []interop.PublicKey{}
|
|
|
|
for _, node := range n {
|
|
|
|
key := node.PublicKey
|
|
|
|
keys = append(keys, key)
|
|
|
|
}
|
|
|
|
|
|
|
|
return contract.CreateMultisigAccount(threshold, keys)
|
|
|
|
}
|