2021-01-26 10:33:28 +00:00
|
|
|
package innerring
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-02-01 16:20:33 +00:00
|
|
|
"encoding/hex"
|
2021-01-26 10:33:28 +00:00
|
|
|
"math/big"
|
|
|
|
|
|
|
|
auditAPI "github.com/nspcc-dev/neofs-api-go/pkg/audit"
|
|
|
|
containerAPI "github.com/nspcc-dev/neofs-api-go/pkg/container"
|
|
|
|
netmapAPI "github.com/nspcc-dev/neofs-api-go/pkg/netmap"
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/object"
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/owner"
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/storagegroup"
|
|
|
|
crypto "github.com/nspcc-dev/neofs-crypto"
|
|
|
|
"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"
|
2021-01-26 10:33:28 +00:00
|
|
|
auditClient "github.com/nspcc-dev/neofs-node/pkg/morph/client/audit/wrapper"
|
|
|
|
balanceClient "github.com/nspcc-dev/neofs-node/pkg/morph/client/balance/wrapper"
|
2021-02-01 16:20:33 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/morph/client/container/wrapper"
|
|
|
|
containerClient "github.com/nspcc-dev/neofs-node/pkg/morph/client/container/wrapper"
|
2021-01-26 10:33:28 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/util/logger"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
2021-02-01 12:40:07 +00:00
|
|
|
type settlementDeps struct {
|
2021-01-26 10:33:28 +00:00
|
|
|
log *logger.Logger
|
|
|
|
|
|
|
|
cnrSrc container.Source
|
|
|
|
|
|
|
|
auditClient *auditClient.ClientWrapper
|
|
|
|
|
|
|
|
nmSrc netmap.Source
|
|
|
|
|
|
|
|
clientCache *ClientCache
|
|
|
|
|
|
|
|
balanceClient *balanceClient.Wrapper
|
|
|
|
}
|
|
|
|
|
2021-02-01 12:40:07 +00:00
|
|
|
type auditSettlementDeps struct {
|
|
|
|
*settlementDeps
|
|
|
|
}
|
|
|
|
|
2021-02-01 16:20:33 +00:00
|
|
|
type basicIncomeSettlementDeps struct {
|
|
|
|
*settlementDeps
|
|
|
|
cnrClient *containerClient.Wrapper
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
return nil, errors.Wrap(err, "could not list audit results in sidechain")
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
return nil, errors.Wrap(err, "could not get audit result")
|
|
|
|
}
|
|
|
|
|
|
|
|
res = append(res, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2021-02-01 12:40:07 +00:00
|
|
|
func (s settlementDeps) ContainerInfo(cid *containerAPI.ID) (common.ContainerInfo, error) {
|
|
|
|
cnr, err := s.cnrSrc.Get(cid)
|
2021-01-26 10:33:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not get container from storage")
|
|
|
|
}
|
|
|
|
|
|
|
|
return (*containerWrapper)(cnr), nil
|
|
|
|
}
|
|
|
|
|
2021-02-01 12:40:07 +00:00
|
|
|
func (s settlementDeps) buildContainer(e uint64, cid *containerAPI.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 {
|
|
|
|
return nil, nil, errors.Wrap(err, "could not get network map from storage")
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
return nil, nil, errors.Wrap(err, "could not get container from sidechain")
|
|
|
|
}
|
|
|
|
|
|
|
|
cn, err := nm.GetContainerNodes(
|
|
|
|
cnr.PlacementPolicy(),
|
|
|
|
cid.ToV2().GetValue(), // may be replace pivot calculation to neofs-api-go
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, errors.Wrap(err, "could not calculate container nodes")
|
|
|
|
}
|
|
|
|
|
|
|
|
return cn, nm, nil
|
|
|
|
}
|
|
|
|
|
2021-02-01 12:40:07 +00:00
|
|
|
func (s settlementDeps) ContainerNodes(e uint64, cid *containerAPI.ID) ([]common.NodeInfo, error) {
|
|
|
|
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{
|
|
|
|
ni: ns[i],
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2021-02-01 12:40:07 +00:00
|
|
|
func (s settlementDeps) SGInfo(addr *object.Address) (audit.SGInfo, error) {
|
|
|
|
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-01-26 10:33:28 +00:00
|
|
|
w, err := owner.NEO3WalletFromPublicKey(crypto.UnmarshalPublicKey(ni.PublicKey()))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
id := owner.NewID()
|
|
|
|
id.SetNeo3Wallet(w)
|
|
|
|
|
|
|
|
return id, nil
|
|
|
|
}
|
|
|
|
|
2021-02-01 16:20:33 +00:00
|
|
|
var (
|
|
|
|
transferAuditDetails = []byte("settlement-audit")
|
|
|
|
basicIncomeAuditDetails = []byte("settlement-basic-income")
|
|
|
|
)
|
2021-01-26 10:33:28 +00:00
|
|
|
|
2021-02-01 12:40:07 +00:00
|
|
|
func (s settlementDeps) transfer(sender, recipient *owner.ID, amount *big.Int, details []byte) {
|
|
|
|
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-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-02-01 12:40:07 +00:00
|
|
|
if err := s.balanceClient.TransferX(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-01-26 10:33:28 +00:00
|
|
|
}); 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-01 12:40:07 +00:00
|
|
|
func (a auditSettlementDeps) Transfer(sender, recipient *owner.ID, amount *big.Int) {
|
|
|
|
a.transfer(sender, recipient, amount, transferAuditDetails)
|
|
|
|
}
|
|
|
|
|
2021-02-01 16:20:33 +00:00
|
|
|
func (b basicIncomeSettlementDeps) Transfer(sender, recipient *owner.ID, amount *big.Int) {
|
|
|
|
b.transfer(sender, recipient, amount, basicIncomeAuditDetails)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b basicIncomeSettlementDeps) BasicRate() uint64 {
|
|
|
|
return 1_0000_0000 // fixme: read from config and from chain
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b basicIncomeSettlementDeps) Estimations(epoch uint64) ([]*wrapper.Estimations, error) {
|
|
|
|
estimationIDs, err := b.cnrClient.ListLoadEstimationsByEpoch(epoch)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
result := make([]*wrapper.Estimations, 0, len(estimationIDs))
|
|
|
|
|
|
|
|
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-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,
|
|
|
|
Container: b.dep,
|
|
|
|
Placement: b.dep,
|
|
|
|
Exchange: b.dep,
|
|
|
|
})
|
|
|
|
}
|