Skip to content

Commit

Permalink
Unexpose some functions.
Browse files Browse the repository at this point in the history
gfyrag committed Jun 13, 2022
1 parent 9fab781 commit 58b2583
Showing 3 changed files with 27 additions and 27 deletions.
14 changes: 7 additions & 7 deletions pkg/ledger/ledger.go
Original file line number Diff line number Diff line change
@@ -94,7 +94,7 @@ func (l *Ledger) processTx(ctx context.Context, ts []core.TransactionData) (*Com
return nil, NewTransactionCommitError(i, NewValidationError("transaction has no postings"))
}

txVolumeAggregator := volumeAggregator.NextTx()
txVolumeAggregator := volumeAggregator.nextTx()

for _, p := range t.Postings {
if p.Amount < 0 {
@@ -109,13 +109,13 @@ func (l *Ledger) processTx(ctx context.Context, ts []core.TransactionData) (*Com
if !core.AssetIsValid(p.Asset) {
return nil, NewTransactionCommitError(i, NewValidationError("invalid asset"))
}
err := txVolumeAggregator.Transfer(ctx, p.Source, p.Destination, p.Asset, uint64(p.Amount))
err := txVolumeAggregator.transfer(ctx, p.Source, p.Destination, p.Asset, uint64(p.Amount))
if err != nil {
return nil, NewTransactionCommitError(i, err)
}
}

for addr, volumes := range txVolumeAggregator.PostCommitVolumes() {
for addr, volumes := range txVolumeAggregator.postCommitVolumes() {
for asset, volume := range volumes {
if addr == "world" {
continue
@@ -152,8 +152,8 @@ func (l *Ledger) processTx(ctx context.Context, ts []core.TransactionData) (*Com
TransactionData: t,
ID: nextTxId,
Timestamp: time.Now().UTC().Format(time.RFC3339),
PostCommitVolumes: txVolumeAggregator.PostCommitVolumes(),
PreCommitVolumes: txVolumeAggregator.PreCommitVolumes(),
PostCommitVolumes: txVolumeAggregator.postCommitVolumes(),
PreCommitVolumes: txVolumeAggregator.preCommitVolumes(),
}
generatedTxs = append(generatedTxs, tx)
newLog := core.NewTransactionLog(lastLog, tx)
@@ -163,8 +163,8 @@ func (l *Ledger) processTx(ctx context.Context, ts []core.TransactionData) (*Com
}

return &CommitResult{
PreCommitVolumes: volumeAggregator.AggregatedPreCommitVolumes(),
PostCommitVolumes: volumeAggregator.AggregatedPostCommitVolumes(),
PreCommitVolumes: volumeAggregator.aggregatedPreCommitVolumes(),
PostCommitVolumes: volumeAggregator.aggregatedPostCommitVolumes(),
GeneratedTransactions: generatedTxs,
GeneratedLogs: generatedLogs,
}, nil
16 changes: 8 additions & 8 deletions pkg/ledger/volume_agg.go
Original file line number Diff line number Diff line change
@@ -14,15 +14,15 @@ type transactionVolumeAggregator struct {
previousTx *transactionVolumeAggregator
}

func (tva *transactionVolumeAggregator) PostCommitVolumes() core.AggregatedVolumes {
func (tva *transactionVolumeAggregator) postCommitVolumes() core.AggregatedVolumes {
return tva.postVolumes
}

func (tva *transactionVolumeAggregator) PreCommitVolumes() core.AggregatedVolumes {
func (tva *transactionVolumeAggregator) preCommitVolumes() core.AggregatedVolumes {
return tva.preVolumes
}

func (tva *transactionVolumeAggregator) Transfer(ctx context.Context, from, to, asset string, amount uint64) error {
func (tva *transactionVolumeAggregator) transfer(ctx context.Context, from, to, asset string, amount uint64) error {
if tva.preVolumes == nil {
tva.preVolumes = core.AggregatedVolumes{}
}
@@ -75,7 +75,7 @@ type volumeAggregator struct {
txs []*transactionVolumeAggregator
}

func (agg *volumeAggregator) NextTx() *transactionVolumeAggregator {
func (agg *volumeAggregator) nextTx() *transactionVolumeAggregator {
var previousTx *transactionVolumeAggregator
if len(agg.txs) > 0 {
previousTx = agg.txs[len(agg.txs)-1]
@@ -88,11 +88,11 @@ func (agg *volumeAggregator) NextTx() *transactionVolumeAggregator {
return tva
}

func (agg *volumeAggregator) AggregatedPostCommitVolumes() core.AggregatedVolumes {
func (agg *volumeAggregator) aggregatedPostCommitVolumes() core.AggregatedVolumes {
ret := core.AggregatedVolumes{}
for i := len(agg.txs) - 1; i >= 0; i-- {
tx := agg.txs[i]
postVolumes := tx.PostCommitVolumes()
postVolumes := tx.postCommitVolumes()
for account, volumes := range postVolumes {
for asset, volume := range volumes {
if _, ok := ret[account]; !ok {
@@ -107,11 +107,11 @@ func (agg *volumeAggregator) AggregatedPostCommitVolumes() core.AggregatedVolume
return ret
}

func (agg *volumeAggregator) AggregatedPreCommitVolumes() core.AggregatedVolumes {
func (agg *volumeAggregator) aggregatedPreCommitVolumes() core.AggregatedVolumes {
ret := core.AggregatedVolumes{}
for i := 0; i < len(agg.txs); i++ {
tx := agg.txs[i]
preVolumes := tx.PreCommitVolumes()
preVolumes := tx.preCommitVolumes()
for account, volumes := range preVolumes {
for asset, volume := range volumes {
if _, ok := ret[account]; !ok {
24 changes: 12 additions & 12 deletions pkg/ledger/volume_agg_test.go
Original file line number Diff line number Diff line change
@@ -56,9 +56,9 @@ func TestVolumeAggregator(t *testing.T) {
require.NoError(t, store.AppendLog(context.Background(), firstTxLog, secondTxLog))

volumeAggregator := newVolumeAggregator(store)
firstTx := volumeAggregator.NextTx()
require.NoError(t, firstTx.Transfer(context.Background(), "bob", "alice", "USD", 100))
require.NoError(t, firstTx.Transfer(context.Background(), "bob", "zoro", "USD", 50))
firstTx := volumeAggregator.nextTx()
require.NoError(t, firstTx.transfer(context.Background(), "bob", "alice", "USD", 100))
require.NoError(t, firstTx.transfer(context.Background(), "bob", "zoro", "USD", 50))

require.Equal(t, core.AggregatedVolumes{
"bob": core.Volumes{
@@ -76,7 +76,7 @@ func TestVolumeAggregator(t *testing.T) {
Input: 50,
},
},
}, firstTx.PostCommitVolumes())
}, firstTx.postCommitVolumes())
require.Equal(t, core.AggregatedVolumes{
"bob": core.Volumes{
"USD": {
@@ -93,11 +93,11 @@ func TestVolumeAggregator(t *testing.T) {
Input: 0,
},
},
}, firstTx.PreCommitVolumes())
}, firstTx.preCommitVolumes())

secondTx := volumeAggregator.NextTx()
require.NoError(t, secondTx.Transfer(context.Background(), "alice", "fred", "USD", 50))
require.NoError(t, secondTx.Transfer(context.Background(), "bob", "fred", "USD", 25))
secondTx := volumeAggregator.nextTx()
require.NoError(t, secondTx.transfer(context.Background(), "alice", "fred", "USD", 50))
require.NoError(t, secondTx.transfer(context.Background(), "bob", "fred", "USD", 25))
require.Equal(t, core.AggregatedVolumes{
"bob": core.Volumes{
"USD": {
@@ -115,7 +115,7 @@ func TestVolumeAggregator(t *testing.T) {
Input: 75,
},
},
}, secondTx.PostCommitVolumes())
}, secondTx.postCommitVolumes())
require.Equal(t, core.AggregatedVolumes{
"bob": core.Volumes{
"USD": {
@@ -130,9 +130,9 @@ func TestVolumeAggregator(t *testing.T) {
"fred": core.Volumes{
"USD": {},
},
}, secondTx.PreCommitVolumes())
}, secondTx.preCommitVolumes())

aggregatedPostVolumes := volumeAggregator.AggregatedPostCommitVolumes()
aggregatedPostVolumes := volumeAggregator.aggregatedPostCommitVolumes()
require.Equal(t, core.AggregatedVolumes{
"bob": core.Volumes{
"USD": {
@@ -158,7 +158,7 @@ func TestVolumeAggregator(t *testing.T) {
},
}, aggregatedPostVolumes)

aggregatedPreVolumes := volumeAggregator.AggregatedPreCommitVolumes()
aggregatedPreVolumes := volumeAggregator.aggregatedPreCommitVolumes()
require.Equal(t, core.AggregatedVolumes{
"bob": core.Volumes{
"USD": {

0 comments on commit 58b2583

Please sign in to comment.