Android Example [B4X] Blockchain Example - Create Your Own Cryptocurrency (part 1/2)

Blockchain
A digital ledger in which transactions made in bitcoin
or another cryptocurrency are recorded chronologically and publicly.
In more general terms, it’s a public database where new data are stored
in a container called a block and are added to an immutable chain
(hence blockchain) with data added in the past. In the case of Bitcoin
and other cryptocurrencies, these data are groups of transactions.
The data can, of course, be of any type.

Blockchain technology has given rise to new, fully digital currencies
like Bitcoin and Litecoin that aren’t issued or managed by a central authority.
This brings new freedom to individuals who believe that today’s banking systems
are a scam or subject to failure. Blockchain has also revolutionized distributed
computing in the form of technologies like Ethereum, which has introduced
interesting concepts like smart contracts.


Here's a B4X port of the first part of the example found in this page.
https://medium.com/crypto-currently/lets-build-the-tiniest-blockchain-e70965a248b
Author: https://medium.com/@aunyks

upload_2017-12-13_21-11-30.png


The example is presented in B4J format, but the code should be compatible with any B4X product.
 

Attachments

  • blockchain example.zip
    2.8 KB · Views: 1,040
Last edited:

wonder

Expert
Licensed User
Longtime User
Thank you!! :)

If you'd like to give it a try, you'd need to run it on a Linux environment (Ubuntu on Windows 10 / Linux VM / Linux machine).
It runs on Android as well, using the Termux app. It won't run on my NinjaPython B4A lib (for now).

Linux instructions:
- Open terminal
- Install Python 2.7.x (sudo apt-get ...)
- Try to run ./testnet.sh and get a lot of errors (missing libraries)
- Install the missing libraries one by one (repeat the previous step) using pip install
- Once every lib is installed, ./testnet.sh should run without problems.

Testnet:
- Several terminal windows will open
- Each terminal represent a node in the network with the exception of the Broadcaster window
- Find the broadcaster window and enter your name
- You may now broadcast messages to the entire network

- You may also open another terminal and run ./broadcast.py with a different user name

What is happening:
- The broadcaster app sends messages to an initial node, which in turn propagates it to the other nodes
- The idea behind it is based of how viral cat videos / memes / ideas spread accross the web

Network topology:
- The ideal numer of connections for each node is 2-3
- Whenever a node gets diconnected, the network tries to heal itself by establishing new connections
- The idea behind it is how the human brain heals itself after injury by rewiring itself

How are the connections established:
upload_2018-2-20_10-12-54.png


Note: If no better option is presented, nodes are willing to accept more connections than usual.
 
Last edited:

tufanv

Expert
Licensed User
Longtime User
following this project with great curiosity . Incredible work. Thanks !
Thank you!! :)

If you'd like to give it a try, you'd need to run it on a Linux environment (Ubuntu on Windows 10 / Linux VM / Linux machine).
It runs on Android as well, using the Termux app. It won't run on my NinjaPython B4A lib (for now).

Linux instructions:
- Open terminal
- Install Python 2.7.x (sudo apt-get ...)
- Try to run ./testnet.sh and get a lot of errors (missing libraries)
- Install the missing libraries one by one (repeat the previous step) using pip install
- Once every lib is installed, ./testnet.sh should run without problems.

Testnet:
- Several terminal windows will open
- Each terminal represent a node in the network with the exception of the Broadcaster window
- Find the broadcaster window and enter your name
- You may now broadcast messages to the entire network

- You may also open another terminal and run ./broadcast.py with a different user name

What is happening:
- The broadcaster app sends messages to an initial node, which in turn propagates it to the other nodes
- The idea behind it is based of how viral cat videos / memes / ideas spread accross the web

Network topology:
- The ideal numer of connections for each node is 2-3
- Whenever a node gets diconnected, the network tries to heal itself by establishing new connections
- The idea behind it is how the human brain heals itself after injury by rewiring itself

How are the connections established:
View attachment 64853

Note: If no better option is presented, nodes are willing to accept more connections than usual.
 

Widget

Well-Known Member
Licensed User
Longtime User
In general terms, how does node [F] get authenticated before it is allowed to connect to the network? Is it using PKI? And is the code on node [F] verified before connecting? Otherwise it would be simple matter to add a rogue node [F] with a trojan that would take over the network by allowing more of its own to connect and gain majority control.
 

KMatle

Expert
Licensed User
Longtime User
Like many examples they explain how it works in simple words and then they "jump lightyears" (e.g. "there is software with encryption and a huge network"). I understand how it works but there are questions left:

1. Who was the first "node"?

At the beginning the "first node" claims to be the owner of it all (like a god). All other nodes are just a clone (same sw and data) of the first one. As I've understood enyone can be the first node. It depends on if others will join "your" network.

2. Which is the "real" network and how do I know it?

I could create an own blockchain network claiming "I'm genuine and only my clones must be trusted" and/or "all other networks are void"

3. Is there an instance (like a certification authority) which ensures that I'm in the correct network?

4. If there can be n networks, it's like banks/countries with different currencies again

If a lot of people create their own network there will be millions of it and not only ONE (as it might be wanted). So where's the advantage? When a group of banks are going to create "their" network, we're back from where we started.

5. It's all about money. So there will abe a currency like BitCoin or other's

Assuming there will be more than one network, we will have "network currencies" and exchange reates, etc., aren't we?

6. Who guaranties that one network will be online for ever?

Banks can't guaranty that, too but they are under "control" of states (law) or other authorities.

7. What if a complete network (more exact: all nodes) don't want to be member of THAT network and change to another (due to financial reasons or maybe because it's "cooler" to be there)

All data will be lost assuming they aren't interested to migrate the data to the new network. What if only one node wants to migrate the data to the new network. Will they trust this single node and accept all the data? I don't think so.
 

Widget

Well-Known Member
Licensed User
Longtime User
KMatle. Those are all good points to consider. I think these types of block chains would be great for communities, like a company store approach. Or for bartering in your community. About 5 years ago I heard of an Indian reservation thinking of creating their own blockchain instead of using Bitcoin. (I bet they are kicking themselves over that.) They would use the blockchain to keep track of services and funding for their own community, like a semi-public ledger to prevent misuse of funds.
 

wonder

Expert
Licensed User
Longtime User
Hello again and thank you for your interest in this project! :)

Before answering your questions, a few things I'd like to share:
- I'm not trying to create any sort of cryptocurrency, I'm just trying to understand how it works.
- This thread has shifted its focus from a "simple blockchain example" to "my journey into understanding how to create a decentralized network/application".
- My idea is to divide this project into separate independent layers:
>> Network: currently being implemented in Python 2.7, codenamed "xNode" (work-in-progress: requires static IP address or port-forwarding).
>> Data Integrity: currently implemented in B4X as a simple blockchain (first post).
>> Proof Model: not implemented yet, to be determined (proof-of-work / proof-of-stake / other).
>> Application: not implemented yet, to be determined (cryptocurrency / smart-contracts / private messaging / other).
- As of right now, the network layer has no relationship to with the data integrity layer (blockchain).

In general terms, how does node [F] get authenticated before it is allowed to connect to the network?
Current state: There is no authentication method.
My goal: No need for authentication. The network layer should be open for anyone to join.
Due to cryptography and/or math, malicious nodes should have no way of affecting the network.

1. Who was the first "node"?
There is no relevance to who the first node is. You answered the question yourself:
As I've understood anyone can be the first node. It depends on if others will join "your" network.
My goal: The node source code should be public (and therefore modifiable). The only thing that should matter is data integrity / valid transactions.

2. Which is the "real" network and how do I know it?

I could create an own blockchain network claiming "I'm genuine and only my clones must be trusted" and/or "all other networks are void"

3. Is there an instance (like a certification authority) which ensures that I'm in the correct network?

4. If there can be n networks, it's like banks/countries with different currencies again

If a lot of people create their own network there will be millions of it and not only ONE (as it might be wanted). So where's the advantage? When a group of banks are going to create "their" network, we're back from where we started.

5. It's all about money. So there will abe a currency like BitCoin or other's

Assuming there will be more than one network, we will have "network currencies" and exchange reates, etc., aren't we?

6. Who guaranties that one network will be online for ever?

Banks can't guaranty that, too but they are under "control" of states (law) or other authorities.

7. What if a complete network (more exact: all nodes) don't want to be member of THAT network and change to another (due to financial reasons or maybe because it's "cooler" to be there)

All data will be lost assuming they aren't interested to migrate the data to the new network. What if only one node wants to migrate the data to the new network. Will they trust this single node and accept all the data? I don't think so.
Please give me a few days to be able to do some research and come up with some proper answers / points-of-discussion. :)
Meanwhile, I believe this video does shed some light on some of the topics mentioned above:

Again, this is just an exploratory journey, I'm not looking into creating anything to be deployed in the real world.
 

tufanv

Expert
Licensed User
Longtime User
Like many examples they explain how it works in simple words and then they "jump lightyears" (e.g. "there is software with encryption and a huge network"). I understand how it works but there are questions left:

1. Who was the first "node"?

At the beginning the "first node" claims to be the owner of it all (like a god). All other nodes are just a clone (same sw and data) of the first one. As I've understood enyone can be the first node. It depends on if others will join "your" network.

2. Which is the "real" network and how do I know it?

I could create an own blockchain network claiming "I'm genuine and only my clones must be trusted" and/or "all other networks are void"

3. Is there an instance (like a certification authority) which ensures that I'm in the correct network?

4. If there can be n networks, it's like banks/countries with different currencies again

If a lot of people create their own network there will be millions of it and not only ONE (as it might be wanted). So where's the advantage? When a group of banks are going to create "their" network, we're back from where we started.

5. It's all about money. So there will abe a currency like BitCoin or other's

Assuming there will be more than one network, we will have "network currencies" and exchange reates, etc., aren't we?

6. Who guaranties that one network will be online for ever?

Banks can't guaranty that, too but they are under "control" of states (law) or other authorities.

7. What if a complete network (more exact: all nodes) don't want to be member of THAT network and change to another (due to financial reasons or maybe because it's "cooler" to be there)

All data will be lost assuming they aren't interested to migrate the data to the new network. What if only one node wants to migrate the data to the new network. Will they trust this single node and accept all the data? I don't think so.

Satoshi's white paper talks briefly about networks like this . I know it does not answer all the questions but I will write what I know about these longer when i have time.

5. Network The steps to run the network are as follows: 1) New transactions are broadcast to all nodes. 2) Each node collects new transactions into a block. 3) Each node works on finding a difficult proof-of-work for its block. 4) When a node finds a proof-of-work, it broadcasts the block to all nodes. 5) Nodes accept the block only if all transactions in it are valid and not already spent. 6) Nodes express their acceptance of the block by working on creating the next block in the chain, using the hash of the accepted block as the previous hash. Nodes always consider the longest chain to be the correct one and will keep working on extending it. If two nodes broadcast different versions of the next block simultaneously, some nodes may receive one or the other first. In that case, they work on the first one they received, but save the other branch in case it becomes longer. The tie will be broken when the next proofof-work is found and one branch becomes longer; the nodes that were working on the other branch will then switch to the longer one. 3 Block Prev Hash Nonce Tx Tx ... Block Prev Hash Nonce Tx Tx ... New transaction broadcasts do not necessarily need to reach all nodes. As long as they reach many nodes, they will get into a block before long. Block broadcasts are also tolerant of dropped messages. If a node does not receive a block, it will request it when it receives the next block and realizes it missed one.

I also think that the purpose must not be creating a cryptocurrency , it must be the tech itself so we can use that for many purposes like : voting systems that cant be hacked or changed ..
 

wonder

Expert
Licensed User
Longtime User
@tufanv thank you for your support, I've been quite busy at work and haven't had any time to improve this project in the last two weeks.

In sum, to create a decentralized service, app or cryptocurrency, one has to know a lot more than just how to build a blockchain.
It is possible to do so with B4X, we have all the tools, but it does require a lot of research in the areas of networking and cryptography.
 
Top