Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ROSE-231: Call /account/coins endpoint to add account coin info in coin storage #392

Merged
merged 1 commit into from
Mar 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 43 additions & 0 deletions mocks/utils/fetcher_helper.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 15 additions & 8 deletions storage/modules/coin_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -501,21 +501,28 @@ func (c *CoinStorage) GetLargestCoin(
// This is used when importing prefunded addresses.
func (c *CoinStorage) SetCoinsImported(
ctx context.Context,
accountBalances []*utils.AccountBalance,
accts []*types.AccountIdentifier,
acctCoinsResp []*utils.AccountCoinsResponse,
) error {
var accountCoins []*types.AccountCoin
for _, accountBalance := range accountBalances {
for _, coin := range accountBalance.Coins {
accountCoin := &types.AccountCoin{
Account: accountBalance.Account,
// Request array length should always equal response array length.
// But we still check it for sure.
if len(accts) != len(acctCoinsResp) {
return errors.ErrCoinImportFailed
}

var acctCoins []*types.AccountCoin
for i, resp := range acctCoinsResp {
for _, coin := range resp.Coins {
acctCoin := &types.AccountCoin{
Account: accts[i],
Coin: coin,
}

accountCoins = append(accountCoins, accountCoin)
acctCoins = append(acctCoins, acctCoin)
}
}

if err := c.AddCoins(ctx, accountCoins); err != nil {
if err := c.AddCoins(ctx, acctCoins); err != nil {
return fmt.Errorf("%w: %v", errors.ErrCoinImportFailed, err)
}

Expand Down
14 changes: 11 additions & 3 deletions storage/modules/coin_storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,15 @@ var (
},
},
}

acctCoins = []*utils.AccountCoinsResponse{
{
Coins: accBalance1.Coins,
},
{
Coins: accBalance2.Coins,
},
}
)

func TestCoinStorage(t *testing.T) {
Expand Down Expand Up @@ -961,9 +970,8 @@ func TestCoinStorage(t *testing.T) {
})

t.Run("SetCoinsImported", func(t *testing.T) {
accBalances := []*utils.AccountBalance{accBalance1, accBalance2}

err := c.SetCoinsImported(ctx, accBalances)
accts := []*types.AccountIdentifier{accBalance1.Account, accBalance2.Account}
err := c.SetCoinsImported(ctx, accts, acctCoins)
assert.NoError(t, err)

mockHelper.On(
Expand Down
59 changes: 59 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,14 @@ type FetcherHelper interface {
block *types.PartialBlockIdentifier,
currencies []*types.Currency,
) (*types.BlockIdentifier, []*types.Amount, map[string]interface{}, *fetcher.Error)

AccountCoinsRetry(
ctx context.Context,
network *types.NetworkIdentifier,
acct *types.AccountIdentifier,
includeMempool bool,
currencies []*types.Currency,
) (*types.BlockIdentifier, []*types.Coin, map[string]interface{}, *fetcher.Error)
}

type BlockStorageHelper interface {
Expand Down Expand Up @@ -491,6 +499,57 @@ func GetAccountBalances(
return accountBalances, nil
}

// -------------------------------------------------------------------------------
// ----------------- Helper struct for fetching account coins --------------------
// -------------------------------------------------------------------------------

// AccountCoinsRequest defines the required information to get an account's coins.
type AccountCoinsRequest struct {
Account *types.AccountIdentifier
Network *types.NetworkIdentifier
Currencies []*types.Currency
IncludeMempool bool
}

// AccountCoins defines an account's coins info at tip.
type AccountCoinsResponse struct {
Coins []*types.Coin
}

// GetAccountCoins calls /account/coins endpoint and returns an array of coins at tip.
func GetAccountCoins(
shiatcb marked this conversation as resolved.
Show resolved Hide resolved
ctx context.Context,
fetcher FetcherHelper,
acctCoinsReqs []*AccountCoinsRequest,
) ([]*AccountCoinsResponse, error) {
var acctCoins []*AccountCoinsResponse
for _, req := range acctCoinsReqs {
_, coins, _, err := fetcher.AccountCoinsRetry(
ctx,
req.Network,
req.Account,
req.IncludeMempool,
req.Currencies,
)

if err != nil {
return nil, err.Err
}

resp := &AccountCoinsResponse{
Coins: coins,
}

acctCoins = append(acctCoins, resp)
}

return acctCoins, nil
}

// -------------------------------------------------------------------------------
// ------------------- End of helper struct for account coins --------------------
// -------------------------------------------------------------------------------

// AtTip returns a boolean indicating if a block timestamp
// is within tipDelay from the current time.
func AtTip(
Expand Down