2023-08-18 14:12:24 +00:00
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
2023-08-24 12:27:24 +00:00
|
|
|
"crypto/sha256"
|
2023-08-18 14:12:24 +00:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
containerContract "git.frostfs.info/TrueCloudLab/frostfs-contract/container"
|
|
|
|
containercore "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/core/container"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/client"
|
|
|
|
apistatus "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client/status"
|
|
|
|
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
|
2023-11-14 11:21:58 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/user"
|
|
|
|
"github.com/mr-tron/base58"
|
2023-08-18 14:12:24 +00:00
|
|
|
)
|
|
|
|
|
2023-08-24 12:27:24 +00:00
|
|
|
func (x *containerSource) DeletionInfo(cnr cid.ID) (*containercore.DelInfo, error) {
|
|
|
|
return DeletionInfo((*Client)(x), cnr)
|
|
|
|
}
|
|
|
|
|
|
|
|
type deletionInfo interface {
|
|
|
|
DeletionInfo(cid []byte) (*containercore.DelInfo, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
func DeletionInfo(c deletionInfo, cnr cid.ID) (*containercore.DelInfo, error) {
|
|
|
|
binCnr := make([]byte, sha256.Size)
|
|
|
|
cnr.Encode(binCnr)
|
|
|
|
|
|
|
|
return c.DeletionInfo(binCnr)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) DeletionInfo(cid []byte) (*containercore.DelInfo, error) {
|
2023-08-18 14:12:24 +00:00
|
|
|
prm := client.TestInvokePrm{}
|
|
|
|
prm.SetMethod(deletionInfoMethod)
|
|
|
|
prm.SetArgs(cid)
|
|
|
|
|
|
|
|
res, err := c.client.TestInvoke(prm)
|
|
|
|
if err != nil {
|
|
|
|
if strings.Contains(err.Error(), containerContract.NotFoundError) {
|
|
|
|
return nil, new(apistatus.ContainerNotFound)
|
|
|
|
}
|
2024-12-04 08:17:13 +00:00
|
|
|
return nil, fmt.Errorf("test invoke (%s): %w", deletionInfoMethod, err)
|
2023-08-18 14:12:24 +00:00
|
|
|
} else if ln := len(res); ln != 1 {
|
|
|
|
return nil, fmt.Errorf("unexpected stack item count (%s): %d", deletionInfoMethod, ln)
|
|
|
|
}
|
|
|
|
|
|
|
|
arr, err := client.ArrayFromStackItem(res[0])
|
|
|
|
if err != nil {
|
2024-12-12 12:06:20 +00:00
|
|
|
return nil, fmt.Errorf("get item array of container (%s): %w", deletionInfoMethod, err)
|
2023-08-18 14:12:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(arr) != 2 {
|
|
|
|
return nil, fmt.Errorf("unexpected container stack item count (%s): %d", deletionInfoMethod, len(arr))
|
|
|
|
}
|
|
|
|
|
2023-11-14 11:21:58 +00:00
|
|
|
rawOwner, err := client.BytesFromStackItem(arr[0])
|
2023-08-18 14:12:24 +00:00
|
|
|
if err != nil {
|
2024-12-12 12:06:20 +00:00
|
|
|
return nil, fmt.Errorf("get byte array of container (%s): %w", deletionInfoMethod, err)
|
2023-08-18 14:12:24 +00:00
|
|
|
}
|
|
|
|
|
2023-11-14 11:21:58 +00:00
|
|
|
var owner user.ID
|
|
|
|
if err := owner.DecodeString(base58.Encode(rawOwner)); err != nil {
|
2024-12-12 12:06:20 +00:00
|
|
|
return nil, fmt.Errorf("decode container owner id (%s): %w", deletionInfoMethod, err)
|
2023-11-14 11:21:58 +00:00
|
|
|
}
|
|
|
|
|
2023-11-14 11:47:31 +00:00
|
|
|
epoch, err := client.BigIntFromStackItem(arr[1])
|
2023-08-18 14:12:24 +00:00
|
|
|
if err != nil {
|
2024-12-12 12:06:20 +00:00
|
|
|
return nil, fmt.Errorf("get byte array of container signature (%s): %w", deletionInfoMethod, err)
|
2023-08-18 14:12:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &containercore.DelInfo{
|
|
|
|
Owner: owner,
|
2023-11-14 11:47:31 +00:00
|
|
|
Epoch: epoch.Uint64(),
|
2023-08-18 14:12:24 +00:00
|
|
|
}, nil
|
|
|
|
}
|