forked from stellar/go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
change.go
246 lines (211 loc) · 7.01 KB
/
change.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package ingest
import (
"bytes"
"github.com/pownieh/stellar_go/support/errors"
"github.com/pownieh/stellar_go/xdr"
)
// Change is a developer friendly representation of LedgerEntryChanges.
// It also provides some helper functions to quickly check if a given
// change has occurred in an entry.
//
// If an entry is created: Pre is nil and Post is not nil.
// If an entry is updated: Pre is not nil and Post is not nil.
// If an entry is removed: Pre is not nil and Post is nil.
type Change struct {
Type xdr.LedgerEntryType
Pre *xdr.LedgerEntry
Post *xdr.LedgerEntry
}
// GetChangesFromLedgerEntryChanges transforms LedgerEntryChanges to []Change.
// Each `update` and `removed` is preceded with `state` and `create` changes
// are alone, without `state`. The transformation we're doing is to move each
// change (state/update, state/removed or create) to an array of pre/post pairs.
// Then:
// - for create, pre is null and post is a new entry,
// - for update, pre is previous state and post is the current state,
// - for removed, pre is previous state and post is null.
//
// stellar-core source:
// https://github.com/stellar/stellar-core/blob/e584b43/src/ledger/LedgerTxn.cpp#L582
func GetChangesFromLedgerEntryChanges(ledgerEntryChanges xdr.LedgerEntryChanges) []Change {
changes := make([]Change, 0, len(ledgerEntryChanges))
for i, entryChange := range ledgerEntryChanges {
switch entryChange.Type {
case xdr.LedgerEntryChangeTypeLedgerEntryCreated:
created := entryChange.MustCreated()
changes = append(changes, Change{
Type: created.Data.Type,
Pre: nil,
Post: &created,
})
case xdr.LedgerEntryChangeTypeLedgerEntryUpdated:
state := ledgerEntryChanges[i-1].MustState()
updated := entryChange.MustUpdated()
changes = append(changes, Change{
Type: state.Data.Type,
Pre: &state,
Post: &updated,
})
case xdr.LedgerEntryChangeTypeLedgerEntryRemoved:
state := ledgerEntryChanges[i-1].MustState()
changes = append(changes, Change{
Type: state.Data.Type,
Pre: &state,
Post: nil,
})
case xdr.LedgerEntryChangeTypeLedgerEntryState:
continue
default:
panic("Invalid LedgerEntryChangeType")
}
}
return changes
}
// LedgerEntryChangeType returns type in terms of LedgerEntryChangeType.
func (c *Change) LedgerEntryChangeType() xdr.LedgerEntryChangeType {
switch {
case c.Pre == nil && c.Post != nil:
return xdr.LedgerEntryChangeTypeLedgerEntryCreated
case c.Pre != nil && c.Post == nil:
return xdr.LedgerEntryChangeTypeLedgerEntryRemoved
case c.Pre != nil && c.Post != nil:
return xdr.LedgerEntryChangeTypeLedgerEntryUpdated
default:
panic("Invalid state of Change (Pre == nil && Post == nil)")
}
}
// getLiquidityPool gets the most recent state of the LiquidityPool that exists or existed.
func (c *Change) getLiquidityPool() (*xdr.LiquidityPoolEntry, error) {
var entry *xdr.LiquidityPoolEntry
if c.Pre != nil {
entry = c.Pre.Data.LiquidityPool
}
if c.Post != nil {
entry = c.Post.Data.LiquidityPool
}
if entry == nil {
return &xdr.LiquidityPoolEntry{}, errors.New("this change does not include a liquidity pool")
}
return entry, nil
}
// GetLiquidityPoolType returns the liquidity pool type.
func (c *Change) GetLiquidityPoolType() (xdr.LiquidityPoolType, error) {
lp, err := c.getLiquidityPool()
if err != nil {
return xdr.LiquidityPoolType(0), err
}
return lp.Body.Type, nil
}
// AccountChangedExceptSigners returns true if account has changed WITHOUT
// checking the signers (except master key weight!). In other words, if the only
// change is connected to signers, this function will return false.
func (c *Change) AccountChangedExceptSigners() (bool, error) {
if c.Type != xdr.LedgerEntryTypeAccount {
panic("This should not be called on changes other than Account changes")
}
// New account
if c.Pre == nil {
return true, nil
}
// Account merged
// c.Pre != nil at this point.
if c.Post == nil {
return true, nil
}
// c.Pre != nil && c.Post != nil at this point.
if c.Pre.LastModifiedLedgerSeq != c.Post.LastModifiedLedgerSeq {
return true, nil
}
// Don't use short assignment statement (:=) to ensure variables below
// are not pointers (if `xdr` package changes in the future)!
var preAccountEntry, postAccountEntry xdr.AccountEntry
preAccountEntry = c.Pre.Data.MustAccount()
postAccountEntry = c.Post.Data.MustAccount()
// preAccountEntry and postAccountEntry are copies so it's fine to
// modify them here, EXCEPT pointers inside them!
if preAccountEntry.Ext.V == 0 {
preAccountEntry.Ext.V = 1
preAccountEntry.Ext.V1 = &xdr.AccountEntryExtensionV1{
Liabilities: xdr.Liabilities{
Buying: 0,
Selling: 0,
},
}
}
preAccountEntry.Signers = nil
if postAccountEntry.Ext.V == 0 {
postAccountEntry.Ext.V = 1
postAccountEntry.Ext.V1 = &xdr.AccountEntryExtensionV1{
Liabilities: xdr.Liabilities{
Buying: 0,
Selling: 0,
},
}
}
postAccountEntry.Signers = nil
preBinary, err := preAccountEntry.MarshalBinary()
if err != nil {
return false, errors.Wrap(err, "Error running preAccountEntry.MarshalBinary")
}
postBinary, err := postAccountEntry.MarshalBinary()
if err != nil {
return false, errors.Wrap(err, "Error running postAccountEntry.MarshalBinary")
}
return !bytes.Equal(preBinary, postBinary), nil
}
// AccountSignersChanged returns true if account signers have changed.
// Notice: this will return true on master key changes too!
func (c *Change) AccountSignersChanged() bool {
if c.Type != xdr.LedgerEntryTypeAccount {
panic("This should not be called on changes other than Account changes")
}
// New account so new master key (which is also a signer)
if c.Pre == nil {
return true
}
// Account merged. Account being merge can still have signers.
// c.Pre != nil at this point.
if c.Post == nil {
return true
}
// c.Pre != nil && c.Post != nil at this point.
preAccountEntry := c.Pre.Data.MustAccount()
postAccountEntry := c.Post.Data.MustAccount()
preSigners := preAccountEntry.SignerSummary()
postSigners := postAccountEntry.SignerSummary()
if len(preSigners) != len(postSigners) {
return true
}
for postSigner, postWeight := range postSigners {
preWeight, exist := preSigners[postSigner]
if !exist {
return true
}
if preWeight != postWeight {
return true
}
}
preSignerSponsors := preAccountEntry.SignerSponsoringIDs()
postSignerSponsors := postAccountEntry.SignerSponsoringIDs()
if len(preSignerSponsors) != len(postSignerSponsors) {
return true
}
for i := 0; i < len(preSignerSponsors); i++ {
preSponsor := preSignerSponsors[i]
postSponsor := postSignerSponsors[i]
if preSponsor == nil && postSponsor != nil {
return true
} else if preSponsor != nil && postSponsor == nil {
return true
} else if preSponsor != nil && postSponsor != nil {
preSponsorAccountID := xdr.AccountId(*preSponsor)
preSponsorAddress := preSponsorAccountID.Address()
postSponsorAccountID := xdr.AccountId(*postSponsor)
postSponsorAddress := postSponsorAccountID.Address()
if preSponsorAddress != postSponsorAddress {
return true
}
}
}
return false
}