Esempio n. 1
0
 def run(type_op):
     highest_block = self.blockchain.get_highest_block()
     accounts, txs, validators, contract_accounts = self.blockchain.get_state(
         Block.calc_hash(highest_block))
     new_state, shard_num = Transaction.run_contract(contract_accounts[from_vk], vk, type_op)
     new_tx = Transaction(0, vk, from_vk, 0, 0, shard_num=shard_num)
     new_tx.new_state = new_state
     publish(new_tx)
Esempio n. 2
0
 def run_contract(self):
     accounts, txs, validators, contract_accounts = self.blockCallBack.blockchain.get_state(
         Block.calc_hash(self.blockCallBack.blockchain.get_highest_block()))
     if self.vk.to_string().hex() in contract_accounts:
         tx = Transaction(0, self.vk.to_string().hex(), RUN_ADDR, 0, 0)
         self.txCallBack.publish(tx)
         return True
     return False
Esempio n. 3
0
    def message(self, pubnub, message):
        Logger.pubnub_log(message)
        tx = Transaction.from_dict(message.message)
        # if tx.verify_sign():
        self.blockchain.tx_pool_sem.acquire()
        self.tx_pool.add(tx)
        self.blockchain.tx_pool_sem.release()
        if tx.to_vk == RUN_ADDR:
            highest_block = self.blockchain.get_highest_block()
            accounts, txs, validators, contract_accounts = self.blockchain.get_state(Block.calc_hash(highest_block))
            if tx.from_vk in contract_accounts and self.vk in validators:
                def f(contract_accounts, vk, publish, from_vk):
                    def check_completion(type_op):
                        # type_op is either "m", "s" or "r"
                        Logger.p("Checking completion," + type_op)
                        highest_block = self.blockchain.get_highest_block()
                        accounts, txs, validators, contract_accounts = self.blockchain.get_state(
                            Block.calc_hash(highest_block))
                        state = contract_accounts[tx.from_vk].state
                        print(state)
                        for elm in state:
                            if (type(elm) == tuple or type(elm) == list) and elm[1] == type_op:
                                continue
                            else:
                                return False
                        return True

                    def run(type_op):
                        highest_block = self.blockchain.get_highest_block()
                        accounts, txs, validators, contract_accounts = self.blockchain.get_state(
                            Block.calc_hash(highest_block))
                        new_state, shard_num = Transaction.run_contract(contract_accounts[from_vk], vk, type_op)
                        new_tx = Transaction(0, vk, from_vk, 0, 0, shard_num=shard_num)
                        new_tx.new_state = new_state
                        publish(new_tx)

                    run("m")
                    while not check_completion("m"):
                        time.sleep(SLEEP_BETWEEN_COMPLETION_CHECK)
                    run("s")
                    while not check_completion("s"):
                        time.sleep(SLEEP_BETWEEN_COMPLETION_CHECK)
                    run("r")

                Logger.p("Creating and starting new thread")
                Thread(target=f, args=(contract_accounts, self.vk, self.publish, tx.from_vk)).start()
            else:
                Logger.p("Tx has RUN_ADDR but not a contract account or you are not in validator set")
        else:
            Logger.p("Tx does not have RUN_ADDR")
        Logger.p("signature verified, tx added to tx pool")
Esempio n. 4
0
 def check_completion(type_op):
     # type_op is either "m", "s" or "r"
     Logger.p("Checking completion," + type_op)
     highest_block = self.blockchain.get_highest_block()
     accounts, txs, validators, contract_accounts = self.blockchain.get_state(
         Block.calc_hash(highest_block))
     state = contract_accounts[tx.from_vk].state
     print(state)
     for elm in state:
         if (type(elm) == tuple or type(elm) == list) and elm[1] == type_op:
             continue
         else:
             return False
     return True
Esempio n. 5
0
 def get_complete_state(self):
     # print(self.blockCallBack.blockchain)
     # print(self.txCallBack.tx_pool)
     accounts, applied_txs, validators, contract_accounts = self.blockCallBack.blockchain.get_state(
         Block.calc_hash(self.blockCallBack.blockchain.get_highest_block()))
     return accounts, applied_txs, validators, contract_accounts