Implementing Proof of Stake Part — 5

Accounts, Stake and Validators

Kashish Khullar
Coinmonks
Published in
4 min readFeb 10, 2019

--

In the previous post, we create a wallet, signed transactions and broadcasted them to the network. In this post we will add a few extra features into our app. We will implement an account model, stake model, and validator list.

Accounts

Bitcoin uses the concept of UTXOs whereas Ethereum uses an Account model to track node balances. Account model is yet another merkel tree in Ethereum just like the blockchain. Each transaction when executed either increments the balance for a particular account or decrements it. Therefore it can be simply considered as a large key-value hash table where the key is the accounts of the nodes and value is the balance. Instead of actually creating merkel tree, we’ll just create an object in our application to create an account model.

So let's create an account model by adding a new file in the blockchain folder namedaccount.js

Our account model should serve the following functionality —

  1. Increment balance
  2. Decrement balance
  3. Transfer coins
  4. Transfer fee
  5. Update account state
  6. Get the balance of a given account

The Account class has two properties addresses and balances corresponding to those addresses.

There are two separate functions for both crediting and debiting. The getBalance function simply returns the value of a given key. Transfer function utilizes the increment and decrement functions to emulate transfer of value and lastly, the update function takes a transaction and executes it.

Note: The initialize function kinda works around because the balances object doesn’t have the addresses or keys and it's default balance as zero when a new account is created, so this function safely handles that situation and assigns a zero balance to each account.

Wallet and balance

We can now add functions in our wallet to get the balance of the node. To keep code clean, we’ll add a function in the blockchain class to get the balance of any node and reuse the code in the wallet.

 // blockchain.js  getBalance(publicKey) {
return this.accounts.getBalance(publicKey);
}

We can now you this function in the wallet class to check the balance before creating a transaction.

We will add a transfer fee function in the later tutorials.

Stake

We need a way to track the number of coins a node has staked. In Ethereum, each block holds the staked amount but we will keep things simple and create a separate model for it that generalizes the concept. Since the node with the maximum stake is chosen as the leader or block forger, we find the next leader by looking up the staked coins in this model. The functionality of the stake model would be similar to the account model.

Create a stake model by adding a new file in the blockchain folder named stake.js

The addStake function increments the staked amount of the node. The getStake function returns the staked amount. The getMax function returns the node’s address that has the maximum staked coins. The update function is just a wrapper for the addStake function which executes a transaction.

Validator

Validator nodes are different from normal nodes in PoS. Validator nodes can add stake, be elected as leader and create blocks. But not all nodes can be a validator. Only those nodes that send a special transaction which contains a validator fee can become a validator. The fee or coins are later burnt and are not used. In Casper each node, previously had to pay 1000 ethers and now 32 ethers to become a validator.

To add this feature into our project, we will create a file named validators.js in the blockchain folder which will contain a list of addresses that are validators and have submitted the fee.

The fee can be any integer, here I have 30 and the recipient of this amount is an address “0” which is a special address where all the coins that are supposed to be burnt are sent. The update the function simply adds the address to the list if the conditions match.

Transaction Types

There three types of transactions in this application —

  1. TRANSACTION — for regular transactions (couldn’t think of a better name)
  2. STAKE — for staking coins
  3. VALIDATOR_FEE — a transaction with a fixed amount for becoming a validator

Thank you for reading. In the next part, we’ll create an ICO for our coin, add a static leader and generate and broadcast blocks. Hope you enjoyed coding. If you found this helpful throw a bunch of claps.

If you have any questions about blockchain, ethereum or the world in general, leave a comment. :)

k

Get Best Software Deals Directly In Your Inbox

--

--