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

Cache creators in cow base #2939

Merged
merged 1 commit into from
Sep 23, 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
1 change: 1 addition & 0 deletions ledger/appcow.go
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,7 @@ func MakeDebugBalances(l ledgerForCowBase, round basics.Round, proto protocol.Co
rnd: round - 1,
proto: config.Consensus[proto],
accounts: make(map[basics.Address]basics.AccountData),
creators: make(map[creatable]FoundAddress),
}

hdr := bookkeeping.BlockHeader{
Expand Down
30 changes: 29 additions & 1 deletion ledger/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,17 @@ const maxPaysetHint = 20000
// transaction group.
const asyncAccountLoadingThreadCount = 4

type creatable struct {
cindex basics.CreatableIndex
ctype basics.CreatableType
}

// FoundAddress is a wrapper for an address and a boolean.
type FoundAddress struct {
Copy link
Contributor

Choose a reason for hiding this comment

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

why is it exported? The struct name sound a bit weird. Maybe cachedCreator or something?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

FoundAddress is also used in evalIndexer.go where this type needs to be exposed to Indexer. I think this name is ok for caching as well. FoundAddress encodes an address that is found (or not found). That is exactly what we want to cache.

Address basics.Address
Exists bool
}

type roundCowBase struct {
l ledgerForCowBase

Expand All @@ -79,10 +90,26 @@ type roundCowBase struct {
// are beyond the scope of this cache.
// The account data store here is always the account data without the rewards.
accounts map[basics.Address]basics.AccountData

// Similar cache for asset/app creators.
creators map[creatable]FoundAddress
}

func (x *roundCowBase) getCreator(cidx basics.CreatableIndex, ctype basics.CreatableType) (basics.Address, bool, error) {
return x.l.GetCreatorForRound(x.rnd, cidx, ctype)
creatable := creatable{cindex: cidx, ctype: ctype}

if foundAddress, ok := x.creators[creatable]; ok {
return foundAddress.Address, foundAddress.Exists, nil
}

address, exists, err := x.l.GetCreatorForRound(x.rnd, cidx, ctype)
if err != nil {
return basics.Address{}, false, fmt.Errorf(
"roundCowBase.getCreator() cidx: %d ctype: %v err: %w", cidx, ctype, err)
}

x.creators[creatable] = FoundAddress{Address: address, Exists: exists}
return address, exists, nil
}

// lookup returns the non-rewarded account data for the provided account address. It uses the internal per-round cache
Expand Down Expand Up @@ -412,6 +439,7 @@ func startEvaluator(l ledgerForEvaluator, hdr bookkeeping.BlockHeader, proto con
txnCount: prevHeader.TxnCounter,
proto: proto,
accounts: make(map[basics.Address]basics.AccountData),
creators: make(map[creatable]FoundAddress),
}

eval := &BlockEvaluator{
Expand Down
6 changes: 0 additions & 6 deletions ledger/evalIndexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,6 @@ import (
"github.com/algorand/go-algorand/ledger/ledgercore"
)

// FoundAddress is a wrapper for an address and a boolean.
type FoundAddress struct {
Address basics.Address
Exists bool
}

// A ledger interface that Indexer implements. This is a simplified version of the
// ledgerForEvaluator interface. Certain functions that the evaluator doesn't use
// in the trusting mode are excluded, and the present functions only request data
Expand Down
69 changes: 69 additions & 0 deletions ledger/eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1732,3 +1732,72 @@ func TestAppInsMinBalance(t *testing.T) {
vb := l.endBlock(t, eval)
assert.Len(t, vb.delta.ModifiedAppLocalStates, 50)
}

type getCreatorForRoundResult struct {
address basics.Address
exists bool
}

type testCowBaseLedger struct {
creators []getCreatorForRoundResult
}

func (l *testCowBaseLedger) BlockHdr(basics.Round) (bookkeeping.BlockHeader, error) {
return bookkeeping.BlockHeader{}, errors.New("not implemented")
}

func (l *testCowBaseLedger) CheckDup(config.ConsensusParams, basics.Round, basics.Round, basics.Round, transactions.Txid, TxLease) error {
return errors.New("not implemented")
}

func (l *testCowBaseLedger) LookupWithoutRewards(basics.Round, basics.Address) (basics.AccountData, basics.Round, error) {
return basics.AccountData{}, basics.Round(0), errors.New("not implemented")
}

func (l *testCowBaseLedger) GetCreatorForRound(_ basics.Round, cindex basics.CreatableIndex, ctype basics.CreatableType) (basics.Address, bool, error) {
res := l.creators[0]
l.creators = l.creators[1:]
return res.address, res.exists, nil
}

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

addresses := make([]basics.Address, 3)
for i := 0; i < len(addresses); i++ {
_, err := rand.Read(addresses[i][:])
require.NoError(t, err)
}

creators := []getCreatorForRoundResult{
{address: addresses[0], exists: true},
{address: basics.Address{}, exists: false},
{address: addresses[1], exists: true},
{address: basics.Address{}, exists: false},
}
l := testCowBaseLedger{
creators: creators,
}

base := roundCowBase{
l: &l,
creators: map[creatable]FoundAddress{},
}

cindex := []basics.CreatableIndex{9, 10, 9, 10}
ctype := []basics.CreatableType{
basics.AssetCreatable,
basics.AssetCreatable,
basics.AppCreatable,
basics.AppCreatable,
}
for i := 0; i < 2; i++ {
for j, expected := range creators {
address, exists, err := base.getCreator(cindex[j], ctype[j])
require.NoError(t, err)

assert.Equal(t, expected.address, address)
assert.Equal(t, expected.exists, exists)
}
}
}