2021-07-04 11:00:56 +00:00
|
|
|
package container
|
2020-10-27 12:14:06 +00:00
|
|
|
|
|
|
|
import (
|
2021-01-14 15:33:50 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop"
|
2020-10-27 12:14:06 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/contract"
|
2020-12-18 11:07:33 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/iterator"
|
2021-03-12 12:16:36 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/native/crypto"
|
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-10-27 12:14:06 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/runtime"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/storage"
|
2021-02-02 16:39:16 +00:00
|
|
|
"github.com/nspcc-dev/neofs-contract/common"
|
2020-10-27 12:14:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
2021-01-22 16:45:26 +00:00
|
|
|
storageNode struct {
|
|
|
|
info []byte
|
|
|
|
}
|
|
|
|
|
2021-05-21 13:27:01 +00:00
|
|
|
Container struct {
|
|
|
|
value []byte
|
|
|
|
sig interop.Signature
|
|
|
|
pub interop.PublicKey
|
|
|
|
token []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
ExtendedACL struct {
|
|
|
|
value []byte
|
|
|
|
sig interop.Signature
|
|
|
|
pub interop.PublicKey
|
|
|
|
token []byte
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
2021-01-22 16:45:26 +00:00
|
|
|
|
|
|
|
estimation struct {
|
|
|
|
from interop.PublicKey
|
|
|
|
size int
|
|
|
|
}
|
|
|
|
|
|
|
|
containerSizes struct {
|
|
|
|
cid []byte
|
|
|
|
estimations []estimation
|
|
|
|
}
|
2020-10-27 12:14:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
neofsIDContractKey = "identityScriptHash"
|
|
|
|
balanceContractKey = "balanceScriptHash"
|
|
|
|
netmapContractKey = "netmapScriptHash"
|
2021-09-29 10:00:32 +00:00
|
|
|
nnsContractKey = "nnsScriptHash"
|
|
|
|
nnsRootKey = "nnsRoot"
|
2021-04-27 10:48:40 +00:00
|
|
|
notaryDisabledKey = "notary"
|
|
|
|
|
|
|
|
containerFeeKey = "ContainerFee"
|
2020-12-18 11:07:33 +00:00
|
|
|
|
|
|
|
containerIDSize = 32 // SHA256 size
|
2021-01-22 16:45:26 +00:00
|
|
|
|
2021-06-30 16:08:59 +00:00
|
|
|
estimateKeyPrefix = "cnr"
|
|
|
|
estimatePostfixSize = 10
|
|
|
|
cleanupDelta = 3
|
2020-10-27 12:14:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2021-04-07 06:17:00 +00:00
|
|
|
eACLPrefix = []byte("eACL")
|
2020-10-27 12:14:06 +00:00
|
|
|
)
|
|
|
|
|
2021-09-29 10:00:32 +00:00
|
|
|
// OnNEP11Payment is needed for registration with contract as owner to work.
|
|
|
|
func OnNEP11Payment(a interop.Hash160, b int, c []byte, d interface{}) {
|
|
|
|
}
|
|
|
|
|
2021-05-12 08:31:07 +00:00
|
|
|
func _deploy(data interface{}, isUpdate bool) {
|
2021-06-03 08:25:05 +00:00
|
|
|
ctx := storage.GetContext()
|
|
|
|
|
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
|
|
|
addrNetmap := args[1].(interop.Hash160)
|
|
|
|
addrBalance := args[2].(interop.Hash160)
|
|
|
|
addrID := args[3].(interop.Hash160)
|
2021-09-29 10:00:32 +00:00
|
|
|
addrNNS := args[4].(interop.Hash160)
|
|
|
|
nnsRoot := args[5].(string)
|
2020-10-27 12:14:06 +00:00
|
|
|
|
|
|
|
if len(addrNetmap) != 20 || len(addrBalance) != 20 || len(addrID) != 20 {
|
2021-09-20 14:23:19 +00:00
|
|
|
panic("incorrect length of contract script hash")
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
storage.Put(ctx, netmapContractKey, addrNetmap)
|
|
|
|
storage.Put(ctx, balanceContractKey, addrBalance)
|
|
|
|
storage.Put(ctx, neofsIDContractKey, addrID)
|
2021-09-29 10:00:32 +00:00
|
|
|
storage.Put(ctx, nnsContractKey, addrNNS)
|
|
|
|
storage.Put(ctx, nnsRootKey, nnsRoot)
|
2020-10-27 12:14:06 +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("container contract notary disabled")
|
2021-04-27 10:48:40 +00:00
|
|
|
}
|
|
|
|
|
2021-09-29 10:00:32 +00:00
|
|
|
// add NNS root for container alias domains
|
|
|
|
contract.Call(addrNNS, "addRoot", contract.All, nnsRoot)
|
|
|
|
|
2020-10-27 12:14:06 +00:00
|
|
|
runtime.Log("container contract initialized")
|
|
|
|
}
|
|
|
|
|
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 committee.
|
2021-09-21 12:58:37 +00:00
|
|
|
func Update(script []byte, manifest []byte, data interface{}) {
|
2021-09-20 15:41:46 +00:00
|
|
|
if !common.HasUpdateAccess() {
|
|
|
|
panic("only committee can update contract")
|
2021-02-11 15:55:32 +00:00
|
|
|
}
|
|
|
|
|
2021-06-03 08:21:40 +00:00
|
|
|
contract.Call(interop.Hash160(management.Hash), "update", contract.All, script, manifest, data)
|
2021-02-11 15:55:32 +00:00
|
|
|
runtime.Log("container contract updated")
|
|
|
|
}
|
|
|
|
|
2021-07-07 15:12:28 +00:00
|
|
|
// Put method creates new container if it was invoked by Alphabet nodes
|
|
|
|
// of the Inner Ring. Otherwise it produces containerPut notification.
|
|
|
|
//
|
|
|
|
// Container should be stable marshaled Container structure from API.
|
|
|
|
// Signature is a RFC6979 signature of Container.
|
|
|
|
// PublicKey contains public key of the signer.
|
|
|
|
// Token is optional and should be stable marshaled SessionToken structure from
|
|
|
|
// API.
|
2021-05-21 11:37:31 +00:00
|
|
|
func Put(container []byte, signature interop.Signature, publicKey interop.PublicKey, token []byte) {
|
2021-09-29 10:00:32 +00:00
|
|
|
PutNamed(container, signature, publicKey, token, "", "")
|
|
|
|
}
|
|
|
|
|
|
|
|
// PutNamed is similar to put but also sets a TXT record in nns contract.
|
|
|
|
// Note that zone must exist.
|
|
|
|
func PutNamed(container []byte, signature interop.Signature,
|
|
|
|
publicKey interop.PublicKey, token []byte,
|
|
|
|
name, zone string) {
|
2021-03-09 19:15:58 +00:00
|
|
|
ctx := storage.GetContext()
|
2021-04-29 13:14:25 +00:00
|
|
|
notaryDisabled := storage.Get(ctx, notaryDisabledKey).(bool)
|
2021-03-09 19:15:58 +00:00
|
|
|
|
2021-06-30 13:59:50 +00:00
|
|
|
ownerID := ownerFromBinaryContainer(container)
|
2021-03-12 12:16:36 +00:00
|
|
|
containerID := crypto.Sha256(container)
|
2021-03-04 19:21:49 +00:00
|
|
|
neofsIDContractAddr := storage.Get(ctx, neofsIDContractKey).(interop.Hash160)
|
2021-05-21 13:27:01 +00:00
|
|
|
cnr := Container{
|
|
|
|
value: container,
|
|
|
|
sig: signature,
|
|
|
|
pub: publicKey,
|
|
|
|
token: token,
|
|
|
|
}
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2021-09-29 10:00:32 +00:00
|
|
|
var (
|
|
|
|
needRegister bool
|
|
|
|
nnsContractAddr interop.Hash160
|
|
|
|
domain string
|
|
|
|
)
|
|
|
|
if name != "" {
|
|
|
|
if zone == "" {
|
|
|
|
zone = storage.Get(ctx, nnsRootKey).(string)
|
|
|
|
}
|
|
|
|
nnsContractAddr = storage.Get(ctx, nnsContractKey).(interop.Hash160)
|
|
|
|
domain = name + "." + zone
|
|
|
|
needRegister = checkNiceNameAvailable(nnsContractAddr, domain)
|
|
|
|
}
|
|
|
|
|
2021-04-29 13:14:25 +00:00
|
|
|
var ( // for invocation collection without notary
|
|
|
|
alphabet = common.AlphabetNodes()
|
|
|
|
nodeKey []byte
|
|
|
|
alphabetCall bool
|
|
|
|
)
|
|
|
|
|
|
|
|
if notaryDisabled {
|
|
|
|
nodeKey = common.InnerRingInvoker(alphabet)
|
|
|
|
alphabetCall = len(nodeKey) != 0
|
|
|
|
} else {
|
|
|
|
multiaddr := common.AlphabetAddress()
|
|
|
|
alphabetCall = runtime.CheckWitness(multiaddr)
|
|
|
|
}
|
|
|
|
|
2021-04-07 06:17:00 +00:00
|
|
|
from := common.WalletToScriptHash(ownerID)
|
2021-03-17 13:54:55 +00:00
|
|
|
netmapContractAddr := storage.Get(ctx, netmapContractKey).(interop.Hash160)
|
|
|
|
balanceContractAddr := storage.Get(ctx, balanceContractKey).(interop.Hash160)
|
2021-02-08 15:32:38 +00:00
|
|
|
containerFee := contract.Call(netmapContractAddr, "config", contract.ReadOnly, containerFeeKey).(int)
|
2021-06-15 12:34:45 +00:00
|
|
|
balance := contract.Call(balanceContractAddr, "balanceOf", contract.ReadOnly, from).(int)
|
2021-04-07 06:17:00 +00:00
|
|
|
details := common.ContainerFeeTransferDetails(containerID)
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2021-06-15 12:34:45 +00:00
|
|
|
if !alphabetCall {
|
|
|
|
if balance < containerFee*len(alphabet) {
|
|
|
|
panic("insufficient balance to create container")
|
|
|
|
}
|
|
|
|
runtime.Notify("containerPut", container, signature, publicKey, token)
|
|
|
|
return
|
|
|
|
}
|
2021-02-19 15:42:25 +00:00
|
|
|
// todo: check if new container with unique container id
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2021-04-29 13:14:25 +00:00
|
|
|
if notaryDisabled {
|
|
|
|
threshold := len(alphabet)*2/3 + 1
|
|
|
|
id := common.InvokeID([]interface{}{container, signature, publicKey}, []byte("put"))
|
|
|
|
|
|
|
|
n := common.Vote(ctx, id, nodeKey)
|
|
|
|
if n < threshold {
|
2021-05-21 11:37:31 +00:00
|
|
|
return
|
2021-04-29 13:14:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
common.RemoveVotes(ctx, id)
|
|
|
|
}
|
|
|
|
|
2021-03-24 08:10:31 +00:00
|
|
|
for i := 0; i < len(alphabet); i++ {
|
|
|
|
node := alphabet[i]
|
2021-02-19 15:42:25 +00:00
|
|
|
to := contract.CreateStandardAccount(node.PublicKey)
|
|
|
|
|
2021-05-21 11:37:31 +00:00
|
|
|
contract.Call(balanceContractAddr, "transferX",
|
2021-02-19 15:42:25 +00:00
|
|
|
contract.All,
|
|
|
|
from,
|
|
|
|
to,
|
|
|
|
containerFee,
|
2021-04-07 06:17:00 +00:00
|
|
|
details,
|
2021-02-19 15:42:25 +00:00
|
|
|
)
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2021-05-21 13:27:01 +00:00
|
|
|
addContainer(ctx, containerID, ownerID, cnr)
|
|
|
|
|
2021-09-29 10:00:32 +00:00
|
|
|
if name != "" {
|
|
|
|
if needRegister {
|
|
|
|
const (
|
|
|
|
defaultRefresh = 3600 // 1 hour
|
|
|
|
defaultRetry = 600 // 10 min
|
|
|
|
defaultExpire = 604800 // 1 week
|
|
|
|
defaultTTL = 3600 // 1 hour
|
|
|
|
)
|
|
|
|
res := contract.Call(nnsContractAddr, "register", contract.All,
|
|
|
|
domain, runtime.GetExecutingScriptHash(), "ops@nspcc.ru",
|
|
|
|
defaultRefresh, defaultRetry, defaultExpire, defaultTTL).(bool)
|
|
|
|
if !res {
|
|
|
|
panic("can't register the domain " + domain)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
contract.Call(nnsContractAddr, "addRecord", contract.All,
|
|
|
|
domain, 16 /* TXT */, std.Base58Encode(containerID))
|
|
|
|
}
|
|
|
|
|
2021-05-21 13:27:01 +00:00
|
|
|
if len(token) == 0 { // if container created directly without session
|
|
|
|
contract.Call(neofsIDContractAddr, "addKey", contract.All, ownerID, [][]byte{publicKey})
|
|
|
|
}
|
2021-02-19 15:42:25 +00:00
|
|
|
|
|
|
|
runtime.Log("put: added new container")
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2021-09-29 10:00:32 +00:00
|
|
|
// checkNiceNameAvailable checks if nice name is available for the container.
|
|
|
|
// It panics if the name is taken. Returned value specifies if new domain registration is needed.
|
|
|
|
func checkNiceNameAvailable(nnsContractAddr interop.Hash160, domain string) bool {
|
|
|
|
isAvail := contract.Call(nnsContractAddr, "isAvailable",
|
|
|
|
contract.ReadStates|contract.AllowCall, domain).(bool)
|
|
|
|
if isAvail {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
res := contract.Call(nnsContractAddr, "getRecords",
|
|
|
|
contract.ReadStates|contract.AllowCall, domain, 16 /* TXT */)
|
|
|
|
if res != nil {
|
|
|
|
panic("name is already taken")
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-07-07 15:12:28 +00:00
|
|
|
// Delete method removes container from contract storage if it was
|
|
|
|
// invoked by Alphabet nodes of the Inner Ring. Otherwise it produces
|
|
|
|
// containerDelete notification.
|
|
|
|
//
|
|
|
|
// Signature is a RFC6979 signature of container ID.
|
|
|
|
// Token is optional and should be stable marshaled SessionToken structure from
|
|
|
|
// API.
|
2021-05-21 11:37:31 +00:00
|
|
|
func Delete(containerID []byte, signature interop.Signature, token []byte) {
|
2021-03-09 19:15:58 +00:00
|
|
|
ctx := storage.GetContext()
|
2021-04-29 13:14:25 +00:00
|
|
|
notaryDisabled := storage.Get(ctx, notaryDisabledKey).(bool)
|
2021-03-09 19:15:58 +00:00
|
|
|
|
2020-10-27 12:14:06 +00:00
|
|
|
ownerID := getOwnerByID(ctx, containerID)
|
|
|
|
if len(ownerID) == 0 {
|
2021-09-20 13:53:23 +00:00
|
|
|
return
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2021-04-29 13:14:25 +00:00
|
|
|
var ( // for invocation collection without notary
|
|
|
|
alphabet []common.IRNode
|
|
|
|
nodeKey []byte
|
|
|
|
alphabetCall bool
|
|
|
|
)
|
|
|
|
|
|
|
|
if notaryDisabled {
|
|
|
|
alphabet = common.AlphabetNodes()
|
|
|
|
nodeKey = common.InnerRingInvoker(alphabet)
|
|
|
|
alphabetCall = len(nodeKey) != 0
|
|
|
|
} else {
|
|
|
|
multiaddr := common.AlphabetAddress()
|
|
|
|
alphabetCall = runtime.CheckWitness(multiaddr)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !alphabetCall {
|
2021-05-21 13:27:01 +00:00
|
|
|
runtime.Notify("containerDelete", containerID, signature, token)
|
2021-05-21 11:37:31 +00:00
|
|
|
return
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2021-04-29 13:14:25 +00:00
|
|
|
if notaryDisabled {
|
|
|
|
threshold := len(alphabet)*2/3 + 1
|
|
|
|
id := common.InvokeID([]interface{}{containerID, signature}, []byte("delete"))
|
|
|
|
|
|
|
|
n := common.Vote(ctx, id, nodeKey)
|
|
|
|
if n < threshold {
|
2021-05-21 11:37:31 +00:00
|
|
|
return
|
2021-04-29 13:14:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
common.RemoveVotes(ctx, id)
|
|
|
|
}
|
|
|
|
|
2021-02-19 15:42:25 +00:00
|
|
|
removeContainer(ctx, containerID, ownerID)
|
|
|
|
runtime.Log("delete: remove container")
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2021-07-07 15:12:28 +00:00
|
|
|
// Get method returns structure that contains stable marshaled Container structure,
|
|
|
|
// signature, public key of the container creator and stable marshaled SessionToken
|
|
|
|
// structure if it was provided.
|
2021-05-21 13:27:01 +00:00
|
|
|
func Get(containerID []byte) Container {
|
2021-03-09 19:15:58 +00:00
|
|
|
ctx := storage.GetReadOnlyContext()
|
2021-05-21 13:27:01 +00:00
|
|
|
return getContainer(ctx, containerID)
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2021-07-07 15:12:28 +00:00
|
|
|
// Owner method returns 25 byte Owner ID of the container.
|
2020-10-27 12:14:06 +00:00
|
|
|
func Owner(containerID []byte) []byte {
|
2021-03-09 19:15:58 +00:00
|
|
|
ctx := storage.GetReadOnlyContext()
|
2020-10-27 12:14:06 +00:00
|
|
|
return getOwnerByID(ctx, containerID)
|
|
|
|
}
|
|
|
|
|
2021-07-07 15:12:28 +00:00
|
|
|
// List method returns list of all container IDs owned by specified owner.
|
2020-10-27 12:14:06 +00:00
|
|
|
func List(owner []byte) [][]byte {
|
2021-03-09 19:15:58 +00:00
|
|
|
ctx := storage.GetReadOnlyContext()
|
|
|
|
|
2020-12-18 11:07:33 +00:00
|
|
|
if len(owner) == 0 {
|
|
|
|
return getAllContainers(ctx)
|
|
|
|
}
|
|
|
|
|
2020-10-27 12:14:06 +00:00
|
|
|
var list [][]byte
|
|
|
|
|
2021-06-30 13:59:50 +00:00
|
|
|
it := storage.Find(ctx, owner, storage.ValuesOnly)
|
|
|
|
for iterator.Next(it) {
|
|
|
|
id := iterator.Value(it).([]byte)
|
|
|
|
list = append(list, id)
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return list
|
|
|
|
}
|
|
|
|
|
2021-07-07 15:12:28 +00:00
|
|
|
// SetEACL method sets new extended ACL table related to the contract
|
|
|
|
// if it was invoked by Alphabet nodes of the Inner Ring. Otherwise it produces
|
|
|
|
// setEACL notification.
|
|
|
|
//
|
|
|
|
// EACL should be stable marshaled EACLTable structure from API.
|
|
|
|
// Signature is a RFC6979 signature of Container.
|
|
|
|
// PublicKey contains public key of the signer.
|
|
|
|
// Token is optional and should be stable marshaled SessionToken structure from
|
|
|
|
// API.
|
2021-05-21 11:37:31 +00:00
|
|
|
func SetEACL(eACL []byte, signature interop.Signature, publicKey interop.PublicKey, token []byte) {
|
2021-03-09 19:15:58 +00:00
|
|
|
ctx := storage.GetContext()
|
2021-05-11 14:42:02 +00:00
|
|
|
notaryDisabled := storage.Get(ctx, notaryDisabledKey).(bool)
|
2021-03-09 19:15:58 +00:00
|
|
|
|
2020-10-27 12:14:06 +00:00
|
|
|
// get container ID
|
|
|
|
offset := int(eACL[1])
|
|
|
|
offset = 2 + offset + 4
|
|
|
|
containerID := eACL[offset : offset+32]
|
|
|
|
|
|
|
|
ownerID := getOwnerByID(ctx, containerID)
|
|
|
|
if len(ownerID) == 0 {
|
2021-09-20 14:23:19 +00:00
|
|
|
panic("container does not exist")
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2021-05-11 14:42:02 +00:00
|
|
|
var ( // for invocation collection without notary
|
|
|
|
alphabet []common.IRNode
|
|
|
|
nodeKey []byte
|
|
|
|
alphabetCall bool
|
|
|
|
)
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2021-05-11 14:42:02 +00:00
|
|
|
if notaryDisabled {
|
|
|
|
alphabet = common.AlphabetNodes()
|
|
|
|
nodeKey = common.InnerRingInvoker(alphabet)
|
|
|
|
alphabetCall = len(nodeKey) != 0
|
|
|
|
} else {
|
|
|
|
multiaddr := common.AlphabetAddress()
|
|
|
|
alphabetCall = runtime.CheckWitness(multiaddr)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !alphabetCall {
|
2021-05-21 13:27:01 +00:00
|
|
|
runtime.Notify("setEACL", eACL, signature, publicKey, token)
|
2021-05-21 11:37:31 +00:00
|
|
|
return
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2021-05-21 13:27:01 +00:00
|
|
|
rule := ExtendedACL{
|
|
|
|
value: eACL,
|
|
|
|
sig: signature,
|
|
|
|
pub: publicKey,
|
|
|
|
token: token,
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
key := append(eACLPrefix, containerID...)
|
2021-05-11 14:42:02 +00:00
|
|
|
|
|
|
|
if notaryDisabled {
|
|
|
|
threshold := len(alphabet)*2/3 + 1
|
|
|
|
id := common.InvokeID([]interface{}{eACL}, []byte("setEACL"))
|
|
|
|
|
|
|
|
n := common.Vote(ctx, id, nodeKey)
|
|
|
|
if n < threshold {
|
2021-05-21 11:37:31 +00:00
|
|
|
return
|
2021-05-11 14:42:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
common.RemoveVotes(ctx, id)
|
|
|
|
}
|
|
|
|
|
2021-02-02 17:36:20 +00:00
|
|
|
common.SetSerialized(ctx, key, rule)
|
2020-10-27 12:14:06 +00:00
|
|
|
|
|
|
|
runtime.Log("setEACL: success")
|
|
|
|
}
|
|
|
|
|
2021-07-07 15:12:28 +00:00
|
|
|
// EACL method returns structure that contains stable marshaled EACLTable structure,
|
|
|
|
// signature, public key of the extended ACL setter and stable marshaled SessionToken
|
|
|
|
// structure if it was provided.
|
2021-05-21 13:27:01 +00:00
|
|
|
func EACL(containerID []byte) ExtendedACL {
|
2021-03-09 19:15:58 +00:00
|
|
|
ctx := storage.GetReadOnlyContext()
|
|
|
|
|
2021-01-14 15:33:50 +00:00
|
|
|
ownerID := getOwnerByID(ctx, containerID)
|
|
|
|
if len(ownerID) == 0 {
|
2021-09-20 14:23:19 +00:00
|
|
|
panic("container does not exist")
|
2021-01-14 15:33:50 +00:00
|
|
|
}
|
|
|
|
|
2021-05-11 14:48:25 +00:00
|
|
|
return getEACL(ctx, containerID)
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2021-07-07 15:12:28 +00:00
|
|
|
// PutContainerSize method saves container size estimation in contract
|
|
|
|
// memory. Can be invoked only by Storage nodes from the network map. Method
|
|
|
|
// checks witness based on the provided public key of the Storage node.
|
2021-05-21 11:37:31 +00:00
|
|
|
func PutContainerSize(epoch int, cid []byte, usedSize int, pubKey interop.PublicKey) {
|
2021-03-09 19:15:58 +00:00
|
|
|
ctx := storage.GetContext()
|
|
|
|
|
2021-01-22 16:45:26 +00:00
|
|
|
if !runtime.CheckWitness(pubKey) {
|
2021-09-20 14:23:19 +00:00
|
|
|
panic("invalid witness of container size estimation")
|
2021-01-22 16:45:26 +00:00
|
|
|
}
|
|
|
|
|
2021-03-09 19:15:58 +00:00
|
|
|
if !isStorageNode(ctx, pubKey) {
|
2021-09-20 14:23:19 +00:00
|
|
|
panic("putContainerSize method must be invoked by storage node from network map")
|
2021-01-22 16:45:26 +00:00
|
|
|
}
|
|
|
|
|
2021-06-30 16:08:59 +00:00
|
|
|
key := estimationKey(epoch, cid, pubKey)
|
2021-01-22 16:45:26 +00:00
|
|
|
|
2021-06-30 16:08:59 +00:00
|
|
|
s := estimation{
|
2021-01-22 16:45:26 +00:00
|
|
|
from: pubKey,
|
|
|
|
size: usedSize,
|
2021-06-30 16:08:59 +00:00
|
|
|
}
|
2021-01-22 16:45:26 +00:00
|
|
|
|
2021-03-12 12:16:36 +00:00
|
|
|
storage.Put(ctx, key, std.Serialize(s))
|
2021-01-22 16:45:26 +00:00
|
|
|
|
|
|
|
runtime.Log("container: saved container size estimation")
|
|
|
|
}
|
|
|
|
|
2021-07-07 15:12:28 +00:00
|
|
|
// GetContainerSize method returns container ID and slice of container
|
|
|
|
// estimations. Container estimation includes public key of the Storage Node
|
|
|
|
// that registered estimation and value of estimation.
|
|
|
|
//
|
|
|
|
// Use ID obtained from ListContainerSizes method. Estimations are removed
|
|
|
|
// from contract storage every epoch, see NewEpoch method, therefore method
|
|
|
|
// can return different results in different epochs.
|
2021-01-22 16:45:26 +00:00
|
|
|
func GetContainerSize(id []byte) containerSizes {
|
2021-03-09 19:15:58 +00:00
|
|
|
ctx := storage.GetReadOnlyContext()
|
2021-06-30 16:08:59 +00:00
|
|
|
|
|
|
|
// this `id` expected to be from `ListContainerSizes`
|
|
|
|
// therefore it is not contains postfix, we ignore it in the cut.
|
|
|
|
ln := len(id)
|
|
|
|
cid := id[ln-containerIDSize : ln]
|
|
|
|
|
|
|
|
return getContainerSizeEstimation(ctx, id, cid)
|
2021-01-22 16:45:26 +00:00
|
|
|
}
|
|
|
|
|
2021-07-07 15:12:28 +00:00
|
|
|
// ListContainerSizes method returns IDs of container size estimations
|
|
|
|
// that has been registered for specified epoch.
|
2021-01-22 16:45:26 +00:00
|
|
|
func ListContainerSizes(epoch int) [][]byte {
|
2021-03-09 19:15:58 +00:00
|
|
|
ctx := storage.GetReadOnlyContext()
|
|
|
|
|
2021-01-22 16:45:26 +00:00
|
|
|
var buf interface{} = epoch
|
|
|
|
|
|
|
|
key := []byte(estimateKeyPrefix)
|
|
|
|
key = append(key, buf.([]byte)...)
|
|
|
|
|
2021-02-08 15:23:08 +00:00
|
|
|
it := storage.Find(ctx, key, storage.KeysOnly)
|
2021-01-22 16:45:26 +00:00
|
|
|
|
2021-06-30 16:08:59 +00:00
|
|
|
uniq := map[string]struct{}{}
|
2021-01-22 16:45:26 +00:00
|
|
|
|
|
|
|
for iterator.Next(it) {
|
2021-06-30 16:08:59 +00:00
|
|
|
storageKey := iterator.Value(it).([]byte)
|
|
|
|
|
|
|
|
ln := len(storageKey)
|
|
|
|
storageKey = storageKey[:ln-estimatePostfixSize]
|
|
|
|
|
|
|
|
uniq[string(storageKey)] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
var result [][]byte
|
|
|
|
|
|
|
|
for k := range uniq {
|
|
|
|
result = append(result, []byte(k))
|
2021-01-22 16:45:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2021-07-07 15:12:28 +00:00
|
|
|
// NewEpoch method removes all container size estimations from epoch older than
|
|
|
|
// epochNum + 3. Can be invoked only by NewEpoch method of the Netmap contract.
|
2021-03-17 13:49:18 +00:00
|
|
|
func NewEpoch(epochNum int) {
|
2021-03-09 19:15:58 +00:00
|
|
|
ctx := storage.GetContext()
|
2021-04-29 13:14:25 +00:00
|
|
|
notaryDisabled := storage.Get(ctx, notaryDisabledKey).(bool)
|
2021-03-09 19:15:58 +00:00
|
|
|
|
2021-04-29 13:14:25 +00:00
|
|
|
if notaryDisabled {
|
|
|
|
indirectCall := common.FromKnownContract(
|
|
|
|
ctx,
|
|
|
|
runtime.GetCallingScriptHash(),
|
|
|
|
netmapContractKey,
|
|
|
|
)
|
|
|
|
if !indirectCall {
|
2021-09-20 14:23:19 +00:00
|
|
|
panic("newEpoch method must be invoked by inner ring")
|
2021-04-29 13:14:25 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
multiaddr := common.AlphabetAddress()
|
|
|
|
if !runtime.CheckWitness(multiaddr) {
|
2021-09-20 14:23:19 +00:00
|
|
|
panic("newEpoch method must be invoked by inner ring")
|
2021-04-29 13:14:25 +00:00
|
|
|
}
|
2021-01-25 20:16:14 +00:00
|
|
|
}
|
|
|
|
|
2021-03-09 19:15:58 +00:00
|
|
|
candidates := keysToDelete(ctx, epochNum)
|
2021-02-19 15:42:25 +00:00
|
|
|
for _, candidate := range candidates {
|
|
|
|
storage.Delete(ctx, candidate)
|
2021-01-25 20:16:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-07 15:12:28 +00:00
|
|
|
// StartContainerEstimation method produces StartEstimation notification.
|
|
|
|
// Can be invoked only by Alphabet nodes of the Inner Ring.
|
2021-05-21 11:37:31 +00:00
|
|
|
func StartContainerEstimation(epoch int) {
|
2021-04-29 13:14:25 +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 = common.AlphabetNodes()
|
|
|
|
nodeKey = common.InnerRingInvoker(alphabet)
|
|
|
|
if len(nodeKey) == 0 {
|
2021-09-20 14:23:19 +00:00
|
|
|
panic("startContainerEstimation method must be invoked by inner ring")
|
2021-04-29 13:14:25 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
multiaddr := common.AlphabetAddress()
|
|
|
|
if !runtime.CheckWitness(multiaddr) {
|
2021-09-20 14:23:19 +00:00
|
|
|
panic("startContainerEstimation method must be invoked by inner ring")
|
2021-04-29 13:14:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if notaryDisabled {
|
|
|
|
threshold := len(alphabet)*2/3 + 1
|
|
|
|
id := common.InvokeID([]interface{}{epoch}, []byte("startEstimation"))
|
|
|
|
|
|
|
|
n := common.Vote(ctx, id, nodeKey)
|
|
|
|
if n < threshold {
|
2021-05-21 11:37:31 +00:00
|
|
|
return
|
2021-04-29 13:14:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
common.RemoveVotes(ctx, id)
|
2021-01-26 17:48:25 +00:00
|
|
|
}
|
|
|
|
|
2021-02-19 15:42:25 +00:00
|
|
|
runtime.Notify("StartEstimation", epoch)
|
|
|
|
runtime.Log("startEstimation: notification has been produced")
|
2021-01-26 17:48:25 +00:00
|
|
|
}
|
|
|
|
|
2021-07-07 15:12:28 +00:00
|
|
|
// StopContainerEstimation method produces StopEstimation notification.
|
|
|
|
// Can be invoked only by Alphabet nodes of the Inner Ring.
|
2021-05-21 11:37:31 +00:00
|
|
|
func StopContainerEstimation(epoch int) {
|
2021-04-29 13:14:25 +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 = common.AlphabetNodes()
|
|
|
|
nodeKey = common.InnerRingInvoker(alphabet)
|
|
|
|
if len(nodeKey) == 0 {
|
2021-09-20 14:23:19 +00:00
|
|
|
panic("stopContainerEstimation method must be invoked by inner ring")
|
2021-04-29 13:14:25 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
multiaddr := common.AlphabetAddress()
|
|
|
|
if !runtime.CheckWitness(multiaddr) {
|
2021-09-20 14:23:19 +00:00
|
|
|
panic("stopContainerEstimation method must be invoked by inner ring")
|
2021-04-29 13:14:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if notaryDisabled {
|
|
|
|
threshold := len(alphabet)*2/3 + 1
|
|
|
|
id := common.InvokeID([]interface{}{epoch}, []byte("stopEstimation"))
|
|
|
|
|
|
|
|
n := common.Vote(ctx, id, nodeKey)
|
|
|
|
if n < threshold {
|
2021-05-21 11:37:31 +00:00
|
|
|
return
|
2021-04-29 13:14:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
common.RemoveVotes(ctx, id)
|
2021-01-26 17:48:25 +00:00
|
|
|
}
|
|
|
|
|
2021-02-19 15:42:25 +00:00
|
|
|
runtime.Notify("StopEstimation", epoch)
|
|
|
|
runtime.Log("stopEstimation: notification has been produced")
|
2021-01-26 17:48:25 +00:00
|
|
|
}
|
|
|
|
|
2021-07-07 15:12:28 +00:00
|
|
|
// Version returns version of the contract.
|
2020-10-27 12:14:06 +00:00
|
|
|
func Version() int {
|
2021-07-29 11:44:53 +00:00
|
|
|
return common.Version
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2021-05-21 13:27:01 +00:00
|
|
|
func addContainer(ctx storage.Context, id, owner []byte, container Container) {
|
2021-06-30 13:59:50 +00:00
|
|
|
containerListKey := append(owner, id...)
|
|
|
|
storage.Put(ctx, containerListKey, id)
|
|
|
|
|
2021-05-21 13:27:01 +00:00
|
|
|
common.SetSerialized(ctx, id, container)
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func removeContainer(ctx storage.Context, id []byte, owner []byte) {
|
2021-06-30 13:59:50 +00:00
|
|
|
containerListKey := append(owner, id...)
|
|
|
|
storage.Delete(ctx, containerListKey)
|
2020-10-27 12:14:06 +00:00
|
|
|
|
|
|
|
storage.Delete(ctx, id)
|
|
|
|
}
|
|
|
|
|
2020-12-18 11:07:33 +00:00
|
|
|
func getAllContainers(ctx storage.Context) [][]byte {
|
|
|
|
var list [][]byte
|
|
|
|
|
2021-02-08 15:23:08 +00:00
|
|
|
it := storage.Find(ctx, []byte{}, storage.KeysOnly)
|
2020-12-18 11:07:33 +00:00
|
|
|
for iterator.Next(it) {
|
2021-02-08 15:23:08 +00:00
|
|
|
key := iterator.Value(it).([]byte) // it MUST BE `storage.KeysOnly`
|
2020-12-18 11:07:33 +00:00
|
|
|
if len(key) == containerIDSize {
|
|
|
|
list = append(list, key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return list
|
|
|
|
}
|
|
|
|
|
2021-05-21 13:27:01 +00:00
|
|
|
func getEACL(ctx storage.Context, cid []byte) ExtendedACL {
|
2020-10-27 12:14:06 +00:00
|
|
|
key := append(eACLPrefix, cid...)
|
|
|
|
data := storage.Get(ctx, key)
|
|
|
|
if data != nil {
|
2021-05-21 13:27:01 +00:00
|
|
|
return std.Deserialize(data.([]byte)).(ExtendedACL)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ExtendedACL{value: []byte{}, sig: interop.Signature{}, pub: interop.PublicKey{}, token: []byte{}}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getContainer(ctx storage.Context, cid []byte) Container {
|
|
|
|
data := storage.Get(ctx, cid)
|
|
|
|
if data != nil {
|
|
|
|
return std.Deserialize(data.([]byte)).(Container)
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2021-05-21 13:27:01 +00:00
|
|
|
return Container{value: []byte{}, sig: interop.Signature{}, pub: interop.PublicKey{}, token: []byte{}}
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2021-06-30 13:59:50 +00:00
|
|
|
func getOwnerByID(ctx storage.Context, cid []byte) []byte {
|
|
|
|
container := getContainer(ctx, cid)
|
2021-09-20 13:57:18 +00:00
|
|
|
if len(container.value) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-06-30 13:59:50 +00:00
|
|
|
return ownerFromBinaryContainer(container.value)
|
|
|
|
}
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2021-06-30 13:59:50 +00:00
|
|
|
func ownerFromBinaryContainer(container []byte) []byte {
|
|
|
|
offset := int(container[1])
|
|
|
|
offset = 2 + offset + 4 // version prefix + version size + owner prefix
|
|
|
|
return container[offset : offset+25] // offset + size of owner
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2021-06-30 16:08:59 +00:00
|
|
|
func estimationKey(epoch int, cid []byte, key interop.PublicKey) []byte {
|
2021-01-22 16:45:26 +00:00
|
|
|
var buf interface{} = epoch
|
|
|
|
|
2021-06-30 16:08:59 +00:00
|
|
|
hash := crypto.Ripemd160(key)
|
|
|
|
|
2021-01-22 16:45:26 +00:00
|
|
|
result := []byte(estimateKeyPrefix)
|
|
|
|
result = append(result, buf.([]byte)...)
|
2021-06-30 16:08:59 +00:00
|
|
|
result = append(result, cid...)
|
2021-01-22 16:45:26 +00:00
|
|
|
|
2021-06-30 16:08:59 +00:00
|
|
|
return append(result, hash[:estimatePostfixSize]...)
|
2021-01-22 16:45:26 +00:00
|
|
|
}
|
|
|
|
|
2021-03-09 19:15:58 +00:00
|
|
|
func getContainerSizeEstimation(ctx storage.Context, key, cid []byte) containerSizes {
|
2021-06-30 16:08:59 +00:00
|
|
|
var estimations []estimation
|
|
|
|
|
|
|
|
it := storage.Find(ctx, key, storage.ValuesOnly)
|
|
|
|
for iterator.Next(it) {
|
|
|
|
rawEstimation := iterator.Value(it).([]byte)
|
|
|
|
est := std.Deserialize(rawEstimation).(estimation)
|
|
|
|
estimations = append(estimations, est)
|
2021-01-22 16:45:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return containerSizes{
|
|
|
|
cid: cid,
|
2021-06-30 16:08:59 +00:00
|
|
|
estimations: estimations,
|
2021-01-22 16:45:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// isStorageNode looks into _previous_ epoch network map, because storage node
|
|
|
|
// announce container size estimation of previous epoch.
|
2021-03-09 19:15:58 +00:00
|
|
|
func isStorageNode(ctx storage.Context, key interop.PublicKey) bool {
|
2021-03-04 19:21:49 +00:00
|
|
|
netmapContractAddr := storage.Get(ctx, netmapContractKey).(interop.Hash160)
|
2021-02-08 15:32:38 +00:00
|
|
|
snapshot := contract.Call(netmapContractAddr, "snapshot", contract.ReadOnly, 1).([]storageNode)
|
2021-01-22 16:45:26 +00:00
|
|
|
|
|
|
|
for i := range snapshot {
|
|
|
|
nodeInfo := snapshot[i].info
|
|
|
|
nodeKey := nodeInfo[2:35] // offset:2, len:33
|
|
|
|
|
2021-02-02 17:36:20 +00:00
|
|
|
if common.BytesEqual(key, nodeKey) {
|
2021-01-22 16:45:26 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
2021-01-25 20:16:14 +00:00
|
|
|
|
2021-03-09 19:15:58 +00:00
|
|
|
func keysToDelete(ctx storage.Context, epoch int) [][]byte {
|
2021-01-25 20:16:14 +00:00
|
|
|
results := [][]byte{}
|
|
|
|
|
2021-02-08 15:23:08 +00:00
|
|
|
it := storage.Find(ctx, []byte(estimateKeyPrefix), storage.KeysOnly)
|
2021-01-25 20:16:14 +00:00
|
|
|
for iterator.Next(it) {
|
2021-06-30 16:08:59 +00:00
|
|
|
k := iterator.Value(it).([]byte)
|
|
|
|
nbytes := k[len(estimateKeyPrefix) : len(k)-containerIDSize-estimatePostfixSize]
|
2021-01-25 20:16:14 +00:00
|
|
|
|
|
|
|
var n interface{} = nbytes
|
|
|
|
|
|
|
|
if epoch-n.(int) > cleanupDelta {
|
|
|
|
results = append(results, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return results
|
|
|
|
}
|