2021-07-04 11:00:56 +00:00
|
|
|
package neofs
|
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-09-20 15:41:46 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/native/ledger"
|
2021-02-11 15:55:32 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/native/management"
|
2021-09-20 15:41:46 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/native/roles"
|
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
|
|
|
|
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
|
|
|
|
2021-06-30 10:59:32 +00:00
|
|
|
maxBalanceAmount = 9000 // Max integer of Fixed12 in JSON bound (2**53-1)
|
2021-05-17 06:30:39 +00:00
|
|
|
maxBalanceAmountGAS = maxBalanceAmount * 1_0000_0000
|
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-05-12 08:31:07 +00:00
|
|
|
// _deploy sets up initial alphabet node keys.
|
|
|
|
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)
|
2021-09-20 15:41:46 +00:00
|
|
|
addrProc := args[1].(interop.Hash160)
|
|
|
|
keys := args[2].([]interop.PublicKey)
|
|
|
|
config := args[3].([][]byte)
|
2021-05-12 08:31:07 +00:00
|
|
|
|
2021-03-09 19:15:58 +00:00
|
|
|
ctx := storage.GetContext()
|
|
|
|
|
2021-02-02 18:26:34 +00:00
|
|
|
var irList []common.IRNode
|
2020-03-30 13:59:54 +00:00
|
|
|
|
2021-05-12 08:31:07 +00:00
|
|
|
if len(keys) == 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")
|
|
|
|
}
|
|
|
|
|
2021-05-12 08:31:07 +00:00
|
|
|
for i := 0; i < len(keys); i++ {
|
|
|
|
pub := keys[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-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
|
|
|
}
|
|
|
|
|
2021-07-29 12:02:25 +00:00
|
|
|
ln := len(config)
|
|
|
|
if ln%2 != 0 {
|
|
|
|
panic("init: bad configuration")
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < ln/2; i++ {
|
|
|
|
key := config[i*2]
|
|
|
|
val := config[i*2+1]
|
|
|
|
|
|
|
|
setConfig(ctx, key, val)
|
|
|
|
}
|
|
|
|
|
2020-08-26 09:15:07 +00:00
|
|
|
runtime.Log("neofs: contract initialized")
|
|
|
|
}
|
2020-07-16 12:06:49 +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 side chain 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
|
|
|
blockHeight := ledger.CurrentIndex()
|
|
|
|
alphabetKeys := roles.GetDesignatedByRole(roles.NeoFSAlphabet, uint32(blockHeight))
|
|
|
|
alphabetCommittee := common.Multiaddress(alphabetKeys, true)
|
2021-03-09 19:15:58 +00:00
|
|
|
|
2021-09-20 15:41:46 +00:00
|
|
|
if !runtime.CheckWitness(alphabetCommittee) {
|
|
|
|
panic("only side chain 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("neofs contract updated")
|
|
|
|
}
|
|
|
|
|
2021-07-16 06:03:34 +00:00
|
|
|
// AlphabetList returns array of alphabet node keys. Use in side chain notary
|
|
|
|
// disabled environment.
|
2021-03-24 10:23:18 +00:00
|
|
|
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.
|
2021-07-16 06:03:34 +00:00
|
|
|
// Used in side chain notary disabled environment.
|
2021-04-20 14:27:15 +00:00
|
|
|
func AlphabetAddress() interop.Hash160 {
|
|
|
|
ctx := storage.GetReadOnlyContext()
|
|
|
|
return multiaddress(getNodes(ctx, alphabetKey))
|
|
|
|
}
|
|
|
|
|
2021-07-16 06:03:34 +00:00
|
|
|
// InnerRingCandidates returns array of structures that contain Inner Ring
|
|
|
|
// candidate node key.
|
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
|
|
|
|
2021-07-16 06:03:34 +00:00
|
|
|
// InnerRingCandidateRemove removes key from the list of Inner Ring candidates.
|
|
|
|
// Can be invoked by Alphabet nodes or candidate itself.
|
|
|
|
//
|
|
|
|
// Method does not return fee back to the candidate.
|
2021-05-21 11:37:31 +00:00
|
|
|
func InnerRingCandidateRemove(key interop.PublicKey) {
|
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 {
|
2021-05-21 11:37:31 +00:00
|
|
|
return
|
2021-04-29 13:15:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
common.RemoveVotes(ctx, hashID)
|
|
|
|
}
|
|
|
|
|
2021-02-02 17:36:20 +00:00
|
|
|
common.SetSerialized(ctx, candidatesKey, nodes)
|
2020-08-26 09:15:07 +00:00
|
|
|
}
|
2020-03-30 13:59:54 +00:00
|
|
|
|
2021-07-16 06:03:34 +00:00
|
|
|
// InnerRingCandidateAdd adds key to the list of Inner Ring candidates.
|
|
|
|
// Can be invoked only by candidate itself.
|
|
|
|
//
|
|
|
|
// This method transfers fee from candidate to contract account.
|
|
|
|
// Fee value specified in NeoFS network config with the key InnerRingCandidateFee.
|
2021-05-21 11:37:31 +00:00
|
|
|
func InnerRingCandidateAdd(key interop.PublicKey) {
|
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-08-26 09:15:07 +00:00
|
|
|
}
|
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.
|
2021-07-16 06:03:34 +00:00
|
|
|
// It takes no more than 9000.0 GAS. Native GAS has precision 8 and
|
|
|
|
// NeoFS balance contract has precision 12. Values bigger than 9000.0 can
|
|
|
|
// break JSON limits for integers when precision is converted.
|
2021-02-08 15:14:03 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-05-17 06:30:39 +00:00
|
|
|
if amount <= 0 {
|
|
|
|
panic("onNEP17Payment: amount must be positive")
|
|
|
|
} else if maxBalanceAmountGAS < amount {
|
|
|
|
panic("onNEP17Payment: out of max amount limit")
|
|
|
|
}
|
|
|
|
|
2020-12-09 13:27:39 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2021-07-16 06:03:34 +00:00
|
|
|
// Withdraw initialize gas asset withdraw from NeoFS. Can be invoked only
|
|
|
|
// by the specified user.
|
|
|
|
//
|
|
|
|
// This method produces Withdraw notification to lock assets in side chain and
|
|
|
|
// transfers withdraw fee from user account to each Alphabet node. If notary
|
|
|
|
// is enabled in main chain, fee is transferred to Processing contract.
|
|
|
|
// Fee value specified in NeoFS network config with the key WithdrawFee.
|
2021-05-21 11:37:31 +00:00
|
|
|
func Withdraw(user interop.Hash160, amount int) {
|
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-03-30 13:59:54 +00:00
|
|
|
|
2021-07-16 06:03:34 +00:00
|
|
|
// Cheque transfers GAS back to the user from contract account, if assets were
|
|
|
|
// successfully locked in NeoFS balance contract. Can be invoked only by
|
|
|
|
// Alphabet nodes.
|
|
|
|
//
|
|
|
|
// This method produces Cheque notification to burn assets in side chain.
|
2021-05-21 11:37:31 +00:00
|
|
|
func Cheque(id []byte, user interop.Hash160, amount int, lockAcc []byte) {
|
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 {
|
2021-05-21 11:37:31 +00:00
|
|
|
return
|
2021-04-29 13:15:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
2020-07-16 08:02:19 +00:00
|
|
|
|
2021-07-16 06:03:34 +00:00
|
|
|
// Bind method produces notification to bind specified public keys in NeoFSID
|
|
|
|
// contract in side chain. Can be invoked only by specified user.
|
|
|
|
//
|
|
|
|
// This method produces Bind notification. Method panics if keys are not
|
|
|
|
// 33 byte long. User argument must be valid 20 byte script hash.
|
2021-05-21 11:37:31 +00:00
|
|
|
func Bind(user []byte, keys []interop.PublicKey) {
|
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-07-16 14:23:52 +00:00
|
|
|
|
2021-07-16 06:03:34 +00:00
|
|
|
// Unbind method produces notification to unbind specified public keys in NeoFSID
|
|
|
|
// contract in side chain. Can be invoked only by specified user.
|
|
|
|
//
|
|
|
|
// This method produces Unbind notification. Method panics if keys are not
|
|
|
|
// 33 byte long. User argument must be valid 20 byte script hash.
|
2021-05-21 11:37:31 +00:00
|
|
|
func Unbind(user []byte, keys []interop.PublicKey) {
|
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-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
|
2021-07-16 06:03:34 +00:00
|
|
|
// public keys. Can be invoked only by alphabet nodes.
|
|
|
|
//
|
|
|
|
// This method used in notary disabled side chain environment. In this case
|
|
|
|
// actual alphabet list should be stored in the NeoFS contract.
|
2021-05-21 11:37:31 +00:00
|
|
|
func AlphabetUpdate(id []byte, args []interop.PublicKey) {
|
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 {
|
2021-05-21 11:37:31 +00:00
|
|
|
return
|
2021-04-29 13:15:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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-08-26 09:15:07 +00:00
|
|
|
}
|
|
|
|
|
2021-07-16 06:03:34 +00:00
|
|
|
// Config returns configuration value of NeoFS configuration. If key does
|
|
|
|
// not exists, returns nil.
|
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
|
|
|
|
2021-07-16 06:03:34 +00:00
|
|
|
// SetConfig key-value pair as a NeoFS runtime configuration value. Can be invoked
|
|
|
|
// only by Alphabet nodes.
|
2021-05-21 11:37:31 +00:00
|
|
|
func SetConfig(id, key, val []byte) {
|
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 {
|
2021-05-21 11:37:31 +00:00
|
|
|
return
|
2021-04-29 13:15:29 +00:00
|
|
|
}
|
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-08-26 09:15:07 +00:00
|
|
|
}
|
2020-07-16 15:59:49 +00:00
|
|
|
|
2021-07-16 06:03:34 +00:00
|
|
|
// ListConfig returns array of structures that contain key and value of all
|
|
|
|
// NeoFS configuration records. Key and value are both byte arrays.
|
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
|
|
|
|
}
|
|
|
|
|
2021-07-16 06:03:34 +00:00
|
|
|
// Version returns version of the contract.
|
2020-08-26 09:15:07 +00:00
|
|
|
func Version() int {
|
2021-07-29 11:44:53 +00:00
|
|
|
return common.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)
|
|
|
|
}
|