-
Notifications
You must be signed in to change notification settings - Fork 46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Per message type fee #376
Per message type fee #376
Changes from 3 commits
43b410e
ac5d8a4
c74db3d
215557c
ead18fc
28b3cab
c4ee0bf
a274bf5
49c792e
36f162f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,16 +26,20 @@ const ( | |
) | ||
|
||
// NewCoin creates a new coin object | ||
func NewCoin(whole int64, fractional int64, | ||
ticker string) Coin { | ||
|
||
func NewCoin(whole int64, fractional int64, ticker string) Coin { | ||
return Coin{ | ||
Whole: whole, | ||
Fractional: fractional, | ||
Ticker: ticker, | ||
} | ||
} | ||
|
||
// NewCoinp returns a pointer to a new coin. | ||
func NewCoinp(whole, fractional int64, ticker string) *Coin { | ||
c := NewCoin(whole, fractional, ticker) | ||
return &c | ||
} | ||
|
||
// ID returns a coin ticker name. | ||
func (c Coin) ID() string { | ||
return c.Ticker | ||
|
@@ -128,10 +132,20 @@ func mul64(a, b int64) (int64, error) { | |
// currencies, or if the combination would cause | ||
// an overflow | ||
func (c Coin) Add(o Coin) (Coin, error) { | ||
// If any of the coins represents no value and does not have a ticker | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Q: a coin without a ticker is an invalid coin. even if it is zero, IMHO. Is this code due to #369 not done yet? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That is a good point. This code is because of https://github.com/iov-one/weave/pull/376/files#diff-e1c2020236c87568b4fce1d6a33e620cR39 It is often in weave codebase that instead of a pointer a zero value is returned and later used, for example In this case I think it does make sense. My logic was
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree with the zero value, uninitialized struct. |
||
// set then it has no influence on the addition result. | ||
if c.Ticker == "" && c.IsZero() { | ||
return o, nil | ||
} | ||
if o.Ticker == "" && o.IsZero() { | ||
return c, nil | ||
} | ||
|
||
if !c.SameType(o) { | ||
err := ErrInvalidCurrency.Newf("adding %s to %s", c.Ticker, o.Ticker) | ||
return Coin{}, err | ||
} | ||
|
||
c.Whole += o.Whole | ||
c.Fractional += o.Fractional | ||
return c.normalize() | ||
|
@@ -225,6 +239,9 @@ func (c Coin) SameType(o Coin) bool { | |
|
||
// Clone provides an independent copy of a coin pointer | ||
func (c *Coin) Clone() *Coin { | ||
if c == nil { | ||
husio marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return nil | ||
} | ||
return &Coin{ | ||
Ticker: c.Ticker, | ||
Whole: c.Whole, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pp: I don't see big value in adding another constructor with pointer result for the tests. I would suggest you make
coin
a non null type in the protobuf via annotations so that you don't have to deal with this case.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are multiple places where
coinp
is declared. This is the fourth time I was declaring it so I thought it deserves its own function.Using non null coin reference is problematic because I want to compare with a coin pointer. This is the case in many places where a result is a coin pointer and that is what you want to check becuase a result can be both a zero value coin instance and
nil
.Does this make sense?
I have pushed a change where I replace pointer to a coin with embedded value 215557c
I still think that
coin.NewCoinp
is useful in other cases and could be used instead of ad-hoc creation ofcoinp
function. What do you think?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have seen (and used) this work-around in lots of test code. Fair enough to bring it here as a helper.