-
Notifications
You must be signed in to change notification settings - Fork 79
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
util: JSONify uint160 using LE instead of BE #785
Conversation
Codecov Report
@@ Coverage Diff @@
## master #785 +/- ##
==========================================
- Coverage 64.34% 64.34% -0.01%
==========================================
Files 141 141
Lines 12934 12916 -18
==========================================
- Hits 8323 8311 -12
+ Misses 4208 4203 -5
+ Partials 403 402 -1
Continue to review full report at Codecov.
|
Available: a.Available, | ||
Precision: a.Precision, | ||
FeeMode: a.FeeMode, | ||
FeeAddress: a.FeeAddress.Reverse(), // FeeAddress should be marshaled in BE but default marshaler uses LE. |
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.
Are you sure about that?
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 looks to me that it's always zero and there are no examples that could prove it being LE or BE in real life. Thus, I'd suggest using the default (which is LE) and not adding yet another wrapper.
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.
Actually, I'm not sure because official RPC API (https://docs.neo.org/docs/en-us/reference/rpc/latest-version/api/getassetstate.html) does not suppose serialization of fields FeeAddress
and FeeMode
(see also https://github.com/neo-project/neo/blob/master-2.x/neo/Ledger/AssetState.cs#L151).
In neo-go these fields are serialized, particularly, FeeAddress
was serialized in BE, so I decided to leave it as it was before.
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 fields ID
and AssetType
of AssetState
JSONify as assetID
and assetType
respectively, but in C# API it should be just id
and type
. Should I fix it?
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 looks to me that it's always zero and there are no examples that could prove it being LE or BE in real life. Thus, I'd suggest using the default (which is LE) and not adding yet another wrapper.
Fixed
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.
official RPC API does not suppose serialization of fields FeeAddress and FeeMode
We can drop them from the answer then.
Should I fix it?
Yep, we should be 100% compatible in our outputs.
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.
Done
a45a2ce
to
7305750
Compare
pkg/wallet/token.go
Outdated
@@ -38,7 +38,7 @@ func NewToken(tokenHash util.Uint160, name, symbol string, decimals int64) *Toke | |||
func (t *Token) MarshalJSON() ([]byte, error) { | |||
m := &tokenAux{ |
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.
Maybe we don't need this struct at all now?
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.
We use this struct in UnmarshalJSON too because it does not contains Address
and we have to fill this field. Unmarshalling into &tokenAux lets us avoid recursion, so perhaps we still need it?
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.
json:"-"
? And maybe this structure shouldn't have an Address
field, but rather an Address()
method (and maybe address
field as a cache, though I doubt it's worth it, this structure is never in the hot path).
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.
Done (added Address() method)
pkg/smartcontract/context/item.go
Outdated
@@ -36,7 +36,7 @@ func (it *Item) AddSignature(pub *keys.PublicKey, sig []byte) { | |||
// MarshalJSON implements json.Marshaler interface. | |||
func (it Item) MarshalJSON() ([]byte, error) { | |||
ci := itemAux{ | |||
Script: it.Script, | |||
Script: it.Script.Reverse(), // script should be marshaled in BE but default marshaler uses LE. |
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'd be nice to check this against some C# examples.
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.
They store Script
as []byte in LE, so fixed
@@ -197,7 +199,7 @@ func (p *Parameter) UnmarshalJSON(data []byte) (err error) { | |||
if err = json.Unmarshal(r.Value, &h); err != nil { | |||
return | |||
} | |||
p.Value = h | |||
p.Value = h.Reverse() // Hash160 should be marshaled in BE but default marshaler uses LE. |
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.
Not sure about this also.
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.
For UInt160
here (https://github.com/neo-project/neo/blob/master-2.x/neo/SmartContract/ContractParameter.cs#L156) they use override ToString()
method (https://github.com/neo-project/neo/blob/master-2.x/neo/UIntBase.cs#L126) which returns BE string
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.
But our internal storage is BE and theirs is LE IIRC, so reversing here gives you LE.
79f0170
to
cac366f
Compare
@@ -123,11 +123,11 @@ func (u *Uint160) UnmarshalJSON(data []byte) (err error) { | |||
return err | |||
} | |||
js = strings.TrimPrefix(js, "0x") | |||
*u, err = Uint160DecodeStringBE(js) | |||
*u, err = Uint160DecodeStringLE(js) |
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.
@roman-khimov Which one should we use here?
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.
LE, that's the whole point of #769.
FeeMode uint8 `json:"fee"` | ||
FeeAddress util.Uint160 `json:"address"` | ||
FeeMode uint8 `json:"-"` | ||
FeeAddress util.Uint160 `json:"-"` |
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.
Why not just drop them?
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.
And make a separate commit, because it's a separate change.
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.
Done
Modified result.AssetState: - removed `FeeMode` and `FeeAddress` fields - fixed json name of `ID` and `AssetType` fields to be consistent with C# RPC server
cac366f
to
2187346
Compare
There's a bug after #785: smartcontract.Parameter of type hash160 should be marshalled in LE (as default marshaller for uint160 does) instead of BE, so fixed.
There's a bug after #785: smartcontract.Parameter of type hash160 should be marshalled in LE (as default marshaller for uint160 does) instead of BE, so fixed.
closes #769