def setUp(self): self.engine = ExecutionEngine() self.econtext = ExecutionContext() self.state_reader = StateReader()
class BlockchainInteropTest(BlockchainFixtureTestCase): engine = None econtext = None state_reader = None @classmethod def leveldb_testpath(self): return os.path.join(settings.DATA_DIR_PATH, 'fixtures/test_chain') @classmethod def setUpClass(cls): super(BlockchainInteropTest, cls).setUpClass() settings.set_loglevel(DEBUG) def setUp(self): self.engine = ExecutionEngine() self.econtext = ExecutionContext(engine=self.engine) self.state_reader = StateReader() def test_interop_getblock(self): height = StackItem.New(9369) self.econtext.EvaluationStack.PushT(height) self.engine.InvocationStack.PushT(self.econtext) self.state_reader.Blockchain_GetBlock(self.engine) block = self.econtext.EvaluationStack.Pop().GetInterface() self.assertIsInstance(block, Block) def test_interop_get_transaction(self): u256 = UInt256.ParseString( '8be9660512991d36e016b8ced6fda5d611d26a0f6e2faaaf1f379496edb3395f') hash = StackItem.New(u256.Data) self.econtext.EvaluationStack.PushT(hash) self.engine.InvocationStack.PushT(self.econtext) self.state_reader.Blockchain_GetTransaction(self.engine) tx = self.econtext.EvaluationStack.Pop().GetInterface() self.assertIsInstance(tx, Transaction) def test_interop_get_bad_transaction(self): u256 = UInt256.ParseString( '8be9660512991d36e016b8ced6fda5d611d26a0f6e2faaaf1f379496edb33956') hash = StackItem.New(u256.Data) self.econtext.EvaluationStack.PushT(hash) self.engine.InvocationStack.PushT(self.econtext) self.state_reader.Blockchain_GetTransaction(self.engine) tx = self.econtext.EvaluationStack.Pop().GetInterface() self.assertIsNone(tx) def test_interop_get_transaction_height(self): u256 = UInt256.ParseString( '8be9660512991d36e016b8ced6fda5d611d26a0f6e2faaaf1f379496edb3395f') hash = StackItem.New(u256.Data) self.econtext.EvaluationStack.PushT(hash) self.engine.InvocationStack.PushT(self.econtext) self.state_reader.Blockchain_GetTransactionHeight(self.engine) height = self.econtext.EvaluationStack.Pop().GetBigInteger() self.assertEqual(height, 9369) def test_interop_get_bad_transaction_height(self): u256 = UInt256.ParseString( '8be9660512991d36e016b8ced6fda5d611d26a0f6e2faaaf1f379496edb33956') hash = StackItem.New(u256.Data) self.econtext.EvaluationStack.PushT(hash) self.engine.InvocationStack.PushT(self.econtext) self.state_reader.Blockchain_GetTransactionHeight(self.engine) height = self.econtext.EvaluationStack.Pop().GetBigInteger() self.assertEqual(height, -1)
class BlockchainInteropTest(BlockchainFixtureTestCase): engine = None econtext = None state_reader = None @classmethod def leveldb_testpath(self): return os.path.join(settings.DATA_DIR_PATH, 'fixtures/test_chain') @classmethod def setUpClass(cls): super(BlockchainInteropTest, cls).setUpClass() settings.set_loglevel(DEBUG) def setUp(self): self.engine = ExecutionEngine() self.econtext = ExecutionContext() self.state_reader = StateReader() def test_interop_getblock(self): height = StackItem.New(1234) self.engine.EvaluationStack.PushT(height) self.state_reader.Blockchain_GetBlock(self.engine) block = self.engine.EvaluationStack.Pop().GetInterface() self.assertIsInstance(block, Block) def test_interop_get_transaction(self): u256 = UInt256.ParseString( 'e4d2ea5df2adf77df91049beccbb16f98863b93a16439c60381eac1f23bff178') hash = StackItem.New(u256.Data) self.engine.EvaluationStack.PushT(hash) self.state_reader.Blockchain_GetTransaction(self.engine) tx = self.engine.EvaluationStack.Pop().GetInterface() self.assertIsInstance(tx, Transaction) def test_interop_get_bad_transaction(self): u256 = UInt256.ParseString( 'e4d2ea5df2adf77df91049beccbb16f98863b93a16439c60381eac1f23bff176') hash = StackItem.New(u256.Data) self.engine.EvaluationStack.PushT(hash) self.state_reader.Blockchain_GetTransaction(self.engine) tx = self.engine.EvaluationStack.Pop().GetInterface() self.assertIsNone(tx) def test_interop_get_transaction_height(self): u256 = UInt256.ParseString( 'e4d2ea5df2adf77df91049beccbb16f98863b93a16439c60381eac1f23bff178') hash = StackItem.New(u256.Data) self.engine.EvaluationStack.PushT(hash) self.state_reader.Blockchain_GetTransactionHeight(self.engine) height = self.engine.EvaluationStack.Pop().GetBigInteger() self.assertEqual(height, 4999) def test_interop_get_bad_transaction_height(self): u256 = UInt256.ParseString( 'e4d2ea5df2adf77df91049beccbb16f98863b93a16439c60381eac1f23bff176') hash = StackItem.New(u256.Data) self.engine.EvaluationStack.PushT(hash) self.state_reader.Blockchain_GetTransactionHeight(self.engine) height = self.engine.EvaluationStack.Pop().GetBigInteger() self.assertEqual(height, -1)
def setUp(self): self.engine = ExecutionEngine() self.econtext = ExecutionContext(Script(self.engine.Crypto, b''), 0) self.engine.InvocationStack.PushT(self.econtext) snapshot = GetBlockchain()._db.createSnapshot() self.state_reader = StateReader(TriggerType.Application, snapshot)
class InteropSerializeDeserializeTestCase(TestCase): engine = None econtext = None state_reader = None def setUp(self): self.engine = ExecutionEngine() self.econtext = ExecutionContext() self.state_reader = StateReader() def test_serialize_struct(self): my_struct = Struct([ StackItem.New(12), StackItem.New(b'Hello World'), StackItem.New(True), Array([StackItem.New(113442), StackItem.New(2), StackItem.New(3)]) ]) self.engine.EvaluationStack.PushT(my_struct) self.state_reader.Runtime_Serialize(self.engine) self.state_reader.Runtime_Deserialize(self.engine) deserialized = self.engine.EvaluationStack.Pop() self.assertIsInstance(deserialized, Struct) self.assertEqual(deserialized.Count, 4) self.assertEqual(deserialized.GetArray()[0], StackItem.New(12)) self.assertEqual(deserialized.GetArray()[1], StackItem.New(b'Hello World')) self.assertEqual(deserialized.GetArray()[2], StackItem.New(True)) subarray = deserialized.GetArray()[3] self.assertIsInstance(subarray, Array) self.assertEqual(subarray.Count, 3) self.assertEqual(subarray.GetArray()[0], StackItem.New(113442)) def test_serialize_array(self): my_array = Array([ StackItem.New(12), StackItem.New(b'Hello World'), StackItem.New(True), Array([StackItem.New(113442), StackItem.New(2), StackItem.New(3)]) ]) self.engine.EvaluationStack.PushT(my_array) self.state_reader.Runtime_Serialize(self.engine) self.state_reader.Runtime_Deserialize(self.engine) deserialized = self.engine.EvaluationStack.Pop() self.assertIsInstance(deserialized, Array) self.assertEqual(deserialized.Count, 4) self.assertEqual(deserialized.GetArray()[0], StackItem.New(12)) self.assertEqual(deserialized.GetArray()[1], StackItem.New(b'Hello World')) self.assertEqual(deserialized.GetArray()[2], StackItem.New(True)) subarray = deserialized.GetArray()[3] self.assertIsInstance(subarray, Array) self.assertEqual(subarray.Count, 3) self.assertEqual(subarray.GetArray()[0], StackItem.New(113442)) def test_serialize_bytearray(self): my_ba = StackItem.New(b'a0f03a') self.engine.EvaluationStack.PushT(my_ba) self.state_reader.Runtime_Serialize(self.engine) res = self.state_reader.Runtime_Deserialize(self.engine) deserialized = self.engine.EvaluationStack.Pop() self.assertEqual(deserialized.GetByteArray(), bytearray(b'a0f03a')) my_ba = StackItem.New(b'Hello Serialization') self.engine.EvaluationStack.PushT(my_ba) self.state_reader.Runtime_Serialize(self.engine) self.state_reader.Runtime_Deserialize(self.engine) deserialized = self.engine.EvaluationStack.Pop() self.assertEqual(deserialized.GetByteArray(), bytearray(b'Hello Serialization')) my_ba = StackItem.New(bytearray(b'\x01\x03\xfa\x99\x42')) self.engine.EvaluationStack.PushT(my_ba) self.state_reader.Runtime_Serialize(self.engine) self.state_reader.Runtime_Deserialize(self.engine) deserialized = self.engine.EvaluationStack.Pop() self.assertEqual(deserialized.GetByteArray(), bytearray(b'\x01\x03\xfa\x99\x42')) def test_serialize_bool(self): # create integer, serialize it via state reader my_bool = StackItem.New(True) self.engine.EvaluationStack.PushT(my_bool) self.state_reader.Runtime_Serialize(self.engine) self.state_reader.Runtime_Deserialize(self.engine) deserialized = self.engine.EvaluationStack.Pop() self.assertIsInstance(deserialized, Boolean) self.assertEqual(deserialized.GetBoolean(), True) my_bool = StackItem.New(False) self.engine.EvaluationStack.PushT(my_bool) self.state_reader.Runtime_Serialize(self.engine) self.state_reader.Runtime_Deserialize(self.engine) deserialized = self.engine.EvaluationStack.Pop() self.assertIsInstance(deserialized, Boolean) self.assertEqual(deserialized.GetBoolean(), False) def test_serialize_integer(self): # create integer, serialize it via state reader my_integer = StackItem.New(1234) self.engine.EvaluationStack.PushT(my_integer) self.state_reader.Runtime_Serialize(self.engine) self.assertEqual(len(self.engine.EvaluationStack.Items), 1) # we will preview what will be pushed onto the stack existing = self.engine.EvaluationStack.Peek(0) ms = StreamManager.GetStream(existing.GetByteArray()) reader = BinaryReader(ms) result = StackItem.DeserializeStackItem(reader) # now run deserialized res = self.state_reader.Runtime_Deserialize(self.engine) self.assertEqual(res, True) deserialized = self.engine.EvaluationStack.Pop() self.assertEqual(deserialized, result) self.assertEqual(deserialized, my_integer) self.assertEqual(deserialized.GetBigInteger(), 1234) def test_serialize_negative_integer(self): # create integer, serialize it via state reader my_integer = StackItem.New(-12324345) self.engine.EvaluationStack.PushT(my_integer) self.state_reader.Runtime_Serialize(self.engine) self.state_reader.Runtime_Deserialize(self.engine) deserialized = self.engine.EvaluationStack.Pop() # self.assertEqual(deserialized, my_integer) self.assertEqual(deserialized.GetBigInteger(), -12324345) def test_serialize_big_integer(self): # create integer, serialize it via state reader my_integer = StackItem.New(23424324242424242424234) self.engine.EvaluationStack.PushT(my_integer) self.state_reader.Runtime_Serialize(self.engine) self.state_reader.Runtime_Deserialize(self.engine) deserialized = self.engine.EvaluationStack.Pop() # self.assertEqual(deserialized, my_integer) self.assertEqual(deserialized.GetBigInteger(), 23424324242424242424234) def test_serialize_zero(self): # create integer, serialize it via state reader my_integer = StackItem.New(0) self.engine.EvaluationStack.PushT(my_integer) self.state_reader.Runtime_Serialize(self.engine) self.state_reader.Runtime_Deserialize(self.engine) deserialized = self.engine.EvaluationStack.Pop() self.assertEqual(deserialized.GetBigInteger(), 0) def test_serialize_map(self): map2 = Map({ StackItem.New(b'a'): StackItem.New(1), StackItem.New(b'b'): StackItem.New(2), StackItem.New(b'c'): StackItem.New(3), }) self.engine.EvaluationStack.PushT(map2) self.state_reader.Runtime_Serialize(self.engine) self.state_reader.Runtime_Deserialize(self.engine) deserialized = self.engine.EvaluationStack.Pop() self.assertEqual(deserialized, map2) map3 = Map({ StackItem.New(b'j'): StackItem.New(8), StackItem.New(b'k'): StackItem.New(2222), }) map2.SetItem(StackItem.New(b'mymap'), map3) self.engine.EvaluationStack.PushT(map2) self.state_reader.Runtime_Serialize(self.engine) self.state_reader.Runtime_Deserialize(self.engine) deserialized = self.engine.EvaluationStack.Pop() self.assertEqual(deserialized, map2) @patch('logzero.logger.error') def test_cant_serialize_iop_item(self, mocked_logger): genesis = Blockchain.GenesisBlock() self.engine.EvaluationStack.PushT(StackItem.FromInterface(genesis)) cant_do = self.state_reader.Runtime_Serialize(self.engine) self.assertEqual(cant_do, False) mocked_logger.assert_called_with( StringIn( 'Cannot serialize item IOp Interface: <neo.Core.Block.Block object' )) @patch('logzero.logger.error') def test_cant_deserialize_item(self, mocked_logger): self.engine.EvaluationStack.PushT(StackItem.New(b'abc')) self.state_reader.Runtime_Deserialize(self.engine) item = self.engine.EvaluationStack.Pop() self.assertIsNone(item) mocked_logger.assert_called_with( StringIn('Could not deserialize stack item with type:'))
def GetStateReader(): from neo.SmartContract.StateReader import StateReader return StateReader()
class InteropSerializeDeserializeTestCase(TestCase): engine = None econtext = None state_reader = None def setUp(self): self.engine = ExecutionEngine() self.econtext = ExecutionContext(engine=self.engine) self.state_reader = StateReader() def test_iter_array(self): my_array = Array([ StackItem.New(12), StackItem.New(b'Hello World'), StackItem.New(True), Array([StackItem.New(113442), StackItem.New(2), StackItem.New(3)]) ]) self.econtext.EvaluationStack.PushT(my_array) self.engine.InvocationStack.PushT(self.econtext) self.state_reader.Enumerator_Create(self.engine) iterable = self.econtext.EvaluationStack.Peek(0).GetInterface() self.assertIsInstance(iterable, ArrayWrapper) keys = [] values = [] while iterable.Next(): currentKey = iterable.Key() keys.append(currentKey.GetBigInteger()) values.append(iterable.Value()) self.assertEqual(keys, [0, 1, 2, 3]) self.assertEqual(values, my_array.GetArray()) def test_iter_map(self): my_map = Map({ StackItem.New('a'): StackItem.New(1), StackItem.New('b'): StackItem.New(3), StackItem.New('d'): StackItem.New(432) }) self.econtext.EvaluationStack.PushT(my_map) self.engine.InvocationStack.PushT(self.econtext) self.state_reader.Iterator_Create(self.engine) iterable = self.econtext.EvaluationStack.Peek(0).GetInterface() self.assertIsInstance(iterable, MapWrapper) keys = [] values = [] while iterable.Next(): keys.append(iterable.Key()) values.append(iterable.Value()) self.assertEqual( keys, [StackItem.New('a'), StackItem.New('b'), StackItem.New('d')]) self.assertEqual(keys, my_map.Keys) self.assertEqual( values, [StackItem.New(1), StackItem.New(3), StackItem.New(432)]) self.assertEqual(values, my_map.Values) def test_iter_array_keys(self): my_array = Array([ StackItem.New(12), StackItem.New(b'Hello World'), StackItem.New(True), Array([StackItem.New(113442), StackItem.New(2), StackItem.New(3)]) ]) self.econtext.EvaluationStack.PushT(my_array) self.engine.InvocationStack.PushT(self.econtext) self.state_reader.Enumerator_Create(self.engine) create_iterkeys = self.state_reader.Iterator_Keys(self.engine) self.assertEqual(create_iterkeys, True) iterkeys = self.econtext.EvaluationStack.Peek(0).GetInterface() self.assertIsInstance(iterkeys, KeysWrapper) keys = [] while iterkeys.Next(): keys.append(iterkeys.Value().GetBigInteger()) self.assertEqual(keys, [0, 1, 2, 3]) def test_iter_array_values(self): my_array = Array([ StackItem.New(12), StackItem.New(b'Hello World'), StackItem.New(True), Array([StackItem.New(113442), StackItem.New(2), StackItem.New(3)]) ]) self.econtext.EvaluationStack.PushT(my_array) self.engine.InvocationStack.PushT(self.econtext) self.state_reader.Enumerator_Create(self.engine) create_itervalues = self.state_reader.Iterator_Values(self.engine) self.assertEqual(create_itervalues, True) itervals = self.econtext.EvaluationStack.Peek(0).GetInterface() self.assertIsInstance(itervals, ValuesWrapper) values = [] while itervals.Next(): values.append(itervals.Value()) self.assertEqual(values, my_array.GetArray()) def test_iter_concat(self): my_array = Array([ StackItem.New(12), StackItem.New(b'Hello World'), StackItem.New(True), Array([StackItem.New(113442), StackItem.New(2), StackItem.New(3)]) ]) my_array2 = Array([ StackItem.New(b'a'), StackItem.New(b'b'), StackItem.New(4), StackItem.New(100) ]) self.econtext.EvaluationStack.PushT(my_array2) self.engine.InvocationStack.PushT(self.econtext) self.state_reader.Enumerator_Create(self.engine) self.econtext.EvaluationStack.PushT(my_array) self.state_reader.Enumerator_Create(self.engine) result = self.state_reader.Enumerator_Concat(self.engine) self.assertEqual(result, True) concatted_enum = self.econtext.EvaluationStack.Peek().GetInterface() self.assertIsInstance(concatted_enum, ConcatenatedEnumerator) values = [] count = 0 while concatted_enum.Next(): count += 1 values.append(concatted_enum.Value()) self.assertEqual(count, 8) self.assertEqual(values, my_array.GetArray() + my_array2.GetArray()) def test_iter_array_bad(self): my_item = StackItem.New(12) self.econtext.EvaluationStack.PushT(my_item) self.engine.InvocationStack.PushT(self.econtext) result = self.state_reader.Enumerator_Create(self.engine) self.assertEqual(result, False) self.assertEqual(self.econtext.EvaluationStack.Count, 0) def test_iter_map_bad(self): my_item = StackItem.New(12) self.econtext.EvaluationStack.PushT(my_item) self.engine.InvocationStack.PushT(self.econtext) result = self.state_reader.Iterator_Create(self.engine) self.assertEqual(result, False) self.assertEqual(self.econtext.EvaluationStack.Count, 0) def test_iter_array_key_bad(self): my_item = StackItem.New(12) self.econtext.EvaluationStack.PushT(my_item) self.engine.InvocationStack.PushT(self.econtext) result = self.state_reader.Iterator_Key(self.engine) self.assertEqual(result, False) self.assertEqual(self.econtext.EvaluationStack.Count, 0) def test_iter_array_values_bad(self): my_item = StackItem.New(12) self.econtext.EvaluationStack.PushT(my_item) self.engine.InvocationStack.PushT(self.econtext) result = self.state_reader.Iterator_Values(self.engine) self.assertEqual(result, False) self.assertEqual(self.econtext.EvaluationStack.Count, 0) def test_iter_array_keys_bad(self): my_item = StackItem.New(12) self.econtext.EvaluationStack.PushT(my_item) self.engine.InvocationStack.PushT(self.econtext) result = self.state_reader.Iterator_Keys(self.engine) self.assertEqual(result, False) self.assertEqual(self.econtext.EvaluationStack.Count, 0)
class InteropSerializeDeserializeTestCase(NeoTestCase): engine = None econtext = None state_reader = None def setUp(self): self.engine = ExecutionEngine(crypto=Crypto.Default()) self.econtext = ExecutionContext(Script(self.engine.Crypto, b''), 0) self.engine.InvocationStack.PushT(self.econtext) snapshot = GetBlockchain()._db.createSnapshot() self.state_reader = StateReader(TriggerType.Application, snapshot) def test_serialize_struct(self): my_struct = Struct([ StackItem.New(12), StackItem.New(b'Hello World'), StackItem.New(True), Array([StackItem.New(113442), StackItem.New(2), StackItem.New(3)]) ]) self.econtext.EvaluationStack.PushT(my_struct) self.engine.InvocationStack.PushT(self.econtext) self.state_reader.Runtime_Serialize(self.engine) self.state_reader.Runtime_Deserialize(self.engine) deserialized = self.econtext.EvaluationStack.Pop() self.assertIsInstance(deserialized, Struct) self.assertEqual(deserialized.Count, 4) self.assertEqual(deserialized.GetArray()[0], StackItem.New(12)) self.assertEqual(deserialized.GetArray()[1], StackItem.New(b'Hello World')) self.assertEqual(deserialized.GetArray()[2], StackItem.New(True)) subarray = deserialized.GetArray()[3] self.assertIsInstance(subarray, Array) self.assertEqual(subarray.Count, 3) self.assertEqual(subarray.GetArray()[0], StackItem.New(113442)) def test_serialize_array(self): my_array = Array([ StackItem.New(12), StackItem.New(b'Hello World'), StackItem.New(True), Array([StackItem.New(113442), StackItem.New(2), StackItem.New(3)]) ]) self.econtext.EvaluationStack.PushT(my_array) self.engine.InvocationStack.PushT(self.econtext) self.state_reader.Runtime_Serialize(self.engine) self.state_reader.Runtime_Deserialize(self.engine) deserialized = self.econtext.EvaluationStack.Pop() self.assertIsInstance(deserialized, Array) self.assertEqual(deserialized.Count, 4) self.assertEqual(deserialized.GetArray()[0], StackItem.New(12)) self.assertEqual(deserialized.GetArray()[1], StackItem.New(b'Hello World')) self.assertEqual(deserialized.GetArray()[2], StackItem.New(True)) subarray = deserialized.GetArray()[3] self.assertIsInstance(subarray, Array) self.assertEqual(subarray.Count, 3) self.assertEqual(subarray.GetArray()[0], StackItem.New(113442)) def test_serialize_bytearray(self): my_ba = StackItem.New(b'a0f03a') self.econtext.EvaluationStack.PushT(my_ba) self.engine.InvocationStack.PushT(self.econtext) self.state_reader.Runtime_Serialize(self.engine) res = self.state_reader.Runtime_Deserialize(self.engine) deserialized = self.econtext.EvaluationStack.Pop() self.assertEqual(deserialized.GetByteArray(), bytearray(b'a0f03a')) my_ba = StackItem.New(b'Hello Serialization') self.econtext.EvaluationStack.PushT(my_ba) self.state_reader.Runtime_Serialize(self.engine) self.state_reader.Runtime_Deserialize(self.engine) deserialized = self.econtext.EvaluationStack.Pop() self.assertEqual(deserialized.GetByteArray(), bytearray(b'Hello Serialization')) my_ba = StackItem.New(bytearray(b'\x01\x03\xfa\x99\x42')) self.econtext.EvaluationStack.PushT(my_ba) self.state_reader.Runtime_Serialize(self.engine) self.state_reader.Runtime_Deserialize(self.engine) deserialized = self.econtext.EvaluationStack.Pop() self.assertEqual(deserialized.GetByteArray(), bytearray(b'\x01\x03\xfa\x99\x42')) def test_serialize_bool(self): # create integer, serialize it via state reader my_bool = StackItem.New(True) self.econtext.EvaluationStack.PushT(my_bool) self.engine.InvocationStack.PushT(self.econtext) self.state_reader.Runtime_Serialize(self.engine) self.state_reader.Runtime_Deserialize(self.engine) deserialized = self.econtext.EvaluationStack.Pop() self.assertIsInstance(deserialized, Boolean) self.assertEqual(deserialized.GetBoolean(), True) my_bool = StackItem.New(False) self.econtext.EvaluationStack.PushT(my_bool) self.state_reader.Runtime_Serialize(self.engine) self.state_reader.Runtime_Deserialize(self.engine) deserialized = self.econtext.EvaluationStack.Pop() self.assertIsInstance(deserialized, Boolean) self.assertEqual(deserialized.GetBoolean(), False) def test_serialize_integer(self): # create integer, serialize it via state reader my_integer = StackItem.New(1234) self.econtext.EvaluationStack.PushT(my_integer) self.engine.InvocationStack.PushT(self.econtext) self.state_reader.Runtime_Serialize(self.engine) self.assertEqual(len(self.econtext.EvaluationStack.Items), 1) # we will preview what will be pushed onto the stack existing = self.econtext.EvaluationStack.Peek(0) ms = StreamManager.GetStream(existing.GetByteArray()) reader = BinaryReader(ms) result = StackItem.DeserializeStackItem(reader) # now run deserialized res = self.state_reader.Runtime_Deserialize(self.engine) self.assertEqual(res, True) deserialized = self.econtext.EvaluationStack.Pop() self.assertEqual(deserialized, result) self.assertEqual(deserialized, my_integer) self.assertEqual(deserialized.GetBigInteger(), 1234) def test_serialize_negative_integer(self): # create integer, serialize it via state reader my_integer = StackItem.New(-12324345) self.econtext.EvaluationStack.PushT(my_integer) self.engine.InvocationStack.PushT(self.econtext) self.state_reader.Runtime_Serialize(self.engine) self.state_reader.Runtime_Deserialize(self.engine) deserialized = self.econtext.EvaluationStack.Pop() # self.assertEqual(deserialized, my_integer) self.assertEqual(deserialized.GetBigInteger(), -12324345) def test_serialize_big_integer(self): # create integer, serialize it via state reader my_integer = StackItem.New(23424324242424242424234) self.econtext.EvaluationStack.PushT(my_integer) self.engine.InvocationStack.PushT(self.econtext) self.state_reader.Runtime_Serialize(self.engine) self.state_reader.Runtime_Deserialize(self.engine) deserialized = self.econtext.EvaluationStack.Pop() # self.assertEqual(deserialized, my_integer) self.assertEqual(deserialized.GetBigInteger(), 23424324242424242424234) def test_serialize_zero(self): # create integer, serialize it via state reader my_integer = StackItem.New(0) self.econtext.EvaluationStack.PushT(my_integer) self.engine.InvocationStack.PushT(self.econtext) self.state_reader.Runtime_Serialize(self.engine) self.state_reader.Runtime_Deserialize(self.engine) deserialized = self.econtext.EvaluationStack.Pop() self.assertEqual(deserialized.GetBigInteger(), 0) def test_serialize_map(self): map2 = Map({ StackItem.New(b'a'): StackItem.New(1), StackItem.New(b'b'): StackItem.New(2), StackItem.New(b'c'): StackItem.New(3), }) self.econtext.EvaluationStack.PushT(map2) self.engine.InvocationStack.PushT(self.econtext) self.state_reader.Runtime_Serialize(self.engine) self.state_reader.Runtime_Deserialize(self.engine) deserialized = self.econtext.EvaluationStack.Pop() self.assertEqual(deserialized, map2) map3 = Map({ StackItem.New(b'j'): StackItem.New(8), StackItem.New(b'k'): StackItem.New(2222), }) map2.SetItem(StackItem.New(b'mymap'), map3) self.econtext.EvaluationStack.PushT(map2) self.state_reader.Runtime_Serialize(self.engine) self.state_reader.Runtime_Deserialize(self.engine) deserialized = self.econtext.EvaluationStack.Pop() self.assertEqual(deserialized, map2) def test_cant_serialize_iop_item(self): genesis = Blockchain.GenesisBlock() self.econtext.EvaluationStack.PushT(StackItem.FromInterface(genesis)) self.engine.InvocationStack.PushT(self.econtext) cant_do = self.state_reader.Runtime_Serialize(self.engine) self.assertEqual(cant_do, False) def test_cant_deserialize_item(self): self.econtext.EvaluationStack.PushT(StackItem.New(b'abc')) self.engine.InvocationStack.PushT(self.econtext) success = self.state_reader.Runtime_Deserialize(self.engine) self.assertFalse(success)