-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Remove As trait bound from Currency::Balance #2205
Conversation
|
looks pretty reasonable, but doesn't build. |
Note that |
Thanks for pointing it out. I guess I need to rework the design... But be able to support |
| pub trait Currency<AccountId> { | ||
| /// The balance of an account. | ||
| type Balance: SimpleArithmetic + As<usize> + As<u64> + Codec + Copy + MaybeSerializeDebug + Default; | ||
| type Balance: SimpleArithmetic + Codec + Copy + MaybeSerializeDebug + Default; |
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.
it seems to me that only having From<u64> and TryInto<u64> on Currency could be interesting (this would assume that a currency is overset of u64, but this is legit to me)
but SimpleArithmetic bouds As<u64> at the moment, this removal only actually removes As<usize>
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.
Also agree with the concept of TryInto<...> (or maybe even TryFrom<...>) instead of the current one. Prevents any API break and seems like a generic-enough approach.
Question though: Balance is SimpleArithmetic and SimpleArithmetic is already As<u64>. Isn't this already indirectly implying that Balance is still bounded by As<u64>?
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.
Question though: Balance is SimpleArithmetic and SimpleArithmetic is already As. Isn't this already indirectly implying that Balance is still bounded by As?
Yes, that's what I tried to say in my second paragraph
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.
Right, I went blind for a sec apparently.
@xlc Did you ever test to see if this PR, with its current state, actually solve your initial problem (storing some kind of Vec as Balance)? I assume no.
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.
No. As other pointed out, Vec is not Copyable so I need to do it differently.
I was thinking to do it with [Balance; 4] or something similar. But regardless the approach, it will likely not able to convert into a number and requires this PR.
You can still wrap it in a structure and implement Copy on it though |
But how would you implement |
aah you're right I thought |
|
I have started removing Please let me know if you guys want me to continue on this path, or I should do it differently. Otherwise I don't want to spend any more time on a PR that is not going to get merged. |
I'm not sure about the way to go, removing it from simple arithmetic but bounding it to every usage of simple arithmetic won't solve the issue, does it ? Having some kind of I'm saying From and TryInto but we may prefer having our traits Sa and TryAs to be able to implement defaults as we want. This at least make every usage of As auditable easily. |
It does make a difference. Only the In the context of the module we are working on, a multi token module (similar to assets module in substrate), I can pass the main token So maybe keep |
|
In general I would like to see |
|
Currency bounds to if the latter then it seems that there is some contradiction: because it seems I can reimplement From using Shl and One. EDIT: or with One and Add |
|
(also it seems a lot of conertion are made between u64 and BlockNumber in core) (EDIT: this is not an argument for or against) |
|
I suggest this PR (if it is not to be continued as-is) to be merged (close one and combine work in one) with #2322 (cc @thiolliere). They both seem to be experimenting toward one goal. |
|
I have reverted the attempt to completely remove The rest of the PR is still a cleanup of the code and can be merged. |
ac3f5a9 to
dc8787c
Compare
gui1117
left a comment
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.
if this PR is about some code refactor then this comments are relevant now.
|
@thiolliere updated |
gui1117
left a comment
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.
So what this PR does is just removing As<usize> from balance associated type. As<u64> is still bound by SimpleArithmetic
| let total_minted = reward * <BalanceOf<T> as As<usize>>::sa(validators.len()); | ||
| let total_rewarded_stake = Self::slot_stake() * <BalanceOf<T> as As<usize>>::sa(validators.len()); | ||
| let len = validators.len() as u64; // validators length can never overflow u64 | ||
| let len = BalanceOf::<T>::sa(len); |
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 would have written this in one line but whatever
* remove As<64> bond * Currency trait refactor * bump spec version
(I don't expect this can be merged. This is more of an issue with related code to show my point.)
I think we should release the
Asbound onBalancetype because it may cause issues (#1377). Even substrate code base have no such issue exists, it is impossible to ensure such issue won't exist on other runtimes.One module requires
Balance: As<usize>shouldn't affect the shared abstractCurrency::Balancetype otherwise it means the abstraction is not correct.In this case the Staking module can add additional trait bound and for people are not using Staking module, they don't need to support
Balance: As<usize>at all.Another reason I want to do this is because this prevents me to use some fancy type to support multi token currency with the existing
Currencytrait.I want to use
Vec<(AssetId, Balance)>as the Balance type but it is impossible to convert such type to a number.