Complete the code to create a block with a timestamp.
block = {
'timestamp': [1],
'data': 'Transaction Data'
}The time.time() function returns the current time in seconds since the epoch, which is used as the timestamp for the block.
Complete the code to calculate the hash of a block's data.
import hashlib block_data = 'Sample Block Data' block_hash = hashlib.sha256([1].encode()).hexdigest()
We hash the string block_data by encoding it first, then applying SHA-256 hashing.
Fix the error in the code to link the current block to the previous block's hash.
current_block = {
'data': 'Some Data',
'previous_hash': [1]
}
previous_block_hash = 'abc123def456'The previous_hash field should store the hash string of the previous block, which is previous_block_hash.
Fill both blanks to create a chain of blocks where each block stores the hash of the previous block.
blockchain = []
first_block = {'data': 'Genesis Block', 'previous_hash': '0'}
blockchain.append(first_block)
new_block = {
'data': 'Second Block',
'previous_hash': [1]
}
blockchain.append(new_block)
print(blockchain[1]['previous_hash'] == [2])The previous_hash of the new block should be the hash of the first block. Here, for simplicity, we link to the first block's hash. The print statement checks if the new block's previous_hash equals the first block's hash.
Fill the blanks to create a block with data, calculate its hash, and link it to the previous block's hash.
import hashlib previous_block = {'data': 'Block 1', 'hash': 'abc123'} new_block = { 'data': [1], 'previous_hash': [2] } block_string = new_block['data'] + new_block['previous_hash'] new_block['hash'] = hashlib.sha256(block_string.encode()).hexdigest() print(new_block['hash'])
The new block's data is 'Block 2'. Its previous_hash is the hash from the previous block, accessed as previous_block['hash'].