本文整理汇总了Python中bigchaindb.Bigchain.write_transaction方法的典型用法代码示例。如果您正苦于以下问题:Python Bigchain.write_transaction方法的具体用法?Python Bigchain.write_transaction怎么用?Python Bigchain.write_transaction使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类bigchaindb.Bigchain
的用法示例。
在下文中一共展示了Bigchain.write_transaction方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from bigchaindb import Bigchain [as 别名]
# 或者: from bigchaindb.Bigchain import write_transaction [as 别名]
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__
# 需要导入模块: from bigchaindb import Bigchain [as 别名]
# 或者: from bigchaindb.Bigchain import write_transaction [as 别名]
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: __init__
# 需要导入模块: from bigchaindb import Bigchain [as 别名]
# 或者: from bigchaindb.Bigchain import write_transaction [as 别名]
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)
示例4: create_write_transaction
# 需要导入模块: from bigchaindb import Bigchain [as 别名]
# 或者: from bigchaindb.Bigchain import write_transaction [as 别名]
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
示例5: mint_coin
# 需要导入模块: from bigchaindb import Bigchain [as 别名]
# 或者: from bigchaindb.Bigchain import write_transaction [as 别名]
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))
示例6: transfer_coin
# 需要导入模块: from bigchaindb import Bigchain [as 别名]
# 或者: from bigchaindb.Bigchain import write_transaction [as 别名]
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']))
示例7: requeue_transactions
# 需要导入模块: from bigchaindb import Bigchain [as 别名]
# 或者: from bigchaindb.Bigchain import write_transaction [as 别名]
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)
示例8: pay_royalties
# 需要导入模块: from bigchaindb import Bigchain [as 别名]
# 或者: from bigchaindb.Bigchain import write_transaction [as 别名]
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))
示例9: ChainQuery
# 需要导入模块: from bigchaindb import Bigchain [as 别名]
# 或者: from bigchaindb.Bigchain import write_transaction [as 别名]
class ChainQuery(object):
def __init__(
self, host=None, port=None, dbname=None, pub_key=None, priv_key=None, keyring=[], consensus_plugin=None
):
self.host = host
self.port = port
self.dbname = dbname
self.conn = r.connect(host=host, port=port, db=dbname)
self.bigchain = Bigchain(
host=host,
port=port,
dbname=dbname,
public_key=pub_key,
private_key=priv_key,
keyring=keyring,
consensus_plugin=consensus_plugin,
)
# test
def test(self):
tables = r.db("bigchain").table_list().run(self.conn)
# print(tables)
return tables
# create key_pair for user
def generate_key_pair(self):
return crypto.generate_key_pair()
# create asset
def create_asset(self, public_key, digital_asset_payload):
tx = self.bigchain.create_transaction(
self.bigchain.me, public_key, None, "CREATE", payload=digital_asset_payload
)
tx_signed = self.bigchain.sign_transaction(tx, self.bigchain.me_private)
return self.bigchain.write_transaction(tx_signed)
# get transaction by payload_uuid
def getTxid_by_payload_uuid(self, payload_uuid):
cursor = (
r.table("bigchain")
.get_all(payload_uuid, index="payload_uuid")
.pluck({"block": {"transactions": "id"}})
.run(self.conn)
)
transactions = list(cursor)
return transactions
# get transaction by payload
def getTxid_by_payload(self, payload):
pass
# get currentowner of a payload(assert)
def getOwnerofAssert(self, payload):
return
# get one's assert
def get_owned_asserts(self, pub_key):
return
# if tx contains someone
def tx_contains_one(self, tx, one_pub):
for condition in tx["conditions"]:
if one_pub in condition["new_owners"]:
return True
for fullfillment in tx["fulfillments"]:
if one_pub in fullfillment["current_owners"]:
return True
# transfer assert to another, old_owner create this transaction,so need old_owner's pub/priv key.
def transfer_assert(self, old_owner_pub, old_owner_priv, new_owner_pub, tx_id):
tx_transfer = self.bigchain.create_transaction(old_owner_pub, new_owner_pub, tx_id, "TRANSFER")
tx_transfer_signed = self.bigchain.sign_transaction(tx_transfer, old_owner_priv)
# check if the transaction is valid
check = self.bigchain.is_valid_transaction(tx_transfer_signed)
if check:
self.bigchain.write_transaction(tx_transfer_signed)
else:
logger.info("this transaction is invalid.")
示例10: open
# 需要导入模块: from bigchaindb import Bigchain [as 别名]
# 或者: from bigchaindb.Bigchain import write_transaction [as 别名]
# "PRICE": "50" ,
# "QUANTITY": "50000" ,
# "SIDE": "S" ,
# "TYPE": "EQ"
# },
# ...]
data = []
with open('data.csv', 'r') as fid:
reader = csv.DictReader(fid)
for row in reader:
data.append(row)
# generate distinct users from the CPTY1 column and assign keys
users = []
for user_name in set([d['CPTY1'] for d in data]):
sk, vk = b.generate_keys()
user = users.append(
{
'name': user_name,
'signing_key': sk,
'verifying_key': vk
}
)
# create assets on the blockchain with the payload and assign to the user
for asset in data:
user = [u for u in users if u['name'] == asset['CPTY1']][0]
tx = b.create_transaction(b.me, user['verifying_key'], None, 'CREATE', payload=asset)
tx_signed = b.sign_transaction(tx, b.me_private)
b.write_transaction(tx_signed)
示例11: Bigchain
# 需要导入模块: from bigchaindb import Bigchain [as 别名]
# 或者: from bigchaindb.Bigchain import write_transaction [as 别名]
from bigchaindb import Bigchain
import writeout
import random
b = Bigchain()
random.seed(3)
for x in range(1,8):
# define a digital asset data payload
digital_asset_payload = {'choice': random.randint(0,100)}
# a create transaction uses the operation `CREATE` and has no inputs
priv, pub = writeout.importData("user"+str(x))
tx = b.create_transaction(b.me, pub, None, 'CREATE', payload=digital_asset_payload)
#tx = b.create_transaction(b.me, pub, None, 'CREATE')
# all transactions need to be signed by the user creating the transaction
tx_signed = b.sign_transaction(tx, b.me_private)
writeout.exportData(tx_signed, "user"+str(x)+"vote")
# write the transaction to the bigchain
# the transaction will be stored in a backlog where it will be validated,
# included in a block, and written to the bigchain
print(b.write_transaction(tx_signed))
示例12:
# 需要导入模块: from bigchaindb import Bigchain [as 别名]
# 或者: from bigchaindb.Bigchain import write_transaction [as 别名]
# create a test user
testuser1_priv, testuser1_pub = crypto.generate_key_pair()
# define a digital asset data payload
digital_asset_payload = {'msg': 'Hello BigchainDB!'}
# a create transaction uses the operation `CREATE` and has no inputs
tx = b.create_transaction(b.me, testuser1_pub, None, 'CREATE', payload=digital_asset_payload)
# all transactions need to be signed by the user creating the transaction
tx_signed = b.sign_transaction(tx, b.me_private)
# write the transaction to the bigchain
# the transaction will be stored in a backlog where it will be validated,
# included in a block, and written to the bigchain
b.write_transaction(tx_signed)
sleep(8)
"""
Read the Creation Transaction from the DB
"""
tx_retrieved = b.get_transaction(tx_signed['id'])
print(json.dumps(tx_retrieved, sort_keys=True, indent=4, separators=(',', ':')))
print(testuser1_pub)
print(b.me)
print(tx_retrieved['id'])
示例13:
# 需要导入模块: from bigchaindb import Bigchain [as 别名]
# 或者: from bigchaindb.Bigchain import write_transaction [as 别名]
payload_A = {
"msg" : "node send -50 to A,is -50",
"issue" : "cost",
"category" : "currency",
"amount" : 50,
"asset":"",
# final owner 's account
"account":300,
"previous":testuser1_last['txid'],
"trader":testuser2_pub
}
tx_create= b.create_transaction(b.me, testuser1_pub, None, 'CREATE', payload=payload_A)
tx_create_signed = b.sign_transaction(tx_create, b.me_private)
if b.is_valid_transaction(tx_create_signed):
b.write_transaction(tx_create_signed)
# node create transaction for b,
testuser2_last=b.get_owned_ids(testuser2_pub).pop()
payload_B = {
"msg" : "node send +50 to B,is +50",
"issue" : "earn",
"category" : "currency",
"amount" : 50,
"asset":"",
# final owner 's account
"account":400,
"previous":testuser2_last['txid'],
"trader":testuser1_pub
}