Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_contract_create_block(self):
hexdata = binascii.unhexlify(self.contract_create_block)
block = Helper.AsSerializableWithType(hexdata, 'neo.Core.Block.Block')
self.assertEqual(block.Index, self.contract_block_index)
result = Blockchain.Default().Persist(block)
self.assertTrue(result)
snapshot = Blockchain.Default()._db.snapshot()
contracts = DBCollection(Blockchain.Default()._db, snapshot, DBPrefix.ST_Contract, ContractState)
contract_added = contracts.TryGet(self.contract_hash)
self.assertIsNotNone(contract_added)
self.assertEqual(contract_added.HasStorage, False)
self.assertEqual(contract_added.Name, b'test create')
self.assertEqual(contract_added.Email, b'flow@neo.org')
code = contract_added.Code
result = binascii.unhexlify(ms.ToArray())
StreamManager.ReleaseStream(ms)
ms = StreamManager.GetStream(result)
reader = BinaryReader(ms)
deserialized_message = Message()
deserialized_message.Deserialize(reader)
StreamManager.ReleaseStream(ms)
dm = deserialized_message
self.assertEqual(dm.Command, 'version')
self.assertEqual(dm.Magic, settings.MAGIC)
checksum = Message.GetChecksum(dm.Payload)
self.assertEqual(checksum, dm.Checksum)
deserialized_version = IOHelper.AsSerializableWithType(dm.Payload, 'neo.Network.Payloads.VersionPayload.VersionPayload')
self.assertEqual(deserialized_version.Port, self.port)
self.assertEqual(deserialized_version.UserAgent, self.ua)
self.assertEqual(deserialized_version.Timestamp, self.payload.Timestamp)
tx = Transaction.DeserializeFrom(reader)
self.assertEqual(tx.ToArray(), self.pb_raw)
self.assertEqual(tx.Hash.ToBytes(), self.pb_hash)
json = tx.ToJson()
self.assertEqual(json['size'], 613)
self.assertEqual(json['type'], 'PublishTransaction')
contract = json['contract']
self.assertEqual(contract['author'], 'Erik Zhang')
self.assertEqual(contract['description'], 'Lock your assets until a timestamp.')
self.assertEqual(contract['code']['hash'], '0xffbd1a7ad1e2348b6b3822426f364bfb4bcce3b9')
self.assertEqual(contract['code']['returntype'], "Boolean")
self.assertEqual(contract['code']['parameters'], ['Integer', 'ByteArray', 'Signature'])
self.assertEqual(Fixed8.FromDecimal(settings.ALL_FEES['PublishTransaction']), tx.SystemFee())
def test_initialize(self):
leader = NodeLeader.Instance()
self.assertEqual(leader.Peers, [])
self.assertEqual(leader.KNOWN_ADDRS, [])
def testMerge(self):
spk = SpikeTrain(spike_times=numpy.arange(0,110,10))
spk2 = SpikeTrain(spike_times=numpy.arange(100,210,10))
spk.merge(spk2)
assert (spk._t_stop == 200) and (len(spk) == 22)
def testMeanRate(self):
poisson_param = 1./40
isi = numpy.random.exponential(poisson_param, 1000)
poisson_times = numpy.cumsum(isi)
spk = SpikeTrain(spike_times = poisson_times)
assert 35 < spk.mean_rate() < 45
def testAppend(self):
spktrain = SpikeTrain(spike_times=numpy.arange(10))
self.assertRaises(Exception, self.spk.append, 0, spktrain)
def test_preprocess(self):
self.features = GeneralNetworkFeatures(logger_level="error")
time, spiketrains = self.features.preprocess(self.time_original, self.values)
self.assertEqual(time, self.time_original)
self.assertIsInstance(spiketrains[0], neo.core.SpikeTrain)
self.assertIsInstance(spiketrains[1], neo.core.SpikeTrain)
self.assertIsInstance(spiketrains[2], neo.core.SpikeTrain)
self.assertIsInstance(spiketrains[3], neo.core.SpikeTrain)
self.assertTrue(np.array_equal(spiketrains[0], self.values[0]))
self.assertTrue(np.array_equal(spiketrains[1], self.values[1]))
self.assertTrue(np.array_equal(spiketrains[2], self.values[2]))
self.assertTrue(np.array_equal(spiketrains[3], self.values[3]))
self.assertEqual(spiketrains[0].t_stop, self.time_original)
with mock.patch('neo.Prompt.Utils.get_input_prompt', return_value="b'abc'") as fake_prompt:
result, abort = Utils.gather_param(0, ContractParameterType.Array, do_continue=False)
self.assertRaises(Exception, "Please provide a list")
self.assertEqual(result, None)
self.assertEqual(abort, True)
# test ContractParameterType.PublicKey
with mock.patch('neo.Prompt.Utils.get_input_prompt', return_value="03cbb45da6072c14761c9da545749d9cfd863f860c351066d16df480602a2024c6") as fake_prompt:
test_wallet_path = shutil.copyfile(
WalletFixtureTestCase.wallet_1_path(),
WalletFixtureTestCase.wallet_1_dest()
)
wallet = UserWallet.Open(
test_wallet_path,
to_aes_key(WalletFixtureTestCase.wallet_1_pass())
)
addr_scripthash = wallet.GetStandardAddress()
result, abort = Utils.gather_param(0, ContractParameterType.PublicKey)
script = b'21' + result.encode_point(True) + b'ac'
pk_script_hash = Crypto.ToScriptHash(script)
self.assertEqual(addr_scripthash, pk_script_hash) # verifies the functionality of ContractParameterType.PublicKey
wallet.Close()
wallet = None
os.remove(WalletFixtureTestCase.wallet_1_dest())
# test unknown ContractParameterType
def test_send_to_address_bad_address(self):
test_wallet_path = shutil.copyfile(
WalletFixtureTestCase.wallet_1_path(),
WalletFixtureTestCase.wallet_1_dest()
)
self.app.wallet = UserWallet.Open(
test_wallet_path,
to_aes_key(WalletFixtureTestCase.wallet_1_pass())
)
address = 'AXjaFSP23Jkbe6Pk9pPGT6NBDs1HVdqaX' # "AXjaFSP23Jkbe6Pk9pPGT6NBDs1HVdqaX" is too short causing ToScriptHash to fail
req = self._gen_post_rpc_req("sendtoaddress", params=['gas', address, 1])
mock_req = mock_post_request(json.dumps(req).encode("utf-8"))
res = json.loads(self.app.home(mock_req))
error = res.get('error', {})
self.assertEqual(error.get('code', None), -32602)
self.assertEqual(error.get('message', None), "Invalid params")
self.app.wallet.Close()
self.app.wallet = None
os.remove(WalletFixtureTestCase.wallet_1_dest())