Skip to content

Commit

Permalink
fix looping logic
Browse files Browse the repository at this point in the history
  • Loading branch information
shrimalmadhur committed Feb 16, 2022
1 parent 0268b5c commit 2293610
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
2 changes: 1 addition & 1 deletion storage/modules/balance_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -1101,7 +1101,7 @@ func (b *BalanceStorage) BootstrapBalances(
// Commit transaction batch by batch rather than commit at one time.
// This helps reduce memory usage and improve running time when bootstrap_balances.json
// contains huge number of accounts.
if i%utils.MaxEntrySizePerTxn == 0 {
if i != 0 && i%utils.MaxEntrySizePerTxn == 0 {
if err := dbTransaction.Commit(ctx); err != nil {
return err
}
Expand Down
40 changes: 39 additions & 1 deletion storage/modules/balance_storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1020,6 +1020,14 @@ func TestBootstrapBalances(t *testing.T) {
account = &types.AccountIdentifier{
Address: "hello",
}

account2 = &types.AccountIdentifier{
Address: "hello world",
}

account3 = &types.AccountIdentifier{
Address: "hello world new",
}
)

ctx := context.Background()
Expand Down Expand Up @@ -1064,6 +1072,16 @@ func TestBootstrapBalances(t *testing.T) {
Value: amount.Value,
Currency: amount.Currency,
},
{
Account: account2,
Value: amount.Value,
Currency: amount.Currency,
},
{
Account: account3,
Value: amount.Value,
Currency: amount.Currency,
},
}, "", " ")
assert.NoError(t, err)

Expand All @@ -1083,7 +1101,7 @@ func TestBootstrapBalances(t *testing.T) {

storage.Initialize(mockHelper, mockHandler)
t.Run("Set balance successfully", func(t *testing.T) {
mockHandler.On("AccountsSeen", ctx, mock.Anything, 1).Return(nil).Once()
mockHandler.On("AccountsSeen", ctx, mock.Anything, 1).Return(nil).Times(3)
err = storage.BootstrapBalances(
ctx,
bootstrapBalancesFile,
Expand All @@ -1101,6 +1119,26 @@ func TestBootstrapBalances(t *testing.T) {
assert.Equal(t, amount, retrievedAmount)
assert.NoError(t, err)

retrievedAmount2, err := storage.GetOrSetBalance(
ctx,
account2,
amount.Currency,
genesisBlockIdentifier,
)

assert.Equal(t, amount, retrievedAmount2)
assert.NoError(t, err)

retrievedAmount3, err := storage.GetOrSetBalance(
ctx,
account3,
amount.Currency,
genesisBlockIdentifier,
)

assert.Equal(t, amount, retrievedAmount3)
assert.NoError(t, err)

// Attempt to update balance
txn := storage.db.Transaction(ctx)
newAccount, err := storage.UpdateBalance(
Expand Down

0 comments on commit 2293610

Please sign in to comment.