In this lesson,
we will learn about Blockchain.
Blockchain is a decentralized, distributed ledger technology
that ensures data integrity and transaction transparency
by recording transactions in a series of immutable blocks.
Immutable means that the blocks are unchangeable
after creation.
An Immutable Database refers to a database where data,
once written, is not able to be altered or deleted.
Immutable databases ensure a permanent record
of blockchain transactions.
Let's learn more about blockchain and immutable databases.
Then, we will conduct a blockchain demonstration.
First, we have blockchain.
Blockchain is a technology that can seem complex,
but is essentially a way to securely record
and share information across a network.
It works like a digital ledger,
similar to a record book
where transactions are written down.
However, unlike traditional ledgers,
blockchain is decentralized and distributed,
meaning it is not controlled
by any single person or organization.
Instead,
it is maintained by a network of computers called nodes
all around the world.
Each transaction or piece of data is recorded in a block.
Once the block is full,
it is linked to a previous one,
forming a chain of blocks,
hence the term blockchain.
What makes blockchain special
is its transparency and security.
Once data is recorded in a block,
it's impossible to change,
providing a tamper-proof record of every transaction.
This feature makes blockchain ideal for situations
where it is crucial to have a transparent
and trustworthy record,
such as financial transactions, voting systems,
or tracking goods in a supply chain.
Every participant in the network
has a copy of the blockchain,
so if someone tries to tamper with the data,
the rest of the network will see that something is wrong,
making blockchain highly resistant to fraud.
Second, we have immutable databases,
and immutable database is a type of database
where the data, once written, cannot be altered or deleted.
A key feature of immutability means that every piece of data
remains exactly as it was when it was first recorded.
Unlike traditional databases
where data can be changed or removed,
immutable databases ensure that a permanent record is kept.
This makes them particularly valuable
in scenarios where it is important
to maintain a historical record without the risk of changes,
such as in compliance, auditing, or legal contexts.
While blockchain is a specific application
of an immutable database,
the two concepts are not the same.
Blockchain uses immutability as one of its core features,
but it also relies on decentralization and cryptography
to provide a secure, transparent way
of managing data across a network.
Immutable databases on their own
do not need to be distributed or decentralized.
They simply ensure that once data is written,
it cannot be modified,
providing a reliable way to preserve information
exactly as it was recorded.
Now, let's demonstrate blockchain on a Kali Linux machine
using an application called Ganache.
Ganache is a personal blockchain for Ethereum development
that we can use to deploy contracts, develop applications,
and run tests.
So while our instance of Ganache will not be made public,
the concepts we can demonstrate would be the same
if it were a publicly distributed blockchain.
For our demonstration, I've installed Node.js,
the Node package manager, and Ganache.
Ganache is running
and we can see that it has provided us
with some starting information.
Here's a breakdown of what we're seeing.
The Available Account section lists 10 Ethereum accounts
that have been created.
Each account has a unique Ethereum address
and an initial balance of 1,000 Ether.
These accounts are used for sending transactions,
deploying smart contracts,
and interacting with the blockchain.
Next, each Ethereum account has a corresponding private key
shown here in hexadecimal format.
These private keys are critical for signing transactions
and proving ownership of accounts.
They're exposed here in Ganache
because it's a development environment,
but in a live blockchain,
private keys must be kept secret
to protect the security of the funds.
Next, the hierarchial deterministic
or HD wallet section shows the mnemonic
or seed phrase used to generate the accounts.
The mnemonic is a set of words
that can recreate the private keys and accounts,
so it must be kept safe in production scenarios.
Next, the default gas prices listed.
Gas prices determine the cost of executing transactions
or smart contracts on the blockchain.
The block gas limit is the maximum amount
of computational work or gas
that can be included in a single block on the blockchain.
It ensures that blocks do not get too large
or expensive to process.
The call gas limit represents the maximum amount of gas
allowed for a single function call or transaction.
This ensures that computational operations
do not exceed reasonable limits.
Finally, we have chain information.
This section provides details
about the blockchain configuration,
including the hard fork being used,
which is Shanghai,
and the chain ID, 1337.
The hard fork refers to a specific set of protocol changes
and updates that are applied to the blockchain.
The chain ID uniquely identifies the blockchain network,
which helps prevent transactions intended for one network
from being accidentally executed on another.
Okay, now we need some way to interact with the blockchain,
so we will install Web3.js with the following command,
sudo npm install web3@1.6.0.
While that's installing,
let me show you the script that I've created.
Cat blockchain_demo.js.
This JavaScript uses the Web3.js library
to interact with a local blockchain.
It demonstrates basic blockchain operations,
such as retreating accounts, checking balances,
sending a transaction,
and displaying transaction details.
The local blockchain is hosted at 127.0.0.1,
or this machine,
on port 8545.
First, this script will retrieve a list of accounts.
These accounts are used to send transactions
and interact with the blockchain.
Next, the script will send the transaction
from the first account to the second account,
transferring one Ether.
The gas limit of this transaction is set to 21,000,
which is the standard amount of gas
required for a simple Ether transfer.
After the transaction is successful,
the script prints out the transaction hash,
which uniquely identifies the transaction on the blockchain.
After sending the transaction,
the script checks the updated balance of the first account
to show the impact of the transaction.
Finally, the details of the transaction will be displayed.
Now, let's run this script using the following command,
sudo node blockchain_demo.js.
Okay.
Here's a breakdown of what we're seeing.
The Available Accounts section
lists our 10 Ethereum accounts.
Each account has a unique address
which serves as its identifier on the blockchain.
Next, we see the initial balance of the first account,
which is 1,000 Ether (ETH).
Next, we see a transaction successful hash.
This indicates
that a transaction was successfully completed.
The transaction hash uniquely identifies this transaction
on the blockchain and serves as a reference
for querying the transaction details
and proves the transaction was recorded.
Next, we see a new balance of the first account,
which is updated to approximately 999 ETH
due to 1 ETH being sent
and the gas fee charged for processing the transaction.
Last, we see the Transaction Details.
We won't review them all,
but there are a few that are relevant to our discussion.
The Block Hash and Block Number indicate the block
where the transaction was recorded.
Next, the transaction index.
This is the position of the transaction within the block.
From and To:
These are the sender and receiver addresses.
Next, we have a Value:
This is the amount of Ether that has been transferred
specified in wei.
Then we see the Gas Price and the Gas.
This indicates the computational cost
for processing the transaction and the fee per unit of gas.
Finally, v, r, and s:
These values are used
in the digital signature of the transaction,
proving it was signed by the sender.
But all we are seeing here is a single transaction.
Let's take a look at the ledger.
All transactions can be summarized
by querying the blockchain to observe transactions.
So I've created a separate script
that will query the blockchain
and provide us with a summary of transactions
that have occurred.
Before we run this script,
let's do one more transaction,
that way we have a little more data to look at.
We'll do this by rerunning the command
sudo node blockchain_demo.js.
Next, let's take a look at our ledger.
We'll do this by running the sudo node ledger.js command.
Okay, here's what we're seeing.
We can see the details about the most recent blocks
of our blockchain,
along with the transactions contained within these blocks.
Here we're viewing our ledger.
We can see the latest block number,
which indicates that block two
is the most recent block in the blockchain.
The number represents the sequential order of blocks,
starting from the genesis block, block 0.
Then we can see the block 2 details,
including the block number,
which is the block's position in the chain,
and the hash for block 2.
Each block in the blockchain is hashed.
A hash is a cryptographic representation
of the block's data,
essentially a digital fingerprint
that uniquely identifies the block.
What makes blockchain truly secure and immutable
is that each block also includes the hash
of the previous block in the chain.
This creates a link between blocks,
forming a continuous tamper-proof chain.
If any information in a block is altered, even slightly,
the block's hash will change.
Since each block contains the previous block's hash,
any change would break the link,
alerting the network that tampering has occurred.
This interconnected structure
means that to alter any single block,
a malicious actor would need to change
all subsequent blocks,
recalculate all hashes,
and gain control of the majority
of the network's computing power,
which is practically impossible.
This reliance on previous block hashes
is what makes blockchain immutable
as it safeguards the entire chain
from unauthorized modifications,
ensuring that once data is recorded,
it remains permanent and secure.
Finally, we have a transaction hash.
This identifies the transaction
and some details about the transaction,
including the transaction parties, the amount transferred,
and the gas used.
As we continue to look down the output,
we can see our first transaction in block number one,
and we can see block 0.
Block 0 is known as the genesis block,
it's the first block in the blockchain.
The genesis block contains no transactions,
which is typical since it primarily serves as the foundation
for all subsequent blocks.
This demonstration showed us the concept
of blockchain's immutability and distributed ledger.
It showed how blocks are sequentially late,
each containing unique transactions.
Additionally, based on each block containing data
from the previous blocks,
we showed that data recorded in a block is immutable,
maintaining a secure and transparent record
of all transactions.
So remember,
blockchain is a decentralized technology
that records data in a secure and transparent way
across a network of computers.
It acts like a digital ledger
where transactions are stored in blocks,
and once a block is full,
it is linked to the previous one,
creating an unchangeable chain.
The key feature of blockchain is its immutability,
meaning once data is recorded, it cannot be altered,
ensuring a trustworthy and tamper-proof record.
Immutable databases, on the other hand,
are systems where data, once written,
cannot be changed or deleted,
preserving information as it was originally recorded.
While blockchain uses immutable databases
as a core component,
it also includes decentralization and cryptography
to add security and transparency
across its entire network.