-
Notifications
You must be signed in to change notification settings - Fork 1
Tutorial Geth
Let's answer to this question with a presentation of some various networks that Geth can works with :
- Main : Full full network. Needs real Ether which can take a long time to mine. Takes hours to a fast sync for the first time.
- Testnet : Test network which you can get free Ether or mine it quickly. Takes hours to do a fast sync for the first time.
- Dev : Local network which you can mine on very quickly (almost immediately). No sync required so you can start working quickly. Downside is that it would not have any contracts that are deployed by Ethereum or other developers.
You can't mine in main network with a basic computer, nor than a gaming one. For more informations see :
- https://motherboard.vice.com/en_us/article/59zdjq/an-idiots-guide-to-building-an-ethereum-mining-rig
- https://www.cryptocompare.com/mining/guides/how-to-build-an-ethereum-mining-rig
Let's set up Testnet to sync and in the meantime we can set up Dev to on our first contract.
Open a new terminal and type :
geth --testnet --syncmode "fast" --cache=512 console
This will start Geth up in fast sync mode and it will start downloading the Blockchain. It has completed the sync when it has downloaded the latest header. You can check the latest header on the testnet here and then run this command in the geth console :
$ web3.eth.blockNumber
You can increase the cache size if you have enough RAM in order to reduce sync time.
Also, you can change the syncmode
as specified here.
The console
parameter will start after the sync an interactive JavaScript environment.
Untils it finised, we will go to Dev network to make some test.
cd ~
geth --datadir ./.ethereum/devnet --dev console
You’ll see there are no blocks syncing :
$ web3.eth.blockNumber
0
That’s it, Dev network is set up.
We need to create an account to mine, and then get Ether.
$ personal.newAccount()
Passphrase:
Repeat passphrase:
“0x...”
$ personal.listAccounts
[“0x...”]
Don't loose your passphrase or your account will be lost !
You can access to a specified account with the array eth.accounts
.
So we can check how much Ether we have like this :
$ eth.getBalance(eth.accounts[0])
The answer is 0. To get Ether, we have to mine, even in a test environment.
$ exit
More about accounts management here.
We can mine in two different ways :
- Start a new terminal like this :
geth --datadir ./.ethereum/devnet --dev --mine --minerthreads 1 --etherbase 0
where :
-
minethreads
is the number of CPU cores to use, -
etherbase
is which account to send the Ether too (0 being first in the list frometh.accounts[0]
). -
In the previous console type :
miner.start(8)
to start andminer.stop()
to stop, where8
is the number of threads.
Let's work this miner, and open a new terminal.
Connect to the client that is running through the miner :
geth --datadir ./.ethereum/devnet --dev attach ipc:./.ethereum/devnet/geth.ipc
and then type like before how much Ether we have :
$ eth.getBalance( eth.accounts[0] )
<a big number>
The Greeter contract is the Ethereum Hello World. The contract is writing in Solidity.
Greeter contract
pragma solidity ^0.4.9;
contract mortal {
/* Define variable owner of the type address*/
address owner;
/* this function is executed at initialization and sets the owner of the contract */
function mortal() {
owner = msg.sender;
}
/* Function to recover the funds on the contract */
function kill() {
if (msg.sender == owner) {
selfdestruct(owner);
}
}
}
contract greeter is mortal {
/* define variable greeting of the type string */
string greeting;
/* this runs when the contract is executed */
function greeter(string _greeting) public {
greeting = _greeting;
}
/* main function */
function greet() constant returns (string) {
return greeting;`
}
}
To compile our Smart Contract, there's two way :
- Use
solc
compiler. - Use Ethereum online compiler called Remix.
We choose the second way as it's more simple to use.
Go to link
and copy our contract.
It will be automatically compiled. Copy the text from the right titled 'Web3 deploy' in 'details' to a text file.
You should replace the /* var of type uint8 here */ ;
with your text like Hello world !
and replace contract name as you want.
Copy past the string got Geth and you should see something like this (it can take up to a minute for the 'mined' response, so be patient) :
undefined
$ null [object Object]
Contract mined! address: 0x... transactionHash: 0x...
You can now test if your first contract works like this :
$ greeter.address
“0x...”
$ greeter.greet();
“Hello world !”
If testnet has finised its sync, you can repeat the previous operations on it !