Exemplo n.º 1
0
    def processor_trans_make(self, content):
        i_ = 0
        # add_from = random.randint(0, 1)
        add_from = 0
        if add_from > 2:
            add_from = 0
        add_to = 0
        add_to_two = random.randint(0, 1)
        _address = [add_from, add_to,  add_to_two]
        result = dict()
        # print(_address)
        fd = open('ad1.txt', 'r')
        line = fd.readlines()
        for i in _address:
            line_ = line[i].rstrip()
            pr, pu = line_.split('ENDDING')
            temp = bytes(pu[2:-1], encoding='utf-8')
            temp = temp.replace(b'\r\n', b'\n')
            public_key = temp.replace(b'\\n', b'\n')
            temp = bytes(pr[2:-1], encoding='utf-8')
            temp = temp.replace(b'\r\n', b'\n')
            private_key = temp.replace(b'\\n', b'\n')
            sha = hashlib.sha256()
            sha.update(public_key)
            public_key_hash = sha.digest()
            result[i] = [public_key, private_key, public_key_hash]
        fd.close()

        for utxo in self.server.blockchain.utxo.utxo.items():
            if utxo[1]['to'] == result[_address[0]][2] and utxo[0] not in self.server.Used:
                self.server.Used.append(utxo[0])
                # print('1')
                i_ = 1
                private_key = serialization.load_pem_private_key(result[_address[0]][1], None,
                                                                 backend=default_backend())
                ipt = TransInput([utxo[0]], result[_address[0]][2])
                opt = TransOutput([(utxo[1]['amount']/2, result[_address[1]][2]), (utxo[1]['amount']/2,
                                                                                   result[_address[2]][2])])
                tran = Transaction(ipt, opt)
                tran.ready(private_key)
                content = trans_to_json(tran)
                requests.post('http://127.0.0.1:23390/transaction_post', data=content)
                requests.post('http://127.0.0.1:23391/transaction_post', data=content)
                if result[_address[0]][2] in self.server.Address.keys():
                    self.server.Address[result[_address[0]][2]][0] -= len(tran.b) \
                                                                          * self.server.throughput / (1000 * 2 * 4)
                    self.server.Address[result[_address[0]][2]][1] = time.time()
                else:
                    self.server.Address[result[_address[0]][2]] = [100, time.time()]
                _ = send_handler(MsgType.TYPE_RESPONSE_OK, tran.b)

                self.request.sendall(_)

                break
        if i_ == 0:
            # print('0')
            _ = send_handler(MsgType.TYPE_RESPONSE_ERROR, b'')
            self.request.sendall(_)
        else:
            pass
Exemplo n.º 2
0
    def processor_trans_make(self, content):
        i_ = 0
        add_from = 0
        add_to = 2
        add_to_two = random.randint(0, 1)
        _address = [add_from, add_to,  add_to_two]
        result = dict()
        # print(_address)
        fd = open('ad1.txt', 'r')
        line = fd.readlines()
        for i in _address:
            line_ = line[i].rstrip()
            pr, pu = line_.split('ENDDING')
            temp = bytes(pu[2:-1], encoding='utf-8')
            temp = temp.replace(b'\r\n', b'\n')
            public_key = temp.replace(b'\\n', b'\n')
            temp = bytes(pr[2:-1], encoding='utf-8')
            temp = temp.replace(b'\r\n', b'\n')
            private_key = temp.replace(b'\\n', b'\n')
            sha = hashlib.sha256()
            sha.update(public_key)
            public_key_hash = sha.digest()
            result[i] = [public_key, private_key, public_key_hash]
        fd.close()

        for utxo in self.server.macro_chain.utxo.utxo.items():
            if utxo[1]['to'] == result[_address[0]][2] and utxo[0] not in self.server.Used:
                self.server.Used.append(utxo[0])
                # print('utxo', utxo)
                i_ = 1
                private_key = serialization.load_pem_private_key(result[_address[0]][1], None,
                                                                 backend=default_backend())
                ipt = TransInput([utxo[0]], result[_address[0]][2])
                opt = TransOutput([(utxo[1]['amount']/2, result[_address[1]][2]), (utxo[1]['amount']/2,
                                                                                   result[_address[2]][2])])
                tran = Transaction(ipt, opt)
                tran.ready(private_key)
                content = trans_to_json(tran)
                requests.post('http://129.211.110.239:8000/transaction_post', data=content)
                requests.post('http://129.211.112.165:8000/transaction_post', data=content)
                requests.post('http://129.211.108.22:8000/transaction_post', data=content)
                requests.post('http://129.211.107.98:8000/transaction_post', data=content)
                requests.post('http://129.211.112.210:8000/transaction_post', data=content)
                requests.post('http://212.129.128.25:8000/transaction_post', data=content)
                requests.post('http://129.211.112.110:8000/transaction_post', data=content)
                requests.post('http://129.211.108.179:8000/transaction_post', data=content)
                requests.post('http://129.211.112.247:8000/transaction_post', data=content)
                requests.post('http://212.64.102.86:8000/transaction_post', data=content)
                _ = send_handler(MsgType.TYPE_RESPONSE_OK, tran.b)
                self.request.sendall(_)
                break
        if i_ == 0:
            _ = send_handler(MsgType.TYPE_RESPONSE_ERROR, b'')
            self.request.sendall(_)
        else:
            pass
Exemplo n.º 3
0
    def __get_trans(self) -> List[Transaction]:

        with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as s:
            s.connect(self.chainbase_address)
            s.sendall(send_handler(MsgType.TYPE_TRANS_READ, b''))
            *_, msgtype, content = recv_parser(s)

            trans = []

            if msgtype == MsgType.TYPE_RESPONSE_OK:
                trans += batch_parser(content)

            private_key = ec.generate_private_key(ec.SECP256K1,
                                                  default_backend())
            public_key = private_key.public_key()
            public_key = public_key.public_bytes(
                Encoding.DER, PublicFormat.SubjectPublicKeyInfo)
            sha = hashlib.sha256()
            sha.update(public_key)
            public_key_hash = sha.digest()
            ipt = TransInput([(TXID(public_key_hash), OUTPUT_INDEX(0))],
                             public_key_hash)

            opt = TransOutput([(ASSET(20),
                                PUBLIC_KEY_HASH(miner_public_key_hash))])
            tran = Transaction(ipt, opt, 0)
            tran.ready(private_key)
            trans.append(tran.b)
            content = trans_to_json(tran)
            requests.post('http://127.0.0.1:23390/transaction_post',
                          data=content)
            if self.block_num < -50:
                self.get_miner_credit(miner_public_key_hash,
                                      0.5 / self.block_num)
            else:
                self.get_miner_credit(miner_public_key_hash, 0.5)
            # print(struct.unpack('=d', result[0]), struct.unpack('=d', result[1]))
            return [Transaction.unpack(t) for t in trans]