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/binary"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/contract"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/crypto"
|
2020-12-18 11:07:33 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/iterator"
|
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
|
|
|
|
}
|
|
|
|
|
2020-10-27 12:14:06 +00:00
|
|
|
extendedACL struct {
|
|
|
|
val []byte
|
|
|
|
sig []byte
|
2021-01-14 15:33:50 +00:00
|
|
|
pub interop.PublicKey
|
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"
|
|
|
|
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 (
|
|
|
|
containerFeeTransferMsg = []byte("container creation fee")
|
|
|
|
eACLPrefix = []byte("eACL")
|
|
|
|
|
|
|
|
ctx storage.Context
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
if runtime.GetTrigger() != runtime.Application {
|
|
|
|
panic("contract has not been called in application node")
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx = storage.GetContext()
|
|
|
|
}
|
|
|
|
|
|
|
|
func Init(addrNetmap, addrBalance, addrID []byte) {
|
|
|
|
if storage.Get(ctx, netmapContractKey) != nil &&
|
|
|
|
storage.Get(ctx, balanceContractKey) != nil &&
|
|
|
|
storage.Get(ctx, neofsIDContractKey) != nil {
|
|
|
|
panic("init: contract already deployed")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(addrNetmap) != 20 || len(addrBalance) != 20 || len(addrID) != 20 {
|
|
|
|
panic("init: incorrect length of contract script hash")
|
|
|
|
}
|
|
|
|
|
|
|
|
storage.Put(ctx, netmapContractKey, addrNetmap)
|
|
|
|
storage.Put(ctx, balanceContractKey, addrBalance)
|
|
|
|
storage.Put(ctx, neofsIDContractKey, addrID)
|
|
|
|
|
|
|
|
runtime.Log("container contract initialized")
|
|
|
|
}
|
|
|
|
|
|
|
|
func Put(container, signature, publicKey []byte) bool {
|
|
|
|
netmapContractAddr := storage.Get(ctx, netmapContractKey).([]byte)
|
2021-02-02 19:20:31 +00:00
|
|
|
innerRing := common.InnerRingList(netmapContractAddr)
|
2020-10-27 12:14:06 +00:00
|
|
|
threshold := len(innerRing)/3*2 + 1
|
|
|
|
|
|
|
|
offset := int(container[1])
|
|
|
|
offset = 2 + offset + 4 // version prefix + version size + owner prefix
|
|
|
|
ownerID := container[offset : offset+25] // offset + size of owner
|
|
|
|
containerID := crypto.SHA256(container)
|
|
|
|
neofsIDContractAddr := storage.Get(ctx, neofsIDContractKey).([]byte)
|
|
|
|
|
|
|
|
// If invoked from storage node, ignore it.
|
|
|
|
// Inner ring will find tx, validate it and send it again.
|
2021-02-02 18:26:34 +00:00
|
|
|
irKey := common.InnerRingInvoker(innerRing)
|
2020-10-27 12:14:06 +00:00
|
|
|
if len(irKey) == 0 {
|
|
|
|
// check provided key
|
|
|
|
if !isSignedByOwnerKey(container, signature, ownerID, publicKey) {
|
|
|
|
// check keys from NeoFSID
|
2021-02-08 15:32:38 +00:00
|
|
|
keys := contract.Call(neofsIDContractAddr, "key", contract.ReadOnly, ownerID).([][]byte)
|
2020-10-27 12:14:06 +00:00
|
|
|
if !verifySignature(container, signature, keys) {
|
|
|
|
panic("put: invalid owner signature")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
runtime.Notify("containerPut", container, signature, publicKey)
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
from := walletToScripHash(ownerID)
|
|
|
|
balanceContractAddr := storage.Get(ctx, balanceContractKey).([]byte)
|
2021-02-08 15:32:38 +00:00
|
|
|
containerFee := contract.Call(netmapContractAddr, "config", contract.ReadOnly, containerFeeKey).(int)
|
2021-02-02 17:42:05 +00:00
|
|
|
hashCandidate := common.InvokeID([]interface{}{container, signature, publicKey}, []byte("put"))
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2021-02-02 17:36:20 +00:00
|
|
|
n := common.Vote(ctx, hashCandidate, irKey)
|
2020-10-27 12:14:06 +00:00
|
|
|
if n >= threshold {
|
2021-02-02 17:36:20 +00:00
|
|
|
common.RemoveVotes(ctx, hashCandidate)
|
2020-10-27 12:14:06 +00:00
|
|
|
// todo: check if new container with unique container id
|
|
|
|
|
|
|
|
for i := 0; i < len(innerRing); i++ {
|
|
|
|
node := innerRing[i]
|
2021-02-02 18:26:34 +00:00
|
|
|
to := contract.CreateStandardAccount(node.PublicKey)
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2020-12-10 14:45:45 +00:00
|
|
|
tx := contract.Call(balanceContractAddr, "transferX",
|
2021-02-08 15:32:38 +00:00
|
|
|
contract.All,
|
2020-10-27 12:14:06 +00:00
|
|
|
from,
|
|
|
|
to,
|
|
|
|
containerFee,
|
|
|
|
containerFeeTransferMsg, // consider add container id to the message
|
|
|
|
)
|
|
|
|
if !tx.(bool) {
|
|
|
|
// todo: consider using `return false` to remove votes
|
|
|
|
panic("put: can't transfer assets for container creation")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
addContainer(ctx, containerID, ownerID, container)
|
|
|
|
// try to remove underscore at v0.92.0
|
2021-02-08 15:32:38 +00:00
|
|
|
_ = contract.Call(neofsIDContractAddr, "addKey", contract.All, ownerID, [][]byte{publicKey})
|
2020-10-27 12:14:06 +00:00
|
|
|
|
|
|
|
runtime.Log("put: added new container")
|
|
|
|
} else {
|
|
|
|
runtime.Log("put: processed invoke from inner ring")
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func Delete(containerID, signature []byte) bool {
|
2021-02-02 19:20:31 +00:00
|
|
|
innerRing := irList()
|
2020-10-27 12:14:06 +00:00
|
|
|
threshold := len(innerRing)/3*2 + 1
|
|
|
|
|
|
|
|
ownerID := getOwnerByID(ctx, containerID)
|
|
|
|
if len(ownerID) == 0 {
|
|
|
|
panic("delete: container does not exist")
|
|
|
|
}
|
|
|
|
|
|
|
|
// If invoked from storage node, ignore it.
|
|
|
|
// Inner ring will find tx, validate it and send it again.
|
2021-02-02 18:26:34 +00:00
|
|
|
irKey := common.InnerRingInvoker(innerRing)
|
2020-10-27 12:14:06 +00:00
|
|
|
if len(irKey) == 0 {
|
|
|
|
// check provided key
|
|
|
|
neofsIDContractAddr := storage.Get(ctx, neofsIDContractKey).([]byte)
|
2021-02-08 15:32:38 +00:00
|
|
|
keys := contract.Call(neofsIDContractAddr, "key", contract.ReadOnly, ownerID).([][]byte)
|
2020-10-27 12:14:06 +00:00
|
|
|
|
|
|
|
if !verifySignature(containerID, signature, keys) {
|
|
|
|
panic("delete: invalid owner signature")
|
|
|
|
}
|
|
|
|
|
|
|
|
runtime.Notify("containerDelete", containerID, signature)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-02-02 17:42:05 +00:00
|
|
|
hashCandidate := common.InvokeID([]interface{}{containerID, signature}, []byte("delete"))
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2021-02-02 17:36:20 +00:00
|
|
|
n := common.Vote(ctx, hashCandidate, irKey)
|
2020-10-27 12:14:06 +00:00
|
|
|
if n >= threshold {
|
2021-02-02 17:36:20 +00:00
|
|
|
common.RemoveVotes(ctx, hashCandidate)
|
2020-10-27 12:14:06 +00:00
|
|
|
removeContainer(ctx, containerID, ownerID)
|
|
|
|
runtime.Log("delete: remove container")
|
|
|
|
} else {
|
|
|
|
runtime.Log("delete: processed invoke from inner ring")
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func Get(containerID []byte) []byte {
|
|
|
|
return storage.Get(ctx, containerID).([]byte)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Owner(containerID []byte) []byte {
|
|
|
|
return getOwnerByID(ctx, containerID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func List(owner []byte) [][]byte {
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func SetEACL(eACL, signature []byte) bool {
|
|
|
|
// 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")
|
|
|
|
}
|
|
|
|
|
|
|
|
neofsIDContractAddr := storage.Get(ctx, neofsIDContractKey).([]byte)
|
2021-02-08 15:32:38 +00:00
|
|
|
keys := contract.Call(neofsIDContractAddr, "key", contract.ReadOnly, ownerID).([][]byte)
|
2020-10-27 12:14:06 +00:00
|
|
|
|
|
|
|
if !verifySignature(eACL, signature, keys) {
|
|
|
|
panic("setEACL: invalid eACL signature")
|
|
|
|
}
|
|
|
|
|
|
|
|
rule := extendedACL{
|
|
|
|
val: eACL,
|
|
|
|
sig: signature,
|
|
|
|
}
|
|
|
|
|
|
|
|
key := append(eACLPrefix, containerID...)
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func EACL(containerID []byte) extendedACL {
|
2021-01-14 15:33:50 +00:00
|
|
|
ownerID := getOwnerByID(ctx, containerID)
|
|
|
|
if len(ownerID) == 0 {
|
|
|
|
panic("getEACL: container does not exists")
|
|
|
|
}
|
|
|
|
|
|
|
|
eacl := getEACL(ctx, containerID)
|
|
|
|
|
|
|
|
if len(eacl.sig) == 0 {
|
|
|
|
return eacl
|
|
|
|
}
|
|
|
|
|
|
|
|
// attach corresponding public key if it was not revoked from neofs id
|
|
|
|
|
|
|
|
neofsIDContractAddr := storage.Get(ctx, neofsIDContractKey).([]byte)
|
2021-02-08 15:32:38 +00:00
|
|
|
keys := contract.Call(neofsIDContractAddr, "key", contract.ReadOnly, ownerID).([][]byte)
|
2021-01-14 15:33:50 +00:00
|
|
|
|
|
|
|
for i := range keys {
|
|
|
|
key := keys[i]
|
|
|
|
if crypto.ECDsaSecp256r1Verify(eacl.val, key, eacl.sig) {
|
|
|
|
eacl.pub = key
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return eacl
|
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 {
|
|
|
|
if !runtime.CheckWitness(pubKey) {
|
|
|
|
panic("container: invalid witness for size estimation")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !isStorageNode(pubKey) {
|
|
|
|
panic("container: only storage nodes can save size estimations")
|
|
|
|
}
|
|
|
|
|
|
|
|
key := estimationKey(epoch, cid)
|
|
|
|
s := getContainerSizeEstimation(key, cid)
|
|
|
|
|
|
|
|
// 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,
|
|
|
|
})
|
|
|
|
|
|
|
|
storage.Put(ctx, key, binary.Serialize(s))
|
|
|
|
|
|
|
|
runtime.Log("container: saved container size estimation")
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetContainerSize(id []byte) containerSizes {
|
|
|
|
return getContainerSizeEstimation(id, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ListContainerSizes(epoch int) [][]byte {
|
|
|
|
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-01-25 20:16:14 +00:00
|
|
|
func ProcessEpoch(epochNum int) {
|
2021-02-02 19:20:31 +00:00
|
|
|
innerRing := irList()
|
2021-01-25 20:16:14 +00:00
|
|
|
threshold := len(innerRing)/3*2 + 1
|
|
|
|
|
2021-02-02 18:26:34 +00:00
|
|
|
irKey := common.InnerRingInvoker(innerRing)
|
2021-01-25 20:16:14 +00:00
|
|
|
if len(irKey) == 0 {
|
|
|
|
panic("processEpoch: this method must be invoked from inner ring")
|
|
|
|
}
|
|
|
|
|
|
|
|
candidates := keysToDelete(epochNum)
|
2021-02-02 17:42:05 +00:00
|
|
|
epochID := common.InvokeID([]interface{}{epochNum}, []byte("epoch"))
|
2021-01-25 20:16:14 +00:00
|
|
|
|
2021-02-02 17:36:20 +00:00
|
|
|
n := common.Vote(ctx, epochID, irKey)
|
2021-01-25 20:16:14 +00:00
|
|
|
if n >= threshold {
|
2021-02-02 17:36:20 +00:00
|
|
|
common.RemoveVotes(ctx, epochID)
|
2021-01-25 20:16:14 +00:00
|
|
|
|
|
|
|
for i := range candidates {
|
|
|
|
candidate := candidates[i]
|
|
|
|
storage.Delete(ctx, candidate)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-26 17:48:25 +00:00
|
|
|
func StartContainerEstimation(epoch int) bool {
|
2021-02-02 19:20:31 +00:00
|
|
|
innerRing := irList()
|
2021-01-26 17:48:25 +00:00
|
|
|
threshold := len(innerRing)/3*2 + 1
|
|
|
|
|
2021-02-02 18:26:34 +00:00
|
|
|
irKey := common.InnerRingInvoker(innerRing)
|
2021-01-26 17:48:25 +00:00
|
|
|
if len(irKey) == 0 {
|
|
|
|
panic("startEstimation: only inner ring nodes can invoke this")
|
|
|
|
}
|
|
|
|
|
2021-02-02 17:42:05 +00:00
|
|
|
hashCandidate := common.InvokeID([]interface{}{epoch}, []byte("startEstimation"))
|
2021-01-26 17:48:25 +00:00
|
|
|
|
2021-02-02 17:36:20 +00:00
|
|
|
n := common.Vote(ctx, hashCandidate, irKey)
|
2021-01-26 17:48:25 +00:00
|
|
|
if n >= threshold {
|
2021-02-02 17:36:20 +00:00
|
|
|
common.RemoveVotes(ctx, hashCandidate)
|
2021-01-26 17:48:25 +00:00
|
|
|
runtime.Notify("StartEstimation", epoch)
|
|
|
|
runtime.Log("startEstimation: notification has been produced")
|
|
|
|
} else {
|
|
|
|
runtime.Log("startEstimation: processed invoke from inner ring")
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func StopContainerEstimation(epoch int) bool {
|
2021-02-02 19:20:31 +00:00
|
|
|
innerRing := irList()
|
2021-01-26 17:48:25 +00:00
|
|
|
threshold := len(innerRing)/3*2 + 1
|
|
|
|
|
2021-02-02 18:26:34 +00:00
|
|
|
irKey := common.InnerRingInvoker(innerRing)
|
2021-01-26 17:48:25 +00:00
|
|
|
if len(irKey) == 0 {
|
|
|
|
panic("stopEstimation: only inner ring nodes can invoke this")
|
|
|
|
}
|
|
|
|
|
2021-02-02 17:42:05 +00:00
|
|
|
hashCandidate := common.InvokeID([]interface{}{epoch}, []byte("stopEstimation"))
|
2021-01-26 17:48:25 +00:00
|
|
|
|
2021-02-02 17:36:20 +00:00
|
|
|
n := common.Vote(ctx, hashCandidate, irKey)
|
2021-01-26 17:48:25 +00:00
|
|
|
if n >= threshold {
|
2021-02-02 17:36:20 +00:00
|
|
|
common.RemoveVotes(ctx, hashCandidate)
|
2021-01-26 17:48:25 +00:00
|
|
|
runtime.Notify("StopEstimation", epoch)
|
|
|
|
runtime.Log("stopEstimation: notification has been produced")
|
|
|
|
} else {
|
|
|
|
runtime.Log("stopEstimation: processed invoke from inner ring")
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-10-27 12:14:06 +00:00
|
|
|
func Version() int {
|
|
|
|
return version
|
|
|
|
}
|
|
|
|
|
|
|
|
func addContainer(ctx storage.Context, id []byte, owner []byte, container []byte) {
|
|
|
|
addOrAppend(ctx, ownersKey, owner)
|
|
|
|
addOrAppend(ctx, owner, id)
|
|
|
|
storage.Put(ctx, id, container)
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-10-27 12:14:06 +00:00
|
|
|
func getEACL(ctx storage.Context, cid []byte) extendedACL {
|
|
|
|
key := append(eACLPrefix, cid...)
|
|
|
|
data := storage.Get(ctx, key)
|
|
|
|
if data != nil {
|
|
|
|
return binary.Deserialize(data.([]byte)).(extendedACL)
|
|
|
|
}
|
|
|
|
|
2021-01-14 15:33:50 +00:00
|
|
|
return extendedACL{val: []byte{}, sig: []byte{}, pub: []byte{}}
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func walletToScripHash(wallet []byte) []byte {
|
|
|
|
return wallet[1 : len(wallet)-4]
|
|
|
|
}
|
|
|
|
|
|
|
|
func verifySignature(msg, sig []byte, keys [][]byte) bool {
|
|
|
|
for i := range keys {
|
|
|
|
key := keys[i]
|
|
|
|
if crypto.ECDsaSecp256r1Verify(msg, key, sig) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func isSignedByOwnerKey(msg, sig, owner, key []byte) bool {
|
|
|
|
if !isOwnerFromKey(owner, key) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return crypto.ECDsaSecp256r1Verify(msg, key, sig)
|
|
|
|
}
|
|
|
|
|
|
|
|
func isOwnerFromKey(owner []byte, key []byte) bool {
|
|
|
|
ownerSH := walletToScripHash(owner)
|
|
|
|
keySH := contract.CreateStandardAccount(key)
|
|
|
|
|
2021-02-02 17:36:20 +00:00
|
|
|
return common.BytesEqual(ownerSH, keySH)
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
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...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getContainerSizeEstimation(key, cid []byte) containerSizes {
|
|
|
|
data := storage.Get(ctx, key)
|
|
|
|
if data != nil {
|
|
|
|
return binary.Deserialize(data.([]byte)).(containerSizes)
|
|
|
|
}
|
|
|
|
|
|
|
|
return containerSizes{
|
|
|
|
cid: cid,
|
|
|
|
estimations: []estimation{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// isStorageNode looks into _previous_ epoch network map, because storage node
|
|
|
|
// announce container size estimation of previous epoch.
|
|
|
|
func isStorageNode(key interop.PublicKey) bool {
|
|
|
|
netmapContractAddr := storage.Get(ctx, netmapContractKey).([]byte)
|
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
|
|
|
|
|
|
|
func keysToDelete(epoch int) [][]byte {
|
|
|
|
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
|
|
|
|
}
|
2021-02-02 19:20:31 +00:00
|
|
|
|
|
|
|
func irList() []common.IRNode {
|
|
|
|
return common.InnerRingListViaStorage(ctx, netmapContractKey)
|
|
|
|
}
|