Exemplo n.º 1
0
 def call_contract_function(self,
                            contract,
                            name,
                            args,
                            sender_key,
                            contract_addr=None,
                            wait=False,
                            check_status=False):
     # If contract address is empty, call the constructor.
     if contract_addr:
         func = getattr(contract.functions, name)
     else:
         func = getattr(contract, name)
     attributes = {
         'nonce': self.get_nonce(privtoaddr(sender_key)),
         **SmartContractBenchBase.REQUEST_BASE
     }
     if contract_addr:
         attributes['receiver'] = decode_hex(contract_addr)
         attributes['to'] = contract_addr
     else:
         attributes['receiver'] = b''
     tx_data = func(*args).buildTransaction(attributes)
     tx_data['data'] = decode_hex(tx_data['data'])
     tx_data['pri_key'] = sender_key
     tx_data['gas_price'] = tx_data['gasPrice']
     tx_data.pop('gasPrice', None)
     tx_data.pop('chainId', None)
     tx_data.pop('to', None)
     transaction = create_transaction(**tx_data)
     self._send_transaction(transaction, wait, check_status)
     return transaction
Exemplo n.º 2
0
 def new_address_and_transfer(self,
                              count=1,
                              amount=100000000000000,
                              wait=False,
                              check_status=False):
     results = []
     for _ in range(count):
         pri_key, pub_key = ec_random_keys()
         transaction = self.transfer(self.default_account_key,
                                     privtoaddr(pri_key), amount, wait,
                                     check_status)
         results.append([pri_key, transaction])
     return results
Exemplo n.º 3
0
 def transfer(self,
              sender_key,
              receiver,
              amount,
              wait=False,
              check_status=False):
     nonce = self.get_nonce(privtoaddr(sender_key))
     transaction = create_transaction(nonce,
                                      1,
                                      21000,
                                      amount,
                                      receiver,
                                      pri_key=sender_key)
     self._send_transaction(transaction, wait, check_status)
     return transaction
Exemplo n.º 4
0
    def run_test(self):
        solc = Solc()
        file_dir = os.path.dirname(os.path.realpath(__file__))
        staking_contract = solc.get_contract_instance(
            abi_file=os.path.join(
                file_dir, "contracts/storage_interest_staking_abi.json"),
            bytecode_file=os.path.join(
                file_dir, "contracts/storage_interest_staking_bytecode.dat"),
        )
        staking_contract_addr = Web3.toChecksumAddress(
            "443c409373ffd5c0bec1dddb7bec830856757b65")

        self.problem = "0x2bc79b7514884ab00da924607d71542cc4fed3beb8518e747726ae30ab6c7944"
        self.solution = "0xc4d2751c52311d0d7efe44e5c4195e058ad5ef4bb89b3e1761b24dc277b132c2"
        self.priv_key = default_config["GENESIS_PRI_KEY"]
        self.sender = encode_hex_0x(privtoaddr(self.priv_key))
        self.sender_checksum = Web3.toChecksumAddress(self.sender)
        self.pub = []
        self.pri = []
        self.rpc = RpcClient(self.nodes[0])
        gas = 50000000
        gas_price = 10

        # lock token for genesis account
        self.tx_conf = {
            "from": self.sender,
            "gas": int_to_hex(gas),
            "gasPrice": int_to_hex(gas_price),
            "chainId": 0
        }
        self.tx_conf['to'] = staking_contract_addr
        tx_data = decode_hex(
            staking_contract.functions.deposit(
                1000000 * 10**18).buildTransaction(self.tx_conf)["data"])
        tx = self.rpc.new_tx(value=0,
                             receiver=staking_contract_addr,
                             data=tx_data,
                             gas=gas,
                             gas_price=gas_price)
        self.rpc.send_tx(tx, True)

        for i in range(10):
            priv_key = random.randint(0, 2**256).to_bytes(32, "big")
            pub_key = encode_hex_0x(privtoaddr(priv_key))
            self.pub.append(pub_key)
            self.pri.append(priv_key)
            transaction = self.rpc.new_tx(sender=self.sender,
                                          receiver=pub_key,
                                          value=1000000 * 10**18,
                                          priv_key=self.priv_key)
            self.rpc.send_tx(transaction, True)
            # deposit 10000 tokens
            tx_data = decode_hex(
                staking_contract.functions.deposit(
                    10000 * 10**18).buildTransaction(self.tx_conf)["data"])
            tx = self.rpc.new_tx(value=0,
                                 sender=pub_key,
                                 receiver=self.tx_conf["to"],
                                 gas=gas,
                                 data=tx_data,
                                 priv_key=priv_key)
            self.rpc.send_tx(tx)
        self.tx_conf = {
            "from": self.sender,
            "gas": int_to_hex(gas),
            "gasPrice": int_to_hex(gas_price),
            "chainId": 0
        }
        self.filter = Filter(from_epoch="earliest", to_epoch="latest_mined")
        self.testEventContract()
        self.tx_conf = {
            "from": self.sender,
            "gas": int_to_hex(gas),
            "gasPrice": int_to_hex(gas_price),
            "chainId": 0
        }
        self.testBallotContract()
        self.tx_conf = {
            "from": self.sender,
            "gas": int_to_hex(gas),
            "gasPrice": int_to_hex(gas_price),
            "chainId": 0
        }
        self.testPayContract()
        self.tx_conf = {
            "from": self.sender,
            "gas": int_to_hex(gas),
            "gasPrice": int_to_hex(gas_price),
            "chainId": 0
        }
        self.testHTLCContract()
        self.tx_conf = {
            "from": self.sender,
            "gas": int_to_hex(gas),
            "gasPrice": int_to_hex(gas_price),
            "chainId": 0
        }
        self.testDaiContract()
        self.tx_conf = {
            "from": self.sender,
            "gas": int_to_hex(gas),
            "gasPrice": int_to_hex(gas_price),
            "chainId": 0
        }
        self.testMappingContract()
        self.tx_conf = {
            "from": self.sender,
            "gas": int_to_hex(gas),
            "gasPrice": int_to_hex(gas_price),
            "chainId": 0
        }
        self.testDaiJoinContract()
        self.log.info("Pass")
Exemplo n.º 5
0
 def __init__(self):
     super().__init__()
     self.nonce_map = {}
     self.default_account_key = default_config["GENESIS_PRI_KEY"]
     self.default_account_address = privtoaddr(self.default_account_key)
Exemplo n.º 6
0
    def run_test(self):
        priv_key = default_config["GENESIS_PRI_KEY"]
        sender = eth_utils.encode_hex(privtoaddr(priv_key))

        # deploy contract
        bytecode_file = os.path.join(
            os.path.dirname(os.path.realpath(__file__)), CONTRACT_PATH)
        assert (os.path.isfile(bytecode_file))
        bytecode = open(bytecode_file).read()
        _, contractAddr = self.deploy_contract(sender, priv_key, bytecode)
        self.log.info("contract deployed")

        contract_epoch = hex(self.rpc[FULLNODE0].epoch_number())

        # call method once
        receipt = self.call_contract(sender, priv_key, contractAddr,
                                     encode_hex_0x(keccak(b"foo()")))
        call_epoch = hex(self.rpc[FULLNODE0].epoch_number())

        # deploy another instance of the contract
        _, contractAddr2 = self.deploy_contract(sender, priv_key, bytecode)

        # call method multiple times
        for ii in range(0, NUM_CALLS - 3):
            self.call_contract(sender, priv_key, contractAddr,
                               encode_hex_0x(keccak(b"foo()")))

        # make sure we have enough blocks to be certain about the validity of previous blocks
        self.log.info("generating blocks...")
        for _ in range(50):
            self.generate_correct_block(FULLNODE0)

        self.log.info("syncing full nodes...")
        sync_blocks(self.nodes[FULLNODE0:FULLNODE1])

        # connect light node to full nodes
        connect_nodes(self.nodes, LIGHTNODE, FULLNODE0)
        connect_nodes(self.nodes, LIGHTNODE, FULLNODE1)

        # make sure we all nodes are in sync
        self.log.info("syncing light node...")
        sync_blocks(self.nodes[:])

        # retrieve contract code
        self.log.info("retrieving contract code...")
        self.check_code(contractAddr, contract_epoch)

        # apply filter, we expect a single log with 2 topics
        self.log.info("testing filter range...")
        self.check_filter(
            Filter(from_epoch="earliest", to_epoch=contract_epoch))
        self.check_filter(Filter(from_epoch="earliest", to_epoch=call_epoch))
        self.check_filter(Filter())
        self.check_filter(Filter(from_epoch="0x0", to_epoch="0x0"))

        # apply filter for specific block, we expect a single log with 3 topics
        self.check_filter(Filter(block_hashes=[receipt["blockHash"]]))

        # apply filter for specific topics
        self.log.info("testing filter topics...")
        self.check_filter(Filter(topics=[CONSTRUCTED_TOPIC]))
        self.check_filter(Filter(topics=[CALLED_TOPIC]))
        self.check_filter(Filter(topics=[None, self.address_to_topic(sender)]))
        self.check_filter(
            Filter(topics=[
                CALLED_TOPIC, None,
                [self.number_to_topic(3),
                 self.number_to_topic(4)]
            ]))

        # apply filter with limit
        self.log.info("testing filter limit...")
        self.check_filter(Filter(limit=("0x%x" % (NUM_CALLS // 2))))

        # apply filter for specific contract address
        self.log.info("testing address filtering...")
        self.check_filter(Filter(address=[contractAddr]))
        self.check_filter(Filter(address=[contractAddr2]))

        self.log.info("Pass")