Skip to content

Json smart contract parser#785

Merged
shargon merged 33 commits intoneo-project:masterfrom
shargon:json-api
May 30, 2019
Merged

Json smart contract parser#785
shargon merged 33 commits intoneo-project:masterfrom
shargon:json-api

Conversation

@shargon
Copy link
Copy Markdown
Member

@shargon shargon commented May 26, 2019

Related to #779

TODO: max length for items

JSON parsing libraries have been observed to differ as to whether or not they make the ordering of object members visible to calling software.

An implementation may set limits on the size of texts that it accepts. An implementation may set limits on the maximum depth of nesting. An implementation may set limits on the range and precision of numbers. An implementation may set limits on the length and character contents of strings.

@shargon shargon requested a review from erikzhang May 26, 2019 18:03
@codecov-io
Copy link
Copy Markdown

Codecov Report

Merging #785 into master will increase coverage by 0.28%.
The diff coverage is 76.47%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #785      +/-   ##
==========================================
+ Coverage   36.82%   37.11%   +0.28%     
==========================================
  Files         174      175       +1     
  Lines       12262    12296      +34     
==========================================
+ Hits         4516     4564      +48     
+ Misses       7746     7732      -14
Impacted Files Coverage Δ
neo/SmartContract/InteropService.cs 15.43% <12.5%> (-0.06%) ⬇️
neo/VM/JsonParser.cs 96.15% <96.15%> (ø)
neo/IO/Json/JObject.cs 68.5% <0%> (+2.36%) ⬆️
neo/IO/Json/JArray.cs 57.95% <0%> (+3.4%) ⬆️
neo/IO/Json/JNumber.cs 47.05% <0%> (+31.37%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 2401a11...b92ec8c. Read the comment docs.

@codecov-io
Copy link
Copy Markdown

codecov-io commented May 26, 2019

Codecov Report

Merging #785 into master will increase coverage by 0.92%.
The diff coverage is 79.79%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #785      +/-   ##
==========================================
+ Coverage   36.82%   37.75%   +0.92%     
==========================================
  Files         174      176       +2     
  Lines       12262    12439     +177     
==========================================
+ Hits         4516     4696     +180     
+ Misses       7746     7743       -3
Impacted Files Coverage Δ
neo/IO/Json/JNumber.cs 79.78% <100%> (+64.1%) ⬆️
neo/SmartContract/InteropService.NEO.cs 12.8% <13.33%> (+0.02%) ⬆️
neo/IO/Caching/OrderedDictionary.cs 47.45% <47.45%> (ø)
neo/IO/Json/JBoolean.cs 65% <65%> (-20.37%) ⬇️
neo/IO/Json/JString.cs 79.31% <90%> (+19.69%) ⬆️
neo/IO/Json/JObject.cs 83.84% <93.1%> (+17.7%) ⬆️
neo/IO/Json/JArray.cs 58.51% <95.45%> (+3.96%) ⬆️
neo/SmartContract/JsonSerializer.cs 97.82% <97.82%> (ø)
... and 1 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 2401a11...427780b. Read the comment docs.

Copy link
Copy Markdown
Member

@erikzhang erikzhang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neo.Json.Serialize - serialize a StackItem object to a JSON string.
Neo.Json.Deserialize - deserialize a JSON string to a StackItem object.

{"a": [1, "2"]}

It will be converted to a Map, whose key is a ByteArray, and value is an Array with 2 elements. The first element is an Integer, and second element is a ByteArray.

igormcoelho
igormcoelho previously approved these changes May 27, 2019
Copy link
Copy Markdown
Contributor

@igormcoelho igormcoelho left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good.

var map = (VM.Types.Map)items;

Assert.IsTrue(map.TryGetValue(new VM.Types.ByteArray(Encoding.UTF8.GetBytes("test")), out var value));
Assert.AreEqual(((VM.Types.Integer)value).GetBigInteger(), 123);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

parsing int to BigInt may be an issue... let's see.

public static readonly uint System_Runtime_GetTime = Register("System.Runtime.GetTime", Runtime_GetTime, 0_00000250);
public static readonly uint System_Runtime_Serialize = Register("System.Runtime.Serialize", Runtime_Serialize, 0_00100000);
public static readonly uint System_Runtime_Deserialize = Register("System.Runtime.Deserialize", Runtime_Deserialize, 0_00500000);
public static readonly uint System_Runtime_ParseJon = Register("System.Runtime.ParseJson", Runtime_ParseJson, 100_00000000/*TODO: Compute gast cost*/);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should abolish fixed prices on Neo 3.0. One problem less, and one problem we cannot solve without market and consensus nodes.

@igormcoelho
Copy link
Copy Markdown
Contributor

I approved what is now... but I agree Erik's interface is better:
Neo.Json.Serialize - serialize a StackItem object to a JSON string.
Neo.Json.Deserialize - deserialize a JSON string to a StackItem object.


private static bool Json_Deserialize(ApplicationEngine engine)
{
var json = Encoding.UTF8.GetString(engine.CurrentContext.EvaluationStack.Pop().GetByteArray());
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we need to handle the illegal characters consistently in smart contracts.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JObject.Parse should deal with this, what do you propose?

Copy link
Copy Markdown
Member

@erikzhang erikzhang May 27, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. We should deal with this in JObject.Parse().

@erikzhang
Copy link
Copy Markdown
Member

I am concerned that different languages handle JSON differently and may lead to inconsistencies in smart contracts. We must impose strict restrictions on JObject.Parse().

@shargon
Copy link
Copy Markdown
Member Author

shargon commented May 27, 2019

Yes, we should be able to parse True as a valid boolean too

@erikzhang
Copy link
Copy Markdown
Member

True is not a valid value in JSON, I think.

@shargon
Copy link
Copy Markdown
Member Author

shargon commented May 27, 2019

So, do you think maybe Python will not produce exactly the same output as us? We can solve this in the specification, and unit tests.

@erikzhang
Copy link
Copy Markdown
Member

Yes, we must ensure that it is handled in strict accordance with the JSON specification.

https://tools.ietf.org/html/rfc7159

@shargon
Copy link
Copy Markdown
Member Author

shargon commented May 27, 2019

I will do with our json classes

@shargon
Copy link
Copy Markdown
Member Author

shargon commented May 29, 2019

@neo-project/core do you think that there are any pending unit test? we need to ensure the deterministic behaviour

@erikzhang
Copy link
Copy Markdown
Member

erikzhang commented May 29, 2019

Yes. See the followings:

JSON parsing libraries have been observed to differ as to whether or not they make the ordering of object members visible to calling software.

For example:

PUSH {"b": 1, "a": 2}
SYSCALL Neo.Json.Deserialize
VALUES

Some implementation may return [1,2], the others may return [2,1]. We must ensure the deterministic behaviour of this.

An implementation may set limits on the size of texts that it accepts. An implementation may set limits on the maximum depth of nesting. An implementation may set limits on the range and precision of numbers. An implementation may set limits on the length and character contents of strings.

And these limits must be set.

@shargon
Copy link
Copy Markdown
Member Author

shargon commented May 29, 2019

I think other clients should avoid libraries and do their own implementation, according to this, that we should specify in the specifications.

@erikzhang
Copy link
Copy Markdown
Member

We must use OrderedDictionary for maintaining JObject's properties.

@shargon
Copy link
Copy Markdown
Member Author

shargon commented May 29, 2019

@erikzhang
Copy link
Copy Markdown
Member

erikzhang commented May 29, 2019

This is a non-generic version of OrderedDictionary. I've created a generic one.

@erikzhang
Copy link
Copy Markdown
Member

I'm afraid you didn't really use OrderedDictionary in the code. @shargon

@shargon
Copy link
Copy Markdown
Member Author

shargon commented May 29, 2019

i forget to commit this file xD

erikzhang
erikzhang previously approved these changes May 30, 2019
erikzhang
erikzhang previously approved these changes May 30, 2019
@shargon shargon merged commit e365f00 into neo-project:master May 30, 2019
@shargon shargon deleted the json-api branch May 30, 2019 10:40
Thacryba pushed a commit to simplitech/neo that referenced this pull request Feb 17, 2020
* release-2.10.0

* Update v2.10.0.md (neo-project#785)

* Update v2.10.0.md

* Update v2.10.0.md

* Update and rename v2.10.0.md to v2.10.0

* Update v2.10.0

* update exchange

* Introduction for gas related rpc (neo-project#788)

Introduction for gas related rpc

* add plugin

* Update v2.10.0.md

* update English files
Thacryba pushed a commit to simplitech/neo that referenced this pull request Feb 17, 2020
* release-2.10.0

* Update v2.10.0.md (neo-project#785)

* Update v2.10.0.md

* Update v2.10.0.md

* Update and rename v2.10.0.md to v2.10.0

* Update v2.10.0

* update exchange

* add new api

* Add new api/gettransactionheight.md

* fix

fix

* Update claimgas.md

* Update importkey.md
@ixje
Copy link
Copy Markdown
Contributor

ixje commented Jul 14, 2020

Some implementation may return [1,2], the others may return [2,1]. We must ensure the deterministic behaviour of this.

An implementation may set limits on the size of texts that it accepts. An implementation may set limits on the maximum depth of nesting. An implementation may set limits on the range and precision of numbers. An implementation may set limits on the length and character contents of strings.

And these limits must be set.

@shargon is it me or is there no code for the following restrictions?

An implementation may set limits on the size of texts that it accepts

An implementation may set limits on the length and character contents of strings.

@shargon
Copy link
Copy Markdown
Member Author

shargon commented Jul 14, 2020

An implementation may set limits on the size of texts that it accepts

The vm has this limmit. Outside the VM you can use any length.

According to the character content. We force strict utf8 now

return Parse(Utility.StrictUTF8.GetBytes(value), max_nest);

@ixje
Copy link
Copy Markdown
Contributor

ixje commented Jul 14, 2020

Then I'm good. Thanks! :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants