Skip to content

Commit 0eb4374

Browse files
committed
Added example at end of step-by-step
1 parent 993fedd commit 0eb4374

File tree

6 files changed

+166
-89
lines changed

6 files changed

+166
-89
lines changed

source/blockchain.rst

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,21 @@
33
Blockchain
44
==========
55

6-
The Credits Blockchain Framework as an application is a distributed software service that runs across
6+
The Credits Blockchain Framework as an application is a distributed software service that runs on
77
multiple servers called :ref:`Nodes <architecture-blockchain-node>`. Every node holds a copy
88
of the Credits blockchain as a data structure. This data structure consists of cryptographically
99
connected :ref:`Blocks <blockchain-block>` and :ref:`States <blockchain-state>`. New blocks and
10-
states are added to the blochchain by :ref:`applying transactions <blockchain-applying-transactions>`
10+
states are added to the blockchain by :ref:`applying transactions <blockchain-applying-transactions>`
1111
and electing the best blocks through node :ref:`consensus <blockchain-consensus>`.
1212

1313
.. _blockchain-state:
1414

1515
Blockchain State
1616
^^^^^^^^^^^^^^^^
1717

18-
State is simply just a key value map of data. Credits Framework has one global state object,
18+
The State is simply just a key-value map of data. Credits Framework has one global state object,
1919
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 the key of the
20+
substates that can contain any information, all that is important is that every key in the
2121
state is a string. An example of a traditional state as shown:
2222

2323
.. code-block:: json
@@ -36,7 +36,7 @@ state is a string. An example of a traditional state as shown:
3636
3737
In the example above there is a ``balances`` state and a ``loans`` state, these states
3838
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. State is ordered
39+
as the key in a state but this doesn't always need to be the case. The State is ordered
4040
by insertion order and is hashed to provide a state hash.
4141

4242

@@ -45,11 +45,11 @@ by insertion order and is hashed to provide a state hash.
4545
Blockchain Block
4646
^^^^^^^^^^^^^^^^
4747

48-
In the simplest terms a block is just a collection of :ref:`transactions <transaction>`.
48+
In the simplest terms, a block is just a collection of :ref:`transactions <transaction>`.
4949
Blocks are formed by taking valid unconfirmed transactions from the internal transactions
5050
pool and making a block that contains these transactions. Once the block is formed
5151
it is distributed in the network and nodes can decide to vote and commit to this block.
52-
A block also contains information of the state of the world that it is built on. By
52+
A block also contains information on the state of the world that it is built on. By
5353
referencing the state, a node can take the block, check that it is starting in the same
5454
place as the creator of the block and then apply the transactions.
5555

@@ -60,11 +60,11 @@ Onboarding transactions
6060
^^^^^^^^^^^^^^^^^^^^^^^
6161

6262
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
63+
node's unconfirmed transactions pool. In case of PaaS deployment, the onboarding happens
6464
by sending the marshalled transaction to the node HTTP API. In other more complex
6565
deployments the HTTP API gateway can be replaced with other transport, e.g. TCP socket,
6666
CLI interface, RPC interface etc. Onboarding here serves as a transport agnostic term
67-
for act of accepting transaction into the node.
67+
for the act of accepting transaction into the node.
6868

6969

7070
.. _blockchain-applying-transactions:
@@ -115,17 +115,51 @@ Any :ref:`Applicable <interfaces-applicable>` object should be able to apply its
115115

116116
.. _blockchain-consensus:
117117

118-
Blockchain Consensus
118+
Blockchain consensus
119119
^^^^^^^^^^^^^^^^^^^^
120120

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.
144+
145+
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.
121155

122156
.. _blockchain-structure:
123157

124158
Blockchain structure
125159
^^^^^^^^^^^^^^^^^^^^
126160

127-
Building from states and blocks the chain can be created. Because credits
128-
has intermediate states its not a direct link from block to block, instead a
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
129163
block is formed from the current state, and then the application of that block to
130164
current state forms the next state.
131165

@@ -234,6 +268,7 @@ Leaving it with a final state of:
234268
}
235269
}
236270
237-
From here onwards other transactions can happen, further mutating global state and adding blocks
238-
to the chain. The process will run indefinitely as long as there is a quorum of nodes in the
239-
network and new valid transactions are coming in.
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.

source/getting_started.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ G-Cloud platform onboarding
2222

2323
In order to sign-up for our G-Cloud offering, you will need to go through our
2424
:ref:`onboarding process <gcloud-onboarding-process>`, including verifying your
25-
organization as a qualified UK government agency.
25+
organisation as a qualified UK government agency.
2626

2727

2828
How to interact with the system

source/interfaces.rst

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ Interfaces
88
Marshallable
99
^^^^^^^^^^^^
1010

11-
The Marshallable interface provides the nessasary methods to convert fully
12-
realised instances into more primative maps and to reverse the process by
11+
The Marshallable interface provides the necessary methods to convert fully
12+
realised instances into more primitive maps and to reverse the process by
1313
converting these maps back into instances.
1414

1515
FQDN
@@ -37,8 +37,8 @@ unmarshalling process to locate an associated Class to instanciate.
3737
marshall
3838
--------
3939

40-
Marshalling is the process of converting a instance into a map of primative
41-
variables which can be then serialized. This allows libraries like ``json`` and
40+
Marshalling is the process of converting an instance into a map of primitive
41+
variables which can be then serialised. This allows libraries like ``json`` and
4242
``msgpack`` to use their ``dumps`` methods to convert the output of
4343
``marshall`` to a string or series of bytes suitable for transport over the
4444
network. The ``fqdn`` used by the framework to resolve the implementor's class
@@ -59,7 +59,7 @@ Marshallable object should also marshall any of it's subcomponents.
5959
unmarshall
6060
----------
6161

62-
Once a marshalled object has been recieved, the unmarshall method may be used to
62+
Once a marshalled object has been received, the unmarshall method may be used to
6363
reverse the process. Like marshalling, unmarshalling should also be performed on
6464
all subcomponents to fully resolve the object.
6565

@@ -82,8 +82,8 @@ all subcomponents to fully resolve the object.
8282
Applicable
8383
^^^^^^^^^^
8484

85-
The ``Applicable`` interface provides the nessasary methods for objects to
86-
perform manipulations against state.
85+
The ``Applicable`` interface provides the necessary methods for objects to
86+
perform manipulations against the state.
8787

8888
verify
8989
------
@@ -108,7 +108,7 @@ apply
108108
-----
109109

110110
The ``apply`` method should manipulate and return state. In the event of an
111-
error, return an errornous Result.
111+
error, return an erroneous Result.
112112

113113
.. code-block:: python
114114
:linenos:
@@ -137,7 +137,7 @@ sign
137137
The ``sign`` method should accept a ``credits.key.SigningKey`` and sign some
138138
sort of challenge stored within the implementor. Then return a new instance of
139139
the implementor with both the signature and the associated
140-
``credits.key.VerifyingKey``. These additional variables can then for example
140+
``credits.key.VerifyingKey``. These additional variables can then, for example,
141141
be used in conjunction with the ``Applicable.verify`` method to check if the
142142
implementor has been signed.
143143

source/step_by_step.rst

Lines changed: 59 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ To create a custom permissioned blockchain with Credits framework you will
88
need to go through these steps:
99

1010
- create required transforms, proofs and transactions
11-
- test and verify validity of the code in your local dev environment
11+
- test and verify the validity of the code in your local dev environment
1212
- start your private blockchain network and upload the code
1313
- create your client application using the same transactions
1414
- hook your client to the network via node API and start transacting on the blockchain
@@ -19,13 +19,13 @@ need to go through these steps:
1919
Create transforms and other modules
2020
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2121

22-
In the simplest scenario you will need to only implement your transforms. You
22+
In the simplest scenario, you will need to only implement your transforms. You
2323
can use ``SingleKeyProof`` provided in the Common library as a default way to
2424
prove transaction validity with one signing key per signature. And your
2525
transaction in the simplest scenario can also be the default ``Transaction``
2626
provided in the common library.
2727

28-
In simplest case you may have just one transform. The transform must implement
28+
So in this simplest case, you may have just one transform. The transform must implement
2929
``credits.transform.Transform``. You can find more details on implementing
3030
transforms and actual examples in :ref:`Transform <transform>` documentation.
3131

@@ -85,9 +85,9 @@ Note that ``check_transform`` is not a substitute for standard Unit Testing,
8585
you should also perform standard unit testing of your Transform's ``verify()``
8686
and ``apply()`` methods to check that all potential outcomes are covered.
8787

88-
You can find a full example of Transform creation and testing in checktransform.py_.
88+
You can find a full example of Transform creation and testing in check_transform.py_.
8989

90-
.. _checktransform.py: https://github.com/CryptoCredits/credits-common/blob/develop/examples/checktransform.py
90+
.. _check_transform.py: https://github.com/CryptoCredits/credits-common/blob/develop/examples/check_transform.py
9191

9292

9393
.. _step-by-step-get-network-upload:
@@ -97,18 +97,18 @@ Get a blockchain network and upload your code
9797

9898
Once your modules are written and tested locally - it's time to deploy a test
9999
blockchain network and see it in action. The easiest way to do this is to use
100-
our public PaaS, which is at the moment avaialable for free. You can register
100+
our public PaaS, which is at the moment available for free. You can register
101101
via the REST API and get a running network in few HTTP requests.
102102

103103
If you're working for a government agency and looking to use our GCloud PaaS API - the process is
104104
essentially same, except that your PaaS account will be disabled by default until we'll get you
105105
through the formal onboarding process.
106106

107107
The most complicated option would be to get Credits framework running on your own infrastructure.
108-
This is technically possible and not that complex, since we ship it in handy prebuilt Docker containers,
108+
This is technically possible and not that complex since we ship it in handy prebuilt Docker containers,
109109
but since at the moment Credits Core is a proprietary software - you will have to go through sales channel
110-
first and purchase license before we'll be able to hand the software to you.
111-
Also the PaaS registration and network bootstrap guide will not apply in this case.
110+
first and purchase a license before we'll be able to hand the software to you.
111+
Also, the PaaS registration and network bootstrap guide will not apply in this case.
112112

113113
Below are the API call steps needed to register with public PaaS and create
114114
a test blockchain network.
@@ -148,7 +148,7 @@ Patch token
148148

149149
After creating the organisation you need to patch your token with rights
150150
definitions to be able to access it. By default you would probably want to
151-
add all permissions at once, however in more complex access cases you may
151+
add all permissions at once, however, in more complex access cases you may
152152
have different tokens with specific access rights configured on each.
153153
See full permissions list in the :ref:`Paas API<paas-api>`.
154154

@@ -162,9 +162,10 @@ Create network
162162

163163
Assuming you have already developed and tested locally your transforms
164164
you can now provide it to bootstrap your blockchain. Please notice that module
165-
inclusion is a path to local file. You need to supply the module contents unescaped
166-
and fully intact including the linebreaks to preserve validity of the Python source,
167-
so it's not possible to include it's contents directly into the ``curl`` call string.
165+
inclusion is a path to a local file. You need to supply the module contents unescaped
166+
and fully intact including the line breaks to preserve the validity of the
167+
Python source, so it's not possible to include it's contents directly into
168+
the ``curl`` call string.
168169

169170
.. code-block:: bash
170171
@@ -207,11 +208,11 @@ Once your network is up and running - you can create the client side application
207208
for it. Essentially you will need to use the same modules that were uploaded to
208209
the network, but incorporate it into the client side application.
209210

210-
Of course the bulk of your clientside application is something we cannot
211+
Of course, the bulk of your clientside application is something we cannot
211212
define, it may be a web system, a mobile app, an IoT device etc.
212-
However the general requirements will be that it has to be able to
213+
However, the general requirements will be that it has to be able to
213214
persistently store client's keys, and will conform to the
214-
Transforms and Proofs interaces uploaded into the blockchain.
215+
Transforms and Proofs interfaces uploaded into the blockchain.
215216

216217
As an example here is the simple Python script that implements
217218
generating user's keys, dumping those to disk (persistence), creating valid
@@ -220,12 +221,49 @@ Transaction and sending it to the node's URL provided.
220221

221222
.. code-block:: python
222223
:linenos:
223-
print("hi")
224224
225+
#!/usr/bin/env python
226+
# -*- coding: utf-8 -*-
227+
import requests
228+
from credits.key import ED25519SigningKey
229+
from credits.address import CreditsAddressProvider
230+
from credits.proof import SingleKeyProof
231+
from credits.transaction import Transaction
225232
226-
You can also find this example in the blockchain_client.py_.
233+
# create a key for Alice using default key provider
234+
alice_key = ED25519SigningKey.new()
227235
228-
.. _blockchain_client.py: https://github.com/CryptoCredits/credits-common/blob/develop/examples/blockchain_client.py
236+
# create Alice's address using default address provider
237+
alice_address = CreditsAddressProvider(alice_key.to_string()).get_address()
238+
239+
# Saving the key to disk by marshalling it
240+
with open("alice_key.json", "w") as out:
241+
out.write(alice_key.marshall()
242+
243+
# Loading it would be also simple when you'll need it
244+
# with open("alice_key.json") as keyfile:
245+
# payload = json.load(keyfile)
246+
# alice_key = ED25519SigningKey.unmarshall(None, payload)
247+
248+
# create transform to send credits from Alice to Bob
249+
transform = BalanceTransform(amount=100, addr_from=alice_address, addr_to="bob_address")
250+
251+
# sign the needed proof with Alice' key
252+
proof = SingleKeyProof(alice_address, 1, transform.get_challenge()).sign(alice_key)
253+
254+
# form a transaction
255+
transaction = Transaction(transform, {alice_address: proof})
256+
257+
# POST your transaction to the node in your network
258+
requests.post("https://public.credits.works/api/v1/node/<your_node_name>/api/v1/transaction",
259+
headers={"Authorization": "<your_api_key>"},
260+
data={"transaction": json.dumps(transaction.marshall())}
261+
)
262+
263+
264+
You can also find this example in the sample_client.py_.
265+
266+
.. _sample_client.py: https://github.com/CryptoCredits/credits-common/blob/develop/examples/sample_client.py
229267
230268
231269
.. _step-by-step-connect-and-start:
@@ -234,7 +272,7 @@ Connect client application to the blockchain
234272
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
235273
236274
Once the application is written and deployed you can start transacting
237-
on the blockchain. If everything is done correcly before - nothing blockchain
238-
specific is needed at this step.
275+
on the blockchain. If everything is done correctly in the previous steps
276+
- nothing blockchain specific is needed at this level.
239277
240278

0 commit comments

Comments
 (0)