示例#1
0
class Election:
    def __init__(self):
        self.bigchain = Bigchain()

    def check_for_quorum(self, next_vote):
        """
        Checks if block has enough invalid votes to make a decision
        """
        next_block = r.table('bigchain')\
            .get(next_vote['vote']['voting_for_block'])\
            .run(self.bigchain.conn)
        if self.bigchain.block_election_status(
                next_block) == self.bigchain.BLOCK_INVALID:
            return next_block

    def requeue_transactions(self, invalid_block):
        """
        Liquidates transactions from invalid blocks so they can be processed again
        """
        logger.info('Rewriting %s transactions from invalid block %s',
                    len(invalid_block['block']['transactions']),
                    invalid_block['id'])
        for tx in invalid_block['block']['transactions']:
            self.bigchain.write_transaction(tx)
        return invalid_block
示例#2
0
class Election:
    """Election class."""

    def __init__(self):
        self.bigchain = Bigchain()

    def check_for_quorum(self, next_vote):
        """
        Checks if block has enough invalid votes to make a decision

        Args:
            next_vote: The next vote.

        """
        next_block = self.bigchain.get_block(
            next_vote['vote']['voting_for_block'])

        block_status = self.bigchain.block_election_status(next_block['id'],
                                                           next_block['block']['voters'])
        if block_status == self.bigchain.BLOCK_INVALID:
            return Block.from_dict(next_block)

    def requeue_transactions(self, invalid_block):
        """
        Liquidates transactions from invalid blocks so they can be processed again
        """
        logger.info('Rewriting %s transactions from invalid block %s',
                    len(invalid_block.transactions),
                    invalid_block.id)
        for tx in invalid_block.transactions:
            self.bigchain.write_transaction(tx)
        return invalid_block
示例#3
0
class Election:

    def __init__(self):
        self.bigchain = Bigchain()

    def check_for_quorum(self, next_vote):
        """
        Checks if block has enough invalid votes to make a decision
        """
        next_block = r.table('bigchain')\
            .get(next_vote['vote']['voting_for_block'])\
            .run(self.bigchain.conn)
        if self.bigchain.block_election_status(next_block) == self.bigchain.BLOCK_INVALID:
            return next_block

    def requeue_transactions(self, invalid_block):
        """
        Liquidates transactions from invalid blocks so they can be processed again
        """
        logger.info('Rewriting %s transactions from invalid block %s',
                    len(invalid_block['block']['transactions']),
                    invalid_block['id'])
        for tx in invalid_block['block']['transactions']:
            self.bigchain.write_transaction(tx)
        return invalid_block
示例#4
0
class Election:
    """Election class."""
    def __init__(self):
        self.bigchain = Bigchain()

    def check_for_quorum(self, next_vote):
        """
        Checks if block has enough invalid votes to make a decision

        Args:
            next_vote: The next vote.

        """
        next_block = self.bigchain.get_block(
            next_vote['vote']['voting_for_block'])

        block_status = self.bigchain.block_election_status(
            next_block['id'], next_block['block']['voters'])
        if block_status == self.bigchain.BLOCK_INVALID:
            return Block.from_dict(next_block)

    def requeue_transactions(self, invalid_block):
        """
        Liquidates transactions from invalid blocks so they can be processed again
        """
        logger.info('Rewriting %s transactions from invalid block %s',
                    len(invalid_block.transactions), invalid_block.id)
        for tx in invalid_block.transactions:
            self.bigchain.write_transaction(tx)
        return invalid_block
示例#5
0
def get():
    # Cryptographic Identities Generation
    alice, bob = generate_keypair(), generate_keypair()
    print(" ")
    # Digital Asset Definition (e.g. bicycle)
    asset = Asset(data={
        "bicycle": {
            "manufacturer": "bkfab",
            "serial_number": "abcd1234"
        }
    })

    # Metadata Definition
    metadata = {'planet': 'earth'}

    # create trnsaction  TODO : owners_before might be node_pubkey in v0.8.0
    tx = Transaction.create([alice.public_key], [alice.public_key],
                            metadata=metadata,
                            asset=asset)

    # sign with private key
    tx = tx.sign([alice.private_key])
    # get tx by id
    b = Bigchain()

    block = b.create_block([tx])
    block_voters = block.to_dict()['block']['voters']
    print(block_voters)
    tx_id = '2cb004cad29c0b79872646558f8c867a4c0aecbc4997f0917'
    tx = b.get_transaction(tx_id)
    print(
        "block status         : ",
        b.block_election_status(
            '2257384a0cee8cf98bd82c3142dd38eee5c268c124b5b357b773b8c6c1fa1221',
            block_voters))
示例#6
0
def bft():
    # Cryptographic Identities Generation
    alice, bob = generate_keypair(), generate_keypair()
    print(" ")
    # Digital Asset Definition (e.g. bicycle)
    asset = Asset(data={
        "bicycle": {
            "manufacturer": "bkfab",
            "serial_number": "abcd1234"
        }
    })

    # Metadata Definition
    metadata = {'planet': 'earth'}

    # create trnsaction  TODO : owners_before might be node_pubkey in v0.8.0
    tx = Transaction.create([alice.public_key], [([alice.public_key], 1)],
                            metadata=metadata,
                            asset=asset)

    # sign with private key
    tx = tx.sign([alice.private_key])
    tx_id = tx.to_dict()['id']
    print("tx_id                       : ", tx_id)

    # create block
    b = Bigchain()
    block = b.create_block([tx])

    print("valid block       timestamp : ",
          block.to_dict()['block']['timestamp'])
    # tamper block
    block.timestamp = '1'
    print("tamper block.timestamp to 1 : ")
    block_id = block.to_dict()['id']
    block_voters = block.to_dict()['block']['voters']

    print("invalid block     timestamp : ",
          block.to_dict()['block']['timestamp'])
    print("tamper_block_id             : ", block_id)

    print("db response of block        : ", b.write_block(block))
    sleep(0)

    last_voted_id = b.get_last_voted_block().id
    vote = b.vote(block_id, last_voted_id, True)
    print("crate vote 'True'           : ", vote)
    print("db response of vote         : ", b.write_vote(vote))

    print("tamper_block status         : ",
          b.block_election_status(block_id, block_voters))
    print("blocks_status_containing_tx : ",
          b.get_blocks_status_containing_tx(tx_id))
    print("wait for 20 sec             : ")
    sleep(20)
    print("blocks_status_containing_tx : ",
          b.get_blocks_status_containing_tx(tx_id))
    print(" ")
示例#7
0
    def check_for_quorum(self):
        """
        Checks if block has enough invalid votes to make a decision
        """
        b = Bigchain()

        while True:
            next_block = self.q_block_new_vote.get()

            # poison pill
            if next_block == 'stop':
                self.q_invalid_blocks.put('stop')
                logger.info('clean exit')
                return

            if b.block_election_status(next_block) == 'invalid':
                self.q_invalid_blocks.put(next_block)
示例#8
0
    def check_for_quorum(self):
        """
        Checks if block has enough invalid votes to make a decision
        """
        b = Bigchain()

        while True:
            next_block = self.q_block_new_vote.get()

            # poison pill
            if next_block == 'stop':
                self.q_invalid_blocks.put('stop')
                logger.info('clean exit')
                return

            if b.block_election_status(next_block) == 'invalid':
                self.q_invalid_blocks.put(next_block)
示例#9
0
def tamper_block():
    # Cryptographic Identities Generation
    alice, bob = generate_keypair(), generate_keypair()
    print(" ")
    # Digital Asset Definition (e.g. bicycle)
    asset = Asset(data={
        "bicycle": {
            "manufacturer": "bkfab",
            "serial_number": "abcd1234"
        }
    })

    # Metadata Definition
    metadata = {'planet': 'earth'}

    # create trnsaction  TODO : owners_before might be node_pubkey in v0.8.0
    tx = Transaction.create([alice.public_key], [([alice.public_key], 1)],
                            metadata=metadata,
                            asset=asset)

    # sign with private key
    tx = tx.sign([alice.private_key])
    tx_id = tx.to_dict()['id']
    print("tx_id                       : ", tx_id)

    # create block
    b = Bigchain()
    block = b.create_block([tx])
    block_id = block.to_dict()['id']
    block_voters = block.to_dict()['block']['voters']
    print("last_block_id    sig        : ", block_id, block.signature)
    print(block.to_dict())
    # tamper block sig
    block.sign(alice.private_key)
    block_dict = block.to_dict()
    print("tamper_block_id  sig        : ", block_dict['id'],
          block_dict['signature'])
    print(block_dict)
    print("db response                 : ",
          b.backend.write_block(serialize(block_dict)))
    sleep(delay)
    print("tamper_block status         : ",
          b.block_election_status(block_dict['id'], block_voters))
    print("blocks_status_containing_tx : ",
          b.get_blocks_status_containing_tx(tx_id))
    print(" ")
from bigchaindb import Bigchain
import rethinkdb as r

b = Bigchain()
block_list = b.get_BlockIdList(None, None)
block_list = list(block_list)

for block in block_list:
    block_voter = b.connection.run(
        r.table('bigchain').get(block))['block']['voters']
    votes = list(b.backend.get_votes_by_block_id(block))
    vote_cast = [vote['vote']['is_block_valid'] for vote in votes]
    block_status = b.block_election_status(block, block_voter)
    print(block, vote_cast, block_status, "\n", votes, "\n")