Skip to content

Commit

Permalink
Merge branch 'master' into bump-soroban-rpc
Browse files Browse the repository at this point in the history
  • Loading branch information
tamirms authored Sep 5, 2023
2 parents 8b72f3e + ebdad11 commit 9a1022c
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 68 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
16 changes: 16 additions & 0 deletions ingest/stats_change_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
8 changes: 7 additions & 1 deletion ingest/stats_change_processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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()))
}
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 9a1022c

Please sign in to comment.