forked from TrueCloudLab/frostfs-node
3e9c578e62
Unified format uses transfer type as the first byte and extra details next. List of transfer types used in contracts defined in `details.go`. It includes: - audit settlement, - basic income collection, - basic income distribution. Signed-off-by: Alex Vanin <alexey@nspcc.ru>
54 lines
1.2 KiB
Go
54 lines
1.2 KiB
Go
package basic
|
|
|
|
import (
|
|
"encoding/hex"
|
|
"math/big"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/innerring/processors/settlement/common"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
func (inc *IncomeSettlementContext) Distribute() {
|
|
inc.mu.Lock()
|
|
defer inc.mu.Unlock()
|
|
|
|
txTable := common.NewTransferTable()
|
|
|
|
bankBalance, err := inc.balances.Balance(inc.bankOwner)
|
|
if err != nil {
|
|
inc.log.Error("can't fetch balance of banking account",
|
|
zap.String("error", err.Error()))
|
|
|
|
return
|
|
}
|
|
|
|
total := inc.distributeTable.Total()
|
|
|
|
inc.distributeTable.Iterate(func(key []byte, n *big.Int) {
|
|
nodeOwner, err := inc.accounts.ResolveKey(nodeInfoWrapper(key))
|
|
if err != nil {
|
|
inc.log.Warn("can't transform public key to owner id",
|
|
zap.String("public_key", hex.EncodeToString(key)),
|
|
zap.String("error", err.Error()))
|
|
|
|
return
|
|
}
|
|
|
|
txTable.Transfer(&common.TransferTx{
|
|
From: inc.bankOwner,
|
|
To: nodeOwner,
|
|
Amount: normalizedValue(n, total, bankBalance),
|
|
})
|
|
})
|
|
|
|
common.TransferAssets(inc.exchange, txTable, common.BasicIncomeDistributionDetails(inc.epoch))
|
|
}
|
|
|
|
func normalizedValue(n, total, limit *big.Int) *big.Int {
|
|
if limit.Cmp(bigZero) == 0 {
|
|
return big.NewInt(0)
|
|
}
|
|
|
|
n.Mul(n, limit)
|
|
return n.Div(n, total)
|
|
}
|