Skip to content

Commit

Permalink
vm: loosen allowed key types when pushing dictionaries with `ScriptBu…
Browse files Browse the repository at this point in the history
…ilder` (#285)
  • Loading branch information
ixje authored Sep 29, 2023
1 parent 522c6dc commit 2cab733
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
7 changes: 5 additions & 2 deletions neo3/vm.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,9 +388,12 @@ def emit_push(self, value) -> ScriptBuilder:
elif isinstance(value, dict):
for k, v in value.items():
# This restriction exists on the VM side where keys to a 'Map' may only be of 'PrimitiveType'
if not isinstance(k, (int, str, bool)):
if not isinstance(
k, (int, str, bool, bytes, serialization.ISerializable)
):
raise ValueError(
f"Unsupported key type {type(k)}. Supported types by the VM are bool, int and str"
f"Unsupported key type {type(k)}. "
f"Supported types by the VM are bool, int, str, bytes or ISerializable"
)
self.emit_push(v)
self.emit_push(k)
Expand Down
8 changes: 7 additions & 1 deletion tests/test_vm.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,18 @@ def test_emit_push_dict(self):
expected = "007b0c016101c8010c016212be"
self.assertEqual(expected, sb.to_array().hex())

data = {b"\x01": 1, b"\x02": 2}
sb = vm.ScriptBuilder()
sb.emit_push(data)
expected = "110c0101120c010212be"
self.assertEqual(expected, sb.to_array().hex())

# test invalid key type
sb = vm.ScriptBuilder()
with self.assertRaises(ValueError) as context:
sb.emit_push({1.0: "abc"})
self.assertEqual(
"Unsupported key type <class 'float'>. Supported types by the VM are bool, int and str",
"Unsupported key type <class 'float'>. Supported types by the VM are bool, int, str, bytes or ISerializable",
str(context.exception),
)

Expand Down

0 comments on commit 2cab733

Please sign in to comment.