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"
|
2022-01-18 07:08:13 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/convert"
|
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-09-29 13:10:29 +00:00
|
|
|
nnsHasAliasKey = "nnsHasAlias"
|
2021-04-27 10:48:40 +00:00
|
|
|
notaryDisabledKey = "notary"
|
|
|
|
|
2021-10-18 11:44:06 +00:00
|
|
|
// RegistrationFeeKey is a key in netmap config which contains fee for container registration.
|
|
|
|
RegistrationFeeKey = "ContainerFee"
|
2021-10-18 10:45:10 +00:00
|
|
|
// AliasFeeKey is a key in netmap config which contains fee for nice-name registration.
|
|
|
|
AliasFeeKey = "ContainerAliasFee"
|
2020-12-18 11:07:33 +00:00
|
|
|
|
2021-11-29 17:58:27 +00:00
|
|
|
// V2 format
|
2020-12-18 11:07:33 +00:00
|
|
|
containerIDSize = 32 // SHA256 size
|
2021-01-22 16:45:26 +00:00
|
|
|
|
2022-01-18 07:08:13 +00:00
|
|
|
singleEstimatePrefix = "est"
|
|
|
|
estimateKeyPrefix = "cnr"
|
|
|
|
estimatePostfixSize = 10
|
2022-04-14 11:56:51 +00:00
|
|
|
// CleanupDelta contains the number of the last epochs for which container estimations are present.
|
2022-01-17 10:51:11 +00:00
|
|
|
CleanupDelta = 3
|
2022-04-14 11:56:51 +00:00
|
|
|
// TotalCleanupDelta contains the number of the epochs after which estimation
|
|
|
|
// will be removed by epoch tick cleanup if any of the nodes hasn't updated
|
|
|
|
// container size and/or container has been removed. It must be greater than CleanupDelta.
|
2022-01-18 07:08:13 +00:00
|
|
|
TotalCleanupDelta = CleanupDelta + 1
|
2021-10-11 07:42:14 +00:00
|
|
|
|
|
|
|
// NotFoundError is returned if container is missing.
|
|
|
|
NotFoundError = "container does not exist"
|
2021-10-04 10:26:23 +00:00
|
|
|
|
|
|
|
// default SOA record field values
|
2022-11-21 12:52:18 +00:00
|
|
|
defaultRefresh = 3600 // 1 hour
|
|
|
|
defaultRetry = 600 // 10 min
|
|
|
|
defaultExpire = 3600 * 24 * 365 * 10 // 10 years
|
|
|
|
defaultTTL = 3600 // 1 hour
|
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
|
|
|
)
|
|
|
|
|
2022-04-14 11:56:51 +00:00
|
|
|
// OnNEP11Payment is needed for registration with contract as the owner to work.
|
2021-09-29 10:00:32 +00:00
|
|
|
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-12-27 07:15:36 +00:00
|
|
|
if isUpdate {
|
2021-12-27 08:49:30 +00:00
|
|
|
args := data.([]interface{})
|
|
|
|
common.CheckVersion(args[len(args)-1].(int))
|
2021-12-27 07:15:36 +00:00
|
|
|
return
|
|
|
|
}
|
2021-06-03 08:25:05 +00:00
|
|
|
|
2021-11-29 16:34:11 +00:00
|
|
|
args := data.(struct {
|
|
|
|
notaryDisabled bool
|
|
|
|
addrNetmap interop.Hash160
|
|
|
|
addrBalance interop.Hash160
|
|
|
|
addrID interop.Hash160
|
|
|
|
addrNNS interop.Hash160
|
|
|
|
nnsRoot string
|
|
|
|
})
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2021-11-29 16:43:01 +00:00
|
|
|
if len(args.addrNetmap) != interop.Hash160Len ||
|
|
|
|
len(args.addrBalance) != interop.Hash160Len ||
|
|
|
|
len(args.addrID) != interop.Hash160Len {
|
2021-09-20 14:23:19 +00:00
|
|
|
panic("incorrect length of contract script hash")
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2021-11-29 16:34:11 +00:00
|
|
|
storage.Put(ctx, netmapContractKey, args.addrNetmap)
|
|
|
|
storage.Put(ctx, balanceContractKey, args.addrBalance)
|
|
|
|
storage.Put(ctx, neofsIDContractKey, args.addrID)
|
|
|
|
storage.Put(ctx, nnsContractKey, args.addrNNS)
|
|
|
|
storage.Put(ctx, nnsRootKey, args.nnsRoot)
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2021-04-27 10:48:40 +00:00
|
|
|
// initialize the way to collect signatures
|
2021-11-29 16:34:11 +00:00
|
|
|
storage.Put(ctx, notaryDisabledKey, args.notaryDisabled)
|
|
|
|
if args.notaryDisabled {
|
2021-04-27 10:48:40 +00:00
|
|
|
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
|
2021-11-29 16:34:11 +00:00
|
|
|
registerNiceNameTLD(args.addrNNS, args.nnsRoot)
|
2021-09-29 10:00:32 +00:00
|
|
|
|
2020-10-27 12:14:06 +00:00
|
|
|
runtime.Log("container contract initialized")
|
|
|
|
}
|
|
|
|
|
2021-10-07 11:00:46 +00:00
|
|
|
func registerNiceNameTLD(addrNNS interop.Hash160, nnsRoot string) {
|
2021-10-04 10:26:23 +00:00
|
|
|
isAvail := contract.Call(addrNNS, "isAvailable", contract.AllowCall|contract.ReadStates,
|
|
|
|
"container").(bool)
|
|
|
|
if !isAvail {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
res := contract.Call(addrNNS, "register", contract.All,
|
2021-12-08 11:51:42 +00:00
|
|
|
nnsRoot, runtime.GetExecutingScriptHash(), "ops@nspcc.ru",
|
2021-10-04 10:26:23 +00:00
|
|
|
defaultRefresh, defaultRetry, defaultExpire, defaultTTL).(bool)
|
|
|
|
if !res {
|
|
|
|
panic("can't register NNS TLD")
|
2021-10-07 11:00:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-14 11:56:51 +00:00
|
|
|
// Update method updates contract source code and manifest. It can be invoked
|
|
|
|
// by committee only.
|
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-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("container contract updated")
|
|
|
|
}
|
|
|
|
|
2022-04-14 11:56:51 +00:00
|
|
|
// Put method creates a new container if it has been invoked by Alphabet nodes
|
|
|
|
// of the Inner Ring. Otherwise, it produces containerPut notification.
|
2021-07-07 15:12:28 +00:00
|
|
|
//
|
2022-04-14 11:56:51 +00:00
|
|
|
// Container should be a stable marshaled Container structure from API.
|
|
|
|
// Signature is a RFC6979 signature of the Container.
|
|
|
|
// PublicKey contains the public key of the signer.
|
|
|
|
// Token is optional and should be a stable marshaled SessionToken structure from
|
2021-07-07 15:12:28 +00:00
|
|
|
// 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-10-18 11:12:26 +00:00
|
|
|
alphabet := common.AlphabetNodes()
|
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-10-18 11:44:06 +00:00
|
|
|
containerFee := contract.Call(netmapContractAddr, "config", contract.ReadOnly, RegistrationFeeKey).(int)
|
2021-06-15 12:34:45 +00:00
|
|
|
balance := contract.Call(balanceContractAddr, "balanceOf", contract.ReadOnly, from).(int)
|
2021-10-18 10:45:10 +00:00
|
|
|
if name != "" {
|
|
|
|
aliasFee := contract.Call(netmapContractAddr, "config", contract.ReadOnly, AliasFeeKey).(int)
|
|
|
|
containerFee += aliasFee
|
|
|
|
}
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2021-10-18 11:12:26 +00:00
|
|
|
if balance < containerFee*len(alphabet) {
|
|
|
|
panic("insufficient balance to create container")
|
2021-06-15 12:34:45 +00:00
|
|
|
}
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2021-04-29 13:14:25 +00:00
|
|
|
if notaryDisabled {
|
2021-10-18 11:12:26 +00:00
|
|
|
nodeKey := common.InnerRingInvoker(alphabet)
|
|
|
|
if len(nodeKey) == 0 {
|
|
|
|
runtime.Notify("containerPut", container, signature, publicKey, token)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-04-29 13:14:25 +00:00
|
|
|
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-10-18 11:12:26 +00:00
|
|
|
} else {
|
|
|
|
multiaddr := common.AlphabetAddress()
|
2021-11-29 12:21:40 +00:00
|
|
|
common.CheckAlphabetWitness(multiaddr)
|
2021-04-29 13:14:25 +00:00
|
|
|
}
|
2021-10-18 11:12:26 +00:00
|
|
|
// todo: check if new container with unique container id
|
|
|
|
|
|
|
|
details := common.ContainerFeeTransferDetails(containerID)
|
2021-04-29 13:14:25 +00:00
|
|
|
|
2021-03-24 08:10:31 +00:00
|
|
|
for i := 0; i < len(alphabet); i++ {
|
|
|
|
node := alphabet[i]
|
2022-03-21 11:01:45 +00:00
|
|
|
to := contract.CreateStandardAccount(node)
|
2021-02-19 15:42:25 +00:00
|
|
|
|
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 {
|
|
|
|
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-09-29 13:10:29 +00:00
|
|
|
|
|
|
|
key := append([]byte(nnsHasAliasKey), containerID...)
|
|
|
|
storage.Put(ctx, key, domain)
|
2021-09-29 10:00:32 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2021-11-30 09:02:59 +00:00
|
|
|
runtime.Log("added new container")
|
2022-04-01 05:52:54 +00:00
|
|
|
runtime.Notify("PutSuccess", containerID, publicKey)
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2022-04-14 11:56:51 +00:00
|
|
|
// checkNiceNameAvailable checks if the nice name is available for the container.
|
2021-09-29 10:00:32 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2021-10-18 11:32:17 +00:00
|
|
|
owner := contract.Call(nnsContractAddr, "ownerOf",
|
|
|
|
contract.ReadStates|contract.AllowCall, domain).(string)
|
2021-10-21 15:59:59 +00:00
|
|
|
if owner != string(common.CommitteeAddress()) && owner != string(runtime.GetExecutingScriptHash()) {
|
|
|
|
panic("committee or container contract must own registered domain")
|
2021-10-18 11:32:17 +00:00
|
|
|
}
|
|
|
|
|
2021-09-29 10:00:32 +00:00
|
|
|
res := contract.Call(nnsContractAddr, "getRecords",
|
|
|
|
contract.ReadStates|contract.AllowCall, domain, 16 /* TXT */)
|
|
|
|
if res != nil {
|
|
|
|
panic("name is already taken")
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-04-14 11:56:51 +00:00
|
|
|
// Delete method removes a container from the contract storage if it has been
|
|
|
|
// invoked by Alphabet nodes of the Inner Ring. Otherwise, it produces
|
2021-07-07 15:12:28 +00:00
|
|
|
// containerDelete notification.
|
|
|
|
//
|
2022-04-14 11:56:51 +00:00
|
|
|
// Signature is a RFC6979 signature of the container ID.
|
|
|
|
// Token is optional and should be a stable marshaled SessionToken structure from
|
2021-07-07 15:12:28 +00:00
|
|
|
// API.
|
2021-10-11 07:42:14 +00:00
|
|
|
//
|
2022-04-14 11:56:51 +00:00
|
|
|
// If the container doesn't exist, it panics with NotFoundError.
|
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)
|
2021-10-11 07:42:14 +00:00
|
|
|
if ownerID == nil {
|
2021-10-25 12:43:57 +00:00
|
|
|
return
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2021-04-29 13:14:25 +00:00
|
|
|
if notaryDisabled {
|
2021-10-19 07:59:40 +00:00
|
|
|
alphabet := common.AlphabetNodes()
|
|
|
|
nodeKey := common.InnerRingInvoker(alphabet)
|
|
|
|
if len(nodeKey) == 0 {
|
|
|
|
runtime.Notify("containerDelete", containerID, signature, token)
|
|
|
|
return
|
|
|
|
}
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2021-04-29 13:14:25 +00:00
|
|
|
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-10-19 07:59:40 +00:00
|
|
|
} else {
|
|
|
|
multiaddr := common.AlphabetAddress()
|
2021-11-29 12:21:40 +00:00
|
|
|
common.CheckAlphabetWitness(multiaddr)
|
2021-04-29 13:14:25 +00:00
|
|
|
}
|
|
|
|
|
2021-09-29 13:10:29 +00:00
|
|
|
key := append([]byte(nnsHasAliasKey), containerID...)
|
|
|
|
domain := storage.Get(ctx, key).(string)
|
|
|
|
if len(domain) != 0 {
|
|
|
|
storage.Delete(ctx, key)
|
|
|
|
// We should do `getRecord` first because NNS record could be deleted
|
2022-04-14 11:56:51 +00:00
|
|
|
// by other means (expiration, manual), thus leading to failing `deleteRecord`
|
|
|
|
// and inability to delete a container. We should also check if we own the record in case.
|
2021-09-29 13:10:29 +00:00
|
|
|
nnsContractAddr := storage.Get(ctx, nnsContractKey).(interop.Hash160)
|
|
|
|
res := contract.Call(nnsContractAddr, "getRecords", contract.ReadStates|contract.AllowCall, domain, 16 /* TXT */)
|
|
|
|
if res != nil && std.Base58Encode(containerID) == string(res.([]interface{})[0].(string)) {
|
|
|
|
contract.Call(nnsContractAddr, "deleteRecords", contract.All, domain, 16 /* TXT */)
|
|
|
|
}
|
|
|
|
}
|
2021-02-19 15:42:25 +00:00
|
|
|
removeContainer(ctx, containerID, ownerID)
|
2021-11-30 09:02:59 +00:00
|
|
|
runtime.Log("remove container")
|
2022-04-01 05:52:54 +00:00
|
|
|
runtime.Notify("DeleteSuccess", containerID)
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2022-04-14 11:56:51 +00:00
|
|
|
// Get method returns a structure that contains a stable marshaled Container structure,
|
|
|
|
// the signature, the public key of the container creator and a stable marshaled SessionToken
|
2021-07-07 15:12:28 +00:00
|
|
|
// structure if it was provided.
|
2021-10-11 07:42:14 +00:00
|
|
|
//
|
2022-04-14 11:56:51 +00:00
|
|
|
// If the container doesn't exist, it panics with NotFoundError.
|
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-10-11 07:42:14 +00:00
|
|
|
cnt := getContainer(ctx, containerID)
|
|
|
|
if len(cnt.value) == 0 {
|
|
|
|
panic(NotFoundError)
|
|
|
|
}
|
|
|
|
return cnt
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2022-04-14 11:56:51 +00:00
|
|
|
// Owner method returns a 25 byte Owner ID of the container.
|
2021-10-11 07:42:14 +00:00
|
|
|
//
|
2022-04-14 11:56:51 +00:00
|
|
|
// If the container doesn't exist, it panics with NotFoundError.
|
2020-10-27 12:14:06 +00:00
|
|
|
func Owner(containerID []byte) []byte {
|
2021-03-09 19:15:58 +00:00
|
|
|
ctx := storage.GetReadOnlyContext()
|
2021-10-11 07:42:14 +00:00
|
|
|
owner := getOwnerByID(ctx, containerID)
|
|
|
|
if owner == nil {
|
|
|
|
panic(NotFoundError)
|
|
|
|
}
|
|
|
|
return owner
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2022-05-24 09:55:36 +00:00
|
|
|
// Count method returns the number of registered containers.
|
|
|
|
func Count() int {
|
|
|
|
count := 0
|
|
|
|
ctx := storage.GetReadOnlyContext()
|
|
|
|
it := storage.Find(ctx, []byte{}, storage.KeysOnly)
|
|
|
|
for iterator.Next(it) {
|
|
|
|
key := iterator.Value(it).([]byte)
|
|
|
|
// V2 format
|
|
|
|
if len(key) == containerIDSize {
|
|
|
|
count++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return count
|
|
|
|
}
|
|
|
|
|
2022-04-14 11:56:51 +00:00
|
|
|
// List method returns a list of all container IDs owned by the 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
|
|
|
|
}
|
|
|
|
|
2022-04-14 11:56:51 +00:00
|
|
|
// SetEACL method sets a new extended ACL table related to the contract
|
|
|
|
// if it was invoked by Alphabet nodes of the Inner Ring. Otherwise, it produces
|
2021-07-07 15:12:28 +00:00
|
|
|
// setEACL notification.
|
|
|
|
//
|
2022-04-14 11:56:51 +00:00
|
|
|
// EACL should be a stable marshaled EACLTable structure from API.
|
|
|
|
// Signature is a RFC6979 signature of the Container.
|
|
|
|
// PublicKey contains the public key of the signer.
|
|
|
|
// Token is optional and should be a stable marshaled SessionToken structure from
|
2021-07-07 15:12:28 +00:00
|
|
|
// API.
|
2021-10-11 07:42:14 +00:00
|
|
|
//
|
2022-04-14 11:56:51 +00:00
|
|
|
// If the container doesn't exist, it panics with NotFoundError.
|
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
|
|
|
|
2021-11-29 17:58:27 +00:00
|
|
|
// V2 format
|
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)
|
2021-10-11 07:42:14 +00:00
|
|
|
if ownerID == nil {
|
|
|
|
panic(NotFoundError)
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2021-05-11 14:42:02 +00:00
|
|
|
if notaryDisabled {
|
2021-10-19 08:23:35 +00:00
|
|
|
alphabet := common.AlphabetNodes()
|
|
|
|
nodeKey := common.InnerRingInvoker(alphabet)
|
|
|
|
if len(nodeKey) == 0 {
|
|
|
|
runtime.Notify("setEACL", eACL, signature, publicKey, token)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
threshold := len(alphabet)*2/3 + 1
|
|
|
|
id := common.InvokeID([]interface{}{eACL}, []byte("setEACL"))
|
|
|
|
|
|
|
|
n := common.Vote(ctx, id, nodeKey)
|
|
|
|
if n < threshold {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
common.RemoveVotes(ctx, id)
|
2021-05-11 14:42:02 +00:00
|
|
|
} else {
|
|
|
|
multiaddr := common.AlphabetAddress()
|
2021-11-29 12:21:40 +00:00
|
|
|
common.CheckAlphabetWitness(multiaddr)
|
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
|
|
|
|
2021-02-02 17:36:20 +00:00
|
|
|
common.SetSerialized(ctx, key, rule)
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2021-11-30 09:02:59 +00:00
|
|
|
runtime.Log("success")
|
2022-04-01 05:52:54 +00:00
|
|
|
runtime.Notify("SetEACLSuccess", containerID, publicKey)
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2022-04-14 11:56:51 +00:00
|
|
|
// EACL method returns a structure that contains a stable marshaled EACLTable structure,
|
|
|
|
// the signature, the public key of the extended ACL setter and a stable marshaled SessionToken
|
2021-07-07 15:12:28 +00:00
|
|
|
// structure if it was provided.
|
2021-10-11 07:42:14 +00:00
|
|
|
//
|
2022-04-14 11:56:51 +00:00
|
|
|
// If the container doesn't exist, it panics with NotFoundError.
|
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)
|
2021-10-11 07:42:14 +00:00
|
|
|
if ownerID == nil {
|
|
|
|
panic(NotFoundError)
|
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
|
2022-04-14 11:56:51 +00:00
|
|
|
// memory. It can be invoked only by Storage nodes from the network map. This method
|
2021-07-07 15:12:28 +00:00
|
|
|
// checks witness based on the provided public key of the Storage node.
|
2021-10-11 07:42:14 +00:00
|
|
|
//
|
2022-04-14 11:56:51 +00:00
|
|
|
// If the container doesn't exist, it panics with NotFoundError.
|
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-10-11 07:42:14 +00:00
|
|
|
if getOwnerByID(ctx, cid) == nil {
|
|
|
|
panic(NotFoundError)
|
|
|
|
}
|
|
|
|
|
2021-11-29 12:21:40 +00:00
|
|
|
common.CheckWitness(pubKey)
|
2021-01-22 16:45:26 +00:00
|
|
|
|
2021-03-09 19:15:58 +00:00
|
|
|
if !isStorageNode(ctx, pubKey) {
|
2021-11-29 10:51:57 +00:00
|
|
|
panic("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))
|
2022-01-18 07:08:13 +00:00
|
|
|
updateEstimations(ctx, epoch, cid, pubKey, false)
|
2021-01-22 16:45:26 +00:00
|
|
|
|
2021-11-30 09:02:59 +00:00
|
|
|
runtime.Log("saved container size estimation")
|
2021-01-22 16:45:26 +00:00
|
|
|
}
|
|
|
|
|
2022-04-14 11:56:51 +00:00
|
|
|
// GetContainerSize method returns the container ID and a slice of container
|
|
|
|
// estimations. Container estimation includes the public key of the Storage Node
|
|
|
|
// that registered estimation and value of estimation.
|
2021-07-07 15:12:28 +00:00
|
|
|
//
|
2022-04-14 11:56:51 +00:00
|
|
|
// Use the ID obtained from ListContainerSizes method. Estimations are removed
|
|
|
|
// from contract storage every epoch, see NewEpoch method; therefore, this method
|
|
|
|
// can return different results during 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
|
|
|
|
2021-11-29 17:58:27 +00:00
|
|
|
// V2 format
|
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
|
|
|
}
|
|
|
|
|
2022-04-14 11:56:51 +00:00
|
|
|
// ListContainerSizes method returns the IDs of container size estimations
|
|
|
|
// that has been registered for the 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
|
2022-04-14 11:56:51 +00:00
|
|
|
// epochNum + 3. It 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-11-29 10:51:57 +00:00
|
|
|
panic("method must be invoked by inner ring")
|
2021-04-29 13:14:25 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
multiaddr := common.AlphabetAddress()
|
2021-11-29 12:21:40 +00:00
|
|
|
common.CheckAlphabetWitness(multiaddr)
|
2021-01-25 20:16:14 +00:00
|
|
|
}
|
|
|
|
|
2022-01-17 09:36:56 +00:00
|
|
|
cleanupContainers(ctx, epochNum)
|
2021-01-25 20:16:14 +00:00
|
|
|
}
|
|
|
|
|
2021-07-07 15:12:28 +00:00
|
|
|
// StartContainerEstimation method produces StartEstimation notification.
|
2022-04-14 11:56:51 +00:00
|
|
|
// It 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
|
2022-03-21 11:01:45 +00:00
|
|
|
alphabet []interop.PublicKey
|
2021-04-29 13:14:25 +00:00
|
|
|
nodeKey []byte
|
|
|
|
)
|
|
|
|
|
|
|
|
if notaryDisabled {
|
|
|
|
alphabet = common.AlphabetNodes()
|
|
|
|
nodeKey = common.InnerRingInvoker(alphabet)
|
|
|
|
if len(nodeKey) == 0 {
|
2021-11-29 10:51:57 +00:00
|
|
|
panic("method must be invoked by inner ring")
|
2021-04-29 13:14:25 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
multiaddr := common.AlphabetAddress()
|
2021-11-29 12:21:40 +00:00
|
|
|
common.CheckAlphabetWitness(multiaddr)
|
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)
|
2021-11-30 09:02:59 +00:00
|
|
|
runtime.Log("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.
|
2022-04-14 11:56:51 +00:00
|
|
|
// It 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
|
2022-03-21 11:01:45 +00:00
|
|
|
alphabet []interop.PublicKey
|
2021-04-29 13:14:25 +00:00
|
|
|
nodeKey []byte
|
|
|
|
)
|
|
|
|
|
|
|
|
if notaryDisabled {
|
|
|
|
alphabet = common.AlphabetNodes()
|
|
|
|
nodeKey = common.InnerRingInvoker(alphabet)
|
|
|
|
if len(nodeKey) == 0 {
|
2021-11-29 10:51:57 +00:00
|
|
|
panic("method must be invoked by inner ring")
|
2021-04-29 13:14:25 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
multiaddr := common.AlphabetAddress()
|
2021-11-29 12:21:40 +00:00
|
|
|
common.CheckAlphabetWitness(multiaddr)
|
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)
|
2021-11-30 09:02:59 +00:00
|
|
|
runtime.Log("notification has been produced")
|
2021-01-26 17:48:25 +00:00
|
|
|
}
|
|
|
|
|
2022-04-14 11:56:51 +00:00
|
|
|
// Version returns the 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`
|
2021-11-29 17:58:27 +00:00
|
|
|
// V2 format
|
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 {
|
2021-11-29 17:58:27 +00:00
|
|
|
// V2 format
|
2021-06-30 13:59:50 +00:00
|
|
|
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
|
|
|
|
|
2021-11-29 16:56:19 +00:00
|
|
|
it := storage.Find(ctx, key, storage.ValuesOnly|storage.DeserializeValues)
|
2021-06-30 16:08:59 +00:00
|
|
|
for iterator.Next(it) {
|
2021-11-29 16:56:19 +00:00
|
|
|
est := iterator.Value(it).(estimation)
|
2021-06-30 16:08:59 +00:00
|
|
|
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
|
2022-04-14 11:56:51 +00:00
|
|
|
// announces container size estimation of the 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 {
|
2021-11-29 17:58:27 +00:00
|
|
|
// V2 format
|
2021-01-22 16:45:26 +00:00
|
|
|
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
|
|
|
|
2022-01-18 07:08:13 +00:00
|
|
|
func updateEstimations(ctx storage.Context, epoch int, cid []byte, pub interop.PublicKey, isUpdate bool) {
|
|
|
|
h := crypto.Ripemd160(pub)
|
|
|
|
estKey := append([]byte(singleEstimatePrefix), cid...)
|
|
|
|
estKey = append(estKey, h...)
|
|
|
|
|
|
|
|
var newEpochs []int
|
|
|
|
rawList := storage.Get(ctx, estKey).([]byte)
|
|
|
|
|
|
|
|
if rawList != nil {
|
|
|
|
epochs := std.Deserialize(rawList).([]int)
|
|
|
|
for _, oldEpoch := range epochs {
|
|
|
|
if !isUpdate && epoch-oldEpoch > CleanupDelta {
|
|
|
|
key := append([]byte(estimateKeyPrefix), convert.ToBytes(oldEpoch)...)
|
|
|
|
key = append(key, cid...)
|
|
|
|
key = append(key, h[:estimatePostfixSize]...)
|
|
|
|
storage.Delete(ctx, key)
|
|
|
|
} else {
|
|
|
|
newEpochs = append(newEpochs, oldEpoch)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
newEpochs = append(newEpochs, epoch)
|
|
|
|
common.SetSerialized(ctx, estKey, newEpochs)
|
|
|
|
}
|
|
|
|
|
2022-01-17 09:36:56 +00:00
|
|
|
func cleanupContainers(ctx storage.Context, epoch int) {
|
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)
|
2021-11-29 17:58:27 +00:00
|
|
|
// V2 format
|
2021-06-30 16:08:59 +00:00
|
|
|
nbytes := k[len(estimateKeyPrefix) : len(k)-containerIDSize-estimatePostfixSize]
|
2021-01-25 20:16:14 +00:00
|
|
|
|
|
|
|
var n interface{} = nbytes
|
|
|
|
|
2022-01-18 07:08:13 +00:00
|
|
|
if epoch-n.(int) > TotalCleanupDelta {
|
2022-01-17 09:36:56 +00:00
|
|
|
storage.Delete(ctx, k)
|
2021-01-25 20:16:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|