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

Use types.Coin in CoinStorage #109

Merged
merged 1 commit into from
Aug 17, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 14 additions & 24 deletions pkg/storage/coin_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,6 @@ func NewCoinStorage(
}
}

// Coin represents some spendable output (typically
// referred to as a UTXO).
type Coin struct {
Identifier *types.CoinIdentifier `json:"identifier"`
TransactionIdentifier *types.TransactionIdentifier `json:"transaction_identifier"`
Operation *types.Operation `json:"operation"`
}

func getCoinKey(identifier *types.CoinIdentifier) []byte {
return []byte(fmt.Sprintf("%s/%s", coinNamespace, identifier.Identifier))
}
Expand All @@ -85,7 +77,7 @@ func getAndDecodeCoin(
ctx context.Context,
transaction DatabaseTransaction,
coinIdentifier *types.CoinIdentifier,
) (bool, *Coin, error) {
) (bool, *types.Coin, error) {
exists, val, err := transaction.Get(ctx, getCoinKey(coinIdentifier))
if err != nil {
return false, nil, fmt.Errorf("%w: unable to query for coin", err)
Expand All @@ -95,7 +87,7 @@ func getAndDecodeCoin(
return false, nil, nil
}

var coin Coin
var coin types.Coin
if err := decode(val, &coin); err != nil {
return false, nil, fmt.Errorf("%w: unable to decode coin", err)
}
Expand All @@ -108,7 +100,6 @@ type wallets map[string]map[string]struct{}
func (c *CoinStorage) tryAddingCoin(
ctx context.Context,
transaction DatabaseTransaction,
blockTransaction *types.Transaction,
operation *types.Operation,
action types.CoinAction,
cache wallets,
Expand All @@ -123,10 +114,9 @@ func (c *CoinStorage) tryAddingCoin(

coinIdentifier := operation.CoinChange.CoinIdentifier

newCoin := &Coin{
Identifier: coinIdentifier,
TransactionIdentifier: blockTransaction.TransactionIdentifier,
Operation: operation,
newCoin := &types.Coin{
CoinIdentifier: coinIdentifier,
Amount: operation.Amount,
}

encodedResult, err := encode(newCoin)
Expand Down Expand Up @@ -305,7 +295,7 @@ func (c *CoinStorage) AddingBlock(
continue
}

if err := c.tryAddingCoin(ctx, transaction, txn, operation, types.CoinCreated, cache); err != nil {
if err := c.tryAddingCoin(ctx, transaction, operation, types.CoinCreated, cache); err != nil {
return nil, fmt.Errorf("%w: unable to add coin", err)
}

Expand Down Expand Up @@ -350,7 +340,7 @@ func (c *CoinStorage) RemovingBlock(

// We add spent coins and remove created coins during a re-org (opposite of
// AddingBlock).
if err := c.tryAddingCoin(ctx, transaction, txn, operation, types.CoinSpent, cache); err != nil {
if err := c.tryAddingCoin(ctx, transaction, operation, types.CoinSpent, cache); err != nil {
return nil, fmt.Errorf("%w: unable to add coin", err)
}

Expand All @@ -371,7 +361,7 @@ func (c *CoinStorage) RemovingBlock(
func (c *CoinStorage) GetCoins(
ctx context.Context,
accountIdentifier *types.AccountIdentifier,
) ([]*Coin, *types.BlockIdentifier, error) {
) ([]*types.Coin, *types.BlockIdentifier, error) {
transaction := c.db.NewDatabaseTransaction(ctx, false)
defer transaction.Discard(ctx)

Expand All @@ -386,10 +376,10 @@ func (c *CoinStorage) GetCoins(
}

if !accountExists {
return []*Coin{}, headBlockIdentifier, nil
return []*types.Coin{}, headBlockIdentifier, nil
}

coinArr := []*Coin{}
coinArr := []*types.Coin{}
for coinIdentifier := range coins {
exists, coin, err := getAndDecodeCoin(
ctx,
Expand Down Expand Up @@ -431,24 +421,24 @@ func (c *CoinStorage) GetLargestCoin(
var coinIdentifier *types.CoinIdentifier
for _, coin := range coins {
if types.Hash(
coin.Operation.Amount.Currency,
coin.Amount.Currency,
) != types.Hash(
currency,
) {
continue
}

val, ok := new(big.Int).SetString(coin.Operation.Amount.Value, 10)
val, ok := new(big.Int).SetString(coin.Amount.Value, 10)
if !ok {
return nil, nil, nil, fmt.Errorf(
"could not parse amount for coin %s",
coin.Identifier.Identifier,
coin.CoinIdentifier.Identifier,
)
}

if bal.Cmp(val) == -1 {
bal = val
coinIdentifier = coin.Identifier
coinIdentifier = coin.CoinIdentifier
}
}

Expand Down
36 changes: 16 additions & 20 deletions pkg/storage/coin_storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,32 +46,28 @@ var (
},
}

accountCoins = []*Coin{
accountCoins = []*types.Coin{
{
Identifier: &types.CoinIdentifier{Identifier: "coin1"},
TransactionIdentifier: coinBlock.Transactions[0].TransactionIdentifier,
Operation: coinBlock.Transactions[0].Operations[0],
CoinIdentifier: &types.CoinIdentifier{Identifier: "coin1"},
Amount: coinBlock.Transactions[0].Operations[0].Amount,
},
}

account2Coins = []*Coin{
account2Coins = []*types.Coin{
{
Identifier: &types.CoinIdentifier{Identifier: "coin2"},
TransactionIdentifier: coinBlock.Transactions[0].TransactionIdentifier,
Operation: coinBlock.Transactions[0].Operations[1],
CoinIdentifier: &types.CoinIdentifier{Identifier: "coin2"},
Amount: coinBlock.Transactions[0].Operations[1].Amount,
},
}

account3Coins = []*Coin{
account3Coins = []*types.Coin{
{
Identifier: &types.CoinIdentifier{Identifier: "coin3"},
TransactionIdentifier: coinBlock3.Transactions[0].TransactionIdentifier,
Operation: coinBlock3.Transactions[0].Operations[0],
CoinIdentifier: &types.CoinIdentifier{Identifier: "coin3"},
Amount: coinBlock3.Transactions[0].Operations[0].Amount,
},
{
Identifier: &types.CoinIdentifier{Identifier: "coin4"},
TransactionIdentifier: coinBlock3.Transactions[1].TransactionIdentifier,
Operation: coinBlock3.Transactions[1].Operations[0],
CoinIdentifier: &types.CoinIdentifier{Identifier: "coin4"},
Amount: coinBlock3.Transactions[1].Operations[0].Amount,
},
}

Expand Down Expand Up @@ -265,7 +261,7 @@ func TestCoinStorage(t *testing.T) {
t.Run("get coins of unset account", func(t *testing.T) {
coins, block, err := c.GetCoins(ctx, account)
assert.NoError(t, err)
assert.Equal(t, []*Coin{}, coins)
assert.Equal(t, []*types.Coin{}, coins)
assert.Equal(t, blockIdentifier, block)

bal, coinIdentifier, block, err := c.GetLargestCoin(ctx, account, currency)
Expand Down Expand Up @@ -310,7 +306,7 @@ func TestCoinStorage(t *testing.T) {

coins, block, err := c.GetCoins(ctx, account)
assert.NoError(t, err)
assert.Equal(t, []*Coin{}, coins)
assert.Equal(t, []*types.Coin{}, coins)
assert.Equal(t, blockIdentifier, block)

coins, block, err = c.GetCoins(ctx, account2)
Expand Down Expand Up @@ -339,12 +335,12 @@ func TestCoinStorage(t *testing.T) {

coins, block, err = c.GetCoins(ctx, account)
assert.NoError(t, err)
assert.Equal(t, []*Coin{}, coins)
assert.Equal(t, []*types.Coin{}, coins)
assert.Equal(t, blockIdentifier, block)

coins, block, err = c.GetCoins(ctx, account2)
assert.NoError(t, err)
assert.Equal(t, []*Coin{}, coins)
assert.Equal(t, []*types.Coin{}, coins)
assert.Equal(t, blockIdentifier, block)
})

Expand All @@ -357,7 +353,7 @@ func TestCoinStorage(t *testing.T) {

coins, block, err := c.GetCoins(ctx, account)
assert.NoError(t, err)
assert.Equal(t, []*Coin{}, coins)
assert.Equal(t, []*types.Coin{}, coins)
assert.Equal(t, blockIdentifier, block)

coins, block, err = c.GetCoins(ctx, account3)
Expand Down