Skip to content

Commit

Permalink
ingest: Exclude evicted temporary ledger keys from change reader (#5037)
Browse files Browse the repository at this point in the history
  • Loading branch information
tamirms authored Sep 5, 2023
1 parent 52e4cf3 commit ebdad11
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 67 deletions.
40 changes: 0 additions & 40 deletions ingest/change.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package ingest

import (
"bytes"
"fmt"

"github.com/stellar/go/support/errors"
"github.com/stellar/go/xdr"
Expand Down Expand Up @@ -68,45 +67,6 @@ func GetChangesFromLedgerEntryChanges(ledgerEntryChanges xdr.LedgerEntryChanges)
return changes
}

// GetChangesFromLedgerEntryEvictions transforms evicted LedgerKeys to []Change.
// The generated changes always remove the entries.
func GetChangesFromLedgerEntryEvictions(keys []xdr.LedgerKey) ([]Change, error) {
changes := make([]Change, 0, len(keys))

for _, key := range keys {
state := xdr.LedgerEntry{}
switch key.Type {
case xdr.LedgerEntryTypeContractData:
err := state.Data.SetContractData(&xdr.ContractDataEntry{
Contract: key.ContractData.Contract,
Key: key.ContractData.Key,
Durability: key.ContractData.Durability,
})
if err != nil {
return nil, errors.Wrap(err, "error setting ContractDataEntry")
}
case xdr.LedgerEntryTypeContractCode:
err := state.Data.SetContractCode(&xdr.ContractCodeEntry{
Hash: key.ContractCode.Hash,
})
if err != nil {
return nil, errors.Wrap(err, "error setting ContractCodeEntry")
}
default:
// Currently only contractData and contractCode are evicted by core, so
// we only need to handle those two.
return nil, fmt.Errorf("invalid LedgerEntry eviction type: %s", key.Type)
}
changes = append(changes, Change{
Type: key.Type,
Pre: &state,
Post: nil,
})
}

return changes, nil
}

// LedgerEntryChangeType returns type in terms of LedgerEntryChangeType.
func (c *Change) LedgerEntryChangeType() xdr.LedgerEntryChangeType {
switch {
Expand Down
15 changes: 10 additions & 5 deletions ingest/ledger_change_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,19 @@ func (r *LedgerChangeReader) Read() (Change, error) {
}
return r.Read()
case evictionChangesState:
// Get contract ledgerEntry evictions
keys, err := r.ledgerCloseMeta.EvictedLedgerKeys()
entries, err := r.ledgerCloseMeta.EvictedPersistentLedgerEntries()
if err != nil {
return Change{}, err
}
changes, err := GetChangesFromLedgerEntryEvictions(keys)
if err != nil {
return Change{}, err
changes := make([]Change, len(entries))
for i := range entries {
entry := entries[i]
// when a ledger entry is evicted it is removed from the ledger
changes[i] = Change{
Type: entry.Data.Type,
Pre: &entry,
Post: nil,
}
}
r.pending = append(r.pending, changes...)
r.state++
Expand Down
7 changes: 0 additions & 7 deletions ingest/ledger_change_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -581,13 +581,6 @@ func TestLedgerChangeLedgerCloseMetaV2(t *testing.T) {
isBalance(metaAddress, 900),

// Evictions
isContractDataEviction(
contractAddress,
xdr.ScVal{
Type: xdr.ScValTypeScvSymbol,
Sym: &tempKey,
},
),
isContractDataEviction(
contractAddress,
xdr.ScVal{
Expand Down
32 changes: 17 additions & 15 deletions xdr/ledger_close_meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,25 +144,27 @@ func (l LedgerCloseMeta) UpgradesProcessing() []UpgradeEntryMeta {
}
}

// EvictedLedgerKeys returns the LedgerKeys for both the
// EvictedTemporaryLedgerKeys and and the EvictedPersistentLedgerEntries in a
// ledger.
func (l LedgerCloseMeta) EvictedLedgerKeys() ([]LedgerKey, error) {
// EvictedTemporaryLedgerKeys returns a slice of ledger keys for
// temporary ledger entries that have been evicted in this ledger.
func (l LedgerCloseMeta) EvictedTemporaryLedgerKeys() ([]LedgerKey, error) {
switch l.V {
case 0, 1:
return nil, nil
case 2:
v2 := l.MustV2()
keys := make([]LedgerKey, 0, len(v2.EvictedTemporaryLedgerKeys)+len(v2.EvictedPersistentLedgerEntries))
keys = append(keys, l.MustV2().EvictedTemporaryLedgerKeys...)
for _, entry := range l.MustV2().EvictedPersistentLedgerEntries {
key, err := entry.LedgerKey()
if err != nil {
return nil, err
}
keys = append(keys, key)
}
return keys, nil
return l.MustV2().EvictedTemporaryLedgerKeys, nil
default:
panic(fmt.Sprintf("Unsupported LedgerCloseMeta.V: %d", l.V))
}
}

// EvictedPersistentLedgerEntries returns the persistent ledger entries
// which have been evicted in this ledger.
func (l LedgerCloseMeta) EvictedPersistentLedgerEntries() ([]LedgerEntry, error) {
switch l.V {
case 0, 1:
return nil, nil
case 2:
return l.MustV2().EvictedPersistentLedgerEntries, nil
default:
panic(fmt.Sprintf("Unsupported LedgerCloseMeta.V: %d", l.V))
}
Expand Down

0 comments on commit ebdad11

Please sign in to comment.