Skip to content
This repository was archived by the owner on Mar 28, 2023. It is now read-only.

Commit 2ea6a3b

Browse files
authored
Merge pull request #1974 from OpenBazaar/1904-support-legacy-profiles
Support legacy profiles
2 parents 60c0d85 + 158b823 commit 2ea6a3b

File tree

11 files changed

+937
-496
lines changed

11 files changed

+937
-496
lines changed

core/profile.go

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,12 @@ func (n *OpenBazaarNode) FetchProfile(peerID string, useCache bool) (pb.Profile,
5050
if err != nil || len(b) == 0 {
5151
return pro, err
5252
}
53-
err = jsonpb.UnmarshalString(string(b), &pro)
53+
p, err := repo.UnmarshalJSONProfile(b)
5454
if err != nil {
5555
return pro, err
5656
}
57-
return pro, nil
57+
p.NormalizeDataForAllSchemas()
58+
return *p.GetProtobuf(), nil
5859
}
5960

6061
// UpdateProfile - update user profile
@@ -187,24 +188,18 @@ func (n *OpenBazaarNode) PatchProfile(patch map[string]interface{}) error {
187188
return err
188189
}
189190

190-
repoProfile, err := repo.ProfileFromProtobuf(p)
191+
repoProfile, err := repo.UnmarshalJSONProfile(newProfile)
191192
if err != nil {
192193
return fmt.Errorf("building profile for validation: %s", err.Error())
193194
}
194195

195-
if repoProfile.IsModerationEnabled() {
196-
validatedFees, err := repoProfile.ToValidModeratorFee()
197-
if err != nil {
198-
return err
199-
}
200-
p.ModeratorInfo.Fee = validatedFees
201-
}
196+
repoProfile.NormalizeDataForAllSchemas()
202197

203198
if err := repoProfile.Valid(); err != nil {
204199
return fmt.Errorf("invalid profile: %s", err.Error())
205200
}
206201

207-
return n.UpdateProfile(p)
202+
return n.UpdateProfile(repoProfile.GetProtobuf())
208203
}
209204

210205
func (n *OpenBazaarNode) appendCountsToProfile(profile *pb.Profile) (*pb.Profile, bool) {

repo/currency.go

Lines changed: 63 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -115,103 +115,103 @@ func NewCurrencyValue(amount string, currency CurrencyDefinition) (*CurrencyValu
115115
}
116116

117117
// AmountInt64 returns a valid int64 or an error
118-
func (v *CurrencyValue) AmountInt64() (int64, error) {
119-
if !v.Amount.IsInt64() {
118+
func (c *CurrencyValue) AmountInt64() (int64, error) {
119+
if !c.Amount.IsInt64() {
120120
return 0, ErrCurrencyValueInsufficientPrecision
121121
}
122-
return v.Amount.Int64(), nil
122+
return c.Amount.Int64(), nil
123123
}
124124

125125
// AmountUint64 returns a valid int64 or an error
126-
func (v *CurrencyValue) AmountUint64() (uint64, error) {
127-
if !v.Amount.IsUint64() {
126+
func (c *CurrencyValue) AmountUint64() (uint64, error) {
127+
if !c.Amount.IsUint64() {
128128
return 0, ErrCurrencyValueInsufficientPrecision
129129
}
130-
return v.Amount.Uint64(), nil
130+
return c.Amount.Uint64(), nil
131131
}
132132

133133
// AmountString returns the string representation of the amount
134-
func (v *CurrencyValue) AmountString() string {
135-
if v == nil || v.Amount == nil {
134+
func (c *CurrencyValue) AmountString() string {
135+
if c == nil || c.Amount == nil {
136136
return "0"
137137
}
138-
return v.Amount.String()
138+
return c.Amount.String()
139139
}
140140

141141
// String returns a string representation of a CurrencyValue
142-
func (v *CurrencyValue) String() string {
143-
if v == nil {
142+
func (c *CurrencyValue) String() string {
143+
if c == nil {
144144
return new(CurrencyValue).String()
145145
}
146-
return fmt.Sprintf("%s %s", v.Amount.String(), v.Currency.String())
146+
return fmt.Sprintf("%s %s", c.Amount.String(), c.Currency.String())
147147
}
148148

149149
// Valid returns an error if the CurrencyValue is invalid
150-
func (v *CurrencyValue) Valid() error {
151-
if v.Amount == nil {
150+
func (c *CurrencyValue) Valid() error {
151+
if c.Amount == nil {
152152
return ErrCurrencyValueAmountInvalid
153153
}
154-
if err := v.Currency.Valid(); err != nil {
154+
if err := c.Currency.Valid(); err != nil {
155155
return err
156156
}
157157
return nil
158158
}
159159

160160
// Equal indicates if the amount and variety of currency is equivalent
161-
func (v *CurrencyValue) Equal(other *CurrencyValue) bool {
162-
if v == nil && other == nil {
161+
func (c *CurrencyValue) Equal(other *CurrencyValue) bool {
162+
if c == nil && other == nil {
163163
return true
164164
}
165-
if v == nil || other == nil {
165+
if c == nil || other == nil {
166166
return false
167167
}
168-
if !v.Currency.Equal(other.Currency) {
169-
if v.Currency.Code == other.Currency.Code {
170-
vN, err := v.Normalize()
168+
if !c.Currency.Equal(other.Currency) {
169+
if c.Currency.Code == other.Currency.Code {
170+
cN, err := c.Normalize()
171171
if err != nil {
172172
return false
173173
}
174174
oN, err := other.Normalize()
175175
if err != nil {
176176
return false
177177
}
178-
return vN.Amount.Cmp(oN.Amount) == 0
178+
return cN.Amount.Cmp(oN.Amount) == 0
179179
}
180180
return false
181181
}
182-
return v.Amount.Cmp(other.Amount) == 0
182+
return c.Amount.Cmp(other.Amount) == 0
183183
}
184184

185185
// Normalize updates the CurrencyValue to match the divisibility of the locally defined CurrencyDefinition
186-
func (v *CurrencyValue) Normalize() (*CurrencyValue, error) {
187-
localDef, err := AllCurrencies().Lookup(string(v.Currency.Code))
186+
func (c *CurrencyValue) Normalize() (*CurrencyValue, error) {
187+
localDef, err := AllCurrencies().Lookup(string(c.Currency.Code))
188188
if err != nil {
189189
return nil, err
190190
}
191-
val, _, err := v.AdjustDivisibility(localDef.Divisibility)
191+
val, _, err := c.AdjustDivisibility(localDef.Divisibility)
192192
return val, err
193193
}
194194

195195
// AdjustDivisibility updates the Currency.Divisibility and adjusts the Amount to match the new
196196
// value. An error will be returned if the new divisibility is invalid or produces an unreliable
197197
// result. This is a helper function which is equivalent to ConvertTo using a copy of the
198198
// CurrencyDefinition using the updated divisibility and an exchangeRatio of 1.0
199-
func (v *CurrencyValue) AdjustDivisibility(div uint) (*CurrencyValue, big.Accuracy, error) {
200-
if v.Currency.Divisibility == div {
201-
return v, 0, nil
199+
func (c *CurrencyValue) AdjustDivisibility(div uint) (*CurrencyValue, big.Accuracy, error) {
200+
if c.Currency.Divisibility == div {
201+
return c, 0, nil
202202
}
203-
defWithNewDivisibility := v.Currency
203+
defWithNewDivisibility := c.Currency
204204
defWithNewDivisibility.Divisibility = div
205-
return v.ConvertTo(defWithNewDivisibility, 1.0)
205+
return c.ConvertTo(defWithNewDivisibility, 1.0)
206206
}
207207

208208
// ConvertTo will perform the following math given its arguments are valid:
209-
// v.Amount * exchangeRatio * (final.Currency.Divisibility/v.Currency.Divisibility)
210-
// where v is the receiver, exchangeRatio is the ratio of (1 final.Currency/v.Currency)
211-
// v and final must both be Valid() and exchangeRatio must not be zero. The accuracy
209+
// c.Amount * exchangeRatio * (final.Currency.Divisibility/c.Currency.Divisibility)
210+
// where c is the receiver, exchangeRatio is the ratio of (1 final.Currency/c.Currency)
211+
// c and final must both be Valid() and exchangeRatio must not be zero. The accuracy
212212
// indicates if decimal values were trimmed when converting the value back to integer.
213-
func (v *CurrencyValue) ConvertTo(final CurrencyDefinition, exchangeRatio float64) (*CurrencyValue, big.Accuracy, error) {
214-
if err := v.Valid(); err != nil {
213+
func (c *CurrencyValue) ConvertTo(final CurrencyDefinition, exchangeRatio float64) (*CurrencyValue, big.Accuracy, error) {
214+
if err := c.Valid(); err != nil {
215215
return nil, 0, fmt.Errorf("cannot convert invalid value: %s", err.Error())
216216
}
217217
if err := final.Valid(); err != nil {
@@ -221,15 +221,15 @@ func (v *CurrencyValue) ConvertTo(final CurrencyDefinition, exchangeRatio float6
221221
return nil, 0, ErrCurrencyValueNegativeRate
222222
}
223223

224-
amt := new(big.Float).SetInt(v.Amount)
224+
amt := new(big.Float).SetInt(c.Amount)
225225
exRatio := new(big.Float).SetFloat64(exchangeRatio)
226226
if exRatio == nil {
227227
return nil, 0, fmt.Errorf("exchange ratio (%f) is invalid", exchangeRatio)
228228
}
229229
newAmount := new(big.Float).SetPrec(53).Mul(amt, exRatio)
230230

231-
if v.Currency.Divisibility != final.Divisibility {
232-
initMagnitude := math.Pow10(int(v.Currency.Divisibility))
231+
if c.Currency.Divisibility != final.Divisibility {
232+
initMagnitude := math.Pow10(int(c.Currency.Divisibility))
233233
finalMagnitude := math.Pow10(int(final.Divisibility))
234234
divisibilityRatio := new(big.Float).SetFloat64(finalMagnitude / initMagnitude)
235235
newAmount.Mul(newAmount, divisibilityRatio)
@@ -249,23 +249,39 @@ func (v *CurrencyValue) ConvertTo(final CurrencyDefinition, exchangeRatio float6
249249

250250
// Cmp exposes the (*big.Int).Cmp behavior after verifying currency and adjusting
251251
// for different currency divisibilities.
252-
func (v *CurrencyValue) Cmp(other *CurrencyValue) (int, error) {
253-
if v.Currency.Code.String() != other.Currency.Code.String() {
252+
func (c *CurrencyValue) Cmp(other *CurrencyValue) (int, error) {
253+
if c.Currency.Code.String() != other.Currency.Code.String() {
254254
return 0, ErrCurrencyValueInvalidCmpDifferentCurrencies
255255
}
256-
if v.Currency.Equal(other.Currency) {
257-
return v.Amount.Cmp(other.Amount), nil
256+
if c.Currency.Equal(other.Currency) {
257+
return c.Amount.Cmp(other.Amount), nil
258258
}
259-
if v.Currency.Divisibility > other.Currency.Divisibility {
260-
adjOther, _, err := other.AdjustDivisibility(v.Currency.Divisibility)
259+
if c.Currency.Divisibility > other.Currency.Divisibility {
260+
adjOther, _, err := other.AdjustDivisibility(c.Currency.Divisibility)
261261
if err != nil {
262262
return 0, fmt.Errorf("adjusting other divisibility: %s", err.Error())
263263
}
264-
return v.Amount.Cmp(adjOther.Amount), nil
264+
return c.Amount.Cmp(adjOther.Amount), nil
265265
}
266-
selfAdj, _, err := v.AdjustDivisibility(other.Currency.Divisibility)
266+
selfAdj, _, err := c.AdjustDivisibility(other.Currency.Divisibility)
267267
if err != nil {
268268
return 0, fmt.Errorf("adjusting self divisibility: %s", err.Error())
269269
}
270270
return selfAdj.Amount.Cmp(other.Amount), nil
271271
}
272+
273+
// IsZero returns true if Amount is valid and equal to zero
274+
func (c *CurrencyValue) IsZero() bool {
275+
if c.Amount == nil {
276+
return false
277+
}
278+
return c.Amount.Cmp(big.NewInt(0)) == 0
279+
}
280+
281+
// IsNegative returns true if Amount is valid and less-than zero
282+
func (c *CurrencyValue) IsNegative() bool {
283+
if c.Amount == nil {
284+
return false
285+
}
286+
return c.Amount.Cmp(big.NewInt(0)) == -1
287+
}

repo/currency_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,3 +531,45 @@ func TestCurrencyValueCmp(t *testing.T) {
531531
}
532532
}
533533
}
534+
535+
func TestCurrencyValueIsZero(t *testing.T) {
536+
subject := factory.MustNewCurrencyValue("0", "BTC")
537+
538+
subject.Amount = nil
539+
if subject.IsZero() {
540+
t.Errorf("expected IsZero to be false for (%s), but was not", subject)
541+
}
542+
subject.Amount = big.NewInt(0)
543+
if !subject.IsZero() {
544+
t.Errorf("expected IsZero to be true for (%s), but was not", subject)
545+
}
546+
subject.Amount = big.NewInt(-1)
547+
if subject.IsZero() {
548+
t.Errorf("expected IsZero to be false for (%s), but was not", subject)
549+
}
550+
subject.Amount = big.NewInt(1)
551+
if subject.IsZero() {
552+
t.Errorf("expected IsZero to be false for (%s), but was not", subject)
553+
}
554+
}
555+
556+
func TestCurrencyValueIsNegative(t *testing.T) {
557+
subject := factory.MustNewCurrencyValue("0", "BTC")
558+
559+
subject.Amount = nil
560+
if subject.IsNegative() {
561+
t.Errorf("expected IsNegative to be false for (%s), but was not", subject)
562+
}
563+
subject.Amount = big.NewInt(0)
564+
if subject.IsNegative() {
565+
t.Errorf("expected IsNegative to be false for (%s), but was not", subject)
566+
}
567+
subject.Amount = big.NewInt(-1)
568+
if !subject.IsNegative() {
569+
t.Errorf("expected IsNegative to be true for (%s), but was not", subject)
570+
}
571+
subject.Amount = big.NewInt(1)
572+
if subject.IsNegative() {
573+
t.Errorf("expected IsNegative to be false for (%s), but was not", subject)
574+
}
575+
}

0 commit comments

Comments
 (0)