本文整理汇总了Python中bigchaindb.Bigchain类的典型用法代码示例。如果您正苦于以下问题:Python Bigchain类的具体用法?Python Bigchain怎么用?Python Bigchain使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Bigchain类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
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
示例2: __init__
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
示例3: validate_transactions
def validate_transactions(self):
"""
Checks if the incoming transactions are valid
"""
# create a bigchain instance
b = Bigchain()
while True:
self.monitor.gauge('tx_queue_gauge',
self.q_tx_to_validate.qsize(),
rate=bigchaindb.config['statsd']['rate'])
tx = self.q_tx_to_validate.get()
# poison pill
if tx == 'stop':
self.q_tx_delete.put('stop')
self.q_tx_validated.put('stop')
return
self.q_tx_delete.put(tx['id'])
with self.monitor.timer('validate_transaction', rate=bigchaindb.config['statsd']['rate']):
is_valid_transaction = b.is_valid_transaction(tx)
if is_valid_transaction:
self.q_tx_validated.put(tx)
示例4: create_blocks
def create_blocks(self):
"""
Create a block with valid transactions
"""
# create a bigchain instance
b = Bigchain()
stop = False
while True:
# read up to 1000 transactions
validated_transactions = []
for i in range(1000):
try:
tx = self.q_tx_validated.get(timeout=5)
except queue.Empty:
break
# poison pill
if tx == 'stop':
stop = True
break
validated_transactions.append(tx)
# if there are no transactions skip block creation
if validated_transactions:
# create block
block = b.create_block(validated_transactions)
self.q_block.put(block)
if stop:
self.q_block.put('stop')
return
示例5: get_changefeed
def get_changefeed():
"""Create and return ordered changefeed of blocks starting from
last voted block"""
b = Bigchain()
last_block_id = b.get_last_voted_block().id
feed = backend.query.get_new_blocks_feed(b.connection, last_block_id)
return Node(feed.__next__, name='changefeed')
示例6: validate
def validate(self):
"""
Checks if incoming blocks are valid or not
"""
# create a bigchain instance. All processes should create their own bigchcain instance so that they all
# have their own connection to the database
b = Bigchain()
logger.info('voter waiting for new blocks')
while True:
new_block = self.q_blocks_to_validate.get()
# poison pill
if new_block == 'stop':
self.q_validated_block.put('stop')
return
logger.info('new_block arrived to voter')
block_number = self.v_previous_block_number.value + 1
validity = b.is_valid_block(new_block)
self.q_validated_block.put((new_block,
self.v_previous_block_id.value.decode(),
block_number,
validity))
self.v_previous_block_id.value = new_block['id'].encode()
self.v_previous_block_number.value = block_number
示例7: __init__
class Election:
"""Election class."""
def __init__(self, events_queue=None):
self.bigchain = Bigchain()
self.events_queue = events_queue
def check_for_quorum(self, next_vote):
"""Checks if block has enough invalid votes to make a decision
Args:
next_vote: The next vote.
"""
try:
block_id = next_vote['vote']['voting_for_block']
node = next_vote['node_pubkey']
except KeyError:
return
next_block = self.bigchain.get_block(block_id)
result = self.bigchain.block_election(next_block)
self.handle_block_events(result, block_id)
if result['status'] == self.bigchain.BLOCK_INVALID:
return Block.from_dict(next_block)
# Log the result
if result['status'] != self.bigchain.BLOCK_UNDECIDED:
msg = 'node:%s block:%s status:%s' % \
(node, block_id, result['status'])
# Extra data can be accessed via the log formatter.
# See logging.dictConfig.
logger_results.debug(msg, extra={
'current_vote': next_vote,
'election_result': result,
})
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
def handle_block_events(self, result, block_id):
if self.events_queue:
if result['status'] == self.bigchain.BLOCK_UNDECIDED:
return
elif result['status'] == self.bigchain.BLOCK_INVALID:
event_type = EventTypes.BLOCK_INVALID
elif result['status'] == self.bigchain.BLOCK_VALID:
event_type = EventTypes.BLOCK_VALID
event = Event(event_type, self.bigchain.get_block(block_id))
self.events_queue.put(event)
示例8: create_invalid_tx
def create_invalid_tx():
"""Create and return an invalid transaction.
The transaction is invalid because it's missing the signature."""
b = Bigchain()
tx = b.create_transaction(b.me, b.me, None, 'CREATE')
return tx
示例9: create_write_transaction
def create_write_transaction(tx_left):
b = Bigchain()
while tx_left > 0:
# use uuid to prevent duplicate transactions (transactions with the same hash)
tx = b.create_transaction(b.me, b.me, None, 'CREATE',
payload={'msg': str(uuid.uuid4())})
tx_signed = b.sign_transaction(tx, b.me_private)
b.write_transaction(tx_signed)
tx_left -= 1
示例10: bootstrap
def bootstrap(self):
"""
Before starting handling the new blocks received by the changefeed we need to handle unvoted blocks
added to the bigchain while the process was down
We also need to set the previous_block_id.
"""
b = Bigchain()
last_voted = b.get_last_voted_block()
self.v_previous_block_id.value = last_voted['id'].encode()
示例11: mint_coin
def mint_coin(user_pk):
b = Bigchain()
coin_id = uuid.uuid4()
for i in range(10):
payload = {
'coin_id': str(coin_id),
'coin_share': str(i)
}
tx = b.create_transaction(b.me, user_pk, None, 'CREATE', payload=payload)
tx_signed = b.sign_transaction(tx, b.me_private)
b.write_transaction(tx_signed)
print('MINT {} {} {}'.format(tx['id'], coin_id, i))
示例12: transfer_coin
def transfer_coin(user_vk, user_sk, coin_id):
b = Bigchain()
coins = get_owned_coins(user_vk)
if coin_id in coins.keys():
for tx in coins[coin_id]:
tx_input = {'txid': tx['id'], 'cid': 0}
tx_transfer = b.create_transaction(user_vk, b.me, tx_input, 'TRANSFER',
payload=tx['transaction']['data']['payload'])
tx_transfer_signed = b.sign_transaction(tx_transfer, user_sk)
b.write_transaction(tx_transfer_signed)
print('TRANSFER {} {} {}'.format(tx_transfer_signed['id'],
coin_id, tx['transaction']['data']['payload']['coin_share']))
示例13: requeue_transactions
def requeue_transactions(self):
"""
Liquidates transactions from invalid blocks so they can be processed again
"""
while True:
invalid_block = self.q_invalid_blocks.get()
# poison pill
if invalid_block == 'stop':
logger.info('clean exit')
return
b = Bigchain()
for tx in invalid_block['block']['transactions']:
b.write_transaction(tx)
示例14: pay_royalties
def pay_royalties(label_vk, artist_vk, tx, label_share=7, artist_share=3):
b = Bigchain()
payload = tx['transaction']['data']['payload']
tx_input = {'txid': tx['id'], 'cid': 0}
if int(payload['coin_share']) < artist_share:
new_owner = artist_vk
else:
new_owner = label_vk
tx_royalties = b.create_transaction(b.me, new_owner, tx_input, 'TRANSFER', payload=payload)
tx_royalties_signed = b.sign_transaction(tx_royalties, b.me_private)
b.write_transaction(tx_royalties_signed)
print('ROYALTIES {} {} {} {}'.format(tx_royalties['id'], payload['coin_id'],
payload['coin_share'], new_owner))
示例15: write_blocks
def write_blocks(self):
"""
Write blocks to the bigchain
"""
# create bigchain instance
b = Bigchain()
# Write blocks
while True:
block = self.q_block.get()
# poison pill
if block == 'stop':
return
b.write_block(block)