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

Write app call addresses in txn_participation table #770

Merged
merged 2 commits into from
Nov 4, 2021
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
30 changes: 30 additions & 0 deletions accounting/accounting.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package accounting

import (
"github.com/algorand/go-algorand/data/basics"
"github.com/algorand/go-algorand/data/transactions"
)

// GetTransactionParticipants calls function `add` for every address referenced in the
// given transaction, possibly with repetition.
func GetTransactionParticipants(stxnad *transactions.SignedTxnWithAD, includeInner bool, add func(address basics.Address)) {
txn := &stxnad.Txn

add(txn.Sender)
add(txn.Receiver)
add(txn.CloseRemainderTo)
add(txn.AssetSender)
add(txn.AssetReceiver)
add(txn.AssetCloseTo)
add(txn.FreezeAccount)

for _, address := range txn.ApplicationCallTxnFields.Accounts {
add(address)
}

if includeInner {
for _, inner := range stxnad.ApplyData.EvalDelta.InnerTxns {
GetTransactionParticipants(&inner, includeInner, add)
}
}
}
23 changes: 3 additions & 20 deletions idb/postgres/internal/writer/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/algorand/go-algorand/protocol"
"github.com/jackc/pgx/v4"

"github.com/algorand/indexer/accounting"
"github.com/algorand/indexer/idb"
"github.com/algorand/indexer/idb/postgres/internal/encoding"
"github.com/algorand/indexer/idb/postgres/internal/schema"
Expand Down Expand Up @@ -265,24 +266,6 @@ func (w *Writer) addTransactions(block *bookkeeping.Block, modifiedTxns []transa
return nil
}

func getTransactionParticipantsImpl(stxnad *transactions.SignedTxnWithAD, includeInner bool, add func(address basics.Address)) {
txn := stxnad.Txn

add(txn.Sender)
add(txn.Receiver)
add(txn.CloseRemainderTo)
add(txn.AssetSender)
add(txn.AssetReceiver)
add(txn.AssetCloseTo)
add(txn.FreezeAccount)

if includeInner {
for _, inner := range stxnad.ApplyData.EvalDelta.InnerTxns {
getTransactionParticipantsImpl(&inner, includeInner, add)
}
}
}

// getTransactionParticipants returns referenced addresses from the txn and all inner txns
func getTransactionParticipants(stxnad *transactions.SignedTxnWithAD, includeInner bool) []basics.Address {
const acctsPerTxn = 7
Expand All @@ -302,7 +285,7 @@ func getTransactionParticipants(stxnad *transactions.SignedTxnWithAD, includeInn
res = append(res, address)
}

getTransactionParticipantsImpl(stxnad, includeInner, add)
accounting.GetTransactionParticipants(stxnad, includeInner, add)
return res
}

Expand All @@ -318,7 +301,7 @@ func getTransactionParticipants(stxnad *transactions.SignedTxnWithAD, includeInn
participants[address] = struct{}{}
}

getTransactionParticipantsImpl(stxnad, includeInner, add)
accounting.GetTransactionParticipants(stxnad, includeInner, add)

res := make([]basics.Address, 0, len(participants))
for addr := range participants {
Expand Down
157 changes: 103 additions & 54 deletions idb/postgres/internal/writer/writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,72 +341,121 @@ func TestWriterTxnTableAssetCloseAmount(t *testing.T) {
assert.NoError(t, rows.Err())
}

func TestWriterTxnParticipationTableBasic(t *testing.T) {
db, shutdownFunc := setupPostgres(t)
defer shutdownFunc()

block := bookkeeping.Block{
BlockHeader: bookkeeping.BlockHeader{
Round: basics.Round(2),
GenesisID: test.MakeGenesis().ID(),
GenesisHash: test.GenesisHash,
UpgradeState: bookkeeping.UpgradeState{
CurrentProtocol: test.Proto,
func TestWriterTxnParticipationTable(t *testing.T) {
type testtype struct {
name string
payset transactions.Payset
expected []txnParticipationRow
}

makeBlockFunc := func() bookkeeping.Block {
return bookkeeping.Block{
BlockHeader: bookkeeping.BlockHeader{
Round: basics.Round(2),
GenesisID: test.MakeGenesis().ID(),
GenesisHash: test.GenesisHash,
UpgradeState: bookkeeping.UpgradeState{
CurrentProtocol: test.Proto,
},
},
},
Payset: make(transactions.Payset, 2),
}
}

stxnad0 := test.MakePaymentTxn(
1000, 1, 0, 0, 0, 0, test.AccountA, test.AccountB, basics.Address{},
basics.Address{})
var err error
block.Payset[0], err = block.EncodeSignedTxn(stxnad0.SignedTxn, stxnad0.ApplyData)
require.NoError(t, err)

stxnad1 := test.MakeAssetConfigTxn(
0, 100, 1, false, "ma", "myasset", "myasset.com", test.AccountC)
block.Payset[1], err = block.EncodeSignedTxn(stxnad1.SignedTxn, stxnad1.ApplyData)
require.NoError(t, err)
var tests []testtype
{
stxnad0 := test.MakePaymentTxn(
1000, 1, 0, 0, 0, 0, test.AccountA, test.AccountB, basics.Address{},
basics.Address{})
stib0, err := makeBlockFunc().EncodeSignedTxn(stxnad0.SignedTxn, stxnad0.ApplyData)
require.NoError(t, err)

f := func(tx pgx.Tx) error {
w, err := writer.MakeWriter(tx)
stxnad1 := test.MakeAssetConfigTxn(
0, 100, 1, false, "ma", "myasset", "myasset.com", test.AccountC)
stib1, err := makeBlockFunc().EncodeSignedTxn(stxnad1.SignedTxn, stxnad1.ApplyData)
require.NoError(t, err)

err = w.AddBlock(&block, block.Payset, ledgercore.StateDelta{})
testcase := testtype{
name: "basic",
payset: []transactions.SignedTxnInBlock{stib0, stib1},
expected: []txnParticipationRow{
{
addr: test.AccountA,
round: 2,
intra: 0,
},
{
addr: test.AccountB,
round: 2,
intra: 0,
},
{
addr: test.AccountC,
round: 2,
intra: 1,
},
},
}
tests = append(tests, testcase)
}
{
stxnad := test.MakeCreateAppTxn(test.AccountA)
stxnad.Txn.ApplicationCallTxnFields.Accounts =
[]basics.Address{test.AccountB, test.AccountC}
stib, err := makeBlockFunc().EncodeSignedTxn(stxnad.SignedTxn, stxnad.ApplyData)
require.NoError(t, err)

w.Close()
return nil
testcase := testtype{
name: "app_call_addresses",
payset: []transactions.SignedTxnInBlock{stib},
expected: []txnParticipationRow{
{
addr: test.AccountA,
round: 2,
intra: 0,
},
{
addr: test.AccountB,
round: 2,
intra: 0,
},
{
addr: test.AccountC,
round: 2,
intra: 0,
},
},
}
tests = append(tests, testcase)
}
err = pgutil.TxWithRetry(db, serializable, f, nil)
require.NoError(t, err)

results, err := txnParticipationQuery(db, `SELECT * FROM txn_participation ORDER BY round, intra, addr`)
assert.NoError(t, err)
for _, testcase := range tests {
t.Run(testcase.name, func(t *testing.T) {
db, shutdownFunc := setupPostgres(t)
defer shutdownFunc()

expected := []txnParticipationRow{
{
addr: test.AccountA,
round: 2,
intra: 0,
},
{
addr: test.AccountB,
round: 2,
intra: 0,
},
{
addr: test.AccountC,
round: 2,
intra: 1,
},
}
block := makeBlockFunc()
block.Payset = testcase.payset

f := func(tx pgx.Tx) error {
w, err := writer.MakeWriter(tx)
require.NoError(t, err)

err = w.AddBlock(&block, block.Payset, ledgercore.StateDelta{})
require.NoError(t, err)

w.Close()
return nil
}
err := pgutil.TxWithRetry(db, serializable, f, nil)
require.NoError(t, err)

results, err := txnParticipationQuery(
db, `SELECT * FROM txn_participation ORDER BY round, intra, addr`)
assert.NoError(t, err)

// Verify expected participation
assert.Len(t, results, len(expected))
for i := range results {
assert.Equal(t, expected[i], results[i])
// Verify expected participation
assert.Equal(t, testcase.expected, results)
})
}
}

Expand Down
20 changes: 5 additions & 15 deletions idb/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/jackc/pgx/v4/pgxpool"
log "github.com/sirupsen/logrus"

"github.com/algorand/indexer/accounting"
models "github.com/algorand/indexer/api/generated/v2"
"github.com/algorand/indexer/idb"
"github.com/algorand/indexer/idb/migration"
Expand Down Expand Up @@ -158,20 +159,6 @@ func (db *IndexerDb) init(opts idb.IndexerDbOptions) (chan struct{}, error) {
return db.runAvailableMigrations()
}

// Add addresses referenced in `txn` to `out`.
func getTxnAddresses(txn *transactions.Transaction, out map[basics.Address]struct{}) {
out[txn.Sender] = struct{}{}
out[txn.Receiver] = struct{}{}
out[txn.CloseRemainderTo] = struct{}{}
out[txn.AssetSender] = struct{}{}
out[txn.AssetReceiver] = struct{}{}
out[txn.AssetCloseTo] = struct{}{}
out[txn.FreezeAccount] = struct{}{}
for _, address := range txn.ApplicationCallTxnFields.Accounts {
out[address] = struct{}{}
}
}

// Returns all addresses referenced in `block`.
func getBlockAddresses(block *bookkeeping.Block) map[basics.Address]struct{} {
// Reserve a reasonable memory size for the map.
Expand All @@ -180,7 +167,10 @@ func getBlockAddresses(block *bookkeeping.Block) map[basics.Address]struct{} {
res[block.FeeSink] = struct{}{}
res[block.RewardsPool] = struct{}{}
for _, stib := range block.Payset {
getTxnAddresses(&stib.Txn, res)
addFunc := func(address basics.Address) {
res[address] = struct{}{}
}
accounting.GetTransactionParticipants(&stib.SignedTxnWithAD, true, addFunc)
}

return res
Expand Down