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

prefetcher: enable prefetcher for assets and apps #4352

Merged
merged 8 commits into from
Oct 31, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Do not prefetch zero asset transfers
  • Loading branch information
algorandskiy committed Sep 15, 2022
commit 76b71c92f0c7456683db71de68b1cc58b57077af
11 changes: 7 additions & 4 deletions ledger/internal/prefetcher/prefetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,14 +302,17 @@ func (p *accountPrefetcher) prefetch(ctx context.Context) {
loadAccountsAddResourceTask(nil, basics.CreatableIndex(stxn.Txn.XferAsset), basics.AssetCreatable, task, resourceTasks, queue)
loadAccountsAddResourceTask(&stxn.Txn.AssetSender, basics.CreatableIndex(stxn.Txn.XferAsset), basics.AssetCreatable, task, resourceTasks, queue)
} else {
loadAccountsAddResourceTask(&stxn.Txn.Sender, basics.CreatableIndex(stxn.Txn.XferAsset), basics.AssetCreatable, task, resourceTasks, queue)
if stxn.Txn.AssetAmount == 0 && (stxn.Txn.AssetReceiver == stxn.Txn.Sender) {
// opt in
if stxn.Txn.AssetAmount == 0 && (stxn.Txn.AssetReceiver == stxn.Txn.Sender) { // opt in
loadAccountsAddResourceTask(nil, basics.CreatableIndex(stxn.Txn.XferAsset), basics.AssetCreatable, task, resourceTasks, queue)
}
if stxn.Txn.AssetAmount != 0 { // zero transfer is noop
loadAccountsAddResourceTask(&stxn.Txn.Sender, basics.CreatableIndex(stxn.Txn.XferAsset), basics.AssetCreatable, task, resourceTasks, queue)
}
}
if !stxn.Txn.AssetReceiver.IsZero() {
loadAccountsAddResourceTask(&stxn.Txn.AssetReceiver, basics.CreatableIndex(stxn.Txn.XferAsset), basics.AssetCreatable, task, resourceTasks, queue)
if stxn.Txn.AssetAmount != 0 { // zero transfer is noop
loadAccountsAddResourceTask(&stxn.Txn.AssetReceiver, basics.CreatableIndex(stxn.Txn.XferAsset), basics.AssetCreatable, task, resourceTasks, queue)
}
}
if !stxn.Txn.AssetCloseTo.IsZero() {
loadAccountsAddResourceTask(&stxn.Txn.AssetCloseTo, basics.CreatableIndex(stxn.Txn.XferAsset), basics.AssetCreatable, task, resourceTasks, queue)
cce marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
92 changes: 91 additions & 1 deletion ledger/internal/prefetcher/prefetcher_alignment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ func TestEvaluatorPrefetcherAlignmentAssetOptIn(t *testing.T) {
require.Equal(t, requested, prefetched)
}

func TestEvaluatorPrefetcherAlignmentAssetTransfer(t *testing.T) {
func TestEvaluatorPrefetcherAlignmentAssetOptInCloseTo(t *testing.T) {
partitiontest.PartitionTest(t)

assetID := basics.AssetIndex(5)
Expand Down Expand Up @@ -567,6 +567,96 @@ func TestEvaluatorPrefetcherAlignmentAssetTransfer(t *testing.T) {
require.Equal(t, requested, prefetched)
}

func TestEvaluatorPrefetcherAlignmentAssetTransfer(t *testing.T) {
partitiontest.PartitionTest(t)

assetID := basics.AssetIndex(5)
l := &prefetcherAlignmentTestLedger{
balances: map[basics.Address]ledgercore.AccountData{
rewardsPool(): {
AccountBaseData: ledgercore.AccountBaseData{
MicroAlgos: basics.MicroAlgos{Raw: 1234567890},
},
},
makeAddress(1): {
AccountBaseData: ledgercore.AccountBaseData{
MicroAlgos: basics.MicroAlgos{Raw: 1000001},
TotalAssets: 1,
TotalAssetParams: 1,
},
},
makeAddress(2): {
AccountBaseData: ledgercore.AccountBaseData{
MicroAlgos: basics.MicroAlgos{Raw: 1000002},
},
},
makeAddress(3): {
AccountBaseData: ledgercore.AccountBaseData{
MicroAlgos: basics.MicroAlgos{Raw: 1000003},
},
},
},
assets: map[basics.Address]map[basics.AssetIndex]ledgercore.AssetResource{
makeAddress(1): {
assetID: {
AssetParams: &basics.AssetParams{},
AssetHolding: &basics.AssetHolding{},
},
},
makeAddress(2): {
assetID: {
AssetHolding: &basics.AssetHolding{Amount: 5},
},
},
makeAddress(3): {
assetID: {
AssetHolding: &basics.AssetHolding{},
},
},
},
creators: map[basics.CreatableIndex]basics.Address{
basics.CreatableIndex(assetID): makeAddress(1),
},
}

txn := transactions.Transaction{
Type: protocol.AssetTransferTx,
Header: transactions.Header{
Sender: makeAddress(2),
GenesisHash: genesisHash(),
},
AssetTransferTxnFields: transactions.AssetTransferTxnFields{
XferAsset: assetID,
AssetReceiver: makeAddress(3),
AssetAmount: 1,
},
}

requested, prefetched := run(t, l, txn)

prefetched.Accounts[rewardsPool()] = struct{}{}
require.Equal(t, requested, prefetched)

// zero transfer of any asset
txn = transactions.Transaction{
Type: protocol.AssetTransferTx,
Header: transactions.Header{
Sender: makeAddress(1),
GenesisHash: genesisHash(),
},
AssetTransferTxnFields: transactions.AssetTransferTxnFields{
XferAsset: assetID + 12345,
AssetReceiver: makeAddress(2),
AssetAmount: 0,
},
}

requested, prefetched = run(t, l, txn)

prefetched.Accounts[rewardsPool()] = struct{}{}
require.Equal(t, requested, prefetched)
}

func TestEvaluatorPrefetcherAlignmentAssetClawback(t *testing.T) {
partitiontest.PartitionTest(t)

Expand Down