0
0
Blockchain / Solidityprogramming~10 mins

Blocks, chains, and hashing in Blockchain / Solidity - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a block with a timestamp.

Blockchain / Solidity
block = {
    'timestamp': [1],
    'data': 'Transaction Data'
}
Drag options to blanks, or click blank then click option'
Achain
Bhashlib.sha256()
Ctime.time()
Dblockchain
Attempts:
3 left
💡 Hint
Common Mistakes
Using a hashing function instead of a timestamp.
Using undefined variables like 'blockchain' or 'chain'.
2fill in blank
medium

Complete the code to calculate the hash of a block's data.

Blockchain / Solidity
import hashlib

block_data = 'Sample Block Data'
block_hash = hashlib.sha256([1].encode()).hexdigest()
Drag options to blanks, or click blank then click option'
Ablock_data
Bblockchain
Ctimestamp
Ddata
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to hash undefined variables.
Forgetting to encode the string before hashing.
3fill in blank
hard

Fix the error in the code to link the current block to the previous block's hash.

Blockchain / Solidity
current_block = {
    'data': 'Some Data',
    'previous_hash': [1]
}

previous_block_hash = 'abc123def456'
Drag options to blanks, or click blank then click option'
Ahashlib.sha256()
Bprevious_block
Ccurrent_hash
Dprevious_block_hash
Attempts:
3 left
💡 Hint
Common Mistakes
Using the whole previous block instead of its hash.
Using undefined variables.
4fill in blank
hard

Fill both blanks to create a chain of blocks where each block stores the hash of the previous block.

Blockchain / Solidity
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])
Drag options to blanks, or click blank then click option'
Ablockchain[0]['previous_hash']
Bblockchain[0]['data']
Cblockchain[0]
Dblockchain[0]['hash']
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong index or field for previous hash.
Confusing data with hash.
5fill in blank
hard

Fill the blanks to create a block with data, calculate its hash, and link it to the previous block's hash.

Blockchain / Solidity
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'])
Drag options to blanks, or click blank then click option'
A'Block 2'
Bprevious_block['hash']
C'abc123'
D'Block 1'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong string for data or previous hash.
Not concatenating strings properly before hashing.