def validate_values(self, *params: Any) -> List[Any]: values = [] if len(params) != 2: return values origin, visitor = params values.append(self.contract_hash) from boa3.analyser.astanalyser import IAstAnalyser if not isinstance(visitor, IAstAnalyser): return values from boa3.exception import CompilerError if not isinstance(origin, ast.Call) or len(origin.args) < 1: visitor._log_error( CompilerError.UnfilledArgument(origin.lineno, origin.col_offset, list(self.args.keys())[0])) return values argument_hash = visitor.visit(origin.args[0]) try: if isinstance(argument_hash, str): from boa3.neo import from_hex_str argument_hash = from_hex_str(argument_hash) if isinstance(argument_hash, bytes): values[0] = UInt160(argument_hash) except BaseException: visitor._log_error( CompilerError.InvalidUsage( origin.lineno, origin.col_offset, "Only literal values are accepted for 'script_hash' argument" )) return values
def from_json(cls, json: Dict[str, Any]) -> Transaction: script = base64.b64decode(json['script']) tx = cls(script) if 'signers' in json: signers_json = json['signers'] if not isinstance(signers_json, list): signers_json = [signers_json] tx._signers = [Signer.from_json(js) for js in signers_json] if 'witnesses' in json: witnesses_json = json['witnesses'] if not isinstance(witnesses_json, list): witnesses_json = [witnesses_json] tx._witnesses = [Witness.from_json(js) for js in witnesses_json] if 'attributes' in json: attributes_json = json['attributes'] if not isinstance(attributes_json, list): attributes_json = [attributes_json] tx._attributes = [ tx_attribute.TransactionAttribute.from_json(js) for js in attributes_json ] if 'hash' in json and isinstance(json['hash'], str): tx._hash = UInt256(from_hex_str(json['hash'])) return tx
def test_unregister_candidate(self): path = self.get_contract_path('UnregisterCandidate.py') engine = TestEngine() candidate_pubkey = bytes.fromhex('0296852e74830f48185caec9980d21dee5e8bee3da97d712123c19ee01c2d3f3ae') candidate_scripthash = from_hex_str('a8de26eb4931c674d31885acf722bd82e6bcd06d') result = self.run_smart_contract(engine, path, 'main', candidate_pubkey) self.assertEqual(False, result) result = self.run_smart_contract(engine, path, 'main', candidate_pubkey, signer_accounts=[candidate_scripthash]) self.assertEqual(True, result)
def from_json(cls, json: Dict[str, Any]) -> Block: # 'index' and 'timestamp' fields are required block = cls(int(json['index'])) block._timestamp = int(json['timestamp']) if 'transactions' in json: tx_json = json['transactions'] if not isinstance(tx_json, list): tx_json = [tx_json] block._transactions = [Transaction.from_json(js) for js in tx_json] if 'hash' in json and isinstance(json['hash'], str): block._hash = UInt256(from_hex_str(json['hash'])) return block
def test_get_block_by_hash(self): path = self.get_contract_path('GetBlockByHash.py') engine = TestEngine() engine.increase_block(1) block_hash = bytes(32) result = self.run_smart_contract(engine, path, 'Main', block_hash) self.assertIsNone(result) from boa3.neo import from_hex_str # TODO: using genesis block hash for testing, change when TestEngine returns blocks hashes block_hash = from_hex_str('0x1f4d1defa46faa5e7b9b8d3f79a06bec777d7c26c4aa5f6f5899a291daa87c15') result = self.run_smart_contract(engine, path, 'Main', block_hash) self.assertIsInstance(result, list) self.assertEqual(10, len(result)) self.assertEqual(block_hash, result[0]) self.assertEqual(0, result[6]) # genesis block's index is zero
def test_get_candidates(self): path = self.get_contract_path('GetCandidates.py') engine = TestEngine() # no candidate was registered result = self.run_smart_contract(engine, path, 'main') self.assertEqual(0, len(result)) path_register = self.get_contract_path('RegisterCandidate.py') candidate_pubkey = bytes.fromhex('0296852e74830f48185caec9980d21dee5e8bee3da97d712123c19ee01c2d3f3ae') candidate_scripthash = from_hex_str('a8de26eb4931c674d31885acf722bd82e6bcd06d') self.run_smart_contract(engine, path_register, 'main', candidate_pubkey, signer_accounts=[candidate_scripthash]) # after registering one result = self.run_smart_contract(engine, path, 'main') self.assertEqual(1, len(result)) self.assertEqual(candidate_pubkey, result[0][0]) self.assertEqual(0, result[0][1])
def test_get_block_by_hash(self): path = self.get_contract_path('GetBlockByHash.py') engine = TestEngine() engine.increase_block(1) block_hash = bytes(32) result = self.run_smart_contract(engine, path, 'Main', block_hash) self.assertIsNone(result) from boa3.neo import from_hex_str # TODO: using genesis block hash for testing, change when TestEngine returns blocks hashes block_hash = from_hex_str( '0xc3db4ba50ede4f9e749bd97e1499953ae17e65a415c6bf9e38c01cf92b03d156' ) result = self.run_smart_contract(engine, path, 'Main', block_hash) self.assertIsInstance(result, list) self.assertEqual(9, len(result)) self.assertEqual(block_hash, result[0]) self.assertEqual(0, result[5]) # genesis block's index is zero
def test_vote(self): path = self.get_contract_path('Vote.py') engine = TestEngine() account = bytes(range(20)) n_votes = 100 candidate_pubkey = bytes.fromhex('0296852e74830f48185caec9980d21dee5e8bee3da97d712123c19ee01c2d3f3ae') # will fail check_witness result = self.run_smart_contract(engine, path, 'main', account, candidate_pubkey) self.assertEqual(False, result) # NeoAccountState is None and will return false result = self.run_smart_contract(engine, path, 'main', account, candidate_pubkey, signer_accounts=[account]) self.assertEqual(False, result) # adding NEO to the account will make NeoAccountState not None engine.add_neo(account, n_votes) # it's possible to vote for no one result = self.run_smart_contract(engine, path, 'main', account, None, signer_accounts=[account]) self.assertEqual(True, result) # candidate is not registered yet result = self.run_smart_contract(engine, path, 'main', account, candidate_pubkey, signer_accounts=[account]) self.assertEqual(False, result) path_register = self.get_contract_path('RegisterCandidate.py') candidate_pubkey = bytes.fromhex('0296852e74830f48185caec9980d21dee5e8bee3da97d712123c19ee01c2d3f3ae') candidate_scripthash = from_hex_str('a8de26eb4931c674d31885acf722bd82e6bcd06d') self.run_smart_contract(engine, path_register, 'main', candidate_pubkey, signer_accounts=[candidate_scripthash]) # candidate was registered result = self.run_smart_contract(engine, path, 'main', account, candidate_pubkey, signer_accounts=[account]) self.assertEqual(True, result) path_get = self.get_contract_path('GetCandidates.py') result = self.run_smart_contract(engine, path_get, 'main') self.assertEqual(1, len(result)) self.assertEqual(candidate_pubkey, result[0][0]) self.assertEqual(n_votes, result[0][1])
import sys from boa3.neo import from_hex_str SYS_VERSION_INFO = sys.version_info ONE_BYTE_MAX_VALUE = 255 TWO_BYTES_MAX_VALUE = 256**2 - 1 FOUR_BYTES_MAX_VALUE = 256**4 - 1 SIZE_OF_INT32 = 4 SIZE_OF_INT160 = 20 DEFAULT_UINT32 = 0 ENCODING = 'utf-8' BYTEORDER = 'little' INITIALIZE_METHOD_ID = '_initialize' DEPLOY_METHOD_ID = '_deploy' NEO_SCRIPT = from_hex_str('0xef4073a0f2b305a38ec4050e4d3d28bc40ea63f5') GAS_SCRIPT = from_hex_str('0xd2a4cff31913016155e38e474a2c06d08be276cf') MANAGEMENT_SCRIPT = from_hex_str('0xfffdc93764dbaddd97c48f252a53ea4643faa3fd') CRYPTO_SCRIPT = from_hex_str('0x726cb6e0cd8628a1350a611384688911ab75f51b') STD_LIB_SCRIPT = from_hex_str('0xacce6fd80d44e1796aa0c2c625e9e4e0ce39efc0') ORACLE_SCRIPT = from_hex_str('0xfe924b7cfe89ddd271abaf7210a80a7e11178758')
def from_json(cls, json: Dict[str, Any]) -> Signer: account_hex = json['account'] account = UInt160(from_hex_str(account_hex)) return cls(account)
import sys from boa3.neo import from_hex_str SYS_VERSION_INFO = sys.version_info ONE_BYTE_MAX_VALUE = 255 TWO_BYTES_MAX_VALUE = 256**2 - 1 FOUR_BYTES_MAX_VALUE = 256**4 - 1 SIZE_OF_INT32 = 4 SIZE_OF_INT160 = 20 DEFAULT_UINT32 = 0 ENCODING = 'utf-8' BYTEORDER = 'little' INITIALIZE_METHOD_ID = '_initialize' NEO_SCRIPT = from_hex_str('0xf61eebf573ea36593fd43aa150c055ad7906ab83') GAS_SCRIPT = from_hex_str('0x70e2301955bf1e74cbb31d18c2f96972abadb328') MANAGEMENT_SCRIPT = from_hex_str('0xa501d7d7d10983673b61b7a2d3a813b36f9f0e43')
def from_json(cls, json: Dict[str, Any]) -> Signer: account_hex = json['account'] account = UInt160(from_hex_str(account_hex)) scopes = WitnessScope.get_from_neo_name( json['scopes']) if 'scopes' in json else WitnessScope.CalledByEntry return cls(account, scopes)
IMPORT_WILDCARD = '*' SYS_VERSION_INFO = sys.version_info SYS_VERSION = platform.python_version() BOA_VERSION = boa_version BOA_PACKAGE_PATH = os.path.abspath(f'{__file__}/..') ONE_BYTE_MAX_VALUE = 255 TWO_BYTES_MAX_VALUE = 256 ** 2 - 1 FOUR_BYTES_MAX_VALUE = 256 ** 4 - 1 SIZE_OF_INT32 = 4 SIZE_OF_INT160 = 20 SIZE_OF_ECPOINT = 33 DEFAULT_UINT32 = 0 GAS_DECIMALS = 8 INIT_METHOD_ID = '__init__' INITIALIZE_METHOD_ID = '_initialize' DEPLOY_METHOD_ID = '_deploy' NEO_SCRIPT = from_hex_str('0xef4073a0f2b305a38ec4050e4d3d28bc40ea63f5') GAS_SCRIPT = from_hex_str('0xd2a4cff31913016155e38e474a2c06d08be276cf') CRYPTO_SCRIPT = from_hex_str('0x726cb6e0cd8628a1350a611384688911ab75f51b') LEDGER_SCRIPT = from_hex_str('0xda65b600f7124ce6c79950c1772a36403104f2be') MANAGEMENT_SCRIPT = from_hex_str('0xfffdc93764dbaddd97c48f252a53ea4643faa3fd') ORACLE_SCRIPT = from_hex_str('0xfe924b7cfe89ddd271abaf7210a80a7e11178758') POLICY_SCRIPT = from_hex_str('0xcc5e4edd9f5f8dba8bb65734541df7a1c081c67b') ROLE_MANAGEMENT = from_hex_str('0x49cf4e5378ffcd4dec034fd98a174c5491e395e2') STD_LIB_SCRIPT = from_hex_str('0xacce6fd80d44e1796aa0c2c625e9e4e0ce39efc0')