11.. _blockchain :
22
3- .. _blockchain-general :
4-
53Blockchain
6- ====
4+ ==========
5+
6+ The Credits Blockchain Framework as an application is a distributed software service that runs on
7+ multiple servers called :ref: `Nodes <architecture-blockchain-node >`. Every node holds a copy
8+ of the Credits blockchain as a data structure. This data structure consists of cryptographically
9+ connected :ref: `Blocks <blockchain-block >` and :ref: `States <blockchain-state >`. New blocks and
10+ states are added to the blockchain by :ref: `applying transactions <blockchain-applying-transactions >`
11+ and electing the best blocks through node :ref: `consensus <blockchain-consensus >`.
712
8- Building from state and blocks a chain can be established. Because credits has intermediate states its not a direct link
9- from block to block, instead a block is formed from a state, and then the application of that block forms a new state.
13+ .. _blockchain-state :
1014
11- Imagine starting at state 0.
15+ Blockchain State
16+ ^^^^^^^^^^^^^^^^
17+
18+ The State is simply just a key-value map of data. Credits Framework has one global state object,
19+ this is comprised of many different smaller states. There can be multiple different
20+ substates that can contain any information, all that is important is that every key in the
21+ state is a string. An example of a traditional state as shown:
1222
1323.. code-block :: json
1424
1525 {
16- "balance" : {
26+ "loans" : {
27+ "Bob" : 200 ,
28+ "Dave" : 200
29+ },
30+ "balances" : {
1731 "Alice" : 100 ,
18- "Bob" : 0
32+ "Carol" : 100
33+ }
34+ }
35+
36+
37+ In the example above there is a ``balances `` state and a ``loans `` state, these states
38+ can have many arbitrary number of key value pairs. Usually credits addresses are used
39+ as the key in a state but this doesn't always need to be the case. The State is ordered
40+ by insertion order and is hashed to provide a state hash.
41+
42+
43+ .. _blockchain-block :
44+
45+ Blockchain Block
46+ ^^^^^^^^^^^^^^^^
47+
48+ In the simplest terms, a block is just a collection of :ref: `transactions <transaction >`.
49+ Blocks are formed by taking valid unconfirmed transactions from the internal transactions
50+ pool and making a block that contains these transactions. Once the block is formed
51+ it is distributed in the network and nodes can decide to vote and commit to this block.
52+ A block also contains information on the state of the world that it is built on. By
53+ referencing the state, a node can take the block, check that it is starting in the same
54+ place as the creator of the block and then apply the transactions.
55+
56+
57+ .. _blockchain-onboarding-transactions :
58+
59+ Onboarding transactions
60+ ^^^^^^^^^^^^^^^^^^^^^^^
61+
62+ Onboarding transaction, transform or proof means generally to get it accepted into the
63+ node's unconfirmed transactions pool. In case of PaaS deployment, the onboarding happens
64+ by sending the marshalled transaction to the node HTTP API. In other more complex
65+ deployments the HTTP API gateway can be replaced with other transport, e.g. TCP socket,
66+ CLI interface, RPC interface etc. Onboarding here serves as a transport agnostic term
67+ for the act of accepting transaction into the node.
68+
69+
70+ .. _blockchain-applying-transactions :
71+
72+ Applying transactions
73+ ^^^^^^^^^^^^^^^^^^^^^
74+
75+ Applying a transaction means to execute the :ref: `Transform <transform >` contained
76+ inside transaction against the current global state and get the next global state.
77+ Consider same global state as above:
78+
79+ .. code-block :: json
80+
81+ {
82+ "loans" : {
83+ "Bob" : 200 ,
84+ "Dave" : 200
85+ },
86+ "balances" : {
87+ "Alice" : 100 ,
88+ "Carol" : 100
89+ }
90+ }
91+
92+ If we apply transaction that moves 50 credits from Alice to Bob. Then the next global state will be:
93+
94+ .. code-block :: json
95+
96+ {
97+ "loans" : {
98+ "Bob" : 250 ,
99+ "Dave" : 200
100+ },
101+ "balances" : {
102+ "Alice" : 50 ,
103+ "Carol" : 100
19104 }
20105 }
21106
107+ This will reflect the fact that Alice has loaned further 50 credits to Bob.
108+
109+ Applying a block is the process of applying each transaction in order. Each transaction
110+ will produce a new state once it is applied, and by applying every transaction in the block
111+ this will form the next state of the world after the block.
112+
113+ Any :ref: `Applicable <interfaces-applicable >` object should be able to apply itself.
114+
115+
116+ .. _blockchain-consensus :
117+
118+ Blockchain consensus
119+ ^^^^^^^^^^^^^^^^^^^^
120+
121+ There are many different Consensus mechanisms. Two of the common mechanisms that are talked about in
122+ blockchain are Proof of Work and Proof of Stake.
123+
124+
125+ Proof of Work
126+ -------------
127+ Proof of work is the more commonly talked about mechanism for achieving consensus. Proof of work
128+ requires that a contributor do a deterministically difficult amount of work that is then easy to check.
129+ Bitcoin does this by making miners hash until they get the longest string of zeroes this
130+ artificially slows down block creation in the bitcoin network.
131+ Anyone can mine blocks but given the current normalize difficulty it takes a long time for
132+ non-specialized hardware to mine a valid block. Think of this as like a lottery,
133+ everyone is turning a crank and one person is rewarded every x minutes.
134+
135+
136+ Proof of Stake
137+ --------------
138+
139+ Proof of stake is far more like a traditional election model. Everyone locks up some value as a promise of their
140+ good intentions inside the system and then there are fixed voting rounds where each person votes using the weight of the value locked
141+ up. In an example both Alice and Bob stake 50 value into the system, they both have equal votes but neither have
142+ majority. Both together can vote and provide majority for confirming a block. Anyone can propose a block but only
143+ those with stake can vote.
22144
23- And there is a transaction that moves 50 credits from ``Alice `` to ``Bob ``.
24145
25- This transaction can apply to state 0, so it is formed into a block that builds upon state 0.
146+ Credits consensus
147+ -----------------
148+
149+ Consensus in Credits is at its heart Proof of Stake. Validators bond value against as a promise of their honest intentions.
150+ Validators attempt to create valid blocks of unconfirmed transactions. These blocks are distributed between the validators.
151+ Each validator picks a block to vote on (currently this is the first valid block seen) and then tells the network of their
152+ intention to vote for this block. Once enough votes have been cast for that block to have a winning concensus everyone announces
153+ their intention to commit to that block. With enough voters committed to a block it becomes ratified history and the state of the
154+ world is upgraded.
155+
156+ .. _blockchain-structure :
157+
158+ Blockchain structure
159+ ^^^^^^^^^^^^^^^^^^^^
160+
161+ Building from states and blocks the chain can be created. Because Credits blockchain
162+ has intermediate states it's not a direct link from block to block, instead, a
163+ block is formed from the current state, and then the application of that block to
164+ current state forms the next state.
165+
166+ Imagine starting at the following state 0:
167+
168+ .. code-block :: json
169+
170+ {
171+ "balance" : {
172+ "Alice" : 100 ,
173+ "Bob" : 0
174+ }
175+ }
176+
177+ And there is a transaction that moves 50 credits from ``Alice `` to ``Bob ``. This
178+ transaction can apply to state 0, so it is formed into a block that builds upon state 0.
26179::
27180
28181 +-----------+
@@ -39,8 +192,9 @@ This transaction can apply to state 0, so it is formed into a block that builds
39192 +-----------+
40193
41194
42- The block is distributed between the nodes and references the state it is built on, once the network agrees to make this
43- block each node applies this block to state 0 to produce the next state.
195+ The block is then distributed between the nodes and references the state it is
196+ built on. Once the network agrees to make this block the next one in the chain
197+ each node applies this block to state 0 to produce the next state.
44198::
45199
46200 +-----------+ +-----------+
@@ -57,7 +211,7 @@ block each node applies this block to state 0 to produce the next state.
57211 +-----------+
58212
59213
60- The new state 1 looks like the following
214+ The new state 1 looks like the following:
61215
62216.. code-block :: json
63217
@@ -68,8 +222,9 @@ The new state 1 looks like the following
68222 }
69223 }
70224
71- A new transaction is formed, this transaction moves the remaining 50 from ``Alice `` to ``Bob ``. Another new block is
72- formed looking like such
225+ A new transaction is formed and posted to the blockchain, this transaction
226+ moves the remaining 50 from ``Alice `` to ``Bob ``. Another new block is
227+ formed looking like such:
73228::
74229
75230 +-----------+ +-----------+
@@ -102,7 +257,7 @@ The process continues and block 1 will be applied to state 1, forming the next f
102257 +-----------+ +-----------+
103258
104259
105- Leaving it with a final state of
260+ Leaving it with a final state of:
106261
107262.. code-block :: json
108263
@@ -113,3 +268,7 @@ Leaving it with a final state of
113268 }
114269 }
115270
271+ From here onwards other transactions can happen, further mutating global state
272+ and adding new blocks to the chain. The process will run indefinitely as
273+ long as there is a quorum of nodes in the network and new valid transactions
274+ are coming in.
0 commit comments