2021-01-26 10:33:28 +00:00
|
|
|
package innerring
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-05-31 08:55:38 +00:00
|
|
|
"crypto/ecdsa"
|
|
|
|
"crypto/elliptic"
|
2021-02-01 16:20:33 +00:00
|
|
|
"encoding/hex"
|
2021-05-18 08:12:51 +00:00
|
|
|
"fmt"
|
2021-01-26 10:33:28 +00:00
|
|
|
"math/big"
|
|
|
|
|
2021-05-31 08:55:38 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2021-01-26 10:33:28 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/core/container"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/core/netmap"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/innerring/processors/settlement/audit"
|
2021-02-01 16:20:33 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/innerring/processors/settlement/basic"
|
2021-02-01 12:40:07 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/innerring/processors/settlement/common"
|
2022-01-31 12:32:51 +00:00
|
|
|
auditClient "github.com/nspcc-dev/neofs-node/pkg/morph/client/audit"
|
2022-01-31 09:24:25 +00:00
|
|
|
balanceClient "github.com/nspcc-dev/neofs-node/pkg/morph/client/balance"
|
2022-01-31 13:34:01 +00:00
|
|
|
containerClient "github.com/nspcc-dev/neofs-node/pkg/morph/client/container"
|
2021-01-26 10:33:28 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/util/logger"
|
2021-11-10 07:08:33 +00:00
|
|
|
auditAPI "github.com/nspcc-dev/neofs-sdk-go/audit"
|
|
|
|
containerAPI "github.com/nspcc-dev/neofs-sdk-go/container"
|
|
|
|
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
|
|
|
|
netmapAPI "github.com/nspcc-dev/neofs-sdk-go/netmap"
|
2022-01-26 12:11:13 +00:00
|
|
|
addressSDK "github.com/nspcc-dev/neofs-sdk-go/object/address"
|
2021-11-10 07:08:33 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/owner"
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/storagegroup"
|
2021-01-26 10:33:28 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
2021-02-02 11:12:41 +00:00
|
|
|
type globalConfig interface {
|
|
|
|
BasicIncomeRate() (uint64, error)
|
2021-04-07 10:53:13 +00:00
|
|
|
AuditFee() (uint64, error)
|
2021-02-02 11:12:41 +00:00
|
|
|
}
|
|
|
|
|
2021-02-01 12:40:07 +00:00
|
|
|
type settlementDeps struct {
|
2021-04-07 12:12:36 +00:00
|
|
|
globalConfig
|
|
|
|
|
2021-01-26 10:33:28 +00:00
|
|
|
log *logger.Logger
|
|
|
|
|
|
|
|
cnrSrc container.Source
|
|
|
|
|
2022-01-31 12:32:51 +00:00
|
|
|
auditClient *auditClient.Client
|
2021-01-26 10:33:28 +00:00
|
|
|
|
|
|
|
nmSrc netmap.Source
|
|
|
|
|
|
|
|
clientCache *ClientCache
|
|
|
|
|
2022-01-31 09:24:25 +00:00
|
|
|
balanceClient *balanceClient.Client
|
2021-01-26 10:33:28 +00:00
|
|
|
}
|
|
|
|
|
2021-02-01 12:40:07 +00:00
|
|
|
type auditSettlementDeps struct {
|
|
|
|
*settlementDeps
|
|
|
|
}
|
|
|
|
|
2021-02-01 16:20:33 +00:00
|
|
|
type basicIncomeSettlementDeps struct {
|
|
|
|
*settlementDeps
|
2022-01-31 13:34:01 +00:00
|
|
|
cnrClient *containerClient.Client
|
2021-02-01 16:20:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type basicSettlementConstructor struct {
|
|
|
|
dep *basicIncomeSettlementDeps
|
|
|
|
}
|
|
|
|
|
2021-01-29 07:42:40 +00:00
|
|
|
type auditSettlementCalculator audit.Calculator
|
|
|
|
|
2021-01-26 10:33:28 +00:00
|
|
|
type containerWrapper containerAPI.Container
|
|
|
|
|
|
|
|
type nodeInfoWrapper struct {
|
|
|
|
ni *netmapAPI.Node
|
|
|
|
}
|
|
|
|
|
|
|
|
type sgWrapper storagegroup.StorageGroup
|
|
|
|
|
|
|
|
func (s *sgWrapper) Size() uint64 {
|
|
|
|
return (*storagegroup.StorageGroup)(s).ValidationDataSize()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n nodeInfoWrapper) PublicKey() []byte {
|
|
|
|
return n.ni.PublicKey()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n nodeInfoWrapper) Price() *big.Int {
|
|
|
|
return big.NewInt(int64(n.ni.Price))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *containerWrapper) Owner() *owner.ID {
|
|
|
|
return (*containerAPI.Container)(c).OwnerID()
|
|
|
|
}
|
|
|
|
|
2021-02-01 12:40:07 +00:00
|
|
|
func (s settlementDeps) AuditResultsForEpoch(epoch uint64) ([]*auditAPI.Result, error) {
|
|
|
|
idList, err := s.auditClient.ListAuditResultIDByEpoch(epoch)
|
2021-01-26 10:33:28 +00:00
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("could not list audit results in sidechain: %w", err)
|
2021-01-26 10:33:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
res := make([]*auditAPI.Result, 0, len(idList))
|
|
|
|
|
|
|
|
for i := range idList {
|
2021-02-01 12:40:07 +00:00
|
|
|
r, err := s.auditClient.GetAuditResult(idList[i])
|
2021-01-26 10:33:28 +00:00
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("could not get audit result: %w", err)
|
2021-01-26 10:33:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
res = append(res, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2021-05-31 11:03:17 +00:00
|
|
|
func (s settlementDeps) ContainerInfo(cid *cid.ID) (common.ContainerInfo, error) {
|
2021-02-01 12:40:07 +00:00
|
|
|
cnr, err := s.cnrSrc.Get(cid)
|
2021-01-26 10:33:28 +00:00
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("could not get container from storage: %w", err)
|
2021-01-26 10:33:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return (*containerWrapper)(cnr), nil
|
|
|
|
}
|
|
|
|
|
2021-05-31 11:03:17 +00:00
|
|
|
func (s settlementDeps) buildContainer(e uint64, cid *cid.ID) (netmapAPI.ContainerNodes, *netmapAPI.Netmap, error) {
|
2021-01-26 10:33:28 +00:00
|
|
|
var (
|
|
|
|
nm *netmapAPI.Netmap
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
if e > 0 {
|
2021-02-01 12:40:07 +00:00
|
|
|
nm, err = s.nmSrc.GetNetMapByEpoch(e)
|
2021-01-26 10:33:28 +00:00
|
|
|
} else {
|
2021-02-01 12:40:07 +00:00
|
|
|
nm, err = netmap.GetLatestNetworkMap(s.nmSrc)
|
2021-01-26 10:33:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, nil, fmt.Errorf("could not get network map from storage: %w", err)
|
2021-01-26 10:33:28 +00:00
|
|
|
}
|
|
|
|
|
2021-02-01 12:40:07 +00:00
|
|
|
cnr, err := s.cnrSrc.Get(cid)
|
2021-01-26 10:33:28 +00:00
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, nil, fmt.Errorf("could not get container from sidechain: %w", err)
|
2021-01-26 10:33:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cn, err := nm.GetContainerNodes(
|
|
|
|
cnr.PlacementPolicy(),
|
|
|
|
cid.ToV2().GetValue(), // may be replace pivot calculation to neofs-api-go
|
|
|
|
)
|
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, nil, fmt.Errorf("could not calculate container nodes: %w", err)
|
2021-01-26 10:33:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return cn, nm, nil
|
|
|
|
}
|
|
|
|
|
2021-05-31 11:03:17 +00:00
|
|
|
func (s settlementDeps) ContainerNodes(e uint64, cid *cid.ID) ([]common.NodeInfo, error) {
|
2021-02-01 12:40:07 +00:00
|
|
|
cn, _, err := s.buildContainer(e, cid)
|
2021-01-26 10:33:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ns := cn.Flatten()
|
2021-02-01 12:40:07 +00:00
|
|
|
res := make([]common.NodeInfo, 0, len(ns))
|
2021-01-26 10:33:28 +00:00
|
|
|
|
|
|
|
for i := range ns {
|
|
|
|
res = append(res, &nodeInfoWrapper{
|
2022-03-15 11:39:53 +00:00
|
|
|
ni: &ns[i],
|
2021-01-26 10:33:28 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2022-01-26 12:11:13 +00:00
|
|
|
func (s settlementDeps) SGInfo(addr *addressSDK.Address) (audit.SGInfo, error) {
|
2021-02-01 12:40:07 +00:00
|
|
|
cn, nm, err := s.buildContainer(0, addr.ContainerID())
|
2021-01-26 10:33:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-02-01 12:40:07 +00:00
|
|
|
sg, err := s.clientCache.getSG(context.Background(), addr, nm, cn)
|
2021-01-26 10:33:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return (*sgWrapper)(sg), nil
|
|
|
|
}
|
|
|
|
|
2021-02-01 12:40:07 +00:00
|
|
|
func (s settlementDeps) ResolveKey(ni common.NodeInfo) (*owner.ID, error) {
|
2021-05-31 08:55:38 +00:00
|
|
|
pub, err := keys.NewPublicKeyFromBytes(ni.PublicKey(), elliptic.P256())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-01-21 12:15:10 +00:00
|
|
|
return owner.NewIDFromPublicKey((*ecdsa.PublicKey)(pub)), nil
|
2021-01-26 10:33:28 +00:00
|
|
|
}
|
|
|
|
|
2021-04-07 12:12:36 +00:00
|
|
|
func (s settlementDeps) Transfer(sender, recipient *owner.ID, amount *big.Int, details []byte) {
|
2021-02-01 12:40:07 +00:00
|
|
|
log := s.log.With(
|
2021-01-28 19:53:59 +00:00
|
|
|
zap.Stringer("sender", sender),
|
|
|
|
zap.Stringer("recipient", recipient),
|
|
|
|
zap.Stringer("amount (GASe-12)", amount),
|
2021-04-07 12:12:36 +00:00
|
|
|
zap.String("details", hex.EncodeToString(details)),
|
2021-01-28 19:53:59 +00:00
|
|
|
)
|
|
|
|
|
2021-01-26 10:33:28 +00:00
|
|
|
if !amount.IsInt64() {
|
2021-02-01 12:40:07 +00:00
|
|
|
s.log.Error("amount can not be represented as an int64")
|
2021-01-26 10:33:28 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-04-29 13:50:46 +00:00
|
|
|
params := balanceClient.TransferPrm{
|
2021-01-28 17:05:52 +00:00
|
|
|
Amount: amount.Int64(),
|
2021-01-26 10:33:28 +00:00
|
|
|
From: sender,
|
|
|
|
To: recipient,
|
2021-02-01 12:40:07 +00:00
|
|
|
Details: details,
|
2021-04-29 13:50:46 +00:00
|
|
|
}
|
|
|
|
|
2021-06-02 16:24:30 +00:00
|
|
|
err := s.balanceClient.TransferX(params)
|
2021-04-29 13:50:46 +00:00
|
|
|
if err != nil {
|
2021-01-28 19:53:59 +00:00
|
|
|
log.Error("could not send transfer transaction for audit",
|
2021-01-26 10:33:28 +00:00
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
2021-01-28 19:53:59 +00:00
|
|
|
|
|
|
|
return
|
2021-01-26 10:33:28 +00:00
|
|
|
}
|
2021-01-28 19:53:59 +00:00
|
|
|
|
|
|
|
log.Debug("transfer transaction for audit was successfully sent")
|
2021-01-26 10:33:28 +00:00
|
|
|
}
|
2021-01-29 07:42:40 +00:00
|
|
|
|
2021-02-02 11:12:41 +00:00
|
|
|
func (b basicIncomeSettlementDeps) BasicRate() (uint64, error) {
|
2021-04-07 12:12:36 +00:00
|
|
|
return b.BasicIncomeRate()
|
2021-02-01 16:20:33 +00:00
|
|
|
}
|
|
|
|
|
2021-08-24 10:53:35 +00:00
|
|
|
func (b basicIncomeSettlementDeps) Estimations(epoch uint64) ([]*containerClient.Estimations, error) {
|
2021-02-01 16:20:33 +00:00
|
|
|
estimationIDs, err := b.cnrClient.ListLoadEstimationsByEpoch(epoch)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-08-24 10:53:35 +00:00
|
|
|
result := make([]*containerClient.Estimations, 0, len(estimationIDs))
|
2021-02-01 16:20:33 +00:00
|
|
|
|
|
|
|
for i := range estimationIDs {
|
|
|
|
estimation, err := b.cnrClient.GetUsedSpaceEstimations(estimationIDs[i])
|
|
|
|
if err != nil {
|
|
|
|
b.log.Warn("can't get used space estimation",
|
|
|
|
zap.String("estimation_id", hex.EncodeToString(estimationIDs[i])),
|
|
|
|
zap.String("error", err.Error()))
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
result = append(result, estimation)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2021-02-02 13:45:13 +00:00
|
|
|
func (b basicIncomeSettlementDeps) Balance(id *owner.ID) (*big.Int, error) {
|
|
|
|
return b.balanceClient.BalanceOf(id)
|
|
|
|
}
|
|
|
|
|
2021-01-29 07:42:40 +00:00
|
|
|
func (s *auditSettlementCalculator) ProcessAuditSettlements(epoch uint64) {
|
|
|
|
(*audit.Calculator)(s).Calculate(&audit.CalculatePrm{
|
|
|
|
Epoch: epoch,
|
|
|
|
})
|
|
|
|
}
|
2021-02-01 16:20:33 +00:00
|
|
|
|
|
|
|
func (b *basicSettlementConstructor) CreateContext(epoch uint64) (*basic.IncomeSettlementContext, error) {
|
|
|
|
return basic.NewIncomeSettlementContext(&basic.IncomeSettlementContextPrms{
|
|
|
|
Log: b.dep.log,
|
|
|
|
Epoch: epoch,
|
|
|
|
Rate: b.dep,
|
|
|
|
Estimations: b.dep,
|
2021-02-02 13:45:13 +00:00
|
|
|
Balances: b.dep,
|
2021-02-01 16:20:33 +00:00
|
|
|
Container: b.dep,
|
|
|
|
Placement: b.dep,
|
|
|
|
Exchange: b.dep,
|
2021-02-02 14:17:38 +00:00
|
|
|
Accounts: b.dep,
|
2021-02-01 16:20:33 +00:00
|
|
|
})
|
|
|
|
}
|