diff --git a/ingest/change.go b/ingest/change.go index 80bd21e5a3..7d9b761db2 100644 --- a/ingest/change.go +++ b/ingest/change.go @@ -2,7 +2,6 @@ package ingest import ( "bytes" - "fmt" "github.com/stellar/go/support/errors" "github.com/stellar/go/xdr" @@ -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 { diff --git a/ingest/ledger_change_reader.go b/ingest/ledger_change_reader.go index 65c0ead681..a539e057ea 100644 --- a/ingest/ledger_change_reader.go +++ b/ingest/ledger_change_reader.go @@ -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++ diff --git a/ingest/ledger_change_reader_test.go b/ingest/ledger_change_reader_test.go index 682d3b41fb..dea3794cb5 100644 --- a/ingest/ledger_change_reader_test.go +++ b/ingest/ledger_change_reader_test.go @@ -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{ diff --git a/xdr/ledger_close_meta.go b/xdr/ledger_close_meta.go index f142ad320b..c8b85bae97 100644 --- a/xdr/ledger_close_meta.go +++ b/xdr/ledger_close_meta.go @@ -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)) }