コード例 #1
0
def main():
    # create the API
    api = LedgerApi('127.0.0.1', 8000)

    # create an entity and provide it some wealth
    print('Setup...')
    entity = Entity()
    api.sync(api.tokens.wealth(entity, 100000000))
    print('Setup...complete')

    # create the contract on the ledger
    synergetic_contract = Contract(CONTRACT_TEXT)
    print('Creating contract..')
    api.sync(api.contracts.create(entity, synergetic_contract, 4096))
    print('Contract submitted ({}.{}).'.format(
        synergetic_contract.digest.to_hex(), synergetic_contract.owner))

    # create a whole series of random data to submit to the DAG
    random_ints = [random.randint(0, 200) for _ in range(10)]
    api.sync([
        api.contracts.submit_data(entity,
                                  synergetic_contract.digest,
                                  value=value) for value in random_ints
    ])
    print('Data submitted.')

    print('Waiting...')
    api.wait_for_blocks(10)

    print('Issuing query...')
    result = synergetic_contract.query(api, 'query_result')
    print('Query result:', result)
コード例 #2
0
def main():
    # create the API
    api = LedgerApi('127.0.0.1', 8000)

    # create an entity from a private key stored in hex
    entity = Entity.from_hex(
        '6e8339a0c6d51fc58b4365bf2ce18ff2698d2b8c40bb13fcef7e1ba05df18e4b')

    # create the contract on the ledger
    synergetic_contract = Contract(CONTRACT_TEXT, entity)
    print('Creating contract..')
    api.sync(api.contracts.create(entity, synergetic_contract, 4096))

    # create a whole series of random data to submit to the DAG
    random_ints = [random.randint(0, 200) for _ in range(10)]
    fee = 100000000
    api.sync(
        [api.contracts.submit_data(entity, synergetic_contract.digest, synergetic_contract.address, fee, value=value) \
         for value in random_ints])
    print('Data submitted.')

    print('Waiting...')
    api.wait_for_blocks(10)

    print('Issuing query...')
    result = synergetic_contract.query(api, 'query_result')
    print('Query result:', result)
コード例 #3
0
def run_contract(parameters, test_instance):
    nodes = parameters["nodes"]
    contract_name = parameters["contract_name"]
    wait_for_blocks_num = parameters["wait_for_blocks"]
    for node_index in nodes:
        node_host = "localhost"
        node_port = test_instance._nodes[node_index]._port_start

        api = LedgerApi(node_host, node_port)

        # create the entity from the node's private key
        entity = Entity(get_nodes_private_key(test_instance, node_index))

        try:
            contract_helper = test_instance._nodes[node_index]._contract
        except AttributeError:
            output(
                f"No contract stored in test_instance (node_index={node_index})! Loading from file...")
            contract_helper = SynergeticContractTestHelper(
                contract_name, api, entity, test_instance._workspace)
            contract_helper.load()
        output('Submit data, available balance: ', api.tokens.balance(entity))
        contract_helper.submit_random_data(10, (0, 200))
        api.wait_for_blocks(wait_for_blocks_num)
        valid = contract_helper.validate_execution()
        if not valid:
            output(
                f"Synergetic contract ({contract_name}) execution failed on node {node_index}!")
            raise Exception(
                f"Synergetic contract ({contract_name}) execution failed on node {node_index}!")
        else:
            output(
                f"Synergetic contract ({contract_name}) executed on node {node_index} ")
コード例 #4
0
ファイル: testcase.py プロジェクト: devjsc/ledger
 def wait_for_blocks(self, node_index, number_of_blocks):
     """
     Wait for a specific number of blocks in the selected node
     :param node_index: which node we are interested in
     :param number_of_blocks: for how many new block to wait
     :return:
     """
     port = self._nodes[node_index]._port_start
     output(f"Waiting for {number_of_blocks} blocks on node {port}")
     api = LedgerApi("localhost", port)
     api.wait_for_blocks(number_of_blocks)