예제 #1
0
    def run_test(self):
        """Main test logic"""

        self.log.info("Starting test!")
        [n0, n1, n2] = self.nodes  # n2 is the erasing node

        self.log.info("Generate a few blocks upfront to make sure pruning kicks in.")
        # On pruning, we must have a chain longer than PruneAfterHeight and bigger than 550 MiB.
        mine_large_blocks(n0, nblocks=200)
        self.nodes[0].generate(nblocks=200)

        self.log.info("Build a \"bad\" transaction.")
        bad_data = 'n42MaFLwantedToTestThisKYP112MM9jE'
        tx_bad = n1.createrawtransaction([], {bad_data: 0.001})
        (tx_bad, txid_bad) = fund_sign_send(n1, tx_bad)  # also adds inputs and change output
        tx_bad_vouts = n1.decoderawtransaction(tx_bad)['vout']

        self.log.info("Add tx to a block, mine a few big blocks on top.")
        self.sync_all()
        block_hash_bad = n0.generate(nblocks=1)[0]
        # significantly lower nblocks might cause pruning not to work (needs changes to bitcoind pruning logic)
        mine_large_blocks(n0, nblocks=300)
        self.nodes[0].generate(nblocks=300)
        self.sync_all()

        erase_target = {block_hash_bad: {txid_bad: list(range(len(tx_bad_vouts)))}}

        self.log.info("Assert that node 2 serves the tx via RPC.")
        assert_equal(bytes_to_hex_str(hash256(hex_str_to_bytes(n2.getrawtransaction(txid_bad)))), txid_bad)

        self.log.info("Assert that node 2 serves the block with the tx via P2P.")
        n2.add_p2p_connection(P2PInterface())
        n2.p2p.send_message(msg_getdata(inv=[CInv(2, int(block_hash_bad, 16))]))
        n2.p2p.wait_for_block(int(block_hash_bad, 16), timeout=1)

        self.log.info("Stopping node 2.")
        self.stop_node(2)

        self.log.info("Assert that UTXOs not erased according to tool.")
        assert_equal(tool.check(erase_target, n2.datadir, 'regtest'), False)

        def react_to_ui_request(request):
            assert("enable pruning" in request)
            self.log.info("Configuring node 2 to enable pruning.")
            append_config(n2.datadir, ["prune=1"])

            assert("start your node" in request)
            self.log.info("Starting node 2.")
            self.start_node(2)
            connect_nodes_bi(self.nodes, 0, 2)

        self.log.info("Erasing using tool.")
        tool.interactive_erase(erase_target, n2.datadir, 'regtest', self.log.info, react_to_ui_request)

        self.log.info("Assert that the tx's block can't be obtained from node 2 via P2P anymore.")
        n2.add_p2p_connection(P2PInterface())
        n2.p2p.send_message(msg_getdata(inv=[CInv(2, int(block_hash_bad, 16))]))
        assert_raises(AssertionError, n2.p2p.wait_for_block, int(block_hash_bad, 16), timeout=1)

        self.log.info("Assert that tx is different now when obtained from node 2 via RPC.")
        assert_raises_rpc_error(-5, None, n2.getrawtransaction, txid_bad)

        self.log.info("Assert that node 2 accepts new blocks.")
        n0.generate(nblocks=1)
        sync_blocks(self.nodes, timeout=1)

        self.log.info("Spend one output of the bad tx, include that in block.")
        tx_bad_vout = [x for x in n1.listunspent() if x['txid'] == txid_bad][0]
        tx_ok = n1.createrawtransaction([tx_bad_vout], {n0.getnewaddress(): 0.5})
        (tx_ok, txid_ok) = fund_sign_send(n1, tx_ok)
        sync_mempools([n0, n1])
        n0.generate(nblocks=1)

        self.log.info("Assert that node 2 accepts the resulting transaction and block.")
        sync_blocks(self.nodes, timeout=1)
        assert_equal(bytes_to_hex_str(hash256(hex_str_to_bytes(n2.getrawtransaction(txid_ok)))), txid_ok)

        self.log.info("Wait for all nodes to sync again, just in case. Should complete immediately.")
        self.sync_all()

        self.log.info("Stopping node 2 (again).")
        self.stop_node(2)

        self.log.info("Assert that UTXOs are erased according to tool.")
        assert_equal(tool.check(erase_target, n2.datadir, 'regtest'), True)
예제 #2
0
#! /user/bin/env python
# -*- utf-8 -*-

import zmq
import sys
from tool import check
context = zmq.Context()

#  Socket to talk to server
print("Connecting to message server...")
socket = context.socket(zmq.SUB)
socket.setsockopt(zmq.RECONNECT_IVL,100)
socket.setsockopt(zmq.RECONNECT_IVL_MAX,500)
socket.connect("tcp://localhost:5555")

socket_market = context.socket(zmq.REQ)
socket_market.connect("tcp://localhost:5556")

socket.setsockopt_string(zmq.SUBSCRIBE,'')  # 消息过滤

while True:
    response = socket.recv()
    response = check(response)
    if response:
        print('Part two received pol {} market query.'.format(response['id']))
        response['part'] = 'two'
        response['result'] = {'two': 'world'}
        socket_market.send_json(response)
        response_market = socket_market.recv_json()
        print(response_market)
예제 #3
0
Created on 2015-10-13
收到请求后回复world
@author: kwsy2015
'''
import zmq
import time
from tool import check

context = zmq.Context()
socket = context.socket(zmq.REP)
# REP连接的是DEALER
socket.connect("tcp://localhost:5560")

while True:
    message = socket.recv_json()
    message = check(message)
    if message:
        print("Received pol {} deal request ...".format(
            message['id'], message))
        time.sleep(1.5)
        message['deal_flag'] = 1
        message['deal_info'] = {
            'sell': {
                '000001': {
                    'price': 12,
                    'number': 200
                }
            },
            'buy': {
                '390010': {
                    'price': 32.56,