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 (
|
2021-06-30 13:59:50 +00:00
|
|
|
version = 1
|
2020-10-27 12:14:06 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
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-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 {
|
2021-06-30 16:09:10 +00:00
|
|
|
migrateContainerLists(ctx) // from v0.9.1 to v0.9.2
|
|
|
|
migrateEstimationStorage(ctx) // from v0.9.1 to v0.9.2
|
2021-06-03 07:49:07 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-05-12 08:31:07 +00:00
|
|
|
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-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-06-30 14:09:59 +00:00
|
|
|
func migrateContainerLists(ctx storage.Context) {
|
|
|
|
const ownersKey = "ownersList"
|
|
|
|
|
|
|
|
containers := getAllContainers(ctx)
|
|
|
|
for i := range containers {
|
|
|
|
containerID := containers[i]
|
|
|
|
ownerID := getOwnerByID(ctx, containerID)
|
|
|
|
|
|
|
|
containerListKey := append(ownerID, containerID...)
|
|
|
|
storage.Put(ctx, containerListKey, containerID)
|
|
|
|
|
|
|
|
storage.Delete(ctx, ownerID)
|
|
|
|
}
|
|
|
|
|
|
|
|
storage.Delete(ctx, ownersKey)
|
|
|
|
}
|
|
|
|
|
2021-06-30 16:09:10 +00:00
|
|
|
func migrateEstimationStorage(ctx storage.Context) {
|
|
|
|
// In fact, this method does not migrate estimation storage because this data
|
|
|
|
// is valid only for one epoch. Migration routine will be quite complex, so
|
|
|
|
// it makes sense to clean all remaining estimations and wait for new one.
|
|
|
|
|
|
|
|
it := storage.Find(ctx, []byte(estimateKeyPrefix), storage.KeysOnly)
|
|
|
|
for iterator.Next(it) {
|
|
|
|
key := iterator.Value(it).([]byte)
|
|
|
|
storage.Delete(ctx, key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-03 07:49:07 +00:00
|
|
|
func Migrate(script []byte, manifest []byte, data interface{}) 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
|
|
|
|
}
|
|
|
|
|
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")
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-05-21 11:37:31 +00:00
|
|
|
func Put(container []byte, signature interop.Signature, publicKey interop.PublicKey, 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
|
|
|
|
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-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)
|
|
|
|
|
|
|
|
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-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 {
|
|
|
|
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)
|
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-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-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-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 {
|
|
|
|
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-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-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-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) {
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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-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-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 {
|
|
|
|
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 {
|
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-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 {
|
|
|
|
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 {
|
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
|
|
|
}
|
|
|
|
|
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) {
|
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)
|
|
|
|
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
|
|
|
|
}
|