Commit graph

35 commits

Author SHA1 Message Date
Roman Khimov
b9ff07f32c stackitem: add limit to serialized items
Standard binary serialization/deserialization is mostly used in VM to put/get
elements into/from storage, so they never should exceed MaxSize (otherwise one
won't be able to deserialize these items).

This patch leaves EncodeBinaryStackItem unprotected, but that's a streaming
interface, so it's up to the user of it to ensure its appropriate use (and our
uses are mostly for native contract's data, so they're fine).
2021-07-06 19:34:02 +03:00
Roman Khimov
8472064bbc stackitem: refactor serialization code, add explicit context 2021-07-06 19:32:52 +03:00
Roman Khimov
1b7b7e4bec stackitem: don't overwrite error when serializing Item
We must get out as quickly as possible.
2021-07-06 17:51:46 +03:00
Roman Khimov
e34fa2e915 stackitem: limit JSONization nesting level
We can have very deep reference types and attempt to JSONize them can easily
lead to OOM (even though there are no recursive references inside). Therefore
we have to limit them. While regular ToJSON() is buffer size limited to
MaxSize, ToJSONWithTypes is not and limiting it to MaxSize output will require
substantial rewriting effort while not really providing fair result, MaxSize
is more about stack item size while its JSON representation can be much bigger
because of various overheads.

Initial thought was to limit it by element count based on
MaxIteratorResultItems, but the problem here is that we don't always have this
limit in contexts using ToJSONWithTypes (like notification event
marshaling). Thus we need some generic limit which would be fine for all
users.

We at the same time have maxJSONDepth used when deserializing from JSON, so
it can be used for marshaling as well, it's not often that we have deeper
structures in real results.

Inspired by neo-project/neo#2521.
2021-07-06 17:33:16 +03:00
Roman Khimov
6879cbcdcc stackitem: properly detect recursive references during serialization
If we're done with element it no longer can lead to recursion error, so fix
cases like `[arr, arr]` where we have two copies of `arr` trigger this error
for no good reason (there is no recursion there).
2021-07-06 17:10:31 +03:00
Evgeniy Stratonikov
8d67a03aec stackitem: fix Buffer deserialization
Fix incorrect application log for transaction in N3 testnet
8eb4076f1f1c07e693eda7e810779488a2d2b50aba9b727fd237cbc3adbec9e9

Signed-off-by: Evgeniy Stratonikov <evgeniy@nspcc.ru>
2021-06-29 11:39:55 +03:00
Anna Shaleva
f95bdd9cb5 vm: serialize + rune as \u002B for byte-contained stackitems
This commit fixes the following Go vs C# state diff:

block 74613: value mismatch for key EwAAAHN0cmVhbXMvDg==: eyJpZCI6MTQsInN0YXJ0IjoxNjIyNTAwMjAwMDAwLCJzdG9wIjoxNjIyNTAyMDAwMDAwLCJkZXBvc2l0IjoxMDAwMDAwMDAsInJlbWFpbmluZyI6MTAwMDAwMDAwLCJzZW5kZXIiOiJmeEY4RDl2ZFU3K0gwcDV3NTlyWllMNytNSlE9IiwicmVjaXBpZW50IjoiSVV6c3pveFV0S1NGVnlZRGczSmdTQTFlbTFNPSJ9 vs eyJpZCI6MTQsInN0YXJ0IjoxNjIyNTAwMjAwMDAwLCJzdG9wIjoxNjIyNTAyMDAwMDAwLCJkZXBvc2l0IjoxMDAwMDAwMDAsInJlbWFpbmluZyI6MTAwMDAwMDAwLCJzZW5kZXIiOiJmeEY4RDl2ZFU3XHUwMDJCSDBwNXc1OXJaWUw3XHUwMDJCTUpRPSIsInJlY2lwaWVudCI6IklVenN6b3hVdEtTRlZ5WURnM0pnU0ExZW0xTT0ifQ==

I.e.:
```
{"id":14,"start":1622500200000,"stop":1622502000000,"deposit":100000000,"remaining":100000000,"sender":"fxF8D9vdU7+H0p5w59rZYL7+MJQ=","recipient":"IUzszoxUtKSFVyYDg3JgSA1em1M="}
```
vs
```
{"id":14,"start":1622500200000,"stop":1622502000000,"deposit":100000000,"remaining":100000000,"sender":"fxF8D9vdU7\u002BH0p5w59rZYL7\u002BMJQ=","recipient":"IUzszoxUtKSFVyYDg3JgSA1em1M="}
```
2021-06-08 16:40:38 +03:00
Roman Khimov
9d2712573f *: enable godot linter and fix all its warnings
It's important for NeoGo to have clean documentation. No functional changes.
2021-05-12 23:17:03 +03:00
Evgeniy Stratonikov
e5fbf04529 stackitem: escape control characters in ToJSON()
Decoding uses `json.Decoder` which handles everything for us.
2021-03-02 10:47:02 +03:00
Evgenii Stratonikov
8c22d27acc state: allow to encode AppExecResult with recursive items
1. Encode them to a special type, decode to `nil`.
2. `Interop` can be encoded in JSON, this info should also be preserved.
2020-12-18 13:04:31 +03:00
Evgenii Stratonikov
18b331d765 stackitem: fix JSON encoding
Encode both `Buffer` and `ByteString` to UTF-8 bytes.
Follow https://github.com/neo-project/neo/pull/1715 .
2020-12-11 13:30:47 +03:00
Roman Khimov
0120a8f239 stackitem: limit buffer/bytearray reads upon deserialization
This is not the way it's done in C#, but that's the most sensible approach to
me.
2020-10-07 23:08:20 +03:00
Roman Khimov
64e9775707 vm/stackitem: limit reads for bigint values
They can't exceed 33 bytes.
2020-10-07 23:07:10 +03:00
Evgenii Stratonikov
96bca91e4b vm: handle very big int creation properly
Determine size as in reference implementation instead of
`big.Int.BitLen()`

Close #1437.
2020-10-07 11:50:42 +03:00
Anna Shaleva
543fd58e93 vm: restrict map key size 2020-09-29 18:46:15 +03:00
Anna Shaleva
1f9b92c295 vm: restrict comparable ByteArray length
MaxByteArrayComparableSize should equals to 65535.
2020-09-29 10:42:01 +03:00
Roman Khimov
324f4c265b stackitem: don't copy existing slices for TryBytes
Most often we only need to read them and it doesn't require copying. Make an
explicit copy (and copy only things we need!) where needed.

After the recent neo-vm tests update our vm package testing time jumped to
~12s, with this change it's now more like ~8s.
2020-08-22 23:36:38 +03:00
Roman Khimov
74097ae8b0 stackitem: add NewPointerWithHash() to save on hash calculations
Inspired by neo-project/neo-vm#352. We can't directly compare slices, so we're
better optimize things we already have. At the same time this code would
behave a bit different if A is to call B and then B is call A and then some
pointer from the first A invocation is to be compared with a pointer from the
second A invocation. Not sure it really matters.
2020-08-22 22:19:44 +03:00
Roman Khimov
93f51f922a stackitem: return error in TryBytes() for big byte strings
Follow neo-project/neo-vm#349.
2020-08-21 21:05:47 +03:00
Roman Khimov
a7670303e8 stackitem: change Bool() to TryBool(), prepare for its failures 2020-08-21 20:55:20 +03:00
Evgenii Stratonikov
b8cc33d0b2 stackitem: implement DeepCopy 2020-08-05 11:19:41 +03:00
Roman Khimov
fa5ff8dd95
Merge pull request #1242 from nspcc-dev/fix/json
Implement lossless stackitem to json conversion
2020-08-04 12:59:47 +03:00
Evgenii Stratonikov
9d6ce4bdcc stackitem: implement unmarshaling from JSON with types
Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
2020-07-31 16:07:16 +03:00
Evgenii Stratonikov
ee5ff40b17 stackitem: allow to convert type from string
Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
2020-07-31 16:07:16 +03:00
Evgenii Stratonikov
c16bb466a0 stackitem: implement marshaling to JSON with types
There are 2 kinds of JSON marshaling:
1. Lossy raw marshaling, when type information is lost and
    map keys are expected to be valid utf-8 strings.
2. Almost lossless marshaling, which can handle any non-recursive item.
    Interop value preserves only type.

This commit implements the second.

Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
2020-07-31 16:07:16 +03:00
Evgenii Stratonikov
b53f0257f5 stackitem: change ByteArray type to ByteString
Adjust only string representation.

Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
2020-07-31 16:07:16 +03:00
Evgenii Stratonikov
5500195d58 stackitem: ensure map keys are a valid UTF-8 2020-07-30 12:37:32 +03:00
Evgenii Stratonikov
f40aba4cd0 vm: convert items to UTF-8 strings
Add `stackitem.ToString` for seamless string conversion.
2020-07-30 12:37:31 +03:00
Evgenii Stratonikov
9252ef65bb vm: make EQUAL type strict
Do not perform type conversions when comparing elements.
2020-07-23 13:01:43 +03:00
Anna Shaleva
29f1e646ed vm: fix stackitem serialisation
We're able to serialise Buffer stackitem, but can not deserealise it
which leads to errors. Fixed.
2020-07-06 15:04:19 +03:00
Evgenii Stratonikov
a980cac0b3 stackitem: support Null item (de-)serialization
It's binary representation contains just type (AnyT).
2020-06-19 11:38:56 +03:00
Evgenii Stratonikov
d836233352 stackitem: allow to (de-)serialize items to JSON
This commit implements behavior identical to that of C# `System.Json.*` interops.
2020-06-16 11:34:01 +03:00
Evgenii Stratonikov
f2f01a08c9 vm: move MaxArraySize and MaxItemSize to stackitem package 2020-06-16 11:32:16 +03:00
Anna Shaleva
7ca2807875 vm/encoding: move bigint from vm to encoding package 2020-06-08 13:27:13 +03:00
Anna Shaleva
783f5ecb01 vm: move StackItem to a separate package
closes #912
2020-06-08 13:27:08 +03:00