Skip to content

Commit 9ad4cdc

Browse files
urvisavlaShaptictamirms
authored
ingest: Add support for ledger entry change type "restore" for Protocol 23 (#5587)
--------- Co-authored-by: George <[email protected]> Co-authored-by: tamirms <[email protected]>
1 parent f18e42e commit 9ad4cdc

23 files changed

+844
-186
lines changed

.github/workflows/horizon.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ jobs:
3535
HORIZON_INTEGRATION_TESTS_CAPTIVE_CORE_USE_DB: true
3636
PROTOCOL_22_CORE_DEBIAN_PKG_VERSION: 22.1.0-2194.0241e79f7.focal
3737
PROTOCOL_22_CORE_DOCKER_IMG: stellar/stellar-core:22.1.0-2194.0241e79f7.focal
38-
PROTOCOL_22_STELLAR_RPC_DOCKER_IMG: stellar/stellar-rpc:22.1.1
38+
PROTOCOL_22_STELLAR_RPC_DOCKER_IMG: stellar/stellar-rpc:22.1.2
3939
PGHOST: localhost
4040
PGPORT: 5432
4141
PGUSER: postgres

ingest/CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ All notable changes to this project will be documented in this file. This projec
44

55
## Pending
66

7+
### Protocol 23 Support
8+
* Added support for the new `RESTORE` ledger entry change type [5587](https://github.com/stellar/go/pull/5587).
9+
710
### Bug Fixes
811
* Update the boundary check in `BufferedStorageBackend` to queue ledgers up to the end boundary, resolving skipped final batch when the `from` ledger doesn't align with file boundary [5563](https://github.com/stellar/go/pull/5563).
912

ingest/change.go

+40-25
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ type Change struct {
4242
// The type of the ledger entry being changed.
4343
Type xdr.LedgerEntryType
4444

45+
// The specific type of change, such as Created, Updated, Removed or Restored.
46+
ChangeType xdr.LedgerEntryChangeType
47+
4548
// The state of the LedgerEntry before the change. This will be nil if the entry was created.
4649
Pre *xdr.LedgerEntry
4750

@@ -134,6 +137,7 @@ func (c Change) LedgerKey() (xdr.LedgerKey, error) {
134137
// - for create, pre is null and post is a new entry,
135138
// - for update, pre is previous state and post is the current state,
136139
// - for removed, pre is previous state and post is null.
140+
// - for restored, pre is null and post is a new/restored entry
137141
//
138142
// stellar-core source:
139143
// https://github.com/stellar/stellar-core/blob/e584b43/src/ledger/LedgerTxn.cpp#L582
@@ -144,24 +148,49 @@ func GetChangesFromLedgerEntryChanges(ledgerEntryChanges xdr.LedgerEntryChanges)
144148
case xdr.LedgerEntryChangeTypeLedgerEntryCreated:
145149
created := entryChange.MustCreated()
146150
changes = append(changes, Change{
147-
Type: created.Data.Type,
148-
Pre: nil,
149-
Post: &created,
151+
Type: created.Data.Type,
152+
Pre: nil,
153+
Post: &created,
154+
ChangeType: entryChange.Type,
150155
})
151156
case xdr.LedgerEntryChangeTypeLedgerEntryUpdated:
152-
state := ledgerEntryChanges[i-1].MustState()
157+
// Update entries always have a previous state entry [state, updated]
158+
// except for contract entries that are restored and updated within the same
159+
// transaction, which appear as [restored, updated]
160+
// For details, see https://github.com/stellar/stellar-protocol/blob/master/core/cap-0062.md
153161
updated := entryChange.MustUpdated()
162+
state, ok := ledgerEntryChanges[i-1].GetState()
163+
if !ok {
164+
state = ledgerEntryChanges[i-1].MustRestored()
165+
}
154166
changes = append(changes, Change{
155-
Type: state.Data.Type,
156-
Pre: &state,
157-
Post: &updated,
167+
Type: state.Data.Type,
168+
Pre: &state,
169+
Post: &updated,
170+
ChangeType: entryChange.Type,
158171
})
159172
case xdr.LedgerEntryChangeTypeLedgerEntryRemoved:
160-
state := ledgerEntryChanges[i-1].MustState()
173+
// Removed entries always have an associated state entry [state, updated]
174+
// except for contract entries that are restored and removed within the same
175+
// transaction, which appear as [restored, removed]
176+
// For details, see https://github.com/stellar/stellar-protocol/blob/master/core/cap-0062.md
177+
state, ok := ledgerEntryChanges[i-1].GetState()
178+
if !ok {
179+
state = ledgerEntryChanges[i-1].MustRestored()
180+
}
181+
changes = append(changes, Change{
182+
Type: state.Data.Type,
183+
Pre: &state,
184+
Post: nil,
185+
ChangeType: entryChange.Type,
186+
})
187+
case xdr.LedgerEntryChangeTypeLedgerEntryRestored:
188+
restored := entryChange.MustRestored()
161189
changes = append(changes, Change{
162-
Type: state.Data.Type,
163-
Pre: &state,
164-
Post: nil,
190+
Type: restored.Data.Type,
191+
Pre: nil,
192+
Post: &restored,
193+
ChangeType: entryChange.Type,
165194
})
166195
case xdr.LedgerEntryChangeTypeLedgerEntryState:
167196
continue
@@ -221,20 +250,6 @@ func sortChanges(changes []Change) {
221250
sort.Stable(newSortableChanges(changes))
222251
}
223252

224-
// LedgerEntryChangeType returns type in terms of LedgerEntryChangeType.
225-
func (c Change) LedgerEntryChangeType() xdr.LedgerEntryChangeType {
226-
switch {
227-
case c.Pre == nil && c.Post != nil:
228-
return xdr.LedgerEntryChangeTypeLedgerEntryCreated
229-
case c.Pre != nil && c.Post == nil:
230-
return xdr.LedgerEntryChangeTypeLedgerEntryRemoved
231-
case c.Pre != nil && c.Post != nil:
232-
return xdr.LedgerEntryChangeTypeLedgerEntryUpdated
233-
default:
234-
panic("Invalid state of Change (Pre == nil && Post == nil)")
235-
}
236-
}
237-
238253
// getLiquidityPool gets the most recent state of the LiquidityPool that exists or existed.
239254
func (c Change) getLiquidityPool() (*xdr.LiquidityPoolEntry, error) {
240255
var entry *xdr.LiquidityPoolEntry

0 commit comments

Comments
 (0)