Professional Documents
Culture Documents
Articles
Guides
Back to Guides
5
#Bitcoin #Blockchain 101 #Blockchain for business #Ethereum #Hash #Solidity
s j f 1K
SHAR
Blockchain Coding: The Many different Languages You Need!
The blockchain technology is incredibly fascinating. It won’t be
far-fetched to think of a future which will be built entirely on it.
So, what do you need to learn in order to start developing on
W eb page converted to PDF w ith the PDFmyURL PDF creation API!
So, what do you need to learn in order to start developing on
the blockchain? Which languages will give you the edge? In
this guide, we will go through some of the more major ones.
What this means is that, in C++, the data and functions are
wrapped into one neat little package called “objects” which
means that once an object is created, it can easily be called and
reused in other programs, which greatly reduces coding time.
#include <iostream.h>
main()
return 0;
So, why do people still use C++ for coding? Surely there are
way more glamorous languages now, why do people still insist
on going back to C++? Why is the bitcoin blockchain coded
on C++?
Copy Semantics:
assert(b == c);
a = b;
assert(a == b && b == c);
Move Semantics:
assert( b = = c);
move (a,b);
assert (a = =c );
When we are using the move semantics, the value of “b” need
not be the unchanged. That is the difference between copy
semantics and move semantics. The biggest advantage of
move semantics is that you can get copies of certain data only
when you need them, which greatly decreases redundancy in
the code and gives a huge performance boost. So as you can
see, this efficient memory management and high performance
are both desirable for the blockchain.
Function Overloading.
Operator Overloading.
Function Overloading:
#include <bits/stdc++.h>
using namespace std;
class A
cout<<x<<endl;
W eb page converted to PDF w ith the PDFmyURL PDF creation API!
cout<<x<<endl;
cout<<x<<endl;
cout<<x=y<<endl;
int main()
obj1.func(2);
obj1.func(2.65);
obj1.func(2,5);
return 0;
Now when you run this function the output will be:
2
2.65
7
So, as you can see, the same function func() was used in 3
different ways.
Operator Overloading:
In C++ the same operator can have more than one meaning.
W eb page converted to PDF w ith the PDFmyURL PDF creation API!
Eg. “+” can be used both for mathematical addition and for
concatenation.
Concatenation basically means taking two strings and
combining them as one.
So 3+4 = 7.
AND
Block+geeks = Blockgeeks.
The same operator, did two different functions, this is
operator overloading.
Just changing the first “T” from upper to lower case drastically
changed the output hash so much.
W eb page converted to PDF w ith the PDFmyURL PDF creation API!
So, how does this affect the blockchain?
class Block
{
W eb page converted to PDF w ith the PDFmyURL PDF creation API!
constructor(index, timestamp, data, previo
usHash = '')
this.index = index;
this.previousHash = previousHash;
this.timestamp = timestamp;
this.data = data;
this.hash = this.calculateHash();
calculateHash()
}
W eb page converted to PDF w ith the PDFmyURL PDF creation API!
}
Code Analysis
Ok, so this right here is out block. So, in the first line of the
code we called the crypto-js library because the sha256 hash
function is not available in JavaScript.
In a block, we take all the contents and hash them to get the
hash of that particular block. We are using the JSON.stringify
function to turn the data of the block into string to hash it.
Ok, so we have the block ready and good to go. Now let’s
connect the blocks together into a blockchain.
class Blockchain
W eb page converted to PDF w ith the PDFmyURL PDF creation API!
class Blockchain
constructor()
this.chain = [this.createGenesisBlock()];
createGenesisBlock()
getLatestBlock()
{
W eb page converted to PDF w ith the PDFmyURL PDF creation API!
return this.chain[this.chain.length - 1];
addBlock(newBlock) {
newBlock.previousHash = this.getLatestBlock
().hash;
newBlock.hash = newBlock.calculateHash();
this.chain.push(newBlock);
isChainValid()
return false;
return false;
return true;
The genesis block is the first block of the blockchain, and the
reason why it is special is because while every bock points to
the block previous to it, the genesis block doesn’t point at
anything. So, the moment a new chain is created, the genesis
block is invoked immediately. Also, you can see a
“createGenesisBlock()” function wherein we have given the data
of the block manually:
createGenesisBlock()
}
W eb page converted to PDF w ith the PDFmyURL PDF creation API!
}
Now that we have built the genesis block, let’s build the rest of
the chain.
getLatestBlock()
addBlock(newBlock) {
W eb page converted to PDF w ith the PDFmyURL PDF creation API!
addBlock(newBlock) {
newBlock.previousHash = this.getLatestBlock
().hash;
newBlock.hash = newBlock.calculateHash();
this.chain.push(newBlock);
If these two values match, then this means that the new block is
legit and it gets added to the blockchain.
We are using the “for” loop to go from the block 1 to the last
block. Genesis block is block 0.
return false;
W eb page converted to PDF w ith the PDFmyURL PDF creation API!
}
return false;
return true;
It is that simple.
print(‘Hello, world!’)
num2 = 6.3
class Block:
def __init__(self, index, timestamp, data, pr
evious_hash):
self.index = index
self.timestamp = timestamp
self.data = data
self.previous_hash = previous_hash
self.hash = self.hash_block()
def hash_block(self):
sha = hasher.sha256()
sha.update(str(self.index) +
str(self.timestamp) +
str(self.data) +
str(self.previous_hash))
return sha.hexdigest()
Code Analysis
def create_genesis_block():
Code Analysis
def next_block(last_block):
this_index = last_block.index + 1
this_timestamp = date.datetime.now()
this_data = "Hey! I'm block " + str(this_index
)
this_hash = last_block.hash
return Block(this_index, this_timestamp,
this_data, this_hash)
Code Analysis
blockchain = [create_genesis_block()]
previous_block = blockchain[0]
num_of_blocks_to_add = 15
So we are running a loop that goes till 15 and adds each and
every block to the blockchain. At the end of the look we are
printing which number block has been added to the blockchain
via showing their index number. Plus, we are printing the Hash
as well.
Obviously in both this and the javascript you could add more
complicated features like Proof Of Work. If you want to learn
how to implement that then it is highly recommended to go
through Gerald Nash’s article. But for now, you at least know
how to create a simple blockchain in Python.
contract BasicIterator
function BasicIterator()
creator = msg.sender;
uint8 x = 0;
x++;
} }
uint8 sum = 0;
uint8 x = 0;
x++;
}
return sum;
}
W eb page converted to PDF w ith the PDFmyURL PDF creation API!
// Section 3: Killing the contract
function kill()
if (msg.sender == creator)
suicide(creator);
integers[x] = x;
x++;
0,1,2,3,4,5,6,7,8,9.
Like what you read? Give us one like or share it to your friends
0 5
Comments
nice post thanks you .. I am a little disappointed to not see ruby and rails .. I use
them to code in both bitcoin and ether blockchains
2
2.65
7
”
Wouldn’t the last be output be 5? How could the function below that executes
x=y on (2, 5) return 7?!!
void func (int x, int y) //third instance of the function takes two integer values
{
cout<<x=y<<endl;
}
—-
Is this really that much faster than declaring separate functions?! Seems like an
extremely easy way to confuse and shoot yourself in the foot.
Scholarships