In this tutorial, we will explain the data flow of Polkadot’s relay chain and parachain system based on our developers’ interpretation, and then demo the steps to run a local relay chain and OAK testnet parachain.
If you are first reader about Polkadot’s architecture and roles, we recommend you to read its offical wiki here.
1.0 Understanding the architecture
1.1 The cross-chain data flow
A transaction first starts from an external trigger and then enters the parachain network(green area). The collator packs the transaction information, passes the Fisherman inspection, and then provides it to the parachain. Followingly, The block header of the parachain is included in the relay chain. After being packaged by the relay chain validator, the transaction is then transferred to another parachain.
1.2 How are the relay chains and parachains connected?
The answer is: through the “Collator”
The collector can be understood as a special network node, it will run
● Full nodes of parachain
● Light nodes of the relay chain
That is, the collector plays the role of an intermediate bridge, which has both the latest transactions and status of the parachain and the latest status of the relay chain. On this basis, the collector can package the latest parachain block and send it to the relay chain, or obtain the block confirmation message of the relay chain to notify the parachain to finalize the latest parachain block.
1.3 Validators
It is the full node of the relay chain, and the relay chain will assign validators to different parachains through random grouping in the validator pool. The Validator will accept the block packaged by the collector and verify the validity, and then use the consensus algorithm to confirm the block submitted by the collector.
1.4 Validity verification
Validity verification refers to the verification of the legality of changes in the state of the ledger by the blockchain system to achieve a consensus across the network.
An ordinary blockchain, such as Ethereum, every time a block is generated, the block will be broadcast to the Ethereum nodes of the entire network. These nodes will verify and execute the transactions in the block to ensure the final state root, etc. The information is consistent with the information in the block header.
The validity verification here means that the Validator in the relay chain runs a verification program to verify the validity of the parachain block to ensure that the state transfer of the ledger is correct. This is what Polkadot calls shared security. concept.
1.5 Polkadot usage scenarios
What is the use of Polkadot? A few examples can be given:
The weather oracle confirmed the occurrence of a hurricane, the IOT oracle confirmed the damage caused, and a private insurance chain issued a token to compensate its policyholders;
● A blockchain project accepts project crowdfunding through ETH, but requires a private bank chain to conduct KYC certification for its participants in the crowdfunding;
● A side chain with decentralized transactions allows users to deposit BTC through the zero-knowledge proof of the Zcash side chain;
● A side chain used for payment processing requires users to authenticate on a private bank chain before using BTC for payment.
2.0 Start relay chain and parachain
2.1 Compile Rococo
# Compile Polkadot with the real overseer feature
git clone https://github.com/paritytech/polkadot
git fetch
git checkout rococo-v1
cargo build --release
2.2 Start Rococo
# Alice
./target/release/polkadot --chain rococo-local-cfde.json --alice --tmp
# Bob (In a separate terminal)
./target/release/polkadot --chain rococo-local-cfde.json --bob --tmp --port 30334
2.3 Compile Parachain
Pull the rococo-v1 branch of OAK-blockchain
# Compile
git clone https://github.com/OAK-Foundation/OAK-blockchain.git
cd OAK-blockchain
git checkout rococo-v1
Add a file named “rust-toolchain” in the root directory with the following content:
nightly-2021-04-15
Run compilation
cargo build --release
2.4 Start Parachain
# Export genesis state
# --parachain-id 200 as an example that can be chosen freely. Make sure to everywhere use the same parachain id./target/release/parachain-collator export-genesis-state --parachain-id 200 > genesis-state
# Export genesis wasm
./target/release/parachain-collator export-genesis-wasm > genesis-wasm
# Collator1
./target/release/parachain-collator --collator --tmp --parachain-id 200 --port 40335 --ws-port 9946 -- --execution wasm --chain ../polkadot/rococo-local-cfde.json --port 30335
# Collator2
./target/release/parachain-collator --collator --tmp --parachain-id 200 --port 40336 --ws-port 9947 -- --execution wasm --chain ../polkadot/rococo-local-cfde.json --port 30336
# Parachain Full Node 1
./target/release/parachain-collator --tmp --parachain-id 200 --port 40337 --ws-port 9948 -- --execution wasm --chain ../polkadot/rococo-local-cfde.json --port 3033
2.5 Setting up parachain
Use sudo to call parachain id, genesis-state and genesis-wasm above parachain id, genesis-state and genesis-wasm on polkadot apps parasSudoWrapper.sudoScheduleParaInitalize to submit parachain registration.
Note: The node needs to connect to ws://localhost:9944
3.0 References
· https://blog.csdn.net/shangsongwww/article/details/90269617
· https://www.jianshu.com/p/b0af5bc97c6c
· https://www.jinse.com/blockchain/817355.html
· https://github.com/paritytech/cumulus
Stay tuned for further information about the OAK Network here on Medium, Twitter and LinkedIn, as well as in our Discord Server!
Please check out our engineering and growth job openings, contact partner@oak.tech for partnerships, or contact@oak.tech for any general inquiries.