Swapping Tokens In and Out of the Stateless Ethereum Sidechain
As a modifcation on the previous article about the Stateless Ethereum Sidechain, first imagine a smart contract whose state is a mapping full of Blocks where a Block is :
Block {
bytes32 root;
bytes32 leaf;
}
So the mapping will be addressed by the blocks roots:
mapping (bytes32 => Block) public blocks;
In this case, a root is simply the Merkle Root of the block data. The first entry in the block data must be a ‘leaf’, meaning the root of the previous block in the chain.
Now, if a node has syncedwith other nodes to acquire a large set of the chain data (the data that makes up the blocks, whose merkle roots are the roots of some of the ‘linked’ blocks in the mapping) we can examine the mapping and find the longest valid chain of these blocks, as linked by the leaves. This is what will be considered ‘the longest valid chain’ or the primary chain.
When a majority of the nodes agree on a ‘longest valid chain’ then that is the sidechain and the nodes can add new blocks onto this chain, one block can be added per 0xBTC mint, by the MintHelper contract which performed the last mint. Thus the chain can grow and blocks below get more and more confirmations.
Valid Transactions on the Sidechain
A transaction follows some standard protocol similar to a bitcoin UTXO, signed by the senders account. The accounts space uses the Ethereum standard accounts. So this Sidechain conceptually works like Ropsten where the account space is the same but the asset/balance ledger is completely different. No EVM methods are allowed, only simple token transfers from A to B. These TX types are ‘transfer’, ‘import’ and ‘export’ and all require ECDSA signatures by the ‘from’ account key.
Importing Tokens into the Sidechain
- Any user with account A can deposit token X of amount Y into the sidechain contract on the Mainnet, this generates a special unique root hash, stored in mapping.
- A special sidechain TX called an ‘import’ can be performed. This immediately fills account A’s token X balance on the sidechain to Y in the sidechain ledger. Can be sent around using ‘transfer’ tx on the sidechain.
Exporting Tokens out of the Sidechain Back to Ethereum
- A special sidechain TX called an ‘export’ can be performed which must be ECDSA signed by the user’s account which contains the tokens on the sidechain. This TX means that they want to destroy these tokens on the sidechains and withdraw them from the smart contract. Further sidechain blocks, to be valid, cannot spend these tokens since they are gone downstream in this branch. This sidechain TX needs 100 confirms.
- After 100 confirms, anyone can call the WithdrawExportedTokens() method in the contract on the Mainnet which requires the root hash of the deepest submitted block (‘B’) at the current moment and which requires a proof that the root of the block ‘A’ containing the ‘Withdraw’ TX is a child of that block ‘B’ and that there are at least 100 confirms between B and A. This withdrawl gets a unique hash (hash of the sidechain tx data) and is stored to a mapping so it can’t be done twice.
This ensures that the majority hashrate of the sidechain nodes all agree that those tokens are being withdrawn and that they are being withdrawn from the sidechain branch that has the most proof of work.