From 52e4cf34518c3970517b44e7bea7a752976011c4 Mon Sep 17 00:00:00 2001 From: tamirms Date: Tue, 5 Sep 2023 16:21:27 +0100 Subject: [PATCH 1/2] Add missing ledger entry types to StatsChangeProcessor (#5038) --- ingest/stats_change_processor.go | 16 ++++++++++++++++ ingest/stats_change_processor_test.go | 8 +++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/ingest/stats_change_processor.go b/ingest/stats_change_processor.go index f82bbc40c4..01f33da466 100644 --- a/ingest/stats_change_processor.go +++ b/ingest/stats_change_processor.go @@ -184,5 +184,21 @@ func (stats *StatsChangeProcessorResults) Map() map[string]interface{} { "stats_liquidity_pools_created": stats.LiquidityPoolsCreated, "stats_liquidity_pools_updated": stats.LiquidityPoolsUpdated, "stats_liquidity_pools_removed": stats.LiquidityPoolsRemoved, + + "stats_contract_data_created": stats.ContractDataCreated, + "stats_contract_data_updated": stats.ContractDataUpdated, + "stats_contract_data_removed": stats.ContractDataRemoved, + + "stats_contract_code_created": stats.ContractCodeCreated, + "stats_contract_code_updated": stats.ContractCodeUpdated, + "stats_contract_code_removed": stats.ContractCodeRemoved, + + "stats_config_settings_created": stats.ConfigSettingsCreated, + "stats_config_settings_updated": stats.ConfigSettingsUpdated, + "stats_config_settings_removed": stats.ConfigSettingsRemoved, + + "stats_expiration_created": stats.ExpirationCreated, + "stats_expiration_updated": stats.ExpirationUpdated, + "stats_expiration_removed": stats.ExpirationRemoved, } } diff --git a/ingest/stats_change_processor_test.go b/ingest/stats_change_processor_test.go index 6b2da6582d..fe14af56e3 100644 --- a/ingest/stats_change_processor_test.go +++ b/ingest/stats_change_processor_test.go @@ -4,8 +4,9 @@ import ( "context" "testing" - "github.com/stellar/go/xdr" "github.com/stretchr/testify/assert" + + "github.com/stellar/go/xdr" ) func TestStatsChangeProcessor(t *testing.T) { @@ -44,6 +45,7 @@ func TestStatsChangeProcessor(t *testing.T) { assert.Equal(t, int64(1), results.ContractDataCreated) assert.Equal(t, int64(1), results.ContractCodeCreated) assert.Equal(t, int64(1), results.ConfigSettingsCreated) + assert.Equal(t, int64(1), results.ExpirationCreated) assert.Equal(t, int64(1), results.AccountsUpdated) assert.Equal(t, int64(1), results.ClaimableBalancesUpdated) @@ -54,6 +56,7 @@ func TestStatsChangeProcessor(t *testing.T) { assert.Equal(t, int64(1), results.ContractDataUpdated) assert.Equal(t, int64(1), results.ContractCodeUpdated) assert.Equal(t, int64(1), results.ConfigSettingsUpdated) + assert.Equal(t, int64(1), results.ExpirationUpdated) assert.Equal(t, int64(1), results.AccountsRemoved) assert.Equal(t, int64(1), results.ClaimableBalancesRemoved) @@ -64,4 +67,7 @@ func TestStatsChangeProcessor(t *testing.T) { assert.Equal(t, int64(1), results.ContractCodeRemoved) assert.Equal(t, int64(1), results.ContractDataRemoved) assert.Equal(t, int64(1), results.ConfigSettingsRemoved) + assert.Equal(t, int64(1), results.ExpirationRemoved) + + assert.Equal(t, len(xdr.LedgerEntryTypeMap)*3, len(results.Map())) } From ebdad11ae10b8f1ed7d7a4f5cabaeb61b28067a3 Mon Sep 17 00:00:00 2001 From: tamirms Date: Tue, 5 Sep 2023 16:57:02 +0100 Subject: [PATCH 2/2] ingest: Exclude evicted temporary ledger keys from change reader (#5037) --- ingest/change.go | 40 ----------------------------- ingest/ledger_change_reader.go | 15 +++++++---- ingest/ledger_change_reader_test.go | 7 ----- xdr/ledger_close_meta.go | 32 ++++++++++++----------- 4 files changed, 27 insertions(+), 67 deletions(-) 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)) }