2023-12-05 09:12:35 +00:00
|
|
|
package contract
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-02-13 08:50:11 +00:00
|
|
|
"errors"
|
2023-12-05 09:12:35 +00:00
|
|
|
"fmt"
|
|
|
|
"math/big"
|
|
|
|
|
2024-02-13 08:50:11 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-contract/commonclient"
|
2023-12-05 09:12:35 +00:00
|
|
|
policycontract "git.frostfs.info/TrueCloudLab/frostfs-contract/policy"
|
|
|
|
policyclient "git.frostfs.info/TrueCloudLab/frostfs-contract/rpcclient/policy"
|
2023-12-08 07:44:13 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/internal/frostfs/policy"
|
2023-12-07 14:13:11 +00:00
|
|
|
frostfsutil "git.frostfs.info/TrueCloudLab/frostfs-s3-gw/internal/frostfs/util"
|
2023-12-21 14:57:12 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
2023-12-05 09:12:35 +00:00
|
|
|
"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"
|
2023-12-21 14:57:12 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient/notary"
|
2023-12-07 14:13:11 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2023-12-05 09:12:35 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/wallet"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Client struct {
|
|
|
|
actor *actor.Actor
|
|
|
|
policyContract *policyclient.Contract
|
2024-02-13 08:50:11 +00:00
|
|
|
contractHash util.Uint160
|
2023-12-05 09:12:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
// RPCAddress is an endpoint to connect to neo rpc.
|
|
|
|
RPCAddress string
|
|
|
|
|
|
|
|
// Contract is hash of contract or its name in NNS.
|
|
|
|
Contract string
|
|
|
|
|
2023-12-21 14:57:12 +00:00
|
|
|
// ProxyContract is hash of proxy contract or its name in NNS to interact with policy.
|
|
|
|
ProxyContract string
|
|
|
|
|
2023-12-05 09:12:35 +00:00
|
|
|
// Key is used to interact with policy contract.
|
|
|
|
// If this is nil than random key will be generated.
|
|
|
|
Key *keys.PrivateKey
|
|
|
|
}
|
|
|
|
|
2024-04-03 08:28:26 +00:00
|
|
|
const (
|
|
|
|
batchSize = 100
|
|
|
|
iteratorChainsByPrefix = "iteratorChainsByPrefix"
|
|
|
|
)
|
|
|
|
|
2023-12-08 07:44:13 +00:00
|
|
|
var _ policy.Contract = (*Client)(nil)
|
2023-12-05 09:12:35 +00:00
|
|
|
|
|
|
|
// New creates new Policy contract wrapper.
|
|
|
|
func New(ctx context.Context, cfg Config) (*Client, error) {
|
2023-12-07 14:13:11 +00:00
|
|
|
contractHash, err := frostfsutil.ResolveContractHash(cfg.Contract, cfg.RPCAddress)
|
2023-12-05 09:12:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("resolve frostfs contract hash: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
key := cfg.Key
|
|
|
|
if key == nil {
|
|
|
|
if key, err = keys.NewPrivateKey(); err != nil {
|
|
|
|
return nil, fmt.Errorf("generate anon private key for policy: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rpcCli, err := rpcclient.New(ctx, cfg.RPCAddress, rpcclient.Options{})
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("create policy rpc client: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-12-21 14:57:12 +00:00
|
|
|
proxyContractHash, err := frostfsutil.ResolveContractHash(cfg.ProxyContract, cfg.RPCAddress)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("resolve frostfs contract hash: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
act, err := actor.New(rpcCli, getSigners(key, proxyContractHash, contractHash))
|
2023-12-05 09:12:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("create new actor: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Client{
|
|
|
|
actor: act,
|
|
|
|
policyContract: policyclient.New(act, contractHash),
|
2024-02-13 08:50:11 +00:00
|
|
|
contractHash: contractHash,
|
2023-12-05 09:12:35 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-12-21 14:57:12 +00:00
|
|
|
func getSigners(key *keys.PrivateKey, proxyHash, contractHash util.Uint160) []actor.SignerAccount {
|
|
|
|
acc := wallet.NewAccountFromPrivateKey(key)
|
|
|
|
|
|
|
|
return []actor.SignerAccount{
|
|
|
|
{
|
|
|
|
Signer: transaction.Signer{
|
|
|
|
Account: proxyHash,
|
|
|
|
Scopes: transaction.CustomContracts,
|
|
|
|
AllowedContracts: []util.Uint160{contractHash},
|
|
|
|
},
|
|
|
|
Account: notary.FakeContractAccount(proxyHash),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Signer: transaction.Signer{
|
|
|
|
Account: acc.Contract.ScriptHash(),
|
|
|
|
Scopes: transaction.CalledByEntry,
|
|
|
|
},
|
|
|
|
Account: acc,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-08 07:44:13 +00:00
|
|
|
func (c *Client) AddChain(kind policycontract.Kind, entity string, name []byte, chain []byte) (util.Uint256, uint32, error) {
|
|
|
|
return c.policyContract.AddChain(big.NewInt(int64(kind)), entity, name, chain)
|
2023-12-05 09:12:35 +00:00
|
|
|
}
|
|
|
|
|
2023-12-08 07:44:13 +00:00
|
|
|
func (c *Client) GetChain(kind policycontract.Kind, entity string, name []byte) ([]byte, error) {
|
|
|
|
return c.policyContract.GetChain(big.NewInt(int64(kind)), entity, name)
|
2023-12-05 09:12:35 +00:00
|
|
|
}
|
|
|
|
|
2023-12-08 07:44:13 +00:00
|
|
|
func (c *Client) RemoveChain(kind policycontract.Kind, entity string, name []byte) (util.Uint256, uint32, error) {
|
|
|
|
return c.policyContract.RemoveChain(big.NewInt(int64(kind)), entity, name)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) ListChains(kind policycontract.Kind, entity string, name []byte) ([][]byte, error) {
|
2024-04-03 08:28:26 +00:00
|
|
|
items, err := commonclient.ReadIteratorItems(c.actor, batchSize, c.contractHash, iteratorChainsByPrefix,
|
|
|
|
big.NewInt(int64(kind)), entity, name)
|
2023-12-05 09:12:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-12-08 07:44:13 +00:00
|
|
|
res := make([][]byte, len(items))
|
2023-12-05 09:12:35 +00:00
|
|
|
for i, item := range items {
|
2023-12-08 07:44:13 +00:00
|
|
|
res[i], err = item.TryBytes()
|
2023-12-05 09:12:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2023-12-08 07:44:13 +00:00
|
|
|
func (c *Client) Wait(tx util.Uint256, vub uint32, err error) error {
|
|
|
|
_, err = c.actor.Wait(tx, vub, err)
|
|
|
|
return err
|
2023-12-05 09:12:35 +00:00
|
|
|
}
|
2024-02-13 08:50:11 +00:00
|
|
|
|
|
|
|
type multiTX struct {
|
|
|
|
contractHash util.Uint160
|
|
|
|
txs []*commonclient.Transaction
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *multiTX) AddChain(entity policycontract.Kind, entityName string, name []byte, chain []byte) {
|
|
|
|
m.wrapCall("addChain", []any{big.NewInt(int64(entity)), entityName, name, chain})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *multiTX) RemoveChain(entity policycontract.Kind, entityName string, name []byte) {
|
|
|
|
m.wrapCall("removeChain", []any{big.NewInt(int64(entity)), entityName, name})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *multiTX) Scripts() ([][]byte, error) {
|
|
|
|
if m.err != nil {
|
|
|
|
return nil, m.err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(m.txs) == 0 {
|
|
|
|
return nil, errors.New("tx isn't initialized")
|
|
|
|
}
|
|
|
|
|
|
|
|
res := make([][]byte, 0, len(m.txs))
|
|
|
|
for _, tx := range m.txs {
|
|
|
|
script, err := tx.Bytes()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
res = append(res, script)
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *multiTX) wrapCall(method string, args []any) {
|
|
|
|
if m.err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if len(m.txs) == 0 {
|
|
|
|
m.err = errors.New("multi tx isn't initialized")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err := m.txs[len(m.txs)-1].WrapCall(method, args)
|
|
|
|
if err == nil {
|
|
|
|
return
|
|
|
|
}
|
2024-08-22 16:13:52 +00:00
|
|
|
if !errors.Is(err, commonclient.ErrTransactionTooLarge) {
|
2024-02-13 08:50:11 +00:00
|
|
|
m.err = err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
tx := commonclient.NewTransaction(m.contractHash)
|
|
|
|
m.err = tx.WrapCall(method, args)
|
|
|
|
if m.err == nil {
|
|
|
|
m.txs = append(m.txs, tx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) StartTx() policy.MultiTransaction {
|
|
|
|
return &multiTX{
|
|
|
|
txs: []*commonclient.Transaction{commonclient.NewTransaction(c.contractHash)},
|
|
|
|
contractHash: c.contractHash,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) SendTx(mtx policy.MultiTransaction) error {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
scripts, err := mtx.Scripts()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range scripts {
|
|
|
|
if _, err = c.actor.Wait(c.actor.SendRun(scripts[i])); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|