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:
|
|
|
|
- InnerRingList
|
|
|
|
- InnerRingCandidates
|
|
|
|
- IsInnerRing
|
|
|
|
- InnerRingCandidateAdd
|
|
|
|
- InnerRingCandidateRemove
|
|
|
|
- InnerRingUpdate
|
|
|
|
|
|
|
|
Config methods:
|
|
|
|
- Config
|
|
|
|
- ListConfig
|
|
|
|
- SetConfig
|
|
|
|
|
|
|
|
Other utility methods:
|
|
|
|
- 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-09 13:38:53 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/binary"
|
2020-07-16 12:06:49 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/contract"
|
2020-03-30 13:59:54 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/crypto"
|
2020-07-16 15:59:49 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/iterator"
|
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"
|
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-15 16:17:34 +00:00
|
|
|
cheque struct {
|
2020-05-28 14:04:32 +00:00
|
|
|
id []byte
|
2020-05-27 12:23:44 +00:00
|
|
|
}
|
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
|
|
|
defaultCandidateFee = 100 * 1_0000_0000 // 100 Fixed8 Gas
|
|
|
|
candidateFeeConfigKey = "InnerRingCandidateFee"
|
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
|
|
|
|
|
|
|
innerRingKey = "innerring"
|
|
|
|
candidatesKey = "candidates"
|
|
|
|
cashedChequesKey = "cheques"
|
|
|
|
|
|
|
|
publicKeySize = 33
|
|
|
|
minInnerRingSize = 3
|
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
|
|
|
|
2020-08-26 09:28:38 +00:00
|
|
|
// Init set up initial inner ring node keys.
|
2021-03-04 19:21:49 +00:00
|
|
|
func Init(owner interop.PublicKey, 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
|
|
|
|
2020-09-17 08:08:41 +00:00
|
|
|
if len(args) < 3 {
|
|
|
|
panic("neofs: at least three inner ring keys must be provided")
|
|
|
|
}
|
|
|
|
|
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-02-02 17:36:20 +00:00
|
|
|
common.SetSerialized(ctx, innerRingKey, irList)
|
|
|
|
common.InitVote(ctx)
|
2021-02-02 18:26:34 +00:00
|
|
|
common.SetSerialized(ctx, candidatesKey, []common.IRNode{})
|
2021-02-02 17:36:20 +00:00
|
|
|
common.SetSerialized(ctx, cashedChequesKey, []cheque{})
|
2020-03-30 13:59:54 +00:00
|
|
|
|
2021-02-11 15:55:32 +00:00
|
|
|
storage.Put(ctx, common.OwnerKey, owner)
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-08-26 09:28:38 +00:00
|
|
|
// InnerRingList returns array of inner ring node keys.
|
2021-02-02 18:26:34 +00:00
|
|
|
func InnerRingList() []common.IRNode {
|
2021-03-09 19:15:58 +00:00
|
|
|
ctx := storage.GetReadOnlyContext()
|
2020-08-26 09:15:07 +00:00
|
|
|
return getInnerRingNodes(ctx, innerRingKey)
|
|
|
|
}
|
2020-03-30 13:59:54 +00:00
|
|
|
|
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()
|
2020-08-26 09:15:07 +00:00
|
|
|
return getInnerRingNodes(ctx, candidatesKey)
|
|
|
|
}
|
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()
|
|
|
|
|
2020-08-26 09:15:07 +00:00
|
|
|
if !runtime.CheckWitness(key) {
|
|
|
|
panic("irCandidateRemove: you should be the owner of the public key")
|
|
|
|
}
|
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
|
2020-08-26 09:15:07 +00:00
|
|
|
candidates := getInnerRingNodes(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-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) {
|
|
|
|
panic("irCandidateAdd: you should be the owner of the public key")
|
|
|
|
}
|
2020-03-30 13:59:54 +00:00
|
|
|
|
2021-02-02 18:26:34 +00:00
|
|
|
c := common.IRNode{PublicKey: key}
|
2020-08-26 09:15:07 +00:00
|
|
|
candidates := getInnerRingNodes(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.
|
2020-08-26 09:15:07 +00:00
|
|
|
func Withdraw(user []byte, amount int) bool {
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2020-08-26 09:19:40 +00:00
|
|
|
amount = amount * 100000000
|
|
|
|
|
2020-08-26 09:15:07 +00:00
|
|
|
tx := runtime.GetScriptContainer()
|
|
|
|
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-03-09 19:15:58 +00:00
|
|
|
ctx := storage.GetContext()
|
2020-08-26 09:15:07 +00:00
|
|
|
irList := getInnerRingNodes(ctx, innerRingKey)
|
|
|
|
threshold := len(irList)/3*2 + 1
|
2020-05-28 14:04:32 +00:00
|
|
|
|
2020-08-26 09:15:07 +00:00
|
|
|
cashedCheques := getCashedCheques(ctx)
|
|
|
|
hashID := crypto.SHA256(id)
|
2020-05-28 14:04:32 +00:00
|
|
|
|
2021-02-02 18:26:34 +00:00
|
|
|
irKey := common.InnerRingInvoker(irList)
|
2020-08-26 09:15:07 +00:00
|
|
|
if len(irKey) == 0 {
|
|
|
|
panic("cheque: invoked by non inner ring node")
|
|
|
|
}
|
2020-05-28 14:04:32 +00:00
|
|
|
|
2020-08-26 09:15:07 +00:00
|
|
|
c := cheque{id: id}
|
2020-07-15 16:31:34 +00:00
|
|
|
|
2020-08-26 09:15:07 +00:00
|
|
|
list, ok := addCheque(cashedCheques, c)
|
|
|
|
if !ok {
|
|
|
|
panic("cheque: non unique id")
|
|
|
|
}
|
2020-03-30 13:59:54 +00:00
|
|
|
|
2021-02-02 17:36:20 +00:00
|
|
|
n := common.Vote(ctx, hashID, irKey)
|
2020-08-26 09:15:07 +00:00
|
|
|
if n >= threshold {
|
2021-02-02 17:36:20 +00:00
|
|
|
common.RemoveVotes(ctx, hashID)
|
2020-08-26 09:15:07 +00:00
|
|
|
|
|
|
|
from := runtime.GetExecutingScriptHash()
|
2020-07-16 08:02:19 +00:00
|
|
|
|
2021-02-09 11:55:58 +00:00
|
|
|
transferred := gas.Transfer(from, user, amount, nil)
|
2020-08-26 09:15:07 +00:00
|
|
|
if !transferred {
|
|
|
|
panic("cheque: failed to transfer funds, aborting")
|
2020-07-16 08:02:19 +00:00
|
|
|
}
|
|
|
|
|
2020-08-26 09:15:07 +00:00
|
|
|
runtime.Log("cheque: funds have been transferred")
|
2020-07-20 13:09:40 +00:00
|
|
|
|
2021-02-02 17:36:20 +00:00
|
|
|
common.SetSerialized(ctx, cashedChequesKey, list)
|
2020-08-26 09:15:07 +00:00
|
|
|
runtime.Notify("Cheque", id, user, amount, lockAcc)
|
|
|
|
}
|
2020-07-16 08:02:19 +00:00
|
|
|
|
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
|
|
|
|
2020-08-26 09:28:38 +00:00
|
|
|
// InnerRingUpdate updates list of inner ring nodes with provided list of
|
|
|
|
// public keys.
|
2021-03-04 19:21:49 +00:00
|
|
|
func InnerRingUpdate(chequeID []byte, args []interop.PublicKey) bool {
|
2021-03-09 19:15:58 +00:00
|
|
|
ctx := storage.GetContext()
|
|
|
|
|
2020-08-26 09:15:07 +00:00
|
|
|
if len(args) < minInnerRingSize {
|
|
|
|
panic("irUpdate: bad arguments")
|
|
|
|
}
|
2020-03-30 13:59:54 +00:00
|
|
|
|
2020-08-26 09:15:07 +00:00
|
|
|
irList := getInnerRingNodes(ctx, innerRingKey)
|
|
|
|
threshold := len(irList)/3*2 + 1
|
2020-07-16 14:23:52 +00:00
|
|
|
|
2021-02-02 18:26:34 +00:00
|
|
|
irKey := common.InnerRingInvoker(irList)
|
2020-08-26 09:15:07 +00:00
|
|
|
if len(irKey) == 0 {
|
|
|
|
panic("innerRingUpdate: invoked by non inner ring node")
|
|
|
|
}
|
2020-07-16 14:23:52 +00:00
|
|
|
|
2020-08-26 09:15:07 +00:00
|
|
|
c := cheque{id: chequeID}
|
2020-03-30 13:59:54 +00:00
|
|
|
|
2020-08-26 09:15:07 +00:00
|
|
|
cashedCheques := getCashedCheques(ctx)
|
2020-05-27 12:23:44 +00:00
|
|
|
|
2020-08-26 09:15:07 +00:00
|
|
|
chequesList, ok := addCheque(cashedCheques, c)
|
|
|
|
if !ok {
|
|
|
|
panic("irUpdate: non unique chequeID")
|
|
|
|
}
|
2020-03-30 13:59:54 +00:00
|
|
|
|
2020-08-26 09:15:07 +00:00
|
|
|
oldNodes := 0
|
|
|
|
candidates := getInnerRingNodes(ctx, candidatesKey)
|
2021-02-02 18:26:34 +00:00
|
|
|
newIR := []common.IRNode{}
|
2020-05-22 08:06:12 +00:00
|
|
|
|
2020-08-26 09:15:07 +00:00
|
|
|
loop:
|
2020-12-14 07:55:30 +00:00
|
|
|
for i := 0; i < len(args); i++ {
|
2020-08-26 09:15:07 +00:00
|
|
|
key := args[i]
|
2020-07-20 13:09:40 +00:00
|
|
|
if len(key) != publicKeySize {
|
2020-08-26 09:15:07 +00:00
|
|
|
panic("irUpdate: invalid public key in inner ring list")
|
2020-05-22 08:06:12 +00:00
|
|
|
}
|
|
|
|
|
2020-08-26 09:15:07 +00:00
|
|
|
// find key in actual inner ring list
|
|
|
|
for j := 0; j < len(irList); j++ {
|
|
|
|
n := irList[j]
|
2021-02-02 18:26:34 +00:00
|
|
|
if common.BytesEqual(n.PublicKey, key) {
|
2020-08-26 09:15:07 +00:00
|
|
|
newIR = append(newIR, n)
|
|
|
|
oldNodes++
|
2020-05-22 08:06:12 +00:00
|
|
|
|
2020-08-26 09:15:07 +00:00
|
|
|
continue loop
|
2020-05-22 08:06:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-26 09:15:07 +00:00
|
|
|
// find key in candidates list
|
|
|
|
candidates, newIR, ok = rmNodeByKey(candidates, newIR, key)
|
|
|
|
if !ok {
|
|
|
|
panic("irUpdate: unknown public key in inner ring list")
|
2020-07-16 15:59:49 +00:00
|
|
|
}
|
2020-08-26 09:15:07 +00:00
|
|
|
}
|
2020-07-16 15:59:49 +00:00
|
|
|
|
2020-08-26 09:15:07 +00:00
|
|
|
if oldNodes < len(newIR)*2/3+1 {
|
|
|
|
panic("irUpdate: inner ring change rate must not be more than 1/3 ")
|
|
|
|
}
|
2020-07-16 15:59:49 +00:00
|
|
|
|
2020-08-26 09:15:07 +00:00
|
|
|
hashID := crypto.SHA256(chequeID)
|
2020-07-16 15:59:49 +00:00
|
|
|
|
2021-02-02 17:36:20 +00:00
|
|
|
n := common.Vote(ctx, hashID, irKey)
|
2020-08-26 09:15:07 +00:00
|
|
|
if n >= threshold {
|
2021-02-02 17:36:20 +00:00
|
|
|
common.RemoveVotes(ctx, hashID)
|
2020-07-16 15:59:49 +00:00
|
|
|
|
2021-02-02 17:36:20 +00:00
|
|
|
common.SetSerialized(ctx, candidatesKey, candidates)
|
|
|
|
common.SetSerialized(ctx, innerRingKey, newIR)
|
|
|
|
common.SetSerialized(ctx, cashedChequesKey, chequesList)
|
2020-07-16 15:59:49 +00:00
|
|
|
|
2020-08-26 09:15:07 +00:00
|
|
|
runtime.Notify("InnerRingUpdate", c.id, newIR)
|
|
|
|
runtime.Log("irUpdate: inner ring 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
|
|
|
// IsInnerRing returns 'true' if key is inside of inner ring list.
|
2020-08-26 09:15:07 +00:00
|
|
|
func IsInnerRing(key []byte) bool {
|
2021-03-09 19:15:58 +00:00
|
|
|
ctx := storage.GetReadOnlyContext()
|
|
|
|
|
2020-08-26 09:15:07 +00:00
|
|
|
if len(key) != publicKeySize {
|
|
|
|
panic("isInnerRing: incorrect public key")
|
|
|
|
}
|
|
|
|
|
|
|
|
irList := getInnerRingNodes(ctx, innerRingKey)
|
|
|
|
for i := range irList {
|
|
|
|
node := irList[i]
|
|
|
|
|
2021-02-02 18:26:34 +00:00
|
|
|
if common.BytesEqual(node.PublicKey, key) {
|
2020-08-26 09:15:07 +00:00
|
|
|
return true
|
2020-07-16 15:59:49 +00:00
|
|
|
}
|
2020-08-26 09:15:07 +00:00
|
|
|
}
|
2020-07-16 15:59:49 +00:00
|
|
|
|
2020-08-26 09:15:07 +00:00
|
|
|
return false
|
|
|
|
}
|
2020-07-16 15:59:49 +00:00
|
|
|
|
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()
|
|
|
|
|
2020-08-26 09:15:07 +00:00
|
|
|
// check if it is inner ring invocation
|
|
|
|
irList := getInnerRingNodes(ctx, innerRingKey)
|
|
|
|
threshold := len(irList)/3*2 + 1
|
2020-07-16 15:59:49 +00:00
|
|
|
|
2021-02-02 18:26:34 +00:00
|
|
|
irKey := common.InnerRingInvoker(irList)
|
2020-08-26 09:15:07 +00:00
|
|
|
if len(irKey) == 0 {
|
|
|
|
panic("setConfig: invoked by non inner ring node")
|
|
|
|
}
|
2020-07-16 15:59:49 +00:00
|
|
|
|
2020-08-26 09:15:07 +00:00
|
|
|
// check unique id of the operation
|
|
|
|
c := cheque{id: id}
|
|
|
|
cashedCheques := getCashedCheques(ctx)
|
2020-07-16 15:59:49 +00:00
|
|
|
|
2020-08-26 09:15:07 +00:00
|
|
|
chequesList, ok := addCheque(cashedCheques, c)
|
|
|
|
if !ok {
|
|
|
|
panic("setConfig: non unique id")
|
|
|
|
}
|
2020-07-16 15:59:49 +00:00
|
|
|
|
2020-08-26 09:15:07 +00:00
|
|
|
// vote for new configuration value
|
|
|
|
hashID := crypto.SHA256(id)
|
2020-07-16 15:59:49 +00:00
|
|
|
|
2021-02-02 17:36:20 +00:00
|
|
|
n := common.Vote(ctx, hashID, irKey)
|
2020-08-26 09:15:07 +00:00
|
|
|
if n >= threshold {
|
2021-02-02 17:36:20 +00:00
|
|
|
common.RemoveVotes(ctx, hashID)
|
2020-07-16 15:59:49 +00:00
|
|
|
|
2020-08-26 09:15:07 +00:00
|
|
|
setConfig(ctx, key, val)
|
2021-02-02 17:36:20 +00:00
|
|
|
common.SetSerialized(ctx, cashedChequesKey, chequesList)
|
2020-07-16 15:59:49 +00:00
|
|
|
|
2020-08-26 09:15:07 +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")
|
|
|
|
}
|
|
|
|
|
|
|
|
setConfig(ctx, candidateFeeConfigKey, defaultCandidateFee)
|
2020-07-16 15:59:49 +00:00
|
|
|
|
2020-08-26 09:15:07 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-07-15 16:26:24 +00:00
|
|
|
// getInnerRingNodes returns deserialized slice of inner ring nodes from storage.
|
2021-02-02 18:26:34 +00:00
|
|
|
func getInnerRingNodes(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-02-02 18:26:34 +00:00
|
|
|
return binary.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-15 16:31:34 +00:00
|
|
|
// getInnerRingNodes returns deserialized slice of used cheques.
|
|
|
|
func getCashedCheques(ctx storage.Context) []cheque {
|
|
|
|
data := storage.Get(ctx, cashedChequesKey)
|
|
|
|
if data != nil {
|
|
|
|
return binary.Deserialize(data.([]byte)).([]cheque)
|
|
|
|
}
|
|
|
|
|
|
|
|
return []cheque{}
|
|
|
|
}
|
|
|
|
|
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-15 16:31:34 +00:00
|
|
|
// addCheque returns slice of cheques with appended cheque 'c' and bool flag
|
|
|
|
// that set to false if cheque 'c' is already presented in the slice 'lst'.
|
|
|
|
func addCheque(lst []cheque, c cheque) ([]cheque, bool) {
|
|
|
|
for i := 0; i < len(lst); i++ {
|
2021-02-02 17:36:20 +00:00
|
|
|
if common.BytesEqual(c.id, lst[i].id) {
|
2020-07-15 16:31:34 +00:00
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lst = append(lst, c)
|
2020-07-20 13:09:40 +00:00
|
|
|
|
2020-07-15 16:31:34 +00:00
|
|
|
return lst, true
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-07-16 14:23:52 +00:00
|
|
|
// rmNodeByKey returns slice of nodes without node with key 'k',
|
|
|
|
// slices of nodes 'add' with node with key 'k' and bool flag,
|
|
|
|
// that set to false if node with a key 'k' does not exists in the slice 'lst'.
|
2021-02-02 18:26:34 +00:00
|
|
|
func rmNodeByKey(lst, add []common.IRNode, k []byte) ([]common.IRNode, []common.IRNode, bool) {
|
2020-07-16 14:23:52 +00:00
|
|
|
var (
|
|
|
|
flag bool
|
2021-02-02 18:26:34 +00:00
|
|
|
newLst = []common.IRNode{} // it is explicit declaration of empty slice, not nil
|
2020-07-16 14:23:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
for i := 0; i < len(lst); i++ {
|
2021-02-02 18:26:34 +00:00
|
|
|
if common.BytesEqual(k, lst[i].PublicKey) {
|
2020-07-16 14:23:52 +00:00
|
|
|
add = append(add, lst[i])
|
|
|
|
flag = true
|
|
|
|
} else {
|
|
|
|
newLst = append(newLst, lst[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return newLst, add, flag
|
|
|
|
}
|