Skip to content

Commit e1fdfec

Browse files
committed
Calculate address balances using only successful transactions
1 parent f63fc46 commit e1fdfec

File tree

5 files changed

+30
-34
lines changed

5 files changed

+30
-34
lines changed

priv/repo/migrations/20180216011950_create_balances_views.exs

+6-6
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ defmodule Explorer.Repo.Migrations.CreateBalancesViews do
1010
COALESCE(MIN(transactions.inserted_at), NOW()) AS inserted_at,
1111
COALESCE(MAX(transactions.inserted_at), NOW()) AS updated_at
1212
FROM addresses
13-
LEFT OUTER JOIN to_addresses ON to_addresses.address_id = addresses.id
14-
LEFT OUTER JOIN transactions ON transactions.id = to_addresses.transaction_id
15-
LEFT OUTER JOIN receipts ON receipts.transaction_id = transactions.id AND receipts.status = 1
13+
INNER JOIN to_addresses ON to_addresses.address_id = addresses.id
14+
INNER JOIN transactions ON transactions.id = to_addresses.transaction_id
15+
INNER JOIN receipts ON receipts.transaction_id = transactions.id AND receipts.status = 1
1616
GROUP BY addresses.id
1717
;
1818
"""
@@ -25,9 +25,9 @@ defmodule Explorer.Repo.Migrations.CreateBalancesViews do
2525
COALESCE(MIN(transactions.inserted_at), NOW()) AS inserted_at,
2626
COALESCE(MAX(transactions.inserted_at), NOW()) AS updated_at
2727
FROM addresses
28-
LEFT OUTER JOIN from_addresses ON from_addresses.address_id = addresses.id
29-
LEFT OUTER JOIN transactions ON transactions.id = from_addresses.transaction_id
30-
LEFT OUTER JOIN receipts ON receipts.transaction_id = transactions.id AND receipts.status = 1
28+
INNER JOIN from_addresses ON from_addresses.address_id = addresses.id
29+
INNER JOIN transactions ON transactions.id = from_addresses.transaction_id
30+
INNER JOIN receipts ON receipts.transaction_id = transactions.id AND receipts.status = 1
3131
GROUP BY addresses.id
3232
;
3333
"""

test/explorer/credit_test.exs

+5-12
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,10 @@ defmodule Explorer.CreditTest do
88
assert Repo.all(Credit) == []
99
end
1010

11-
test "returns a credit when there is an address" do
12-
address = insert(:address)
13-
Credit.refresh
14-
credit = Credit |> preload([:address]) |> Repo.one
15-
assert credit.address == address
16-
end
17-
18-
test "returns zero credits when an address has no transactions" do
11+
test "returns nothing when an address has no transactions" do
1912
insert(:address)
2013
Credit.refresh
21-
assert Repo.one(Credit).value == Decimal.new(0)
14+
assert Repo.one(Credit) == nil
2215
end
2316

2417
test "returns a credit when there is an address with a receipt" do
@@ -30,10 +23,10 @@ defmodule Explorer.CreditTest do
3023
insert(:to_address, transaction: transaction, address: receipient)
3124
Credit.refresh
3225
credits = Credit |> Repo.all
33-
assert credits |> Enum.count == 2
26+
assert credits |> Enum.count == 1
3427
end
3528

36-
test "returns no credit to the sender" do
29+
test "returns no credits to the sender" do
3730
receipient = insert(:address)
3831
sender = insert(:address)
3932
transaction = insert(:transaction, value: 21)
@@ -43,7 +36,7 @@ defmodule Explorer.CreditTest do
4336
address_id = sender.id
4437
Credit.refresh
4538
credit = Credit |> where(address_id: ^address_id) |> Repo.one
46-
assert credit.value == Decimal.new(0)
39+
assert credit == nil
4740
end
4841

4942
test "returns a credit to the receipient" do

test/explorer/debit_test.exs

+5-12
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,10 @@ defmodule Explorer.DebitTest do
88
assert Repo.all(Debit) == []
99
end
1010

11-
test "returns a debit when there is an address" do
12-
address = insert(:address)
13-
Debit.refresh
14-
debit = Debit |> preload([:address]) |> Repo.one
15-
assert debit.address == address
16-
end
17-
18-
test "returns zero debits when an address has no transactions" do
11+
test "returns nothing when an address has no transactions" do
1912
insert(:address)
2013
Debit.refresh
21-
assert Repo.one(Debit).value == Decimal.new(0)
14+
assert Repo.one(Debit) == nil
2215
end
2316

2417
test "returns a debit when there is an address with a receipt" do
@@ -30,7 +23,7 @@ defmodule Explorer.DebitTest do
3023
insert(:to_address, transaction: transaction, address: receipient)
3124
Debit.refresh
3225
debits = Debit |> Repo.all
33-
assert debits |> Enum.count == 2
26+
assert debits |> Enum.count == 1
3427
end
3528

3629
test "returns a debit against the sender" do
@@ -46,7 +39,7 @@ defmodule Explorer.DebitTest do
4639
assert debit.value == Decimal.new(21)
4740
end
4841

49-
test "returns no debit against the receipient" do
42+
test "returns no debits against the receipient" do
5043
receipient = insert(:address)
5144
sender = insert(:address)
5245
transaction = insert(:transaction, value: 21)
@@ -56,7 +49,7 @@ defmodule Explorer.DebitTest do
5649
address_id = receipient.id
5750
Debit.refresh
5851
debit = Debit |> where(address_id: ^address_id) |> Repo.one
59-
assert debit.value == Decimal.new(0)
52+
assert debit == nil
6053
end
6154
end
6255
end

test/explorer/forms/address_form_test.exs

+10-4
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,19 @@ defmodule Explorer.AddressFormTest do
77

88
describe "build/1" do
99
test "returns a balance" do
10-
address = insert(:address, %{hash: "bert"})
11-
insert(:transaction, value: 5) |> with_addresses(%{to: "bert", from: "ernie"})
12-
insert(:transaction, value: 5) |> with_addresses(%{to: "bert", from: "kermit"})
10+
sender = insert(:address)
11+
recipient = insert(:address)
12+
transaction = insert(:transaction, value: 10)
13+
block = insert(:block)
14+
insert(:block_transaction, block: block, transaction: transaction)
15+
insert(:receipt, transaction: transaction, status: 1)
16+
insert(:from_address, address: sender, transaction: transaction)
17+
insert(:to_address, address: recipient, transaction: transaction)
1318

1419
Credit.refresh
1520
Debit.refresh
16-
assert AddressForm.build(Repo.preload(address, [:debit, :credit])).balance == Decimal.new(10)
21+
22+
assert AddressForm.build(Repo.preload(recipient, [:debit, :credit])).balance == Decimal.new(10)
1723
end
1824

1925
test "returns a zero balance when the address does not have balances" do

test/explorer/workers/refresh_balance_test.exs

+4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ defmodule Explorer.Workers.RefreshBalanceTest do
1313
address = insert(:address)
1414
transaction = insert(:transaction, value: 20)
1515
insert(:to_address, address: address, transaction: transaction)
16+
insert(:receipt, transaction: transaction, status: 1)
1617
RefreshBalance.perform
1718
assert Repo.one(Credit).value == Decimal.new(20)
1819
end
@@ -23,6 +24,7 @@ defmodule Explorer.Workers.RefreshBalanceTest do
2324
address = insert(:address)
2425
transaction = insert(:transaction, value: 20)
2526
insert(:from_address, address: address, transaction: transaction)
27+
insert(:receipt, transaction: transaction, status: 1)
2628
RefreshBalance.perform
2729
assert Repo.one(Debit).value == Decimal.new(20)
2830
end
@@ -34,6 +36,7 @@ defmodule Explorer.Workers.RefreshBalanceTest do
3436
address = insert(:address)
3537
transaction = insert(:transaction, value: 20)
3638
insert(:to_address, address: address, transaction: transaction)
39+
insert(:receipt, transaction: transaction, status: 1)
3740
RefreshBalance.perform("credit")
3841
assert Repo.one(Credit).value == Decimal.new(20)
3942
end
@@ -42,6 +45,7 @@ defmodule Explorer.Workers.RefreshBalanceTest do
4245
address = insert(:address)
4346
transaction = insert(:transaction, value: 20)
4447
insert(:from_address, address: address, transaction: transaction)
48+
insert(:receipt, transaction: transaction, status: 1)
4549
RefreshBalance.perform("debit")
4650
assert Repo.one(Debit).value == Decimal.new(20)
4751
end

0 commit comments

Comments
 (0)