diff --git a/pkg/innerring/innerring.go b/pkg/innerring/innerring.go index 335571a0b..31f6586f2 100644 --- a/pkg/innerring/innerring.go +++ b/pkg/innerring/innerring.go @@ -387,13 +387,13 @@ func New(ctx context.Context, log *zap.Logger, cfg *viper.Viper) (*Server, error // create settlement processor dependencies settlementDeps := &settlementDeps{ + globalConfig: globalConfig, log: server.log, cnrSrc: cnrClient, auditClient: server.auditClient, nmSrc: nmClient, clientCache: clientCache, balanceClient: balClient, - cfg: globalConfig, } auditCalcDeps := &auditSettlementDeps{ diff --git a/pkg/innerring/processors/settlement/audit/calculate.go b/pkg/innerring/processors/settlement/audit/calculate.go index 318264eff..74b023143 100644 --- a/pkg/innerring/processors/settlement/audit/calculate.go +++ b/pkg/innerring/processors/settlement/audit/calculate.go @@ -65,8 +65,9 @@ func (c *Calculator) Calculate(p *CalculatePrm) { log.Info("calculate audit settlements") log.Debug("getting results for the previous epoch") + prevEpoch := p.Epoch - 1 - auditResults, err := c.prm.ResultStorage.AuditResultsForEpoch(p.Epoch - 1) + auditResults, err := c.prm.ResultStorage.AuditResultsForEpoch(prevEpoch) if err != nil { log.Error("could not collect audit results") return @@ -99,7 +100,7 @@ func (c *Calculator) Calculate(p *CalculatePrm) { log.Debug("processing transfers") - common.TransferAssets(c.prm.Exchanger, table) + common.TransferAssets(c.prm.Exchanger, table, common.AuditSettlementDetails(prevEpoch)) } func (c *Calculator) processResult(ctx *singleResultCtx) { diff --git a/pkg/innerring/processors/settlement/basic/collect.go b/pkg/innerring/processors/settlement/basic/collect.go index 4e70b2179..6f5ce80f0 100644 --- a/pkg/innerring/processors/settlement/basic/collect.go +++ b/pkg/innerring/processors/settlement/basic/collect.go @@ -70,7 +70,7 @@ func (inc *IncomeSettlementContext) Collect() { }) } - common.TransferAssets(inc.exchange, txTable) + common.TransferAssets(inc.exchange, txTable, common.BasicIncomeCollectionDetails(inc.epoch)) } // avgEstimation returns estimation value for single container. Right now it diff --git a/pkg/innerring/processors/settlement/basic/distribute.go b/pkg/innerring/processors/settlement/basic/distribute.go index 6d16f9cb7..94dea4957 100644 --- a/pkg/innerring/processors/settlement/basic/distribute.go +++ b/pkg/innerring/processors/settlement/basic/distribute.go @@ -41,7 +41,7 @@ func (inc *IncomeSettlementContext) Distribute() { }) }) - common.TransferAssets(inc.exchange, txTable) + common.TransferAssets(inc.exchange, txTable, common.BasicIncomeDistributionDetails(inc.epoch)) } func normalizedValue(n, total, limit *big.Int) *big.Int { diff --git a/pkg/innerring/processors/settlement/common/details.go b/pkg/innerring/processors/settlement/common/details.go new file mode 100644 index 000000000..1cf719f63 --- /dev/null +++ b/pkg/innerring/processors/settlement/common/details.go @@ -0,0 +1,33 @@ +package common + +import ( + "encoding/binary" +) + +var ( + auditPrefix = []byte{0x40} + basicIncomeCollectionPrefix = []byte{0x41} + basicIncomeDistributionPrefix = []byte{0x42} +) + +func AuditSettlementDetails(epoch uint64) []byte { + return details(auditPrefix, epoch) +} + +func BasicIncomeCollectionDetails(epoch uint64) []byte { + return details(basicIncomeCollectionPrefix, epoch) +} + +func BasicIncomeDistributionDetails(epoch uint64) []byte { + return details(basicIncomeDistributionPrefix, epoch) +} + +func details(prefix []byte, epoch uint64) []byte { + prefixLen := len(prefix) + buf := make([]byte, prefixLen+8) + + copy(buf, prefix) + binary.LittleEndian.PutUint64(buf[prefixLen:], epoch) + + return buf +} diff --git a/pkg/innerring/processors/settlement/common/details_test.go b/pkg/innerring/processors/settlement/common/details_test.go new file mode 100644 index 000000000..9755e6aef --- /dev/null +++ b/pkg/innerring/processors/settlement/common/details_test.go @@ -0,0 +1,28 @@ +package common + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestAuditSettlementDetails(t *testing.T) { + var n uint64 = 1994 // 0x7CA + exp := []byte{0x40, 0xCA, 0x07, 0, 0, 0, 0, 0, 0} + got := AuditSettlementDetails(n) + require.Equal(t, exp, got) +} + +func TestBasicIncomeCollectionDetails(t *testing.T) { + var n uint64 = 1994 // 0x7CA + exp := []byte{0x41, 0xCA, 0x07, 0, 0, 0, 0, 0, 0} + got := BasicIncomeCollectionDetails(n) + require.Equal(t, exp, got) +} + +func TestBasicIncomeDistributionDetails(t *testing.T) { + var n uint64 = 1994 // 0x7CA + exp := []byte{0x42, 0xCA, 0x07, 0, 0, 0, 0, 0, 0} + got := BasicIncomeDistributionDetails(n) + require.Equal(t, exp, got) +} diff --git a/pkg/innerring/processors/settlement/common/types.go b/pkg/innerring/processors/settlement/common/types.go index fb2456760..1c621c625 100644 --- a/pkg/innerring/processors/settlement/common/types.go +++ b/pkg/innerring/processors/settlement/common/types.go @@ -50,5 +50,5 @@ type Exchanger interface { // Must transfer amount of GASe-12 from sender to recipient. // // Amount must be positive. - Transfer(sender, recipient *owner.ID, amount *big.Int) + Transfer(sender, recipient *owner.ID, amount *big.Int, details []byte) } diff --git a/pkg/innerring/processors/settlement/common/util.go b/pkg/innerring/processors/settlement/common/util.go index 39bd7840c..a642b42a7 100644 --- a/pkg/innerring/processors/settlement/common/util.go +++ b/pkg/innerring/processors/settlement/common/util.go @@ -56,7 +56,7 @@ func (t *TransferTable) Iterate(f func(*TransferTx)) { } } -func TransferAssets(e Exchanger, t *TransferTable) { +func TransferAssets(e Exchanger, t *TransferTable, details []byte) { t.Iterate(func(tx *TransferTx) { sign := tx.Amount.Sign() if sign == 0 { @@ -68,6 +68,6 @@ func TransferAssets(e Exchanger, t *TransferTable) { tx.Amount.Neg(tx.Amount) } - e.Transfer(tx.From, tx.To, tx.Amount) + e.Transfer(tx.From, tx.To, tx.Amount, details) }) } diff --git a/pkg/innerring/settlement.go b/pkg/innerring/settlement.go index b60337878..ef6a1476f 100644 --- a/pkg/innerring/settlement.go +++ b/pkg/innerring/settlement.go @@ -32,6 +32,8 @@ type globalConfig interface { } type settlementDeps struct { + globalConfig + log *logger.Logger cnrSrc container.Source @@ -43,8 +45,6 @@ type settlementDeps struct { clientCache *ClientCache balanceClient *balanceClient.Wrapper - - cfg globalConfig } type auditSettlementDeps struct { @@ -191,16 +191,12 @@ func (s settlementDeps) ResolveKey(ni common.NodeInfo) (*owner.ID, error) { return id, nil } -var ( - transferAuditDetails = []byte("settlement-audit") - basicIncomeAuditDetails = []byte("settlement-basic-income") -) - -func (s settlementDeps) transfer(sender, recipient *owner.ID, amount *big.Int, details []byte) { +func (s settlementDeps) Transfer(sender, recipient *owner.ID, amount *big.Int, details []byte) { log := s.log.With( zap.Stringer("sender", sender), zap.Stringer("recipient", recipient), zap.Stringer("amount (GASe-12)", amount), + zap.String("details", hex.EncodeToString(details)), ) if !amount.IsInt64() { @@ -225,20 +221,8 @@ func (s settlementDeps) transfer(sender, recipient *owner.ID, amount *big.Int, d log.Debug("transfer transaction for audit was successfully sent") } -func (a auditSettlementDeps) Transfer(sender, recipient *owner.ID, amount *big.Int) { - a.transfer(sender, recipient, amount, transferAuditDetails) -} - -func (a auditSettlementDeps) AuditFee() (uint64, error) { - return a.cfg.AuditFee() -} - -func (b basicIncomeSettlementDeps) Transfer(sender, recipient *owner.ID, amount *big.Int) { - b.transfer(sender, recipient, amount, basicIncomeAuditDetails) -} - func (b basicIncomeSettlementDeps) BasicRate() (uint64, error) { - return b.cfg.BasicIncomeRate() + return b.BasicIncomeRate() } func (b basicIncomeSettlementDeps) Estimations(epoch uint64) ([]*wrapper.Estimations, error) {