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: improve error codes #3815

Merged
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
2 changes: 1 addition & 1 deletion ledger/internal/prefetcher/prefetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ func (p *accountPrefetcher) prefetch(ctx context.Context) {
if done.err != nil {
// if there is an error, report the error to the output channel.
p.outChan <- LoadedTransactionGroup{
Err: done.err,
Err: ledgercore.GroupTaskError{Err: done.err, GroupIdx: done.groupIdx},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With your change, the Err is always either of type ledgercore.GroupTaskError or nil. It sounds as if you should change the Err datatype to be *GroupTaskError.

}
return
}
Expand Down
107 changes: 99 additions & 8 deletions ledger/internal/prefetcher/prefetcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package prefetcher_test

import (
"context"
"fmt"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -50,9 +51,10 @@ func makeAddress(addressSeed int) (o basics.Address) {
const proto = protocol.ConsensusCurrentVersion

type prefetcherTestLedger struct {
round basics.Round
balances map[basics.Address]ledgercore.AccountData
creators map[basics.CreatableIndex]basics.Address
round basics.Round
balances map[basics.Address]ledgercore.AccountData
creators map[basics.CreatableIndex]basics.Address
errorAddress map[basics.Address]bool
}

func (l *prefetcherTestLedger) BlockHdr(basics.Round) (bookkeeping.BlockHeader, error) {
Expand All @@ -61,7 +63,10 @@ func (l *prefetcherTestLedger) BlockHdr(basics.Round) (bookkeeping.BlockHeader,
func (l *prefetcherTestLedger) CheckDup(config.ConsensusParams, basics.Round, basics.Round, basics.Round, transactions.Txid, ledgercore.Txlease) error {
return nil
}
func (l *prefetcherTestLedger) LookupWithoutRewards(_ basics.Round, addr basics.Address) (ledgercore.AccountData, basics.Round, error) {
func (l *prefetcherTestLedger) LookupWithoutRewards(rnd basics.Round, addr basics.Address) (ledgercore.AccountData, basics.Round, error) {
if _, has := l.errorAddress[addr]; has {
return ledgercore.AccountData{}, l.round, fmt.Errorf("Lookup Error")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you might want to use a custom error type here so that you could make errors.Is when testing.

}
if data, has := l.balances[addr]; has {
return data, l.round, nil
}
Expand All @@ -71,9 +76,15 @@ func (l *prefetcherTestLedger) LookupApplication(rnd basics.Round, addr basics.A
return ledgercore.AppResource{}, nil
}
func (l *prefetcherTestLedger) LookupAsset(rnd basics.Round, addr basics.Address, aidx basics.AssetIndex) (ledgercore.AssetResource, error) {
if aidx > 1000000 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of using a threshold, make a given asset index invalid ( i.e. const errorTriggeringAssetIndex = 1000000 )

return ledgercore.AssetResource{}, fmt.Errorf("Asset Lookup Error")
}
return ledgercore.AssetResource{}, nil
}
func (l *prefetcherTestLedger) GetCreatorForRound(_ basics.Round, cidx basics.CreatableIndex, _ basics.CreatableType) (basics.Address, bool, error) {
if cidx > 1000000 {
return basics.Address{}, false, fmt.Errorf("Get Creator Error")
}
if addr, has := l.creators[cidx]; has {
return addr, true, nil
}
Expand Down Expand Up @@ -151,16 +162,17 @@ func compareLoadedResourcesEntries(t *testing.T, expected []prefetcher.LoadedRes
require.Equal(t, expectedForTest, actualForTest)
}

func TestEvaluatorPrefetcher(t *testing.T) {
func TestEvaluatorPrefetcherX(t *testing.T) {
partitiontest.PartitionTest(t)

rnd := basics.Round(5)
var feeSinkAddr = basics.Address{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}

var ledger = &prefetcherTestLedger{
round: rnd,
balances: make(map[basics.Address]ledgercore.AccountData),
creators: make(map[basics.CreatableIndex]basics.Address),
round: rnd,
balances: make(map[basics.Address]ledgercore.AccountData),
creators: make(map[basics.CreatableIndex]basics.Address),
errorAddress: make(map[basics.Address]bool),
}
ledger.balances[makeAddress(1)] = ledgercore.AccountData{
AccountBaseData: ledgercore.AccountBaseData{MicroAlgos: basics.MicroAlgos{Raw: 100000000}},
Expand Down Expand Up @@ -493,6 +505,85 @@ func TestEvaluatorPrefetcher(t *testing.T) {
require.False(t, ok)
})
}

// Test for the GroupIdx in the returned error
tc := testCases[3]
t.Run(tc.name, func(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you're repeating the same test case name, please have this implemented in a separate Test function.

errorReceived := false
groups := make([][]transactions.SignedTxnWithAD, 5)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that this test would be much more readable if you'll use explicit use cases ( as above ), instead of programmatically modifying the test cases.

for i := 0; i < 5; i++ {
groups[i] = make([]transactions.SignedTxnWithAD, 2)
for j := 0; j < 2; j++ {
groups[i][j].SignedTxn = tc.signedTxn
if tc.signedTxn.Txn.Type == protocol.AssetTransferTx {
if i == 2 {
// force error in asset lookup
groups[i][j].SignedTxn.Txn.AssetTransferTxnFields.XferAsset = 1000001
}
}
}
}
preloadedTxnGroupsCh := prefetcher.PrefetchAccounts(context.Background(), ledger, rnd+100, groups, feeSinkAddr, config.Consensus[proto])
for loadedTxnGroup := range preloadedTxnGroupsCh {
if err, ok := loadedTxnGroup.Err.(ledgercore.GroupTaskError); ok {
errorReceived = true
require.Equal(t, int64(2), err.GroupIdx)
}
}
require.True(t, errorReceived)
})

tc = testCases[2]
errorReceived := false
// Test for errors generated from GetCreatorForRound
t.Run(tc.name, func(t *testing.T) {
groups := make([][]transactions.SignedTxnWithAD, 5)
for i := 0; i < 5; i++ {
groups[i] = make([]transactions.SignedTxnWithAD, 10)
for j := 0; j < 10; j++ {
groups[i][j].SignedTxn = tc.signedTxn
if tc.signedTxn.Txn.AssetConfigTxnFields.ConfigAsset > 0 {
// creatable index
tc.signedTxn.Txn.AssetConfigTxnFields.ConfigAsset = 1000001
}
}
}
preloadedTxnGroupsCh := prefetcher.PrefetchAccounts(context.Background(), ledger, rnd+100, groups, feeSinkAddr, config.Consensus[proto])

for loadedTxnGroup := range preloadedTxnGroupsCh {
if err, ok := loadedTxnGroup.Err.(ledgercore.GroupTaskError); ok {
errorReceived = true
fmt.Println(err)
}
}
require.True(t, errorReceived)
})

// Test for errors generated from LookupWithoutRewards
errorReceived = false
t.Run(tc.name, func(t *testing.T) {
groups := make([][]transactions.SignedTxnWithAD, 5)
for i := 0; i < 5; i++ {
groups[i] = make([]transactions.SignedTxnWithAD, 10)
for j := 0; j < 10; j++ {
groups[i][j].SignedTxn = tc.signedTxn
if i > 3 && j > 7 {
// force lookup error
ledger.errorAddress[groups[i][j].SignedTxn.Txn.Sender] = true
}
}
}
preloadedTxnGroupsCh := prefetcher.PrefetchAccounts(context.Background(), ledger, rnd+100, groups, feeSinkAddr, config.Consensus[proto])

for loadedTxnGroup := range preloadedTxnGroupsCh {
if err, ok := loadedTxnGroup.Err.(ledgercore.GroupTaskError); ok {
errorReceived = true
fmt.Println(err)
}
}
require.True(t, errorReceived)
})

}

func TestEvaluatorPrefetcherQueueExpansion(t *testing.T) {
Expand Down
11 changes: 11 additions & 0 deletions ledger/ledgercore/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,14 @@ type ErrNonSequentialBlockEval struct {
func (err ErrNonSequentialBlockEval) Error() string {
return fmt.Sprintf("block evaluation for round %d requires sequential evaluation while the latest round is %d", err.EvaluatorRound, err.LatestRound)
}

// GroupTaskError indicates the group index of the unfulfilled resource
type GroupTaskError struct {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

two requests:

  1. move the GroupTaskError to the prefetcher package.
  2. Make GroupTaskError be used with &GroupTaskError, and have the Unwrap() error implemented, so that the caller could use the errors.Is and errors.As.

Err error
GroupIdx int64
}

// Error satisfies builtin interface `error`
func (err GroupTaskError) Error() string {
return fmt.Sprintf("Prefetch failed for groupIdx %d cause: %v", err.GroupIdx, err.Err)
}