Exemple #1
0
    def __init__(self, initial_text=None):
        '''
        Document helper class to make interaction with the Infinote library
        a little bit easier. Each collaboratively edited document can be
        instantiated as an InfinoteDocument
        :param str initial_text: Start this document with initial text
        '''
        self.log = []
        self._state = State()

        if initial_text is not None:
            self.try_insert([1, '', 0, str(initial_text)])
Exemple #2
0
    def __init__(self, initial_text=None):
        '''
        Document helper class to make interaction with the Infinote library
        a little bit easier. Each collaboratively edited document can be
        instantiated as an InfinoteDocument
        :param str initial_text: Start this document with initial text
        '''
        self.log = []
        self._state = State()

        if initial_text is not None:
            self.insert([1, '', 0, str(initial_text)])
Exemple #3
0
class InfinoteDocument(object):

    def __init__(self, initial_text=None):
        '''
        Document helper class to make interaction with the Infinote library
        a little bit easier. Each collaboratively edited document can be
        instantiated as an InfinoteDocument
        :param str initial_text: Start this document with initial text
        '''
        self.log = []
        self._state = State()

        if initial_text is not None:
            self.try_insert([1, '', 0, str(initial_text)])

    def insert(self, params):
        '''
        try to perform an insert operation and add the operation to the log
        :param list params: [user, vector, position, text]
        '''
        segment = Segment(params[0], params[3])
        buffer = Buffer([segment])
        operation = Insert(params[2], buffer)
        request = DoRequest(params[0], Vector(params[1]), operation)
        if self._state.canExecute(request):
            self._state.execute(request)
            self.log.append(["i", tuple(params)])

    def delete(self, params):
        '''
        try to perform a delete operation and add the operation to the log
        :param list params: [user, vector, position, text_length]
        '''
        operation = Delete(params[2], params[3])
        request = DoRequest(params[0], Vector(params[1]), operation)
        if self._state.canExecute(request):
            self._state.execute(request)
            self.log.append(["d", tuple(params)])

    def undo(self, params):
        '''
        try to perform an undo operation and add the operation to the log
        :param list params: [user]
        '''
        request = UndoRequest(params[0], self._state.vector)
        if self._state.canExecute(request):
            self._state.execute(request)
            self.log.append(["u", tuple(params)])

    def sync(self):
        '''
        Synchronize a document based on it's ot-log
        '''
        for log in self.log:
            if log[0] == 'i':
                self.try_insert(log[1])
            elif log[0] == 'd':
                self.try_delete(log[1])
            elif log[0] == 'u':
                self.try_undo(log[1])

    def get_log(self, limit=None):
        """
        Get the document's ot-log
        :param int limit: Specify a limit on the amount of log-operations
        to replay on the client
        :return: tuple - The document's log of operations
        """
        if limit != None:
            if len(self.log) >= limit:
                return (limit, self.log[-limit:])
            else:
                return (limit, self.log)
        else:
            return (limit, self.log)

    def _state(self):
        '''
        Return this document's current state
        :return: tuple - The document's current state
        '''
        return (self._state.vector.toString(), self._state.buffer.toString())

    state = property(_state)
Exemple #4
0
def generate_valid_source_tx(
        s: protocol.State,
        con: AuthServiceProxy,
        max_tx: int
    ) -> None:
    # Transmit enough funds to addresses so that we won't need
    # to use coinbase transactions.
    # There are at most MAX many transactions in one step. Hence,
    # we need at most that many different addresses. (We can always
    # use the change addresses because our transactions have nearly
    # no costs)

    num_addr = max_tx // 10  # We want at most 50 advertisements to use the
    # same address
    s.source_tx = []

    start = con.getblockcount() + 1
    con.generate(num_addr + 101)
    top = con.getblockcount()

    interval = range(start, top - 100)
    block_hashes = con.batch_([["getblockhash", h] for h in interval])
    blocks = con.batch_([["getblock", ha, 2] for ha in block_hashes])
    del block_hashes
    txs = [block['tx'][0] for block in blocks]
    del blocks

    sent_txs = []
    i = 0
    value = -1
    txid = -1
    n = -1
    for tx in txs:
        for out in tx['vout']:
            if out['scriptPubKey']['type'] == 'pubkey':
                # The pubkey transactions are coinbase transactions because
                value = out['value']
                txid = tx['txid']
                n = out['n']
        if value == -1 or txid == -1 or n == -1:
            raise RuntimeError("No coinbase transaction found.")
        addr = con.getnewaddress()
        sent_value = float(value) / 2  # create two addresses per transaction
        raw_tx = con.createrawtransaction([{'txid': txid, 'vout': n}], {
            addr: sent_value})  # The - is for the fees
        funded_tx = con.fundrawtransaction(raw_tx)
        signed_tx = con.signrawtransactionwithwallet(funded_tx["hex"])
        if signed_tx["complete"] is False:
            raise RuntimeError(
                "bitcoind could not sign the transaction. (During "
                "Initialization)")
        txid = con.sendrawtransaction(signed_tx["hex"])
        sent_txs.append(txid)

        i += 1

        # Create a block each 100 transactions
        if i == 100:
            con.generate(1)
            i = 0

    con.generate(1)

    txs = con.batch_([['getrawtransaction', txid, 1] for txid in sent_txs])
    for tx in txs:
        for out in tx['vout']:
            vout = out['n']
            s.source_tx.append(opreturn.Transaction(tx['txid'], vout))
    c = 0
    for utxo in s.source_tx:
        if not protocol.is_valid_utxo(utxo):
            c += 1
    print("Found %d invalid utxos." % c)
    STEP = args.STEP
    PORT = args.port if args.port is not None else 18443
    State.PORT = PORT
    REPS = args.reps if args.reps is not None else 100  # Number of
    # repetitions per config
    NUM_PEERS = args.peers if args.peers is not None else 10000  # total
    # number of peers

    PROTOCOL = bytes([0x01])  # Protocol used for ads

    # Connect to Daemon
    con = AuthServiceProxy(
        "http://%s:%[email protected]:%s" %
        (State.RPC_USER, State.RPC_PASSWORD, PORT), )
    # Create state in which each block can contain advertisements.
    s = State(1, 1, 0)

    # Load peers
    try:
        peers = get_peers(NUM_PEERS)
    except FileNotFoundError:
        print(
            "File peers/peers_%d.pyc does not exist. Execute 'anonboot/generate_peers.py %d'."
            % (NUM_PEERS, NUM_PEERS))
        sys.exit()

    # Fix problem with wrong format of IPs
    for p in peers:
        if type(p.ip_address) is str:
            p.ip_address = ipaddress.ip_address(p.ip_address)
Exemple #6
0
class InfinoteDocument(object):

    def __init__(self, initial_text=None):
        '''
        Document helper class to make interaction with the Infinote library
        a little bit easier. Each collaboratively edited document can be
        instantiated as an InfinoteDocument
        :param str initial_text: Start this document with initial text
        '''
        self.log = []
        self._state = State()

        if initial_text is not None:
            self.insert([1, '', 0, str(initial_text)])

    def insert(self, params):
        '''
        try to perform an insert operation and add the operation to the log
        :param list params: [user, vector, position, text]
        '''
        segment = Segment(params[0], params[3])
        buffer = Buffer([segment])
        operation = Insert(params[2], buffer)
        request = DoRequest(params[0], Vector(params[1]), operation)
        if self._state.canExecute(request):
            self._state.execute(request)
            self.log.append(["i", tuple(params)])

    def delete(self, params):
        '''
        try to perform a delete operation and add the operation to the log
        :param list params: [user, vector, position, text_length]
        '''
        operation = Delete(params[2], params[3])
        request = DoRequest(params[0], Vector(params[1]), operation)
        if self._state.canExecute(request):
            self._state.execute(request)
            self.log.append(["d", tuple(params)])

    def undo(self, params):
        '''
        try to perform an undo operation and add the operation to the log
        :param list params: [user]
        '''
        request = UndoRequest(params[0], self._state.vector)
        if self._state.canExecute(request):
            self._state.execute(request)
            self.log.append(["u", tuple(params)])

    def sync(self):
        '''
        Synchronize a document based on it's ot-log
        '''
        for log in self.log:
            if log[0] == 'i':
                self.try_insert(log[1])
            elif log[0] == 'd':
                self.try_delete(log[1])
            elif log[0] == 'u':
                self.try_undo(log[1])

    def get_log(self, limit=None):
        """
        Get the document's ot-log
        :param int limit: Specify a limit on the amount of log-operations
        to replay on the client
        :return: tuple - The document's log of operations
        """
        if limit != None:
            if len(self.log) >= limit:
                return (limit, self.log[-limit:])
            else:
                return (limit, self.log)
        else:
            return (limit, self.log)

    def _state(self):
        '''
        Return this document's current state
        :return: tuple - The document's current state
        '''
        return (self._state.vector.toString(), self._state.buffer.toString())

    state = property(_state)
Exemple #7
0
    errors = 0
    last_success = 0
    for i in range(5100, 5300, 1):
        if errors >= 2:
            # 2 consecutive fails occurred
            write("**********************************")
            write(
                "The largest block that could be mined contained {} "
                "TXs.".format(
                    i - 2))
            write("**********************************")
            break
        try:
            ads = []
            tmp_state = State(1, 1, 0)
            for p in peers[:i]:
                ads.append(p.advertise(tmp_state))
            print("Attempt to write {} advertisements".format(len(ads)))
            con = connect()
            for ad in ads:
                input = inputs.popleft()
                raw = con.createrawtransaction([input['in']], [{'data': ad}])
                funded = con.fundrawtransaction(raw, {
                    "changeAddress": input['addr']})
                signed = con.signrawtransactionwithwallet(funded['hex'])
                sent = con.sendrawtransaction(signed['hex'])
                raw = con.getrawtransaction(sent, 1)
                vout = -1
                new_value = input['amount'] - 1
                for v in raw['vout']: