2020-10-27 12:14:06 +00:00
|
|
|
package containercontract
|
|
|
|
|
|
|
|
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 (
|
|
|
|
version = 1
|
|
|
|
ownersKey = "ownersList"
|
|
|
|
|
|
|
|
neofsIDContractKey = "identityScriptHash"
|
|
|
|
balanceContractKey = "balanceScriptHash"
|
|
|
|
netmapContractKey = "netmapScriptHash"
|
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
|
|
|
|
|
|
|
estimateKeyPrefix = "cnr"
|
2021-01-25 20:16:14 +00:00
|
|
|
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-05-12 08:31:07 +00:00
|
|
|
func _deploy(data interface{}, isUpdate bool) {
|
|
|
|
args := data.([]interface{})
|
|
|
|
notaryDisabled := args[0].(bool)
|
|
|
|
owner := args[1].(interop.Hash160)
|
|
|
|
addrNetmap := args[2].(interop.Hash160)
|
|
|
|
addrBalance := args[3].(interop.Hash160)
|
|
|
|
addrID := args[4].(interop.Hash160)
|
|
|
|
|
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-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(addrNetmap) != 20 || len(addrBalance) != 20 || len(addrID) != 20 {
|
|
|
|
panic("init: incorrect length of contract script hash")
|
|
|
|
}
|
|
|
|
|
2021-02-11 15:55:32 +00:00
|
|
|
storage.Put(ctx, common.OwnerKey, owner)
|
2020-10-27 12:14:06 +00:00
|
|
|
storage.Put(ctx, netmapContractKey, addrNetmap)
|
|
|
|
storage.Put(ctx, balanceContractKey, addrBalance)
|
|
|
|
storage.Put(ctx, neofsIDContractKey, addrID)
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-10-27 12:14:06 +00:00
|
|
|
runtime.Log("container contract initialized")
|
|
|
|
}
|
|
|
|
|
2021-02-11 15:55:32 +00:00
|
|
|
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("container contract updated")
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-05-21 13:27:01 +00:00
|
|
|
func Put(container []byte, signature interop.Signature, publicKey interop.PublicKey, token []byte) bool {
|
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
|
|
|
offset := int(container[1])
|
|
|
|
offset = 2 + offset + 4 // version prefix + version size + owner prefix
|
|
|
|
ownerID := container[offset : offset+25] // offset + size of owner
|
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-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)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !alphabetCall {
|
2021-05-21 13:27:01 +00:00
|
|
|
runtime.Notify("containerPut", container, signature, publicKey, token)
|
2020-10-27 12:14:06 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
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-04-07 06:17:00 +00:00
|
|
|
details := common.ContainerFeeTransferDetails(containerID)
|
2020-10-27 12:14:06 +00:00
|
|
|
|
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 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
tx := contract.Call(balanceContractAddr, "transferX",
|
|
|
|
contract.All,
|
|
|
|
from,
|
|
|
|
to,
|
|
|
|
containerFee,
|
2021-04-07 06:17:00 +00:00
|
|
|
details,
|
2021-02-19 15:42:25 +00:00
|
|
|
)
|
|
|
|
if !tx.(bool) {
|
|
|
|
panic("put: can't transfer assets for container creation")
|
|
|
|
}
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2021-05-21 13:27:01 +00:00
|
|
|
addContainer(ctx, containerID, ownerID, cnr)
|
|
|
|
|
|
|
|
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
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-05-21 13:27:01 +00:00
|
|
|
func Delete(containerID []byte, signature interop.Signature, token []byte) bool {
|
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 {
|
|
|
|
panic("delete: container does not exist")
|
|
|
|
}
|
|
|
|
|
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)
|
2020-10-27 12:14:06 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
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-02-02 18:34:17 +00:00
|
|
|
owners := common.GetList(ctx, ownersKey)
|
2020-10-27 12:14:06 +00:00
|
|
|
for i := 0; i < len(owners); i++ {
|
|
|
|
ownerID := owners[i]
|
2021-02-02 17:36:20 +00:00
|
|
|
if len(owner) != 0 && !common.BytesEqual(owner, ownerID) {
|
2020-10-27 12:14:06 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-02-02 18:34:17 +00:00
|
|
|
containers := common.GetList(ctx, ownerID)
|
2020-10-27 12:14:06 +00:00
|
|
|
|
|
|
|
for j := 0; j < len(containers); j++ {
|
|
|
|
container := containers[j]
|
|
|
|
list = append(list, container)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return list
|
|
|
|
}
|
|
|
|
|
2021-05-21 13:27:01 +00:00
|
|
|
func SetEACL(eACL []byte, signature interop.Signature, publicKey interop.PublicKey, token []byte) bool {
|
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 {
|
|
|
|
panic("setEACL: container does not exists")
|
|
|
|
}
|
|
|
|
|
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-11 14:42:02 +00:00
|
|
|
return true
|
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 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
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")
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
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-05-11 14:48:25 +00:00
|
|
|
panic("eACL: container does not exists")
|
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-01-22 16:45:26 +00:00
|
|
|
func PutContainerSize(epoch int, cid []byte, usedSize int, pubKey interop.PublicKey) bool {
|
2021-03-09 19:15:58 +00:00
|
|
|
ctx := storage.GetContext()
|
|
|
|
|
2021-01-22 16:45:26 +00:00
|
|
|
if !runtime.CheckWitness(pubKey) {
|
|
|
|
panic("container: invalid witness for size estimation")
|
|
|
|
}
|
|
|
|
|
2021-03-09 19:15:58 +00:00
|
|
|
if !isStorageNode(ctx, pubKey) {
|
2021-01-22 16:45:26 +00:00
|
|
|
panic("container: only storage nodes can save size estimations")
|
|
|
|
}
|
|
|
|
|
|
|
|
key := estimationKey(epoch, cid)
|
2021-03-09 19:15:58 +00:00
|
|
|
s := getContainerSizeEstimation(ctx, key, cid)
|
2021-01-22 16:45:26 +00:00
|
|
|
|
|
|
|
// do not add estimation twice
|
|
|
|
for i := range s.estimations {
|
|
|
|
est := s.estimations[i]
|
2021-02-02 17:36:20 +00:00
|
|
|
if common.BytesEqual(est.from, pubKey) {
|
2021-01-22 16:45:26 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
s.estimations = append(s.estimations, estimation{
|
|
|
|
from: pubKey,
|
|
|
|
size: usedSize,
|
|
|
|
})
|
|
|
|
|
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")
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetContainerSize(id []byte) containerSizes {
|
2021-03-09 19:15:58 +00:00
|
|
|
ctx := storage.GetReadOnlyContext()
|
|
|
|
return getContainerSizeEstimation(ctx, id, nil)
|
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
|
|
|
|
|
|
|
var result [][]byte
|
|
|
|
|
|
|
|
for iterator.Next(it) {
|
2021-02-08 15:23:08 +00:00
|
|
|
key := iterator.Value(it).([]byte) // it MUST BE `storage.KeysOnly`
|
2021-01-22 16:45:26 +00:00
|
|
|
result = append(result, key)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
panic("newEpoch: this method must be invoked from inner ring")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
multiaddr := common.AlphabetAddress()
|
|
|
|
if !runtime.CheckWitness(multiaddr) {
|
|
|
|
panic("newEpoch: this method must be invoked from inner ring")
|
|
|
|
}
|
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-01-26 17:48:25 +00:00
|
|
|
func StartContainerEstimation(epoch int) bool {
|
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 {
|
|
|
|
panic("startEstimation: only inner ring nodes can invoke this")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
multiaddr := common.AlphabetAddress()
|
|
|
|
if !runtime.CheckWitness(multiaddr) {
|
|
|
|
panic("startEstimation: only inner ring nodes can invoke this")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if notaryDisabled {
|
|
|
|
threshold := len(alphabet)*2/3 + 1
|
|
|
|
id := common.InvokeID([]interface{}{epoch}, []byte("startEstimation"))
|
|
|
|
|
|
|
|
n := common.Vote(ctx, id, nodeKey)
|
|
|
|
if n < threshold {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func StopContainerEstimation(epoch int) bool {
|
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 {
|
|
|
|
panic("stopEstimation: only inner ring nodes can invoke this")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
multiaddr := common.AlphabetAddress()
|
|
|
|
if !runtime.CheckWitness(multiaddr) {
|
|
|
|
panic("stopEstimation: only inner ring nodes can invoke this")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if notaryDisabled {
|
|
|
|
threshold := len(alphabet)*2/3 + 1
|
|
|
|
id := common.InvokeID([]interface{}{epoch}, []byte("stopEstimation"))
|
|
|
|
|
|
|
|
n := common.Vote(ctx, id, nodeKey)
|
|
|
|
if n < threshold {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-10-27 12:14:06 +00:00
|
|
|
func Version() int {
|
|
|
|
return version
|
|
|
|
}
|
|
|
|
|
2021-05-21 13:27:01 +00:00
|
|
|
func addContainer(ctx storage.Context, id, owner []byte, container Container) {
|
2020-10-27 12:14:06 +00:00
|
|
|
addOrAppend(ctx, ownersKey, owner)
|
|
|
|
addOrAppend(ctx, owner, 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) {
|
|
|
|
n := remove(ctx, owner, id)
|
|
|
|
|
|
|
|
// if it was last container, remove owner from the list of owners
|
|
|
|
if n == 0 {
|
|
|
|
_ = remove(ctx, ownersKey, owner)
|
|
|
|
}
|
|
|
|
|
|
|
|
storage.Delete(ctx, id)
|
|
|
|
}
|
|
|
|
|
|
|
|
func addOrAppend(ctx storage.Context, key interface{}, value []byte) {
|
2021-02-02 18:34:17 +00:00
|
|
|
list := common.GetList(ctx, key)
|
2020-10-27 12:14:06 +00:00
|
|
|
for i := 0; i < len(list); i++ {
|
2021-02-02 17:36:20 +00:00
|
|
|
if common.BytesEqual(list[i], value) {
|
2020-10-27 12:14:06 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(list) == 0 {
|
|
|
|
list = [][]byte{value}
|
|
|
|
} else {
|
|
|
|
list = append(list, value)
|
|
|
|
}
|
|
|
|
|
2021-02-02 17:36:20 +00:00
|
|
|
common.SetSerialized(ctx, key, list)
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// remove returns amount of left elements in the list
|
|
|
|
func remove(ctx storage.Context, key interface{}, value []byte) int {
|
|
|
|
var (
|
2021-02-02 18:34:17 +00:00
|
|
|
list = common.GetList(ctx, key)
|
2020-10-27 12:14:06 +00:00
|
|
|
newList = [][]byte{}
|
|
|
|
)
|
|
|
|
|
|
|
|
for i := 0; i < len(list); i++ {
|
2021-02-02 17:36:20 +00:00
|
|
|
if !common.BytesEqual(list[i], value) {
|
2020-10-27 12:14:06 +00:00
|
|
|
newList = append(newList, list[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ln := len(newList)
|
|
|
|
if ln == 0 {
|
|
|
|
storage.Delete(ctx, key)
|
|
|
|
} else {
|
2021-02-02 17:36:20 +00:00
|
|
|
common.SetSerialized(ctx, key, newList)
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ln
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func getOwnerByID(ctx storage.Context, id []byte) []byte {
|
2021-02-02 18:34:17 +00:00
|
|
|
owners := common.GetList(ctx, ownersKey)
|
2020-10-27 12:14:06 +00:00
|
|
|
for i := 0; i < len(owners); i++ {
|
|
|
|
ownerID := owners[i]
|
2021-02-02 18:34:17 +00:00
|
|
|
containers := common.GetList(ctx, ownerID)
|
2020-10-27 12:14:06 +00:00
|
|
|
|
|
|
|
for j := 0; j < len(containers); j++ {
|
|
|
|
container := containers[j]
|
2021-02-02 17:36:20 +00:00
|
|
|
if common.BytesEqual(container, id) {
|
2020-10-27 12:14:06 +00:00
|
|
|
return ownerID
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-22 16:45:26 +00:00
|
|
|
func estimationKey(epoch int, cid []byte) []byte {
|
|
|
|
var buf interface{} = epoch
|
|
|
|
|
|
|
|
result := []byte(estimateKeyPrefix)
|
|
|
|
result = append(result, buf.([]byte)...)
|
|
|
|
|
|
|
|
return append(result, cid...)
|
|
|
|
}
|
|
|
|
|
2021-03-09 19:15:58 +00:00
|
|
|
func getContainerSizeEstimation(ctx storage.Context, key, cid []byte) containerSizes {
|
2021-01-22 16:45:26 +00:00
|
|
|
data := storage.Get(ctx, key)
|
|
|
|
if data != nil {
|
2021-03-12 12:16:36 +00:00
|
|
|
return std.Deserialize(data.([]byte)).(containerSizes)
|
2021-01-22 16:45:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return containerSizes{
|
|
|
|
cid: cid,
|
|
|
|
estimations: []estimation{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-02-08 15:23:08 +00:00
|
|
|
k := iterator.Value(it).([]byte) // it MUST BE `storage.KeysOnly`
|
2021-01-25 20:16:14 +00:00
|
|
|
nbytes := k[len(estimateKeyPrefix) : len(k)-32]
|
|
|
|
|
|
|
|
var n interface{} = nbytes
|
|
|
|
|
|
|
|
if epoch-n.(int) > cleanupDelta {
|
|
|
|
results = append(results, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return results
|
|
|
|
}
|