forked from TrueCloudLab/frostfs-http-gw
73 lines
2 KiB
Go
73 lines
2 KiB
Go
package container
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
containercontract "git.frostfs.info/TrueCloudLab/frostfs-contract/container"
|
|
containerclient "git.frostfs.info/TrueCloudLab/frostfs-contract/rpcclient/container"
|
|
"git.frostfs.info/TrueCloudLab/frostfs-http-gw/internal/handler"
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container"
|
|
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient"
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient/actor"
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
|
"github.com/nspcc-dev/neo-go/pkg/wallet"
|
|
)
|
|
|
|
type Client struct {
|
|
contract *containerclient.Contract
|
|
}
|
|
|
|
type Config struct {
|
|
ContractHash util.Uint160
|
|
Key *keys.PrivateKey
|
|
RPCClient *rpcclient.Client
|
|
}
|
|
|
|
func New(cfg Config) (*Client, error) {
|
|
var err error
|
|
key := cfg.Key
|
|
if key == nil {
|
|
if key, err = keys.NewPrivateKey(); err != nil {
|
|
return nil, fmt.Errorf("generate anon private key for container contract: %w", err)
|
|
}
|
|
}
|
|
acc := wallet.NewAccountFromPrivateKey(key)
|
|
|
|
act, err := actor.NewSimple(cfg.RPCClient, acc)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("create new actor: %w", err)
|
|
}
|
|
|
|
return &Client{
|
|
contract: containerclient.New(act, cfg.ContractHash),
|
|
}, nil
|
|
}
|
|
|
|
func (c *Client) GetContainerByID(cnrID cid.ID) (*container.Container, error) {
|
|
items, err := c.contract.Get(cnrID[:])
|
|
if err != nil {
|
|
if strings.Contains(err.Error(), containercontract.NotFoundError) {
|
|
return nil, fmt.Errorf("%w: %s", handler.ErrContainerNotFound, err)
|
|
}
|
|
return nil, err
|
|
}
|
|
|
|
if len(items) != 4 {
|
|
return nil, fmt.Errorf("unexpected container stack item count: %d", len(items))
|
|
}
|
|
|
|
cnrBytes, err := items[0].TryBytes()
|
|
if err != nil {
|
|
return nil, fmt.Errorf("could not get byte array of container: %w", err)
|
|
}
|
|
|
|
var cnr container.Container
|
|
if err = cnr.Unmarshal(cnrBytes); err != nil {
|
|
return nil, fmt.Errorf("can't unmarshal container: %w", err)
|
|
}
|
|
|
|
return &cnr, nil
|
|
}
|