Skip to content

Commit

Permalink
fix create and delete handling
Browse files Browse the repository at this point in the history
  • Loading branch information
billettc committed Nov 1, 2023
1 parent 82cfaad commit 492ddee
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 30 deletions.
4 changes: 2 additions & 2 deletions accountresolver/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ func (p *Processor) ProcessInstruction(ctx context.Context, stats *Stats, blockN

tableLookupAccount := accountKeys[instruction.Accounts[0]]
p.logger.Debug("Creating address table lookup", zap.String("account", base58.Encode(tableLookupAccount)))
err = p.accountsResolver.CreateOrDelete(ctx, blockNum, trxHash, instructionIndex, tableLookupAccount)
err = p.accountsResolver.CreateOrDelete(tableLookupAccount)
if err != nil {
return fmt.Errorf("creating lookup table %s at block %d: %w", base58.Encode(tableLookupAccount), blockNum, err)
}
Expand Down Expand Up @@ -399,7 +399,7 @@ func (p *Processor) ProcessInstruction(ctx context.Context, stats *Stats, blockN
start := time.Now()
tableLookupAccount := accountKeys[instruction.Accounts[0]]
p.logger.Debug("Deleting address table lookup", zap.String("account", base58.Encode(tableLookupAccount)))
err = p.accountsResolver.CreateOrDelete(ctx, blockNum, trxHash, instructionIndex, tableLookupAccount)
err = p.accountsResolver.CreateOrDelete(tableLookupAccount)
if err != nil {
return fmt.Errorf("deleting lookup table %s at block %d: %w", base58.Encode(tableLookupAccount), blockNum, err)
}
Expand Down
25 changes: 10 additions & 15 deletions accountresolver/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,8 @@ func NewKVDBAccountsResolver(store store.KVStore, logger *zap.Logger) *KVDBAccou
}
}

func (r *KVDBAccountsResolver) CreateOrDelete(ctx context.Context, blockNum uint64, trxHash []byte, instructionIndex string, key Account) error {
err := r.store.Put(ctx, Keys.extendTableLookup(key, blockNum), nil)
if err != nil {
return fmt.Errorf("reseting table account for %s: %w", key, err)
}

err = r.store.FlushPuts(ctx)
if err != nil {
return fmt.Errorf("flushing extended accounts for key %q: %w", key, err)
}

r.pushToCache(blockNum, key.Base58(), nil)

func (r *KVDBAccountsResolver) CreateOrDelete(key Account) error {
r.toCommit[key.Base58()] = nil
return nil
}

Expand All @@ -55,8 +44,14 @@ func (r *KVDBAccountsResolver) CommitBlock(ctx context.Context, blockNum uint64)
return fmt.Errorf("retrieving last accounts for tableKey %q: %w", tableKey, err)
}

extendedAccounts := append(currentAccounts, accounts...)
payload := encodeAccounts(extendedAccounts)
var payload []byte
var extendedAccounts Accounts
if accounts != nil { // nil means delete or create
extendedAccounts = append(currentAccounts, accounts...)
payload = encodeAccounts(extendedAccounts)

}

err = r.store.Put(ctx, Keys.extendTableLookup(tableKey, blockNum), payload)
if err != nil {
return fmt.Errorf("writing extended accounts for tableKey %q: %w", tableKey, err)
Expand Down
22 changes: 9 additions & 13 deletions accountresolver/resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,27 +181,23 @@ func Test_Extend_Multiple_Accounts_Same_Trx(t *testing.T) {
}

func Test_Create_Extend_TableLookupAccount_SameTransaction(t *testing.T) {
trxHash := []byte{0x01}
err := os.RemoveAll("/tmp/my-badger.db")
require.NoError(t, err)

db, err := store.New("badger3:///tmp/my-badger.db")
require.NoError(t, err)

resolver := NewKVDBAccountsResolver(db, zap.NewNop())
err = resolver.CreateOrDelete(context.Background(), 1, trxHash, "0", accountFromBase58(t, a1))
err = resolver.CreateOrDelete(accountFromBase58(t, a1))
require.NoError(t, err)
accounts, _, err := resolver.Resolve(context.Background(), 1, accountFromBase58(t, a1))
require.NoError(t, err)
require.Equal(t, Accounts(nil), accounts)

err = resolver.Extend(accountFromBase58(t, a1), []Account{accountFromBase58(t, a2)})
require.NoError(t, err)

err = resolver.CommitBlock(context.Background(), 1)
require.NoError(t, err)

accounts, _, err = resolver.Resolve(context.Background(), 1, accountFromBase58(t, a1))
accounts, _, err := resolver.Resolve(context.Background(), 1, accountFromBase58(t, a1))
require.NoError(t, err)
require.Equal(t, Accounts(nil), accounts)

Expand All @@ -212,26 +208,23 @@ func Test_Create_Extend_TableLookupAccount_SameTransaction(t *testing.T) {
}

func Test_Create_Extend_TableLookupAccount_SameTransaction_Delete_Other_Block(t *testing.T) {
trxHash, trxHash1 := []byte{0x01}, []byte{0x02}
err := os.RemoveAll("/tmp/my-badger.db")
require.NoError(t, err)

db, err := store.New("badger3:///tmp/my-badger.db")
require.NoError(t, err)

resolver := NewKVDBAccountsResolver(db, zap.NewNop())
err = resolver.CreateOrDelete(context.Background(), 1, trxHash, "0", accountFromBase58(t, a1))
require.NoError(t, err)
accounts, _, err := resolver.Resolve(context.Background(), 1, accountFromBase58(t, a1))
err = resolver.CreateOrDelete(accountFromBase58(t, a1))
require.NoError(t, err)
require.Equal(t, Accounts(nil), accounts)

err = resolver.Extend(accountFromBase58(t, a1), []Account{accountFromBase58(t, a2)})
require.NoError(t, err)

err = resolver.CommitBlock(context.Background(), 1)
require.NoError(t, err)

accounts, _, err = resolver.Resolve(context.Background(), 1, accountFromBase58(t, a1))
accounts, _, err := resolver.Resolve(context.Background(), 1, accountFromBase58(t, a1))
require.NoError(t, err)
require.Equal(t, Accounts(nil), accounts)

Expand All @@ -240,7 +233,10 @@ func Test_Create_Extend_TableLookupAccount_SameTransaction_Delete_Other_Block(t
require.Equal(t, 1, len(accounts))
require.Equal(t, accountFromBase58(t, a2), accounts[0])

err = resolver.CreateOrDelete(context.Background(), 2, trxHash1, "0", accountFromBase58(t, a1))
err = resolver.CreateOrDelete(accountFromBase58(t, a1))
require.NoError(t, err)

err = resolver.CommitBlock(context.Background(), 2)
require.NoError(t, err)

accounts, _, err = resolver.Resolve(context.Background(), 2, accountFromBase58(t, a1))
Expand Down

0 comments on commit 492ddee

Please sign in to comment.