-
Notifications
You must be signed in to change notification settings - Fork 604
Running the Testnet
This guide assumes that you already installed the pyethapp, if not see the Getting Started Page.
Using a testnet, allows users to access a network w/o having to have real Ethers. It is therefore practical to use for contract-testing/developing purposes. It's easily accessible through all clients and has a relatively small state size. Compared to a private chain, the main advantage the testnet has, is that one normally does not have to mine to get transactions and contracts on the chain. Usually there are already a few nodes in the network doing this for you w/o having hefty mining power. So you still can start mining and will get some Ether every now and then even on a mortal machine.
To access the testnet, called morden network, use the option --profile morden
.
Additionally one has to set the state data directory. Where the state is the underlying blockchain/database of the network in use. Hence the live network will have a different state than the testnet.
Create a new folder to store the state of the morden network and specifiy the data directory to this folder using the option --datadir '../pyethapp/state'
. Where 'state' is the newly created state folder for the morden network.
An example call would look like this:
pyethapp --profile morden --data-dir '/home/user/Ethereum/pyethapp/state' run
After you did that you will automatically sync with the testnet. This step takes some time, so go ahead and grab a coffee.
-
For creating, unlocking Accounts, as well as how to get some free Ether (on the testnet) see here.
-
For how to send transactions and creating contracts see here
Having the advantages of a testnet in place, it only makes sense to start developing and deploying contracts on it.
So let's implement an interface for the roulette-contract seen here.
What we want to do in this example is to interact with our roulette-contract on the Blockchain via a HTML file using some browser. So the challenge becomes, how to close the bridge between this HTML file and the full node, for example the pyethapp node on our local machine.
What we need to do is to open our node to potential listeners by opening a JSON-RPC endpoint. Each client-node has a different default endpoint and different options to open them. The Python client opens this endpoint by default.
On the other side, we need to specify in our HTML file how it could become such a listener. The best way to interact from a HTML file with a Ethereum node is by using the Ethereum compatible JavaScript API - web3.js. To install web3.js execute the following code on your command line:
> mkdir PyCasino
> cd PyCasino
> npm install web3
> cd web3
See here on how to use web3.js. Now let's tell our HTML file to import the web3.js library and specify on which server and port our client-node is. As seen before the default Python endpoint is http://localhost:4000
. Hence your JS part in your HTML file should look somewhat similar to this:
<script type="text/javascript" src="../dist/web3.js"></script>
<script type="text/javascript">
if(typeof web3 !== 'undefined')
web3 = new Web3(web3.currentProvider);
else
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:4000"));
</script>
See here for more examples.
Note to start a server hosting the HTML file one can just change to the directory of the HTML file and use the python -m SimpleHTTPServer
command via the console. This host the contents of your HTML file on 'http://localhost:8000'.
So now we opened our node to every listener and specified how our HTML file can listen to the open rpc-port, our HTML file should therefore be able, using the respective default endpoint, to connect to the client. But no, most browsers won't allow this and returning an error similar to this (from the chrome browser):
XMLHttpRequest cannot load http://localhost:4000/. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8000' is therefore not allowed access.
Mozilla writes the following:
A resource makes a cross-origin HTTP request when it requests a resource from a different domain than the one which served itself. For example, an HTML page served from http://domain-a.com makes an src request for http://domain-b.com/image.jpg. Many pages on the web today load resources like CSS stylesheets, images and scripts from separate domains. For security reasons, browsers restrict cross-origin HTTP requests initiated from within scripts.
Well it's true by requesting data from http://localhost:8000 to the default json-rpc endpoint (http://localhost:4000) we made a cross-origin HTTP request.
To allow the cross-origin HTTP request we need to mitigate the possible sources to our node. This can be done using the -c jsonrpc.corsdomain='http://localhost:8000'
command. Opening our node specifically to http://localhost:8000
Since we want to have a nice looking interface we use the css-file from the dapp-styles package. In your NameReg directory type:
> git clone https://github.com/ethereum/dapp-styles#css
Then one needs to link again the css-file in our html-file. This should look somewhat similar to:
<link rel="stylesheet" type="text/css" href="dapp-styles-master/dist/dapp-styles.css">
<script type="text/javascript" src="../dist/web3.js"></script>
<script type="text/javascript">
if(typeof web3 !== 'undefined')
web3 = new Web3(web3.currentProvider);
else
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:4000"));
</script>
To see an example how such an interface could look like see here.