@@ -42,6 +42,9 @@ type Change struct {
42
42
// The type of the ledger entry being changed.
43
43
Type xdr.LedgerEntryType
44
44
45
+ // The specific type of change, such as Created, Updated, Removed or Restored.
46
+ ChangeType xdr.LedgerEntryChangeType
47
+
45
48
// The state of the LedgerEntry before the change. This will be nil if the entry was created.
46
49
Pre * xdr.LedgerEntry
47
50
@@ -134,6 +137,7 @@ func (c Change) LedgerKey() (xdr.LedgerKey, error) {
134
137
// - for create, pre is null and post is a new entry,
135
138
// - for update, pre is previous state and post is the current state,
136
139
// - for removed, pre is previous state and post is null.
140
+ // - for restored, pre is null and post is a new/restored entry
137
141
//
138
142
// stellar-core source:
139
143
// https://github.com/stellar/stellar-core/blob/e584b43/src/ledger/LedgerTxn.cpp#L582
@@ -144,24 +148,49 @@ func GetChangesFromLedgerEntryChanges(ledgerEntryChanges xdr.LedgerEntryChanges)
144
148
case xdr .LedgerEntryChangeTypeLedgerEntryCreated :
145
149
created := entryChange .MustCreated ()
146
150
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 ,
150
155
})
151
156
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
153
161
updated := entryChange .MustUpdated ()
162
+ state , ok := ledgerEntryChanges [i - 1 ].GetState ()
163
+ if ! ok {
164
+ state = ledgerEntryChanges [i - 1 ].MustRestored ()
165
+ }
154
166
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 ,
158
171
})
159
172
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 ()
161
189
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 ,
165
194
})
166
195
case xdr .LedgerEntryChangeTypeLedgerEntryState :
167
196
continue
@@ -221,20 +250,6 @@ func sortChanges(changes []Change) {
221
250
sort .Stable (newSortableChanges (changes ))
222
251
}
223
252
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
-
238
253
// getLiquidityPool gets the most recent state of the LiquidityPool that exists or existed.
239
254
func (c Change ) getLiquidityPool () (* xdr.LiquidityPoolEntry , error ) {
240
255
var entry * xdr.LiquidityPoolEntry
0 commit comments