Commit graph

32 commits

Author SHA1 Message Date
Anna Shaleva
25ed5fcd60 vm: allow custom limit duing Map\Array\Struct deserialization 2023-02-08 10:58:55 +03:00
Anna Shaleva
15fa65d30f vm: export stackitem's deserContext 2023-02-08 10:58:54 +03:00
Roman Khimov
9ba18b5dfa stackitem: serialize/deserialize pointers, fix #2815
They of course can't be serialized, but in protected mode we still need to
handle them somehow.
2022-11-20 16:02:50 +03:00
Roman Khimov
c3d989ebda stackitem: reusable serialization context
We serialize items a lot and this allows to avoid a number of allocations.
2022-06-02 15:38:39 +03:00
Elizaveta Chichindaeva
28908aa3cf [#2442] English Check
Signed-off-by: Elizaveta Chichindaeva <elizaveta@nspcc.ru>
2022-05-04 19:48:27 +03:00
Roman Khimov
decff2bfb9 stackitem: simplify extracting value from map
Going through interfaces doesn't make it faster.
2021-12-01 21:36:35 +03:00
Roman Khimov
af8a4da6d9 stackitem: use type switch more effectively
Reduce the number of allocations, save some time.

name               old time/op    new time/op    delta
EncodeBinary-8       51.2µs ± 8%    44.6µs ± 7%  -12.79%  (p=0.008 n=5+5)
SerializeSimple-8     523ns ± 5%     449ns ± 4%  -14.27%  (p=0.008 n=5+5)

name               old alloc/op   new alloc/op   delta
EncodeBinary-8        329kB ± 0%     329kB ± 0%   -0.12%  (p=0.008 n=5+5)
SerializeSimple-8      432B ± 0%      408B ± 0%   -5.56%  (p=0.008 n=5+5)

name               old allocs/op  new allocs/op  delta
EncodeBinary-8         36.0 ± 0%      20.0 ± 0%  -44.44%  (p=0.008 n=5+5)
SerializeSimple-8      7.00 ± 0%      6.00 ± 0%  -14.29%  (p=0.008 n=5+5)
2021-12-01 21:36:35 +03:00
Roman Khimov
3eed9d06f8 stackitem: add some hint to 'seen' maps
It's empirical, we usually have one container, but four is likely to fit most
of regular cases.
2021-12-01 21:36:25 +03:00
Roman Khimov
d3198c3082 stackitem: avoid going through Value() in serialization
Doesn't change much, but still simpler.

name               old time/op    new time/op    delta
SerializeSimple-8     452ns ±10%     435ns ± 4%   ~     (p=0.356 n=10+9)

name               old alloc/op   new alloc/op   delta
SerializeSimple-8      432B ± 0%      432B ± 0%   ~     (all equal)

name               old allocs/op  new allocs/op  delta
SerializeSimple-8      7.00 ± 0%      7.00 ± 0%   ~     (all equal)
2021-08-23 18:29:07 +03:00
Evgeniy Stratonikov
cff8b1c24e stackitem: use Bool item directly
It is always copied.

Signed-off-by: Evgeniy Stratonikov <evgeniy@nspcc.ru>
2021-08-12 17:53:36 +03:00
Evgeniy Stratonikov
f02d8b4ec4 stackitem: serialize integers to the pre-allocated slice
Signed-off-by: Evgeniy Stratonikov <evgeniy@nspcc.ru>
2021-08-06 11:59:24 +03:00
Roman Khimov
233307aca5 stackitem: completely drop MaxArraySize
Turns out C# VM doesn't have it since preview2, so our limiting of
MaxArraySize in incompatible with it. Removing this limit shouldn't be a
problem with the reference counter we have, both APPEND and SETITEM add things
to reference counter and we can't exceed MaxStackSize. PACK on the other hand
can't get more than MaxStackSize-1 of input elements.

Unify NEWSTRUCT with NEWARRAY* and use better integer checks at the same time.

Multisig limit is still 1024.
2021-07-19 15:42:42 +03:00
Roman Khimov
df2430d5e4 stackitem: limit deserialization to MaxDeserialized items
Follow neo-project/neo#2531. Even though it's not strictly required (our node
handles problematic script just fine) we better be compliant wrt
deserialization behavior. MaxDeserialized is introduced to avoid moving
MaxStackSize which is a VM parameter.
2021-07-19 15:42:42 +03:00
Roman Khimov
aab18c3083 stackitem: introduce Convertible interface
We have a lot of native contract types that are converted to stack items
before serialization, then deserialized as stack items and converted back to
regular structures. stackitem.Convertible allows to remove a lot of repetitive
io.Serializable code.

This also introduces to/from converter in testserdes which unfortunately
required to change util tests to avoid circular references.
2021-07-19 15:42:42 +03:00
Roman Khimov
fbe8bd2d9c stackitem: limit deserialized arrays/maps
See neo-project/neo#2531, even though they use MaxStackSize there resulting
element is not valid unless it has <=MaxArraySize elements.
2021-07-19 15:42:42 +03:00
Roman Khimov
654c4a6589 stackitem: properly pass allowInvalid on binary deserialization
Otherwise DecodeBinaryProtected() is broken for arrays/maps.
2021-07-19 15:42:41 +03:00
Evgeniy Stratonikov
8fdc7e32f5 stackitem: cache serialized results in EncodeBinary
```
name            old time/op    new time/op    delta
EncodeBinary-8    8.39ms ± 1%    0.05ms ±21%  -99.44%  (p=0.000 n=10+9)

name            old alloc/op   new alloc/op   delta
EncodeBinary-8    2.24MB ± 0%    0.33MB ± 0%  -85.29%  (p=0.000 n=9+10)

name            old allocs/op  new allocs/op  delta
EncodeBinary-8     65.6k ± 0%      0.0k ± 0%  -99.95%  (p=0.000 n=10+10)
```

Signed-off-by: Evgeniy Stratonikov <evgeniy@nspcc.ru>
2021-07-13 11:06:19 +03:00
Evgeniy Stratonikov
17a3f17c74 stackitem: use byte-slice directly during encoding
```
name            old time/op    new time/op    delta
EncodeBinary-8    10.6ms ± 1%     8.4ms ± 1%  -20.94%  (p=0.000 n=9+10)

name            old alloc/op   new alloc/op   delta
EncodeBinary-8    1.64MB ± 0%    2.24MB ± 0%  +36.18%  (p=0.000 n=8+9)

name            old allocs/op  new allocs/op  delta
EncodeBinary-8      131k ± 0%       66k ± 0%  -49.98%  (p=0.000 n=10+10)
```

Signed-off-by: Evgeniy Stratonikov <evgeniy@nspcc.ru>
2021-07-13 11:06:18 +03:00
Roman Khimov
5a9efcc654 stackitem: rework error handling
Return good named errors everywhere, export appropriate constants, make
errors.Is() work.
2021-07-07 00:18:00 +03:00
Roman Khimov
0de949b575 stackitem: remove Item/StackItem from function names
They're useless in a package named 'stackitem', make this package a bit more
user-friendly.
2021-07-06 19:56:23 +03:00
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
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
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
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
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
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