Remove Geth Chaindata Mac
然后回车执行,如果返回如下信息,说明Go已经安装成功: go version go1.10 windows/amd64.
by Zack
When I first started developing on the Ethereum platform, syncing a node was one of the first few things I did. With no one to hold my hand and nowhere to consolidate all the common errors I encountered, I wasted weeks just syncing a node. It was such a headache that I almost gave up.
Not knowing the terminology only made it harder.
And so, in this article, I want to consolidate all the common errors you might face and explain the reasons behind each step when syncing a node. Hopefully, you won’t have such a nasty early experience as I did.
A quick intro
In Ethereum, developers create pieces of an application that run on the network. These are called smart contracts. Although you can deploy them manually without syncing to an Ethereum node, in the long run it’s more convenient for the development process to sync (especially when we want to use development frameworks like Truffle).
To sync an Ethereum node, we will need the following software:
- Geth — Client for an Ethereum node.
- Ethereum Wallet — User interface for an Ethereum node.
Let’s get started.
Install Geth
To download Geth, go here for Windows users. Then click on the “Geth for Windows” button.
For MacOS users, I recommend that you download using homebrew. You can do so with the following commands:
Refer to Geth’s instructions for more details.
Check that Geth is installed properly by typing geth version
in the terminal (MacOS) or PowerShell (Windows).
Install Ethereum Wallet
We call the GUI that interacts with the network the “wallet”. You can find many different wallets for Ethereum with a quick Google search (such as Parity, Jaxx, and MyEtherWallet). I personally like using Ethereum Wallet. It has a user-friendly interface, and I’m also a bit biased because it is developed by Ethereum itself.
You can find the installer here.
Note: I prefer Ethereum Wallet to Mist. Mist is basically a browser that renders decentralized applications (dApps) and websites.
For the development of a smart contract, we will only need the Ethereum Wallet.
Mainnet vs Testnet
In Ethereum, there are two main networks: the mainnet and the testnet.
The mainnet is used to transact real Ether. The Ether’s value is tied to real fiat currency via cryptocurrency exchanges.
As developers, we do not want to run application tests with real money. That is what the testnet is for.
We call the testnet Ropsten.
Run Geth and Ethereum Wallet
When you’re developing a smart contract, you should sync the testnet first. We will only need to sync the mainnet when we are ready to deploy.
You will need about 30GB of storage space to sync a testnet. As there are more transactions in the mainnet, you will need about 100GB to sync a mainnet.
Note: some say that you need an SSD storage for fast writing so that the sync can catch up with the latest block. Personally, I find that HDD storage is alright. However, if given the choice, I would definitely use SSD storage.
For Windows users
For easy access, I recommend that you create a folder to store the blockchain. For example, “C:EthereumTestnet”.
After creating the folder, try running the following command:
Edit: It seems like some arguments have changed. If you encountered such error “flag provided but not defined: -data-dir”, try changing the argument name to — datadir instead to states where I stored my chaindata, which is the exact same one I specified for Geth.
For Mac Users
It is slightly simpler for MacOS, because the chaindata is downloaded automatically to the library and not hidden from us. So, we won’t need to specify the data directory.
Nevertheless, I recommend creating a script file to make running Geth and Ethereum Wallet easier.
Note: Geth has to run before Ethereum Wallet.
Running Ethereum Wallet alone will automatically start the syncing process as it will automatically run a Geth client in the background. This is user-friendly, but does not enable the RPC services we want to use. Thus, we want to ensure RPC is enabled in both our Geth and Ethereum Wallet execution.
A few notes
- The syncing process is very long, and can take up to 2–3 days. Please have patience and consider leaving your computer turned on overnight.
- The syncing speed depends on your internet speed, peers count, and writing speed of your storage drive.
- As the data are stored in blocks and linked together, corruption in one block can corrupt the whole chaindata. This can potentially waste your effort in waiting days for the node to sync. Therefore, it is very important to shut down your Geth properly. In certain cases, you might want to rollback. But prevention is better than cure here.
- The progress bar on your Ethereum Wallet is NOT accurate. Relying on it will give you a lot of anxiety and frustration.
- Geth runs on port 30303 for external listening.
- The default port used for internal communication, for example between your wallet and Geth, is 8545.
Make sure your port to Geth client is opened
It’s really important to make sure that the connection to your Geth client is not limited. One huge headache I encountered was to let my firewall limit the number of connections I could have through the Geth client.
You can spot this problem by looking at the peers count. If it stays consistently low at about 1–3 peers (for at least half an hour), there is a good chance your connection is limited. A healthy range is above 5 peers.
MacOS users
When you run Geth, there should be a notification for you to allow connection. Well, of course, click on “Allow.
To check, go to System Preferences> System & Privacy.
Under Firewall tab, click on Firewall Options.
Windows users
To open your port, go to Control panel > System and Security > Windows defender firewall.
Click on Advanced settings. On the side panel, click on Inbound Rules.
The inbound ports you want to open are TCP and UDP 30303. So I created one rule for TCP 30303 and another for UDP 30303.
You may need to open your outbound ports for TCP 30303 too.
Note that any third party firewall/anti-virus might limit your connections as well, so make sure to configure it accordingly.
Attach to Geth
To retrieve more information about your node, you can attach to the Geth client and execute commands with it using the RPC services.
Here’s a simple way to check your syncing status: attach to the client by entering the following command on a separate terminal/console.
geth attach http://127.0.0.1:8545
Again, you might want to store it in a batch/script file for your own convenience.
After running the command, you should see something like this.
Type web3.eth
and you should see a lot of information. To retrieve specific information on syncing, type web3.eth.syncing
instead.
As you can see, the progress bar found at the top of the Ethereum Wallet is merely a comparison between the highestBlock and the currentBlock. As the highestBlock known to your computer might not be the actual highest block, the progress bar might not reflect the real progress.
In fact, the highestBlock and knownStates will continue to increase as you sync your nodes.
Some final points
As the technology is rapidly changing, always use the stable version of the client and wallet to avoid giving yourself more headaches.
Also, note that there is always a workaround for any of the issues you face. When you face a problem syncing, you can try to search for solutions as you are likely not the first one to face the same problem.
And always remind yourself that you do not need to sync a node to develop a smart contract. Doing so only helps to facilitate your understanding of the environment as well as your development process. So, don’t beat yourself up over it if you encounter problems so early in the development process.
For more of such articles, follow me or visit my site — A developer’s perspective.
We have seen in the first part of this tutorial series what a smart contract is, and how it works. Now, we will deploy it to two kinds of test networks.
The tools we will use
The most prominent tools at the moments are:
- Truffle: A development environment, testing framework and asset pipeline for Ethereum. In other words, it helps you develop smart contracts, publish them, and test them, among other things. You can read the docs of the Truffle suite for more informations.
- Ganache: It was called TestRPC before, if you have read a tutorial from a few months ago, chances are they use TestRPC with Truffle, but it was renamed upon the integration of TestRPC within the Truffle Suite. What Ganache does is simple, it creates a virtual Ethereum blockchain, and it generates some fake accounts that we will use during development.
- Mist: It’s a browser for decentralized web apps. It seeks to be the equivalent of Chrome or Firefox, but for Dapps. It’s still insecure and you shouldn’t use it with untrusted dapps as of yet.
- Ethereum wallet: It’s a version of Mist, but only opens one single dapp, the Ethereum Wallet. Mist and Ethereum Wallet are just UI fronts. And we need a core that will connect us to an Ethereum blockchain(It could be the real Ethereum blockchain, or a test one).
- Geth: Is the core application on your computer that will connect you to a blockchain. It can also start a new one (in our case we will create a local test blockchain), create contract, mine ether etc.
We will start by using Truffle and Ganache, and then use Truffle with geth and Mist.
Installations
The requirements for this tutorial are that you know what is and how to use a command-line tool, and you are a bit familiar with NPM.
Truffle
Open up a command-line, and type the next part:
If something goes wrong I advise you to read more on Truffle’s docs.
Ganache
Then, install Ganache’s command-line interface:
If you are unsure about something, here is Ganache’s Github page.
N.B: There is a GUI for Ganache, but because we are such haxors, we will use the CLI.
Let’s start
First, create a new folder, and type the next line:
It will initialize an empty truffle project.
Then copy the Wrestling.sol file from the last tutorial, into the folder “contracts”.
Next, open the folder “migrations” and create a new file named “2_deploy_contracts.js”. Migrations are simply scripts that’ll help us deploy our contracts to a blockchain.
Paste the following code inside, and save.
The first line is there to import the “Wrestling.sol” file from the “contracts” folder, and the 4th line deploys it to the blockchain.
Now, back to the root folder, you’ll see two files, “truffle.js” and “truffle-config.js”. If you are on Windows, remove “truffle.js”, if you are on another system, remove one of them, or keep them both, it’s not important. The reason for that is, on Windows there is a naming problem, and when we will want to execute Truffle commands, it will open the config file “truffle.js” instead of reading what’s inside.
I’m writing this tutorial on Windows, so I’ll remove the “truffle.js” file. Then I’ll put this code inside of “truffle-config.js”:
It basically says, that, when using the development network, connect to the host at “127.0.0.1” (localhost), using the port 7545.
Now we are ready to test our code on a blockchain!
Testing our code
In the first part, we will use Ganache.
Fire a new command-line and type in the following command:
What it does is, it tells ganache-cli to start at the port 7545.
Ganache will generate test accounts for us, they have 100 ether by default, and are unlocked so we can send ether from them freely. The first account for example is the guy right here:
Now, back to our first command-line interface, we execute two commands:
Compile will compile our Solidity code to bytecode (the code that the Ethereum Virtual Machine (EVM) understands), in our case, Ganache emulates the EVM.
Migrate will deploy the code to the blockchain, in our case, the blockchain could be found in the network “development” we set earlier in the “truffle-config.js” file.
Now, if everything has gone as expected, you should see this on your terminal:
Notice that it shows the address of the instantiated Wrestling contract.
On the commande-line interface where ganache-cli is run, you can see the transactions being executed:
Notice that it shows the address of the instantiated Wrestling contract.
Now type the following command to start Truffle’s console. It will help us interact with ganache’s blockchain.
First, we will execute this commands:
It will assign the address of the first account to the variable account0, the second to the variable account1. Web3 is a JavaScript API that wraps RPC calls to help us interact with a blockchain in an easy way.
Then we will write this:
WhatSize 7.2.1 Crack Mac lets you shortly measure the scale in bytes of a given folder and all subfolders and information inside it.You’ll be stunned at what number of ineffective information could be laying round in your onerous disks. The information and folders are robotically sorted by dimension, with the largest sizes first. Whatsize mac crack.
It assigns a reference to the instance of the contract that truffle deployed to the variable “WrestlingInstance”.
Execute the the next line:
It will return the address of the wrestler1, in our case it was the first account. Truffle, during the migration, picked up the default account from Ganache, which is the first account, because we didn’t specify another account address during the migration, or another address in the configuration file of Truffle.
We will then register the second account as an opponent:
Here, the “from” directive tells the function from which account the transaction should be fired.
Upon executing the line, it should return something similar to this:
Notice that the transaction used Gas, and it fired the event “WrestlingStartsEvent”.
You could retrieve the address of the second wrestler by executing the following line:
Now, the players can wrestle:
The “value” directive is used to send ether with a transaction. The “web3.toWei(5, “ether”)” part sends 5 ether, that value is converted to Wei. Wei is the base unit (lowest denomination) of ether. More infos could be found at this link.
Upon executing the last line, the account1 will be the winner, since we put 23 ether in total with it, and it’s more than the double of what we put with the account0.
A little exercise for you is to withdraw the ether from the contract.
Now, it’s up to you to research how to use Truffle’s and Ganache’s advanced features. You can start by reading the docs, and alternatively, here is an excellent introduction to Truffle if you felt lost or want to reinforce your knowledge on what we just saw.
How geth will fit in the picture
Now, if we used Ganache for development, we would want to use something closer to a real environment, if only just to be more familiar with it.
Installations
Start by downloading geth. On Windows, you might need to add geth’s installation folder to your PATH variable.
Download Mist or Ethereum Wallet. For our use, both will be alike, so it doesn’t matter which one you pick.
Onto the creation of the local private test network
In the same root folder, create a new file, and name it “genesis.json”. Then past the following content in it.
The file “genesis.json” is simply the configuration file that geth needs to create a new blockchain. It’s not important to understand the content of the file for the moment.
If you run geth without specifying any parameter, it will try to connect to the mainnet. The mainnet is the main network of Ethereum, the real blockchain of Ethereum.
If you run Mist without specifiying any parameter, it will throw an error if an instance of geth is running. If you tell Mist to connect to the geth instance that is actually running(what we will do in a little moment) it’ll work just fine. If you run Mist while no instance of geth is running, it will start a new instance of geth, and eventually ask you from which blockchain network it should download blocks.
There is a public Ethereum test network, but we will create our own private test network locally, using the “genesis.json” file we created earlier.
Fire up another command-line interface and type in the following (be sure to run it in your project root folder):
We launch geth and specify where our blockchain will be stored, here in the “chaindata” folder (it will be automatically generated), and we initialize it with our “genesis.json” configuration file.
We then start geth using the following command:
The “--rpc” parameter tells geth to accept RPC connections, it’s needed so that truffle can connect to geth.
Open another command-line interface, and start Mist (or Ethereum Wallet), using the same parameters:
The “-rpc” tells Mist (or Ethereum Wallet) to connect to the instance of geth we started.
In the Wallets tab, press Add Account and create a new wallet:
Notice that we are using a private network. Beware of that, you wouldn’t want to use you ether on the Mainnet for development purposes.
I will create an account using the password “123456789”. In a real environment, use a stronger password.
Open up a new command-line interface and run this command:
It will run the console of geth, and we can now interact with it.
With out main account created on the Mist UI, we will run this command inside of “geth attach” console:
It will start a miner, the process that will confirm transactions, and after a few seconds or minutes (depending or your computer), you should start seeing ether being added to your balance (And also to your main account):
Note that if you don’t have enough RAM available, it may not start mining at all. You can stop the mining using the command “miner.stop()”.
Now, open the “truffle-config.js” file again, and modify it like this:
The “ourTestNet” is the configuration necessary to connect to the geth instance. Geth starts by default on port 8545.
In the command-line interface where we launched “geth attach”, we will unlock the account, so we can use it to migrate the Smart Contract from Truffle, using the following command:
Here, I used the address of the account I just created, it would be a different address for you, and the password “123456789” that I’ll use for these tests. Notice that the password is shown in plain text, and you should use a different method with a real account.
Now, back to the command-line interface where we previously launched Truffle, run this command:
It will start migrating the contract to the blockchain that geth is running. Start the miner if you previously stopped it, otherwise, the migration will not be executed.
If the migration was successful, you would see an output like this:
Now, run the following command to start Truffle’s console:
Then run these two commands:
You should have an output like this:
The first line returns the address of the deployed Wresting contract instance.
The second line, will return the Wresting contract ABI. The ABI is basically a description of the contract. It contains a list of its functions and variables among other things.
When copying the address and the ABI, remove the apostrophes highlighted by a red arrow in the screenshot.
Now, back to Mist, we will open the contracts tab, and click on watch contract.
Then we will past the address and the ABI of the Wrestling contract we deployed:
Click on “OK”, then it will be shown in your watched contracts list. Click on it and it will open the contract page. If you scroll down, you’ll see something like this:
Use the select a function part to interact with the contract. It’s the same as we did above using Truffle’s console.
And that’s it, we saw how Ganache and geth come in play. When you will want to deploy a contract to the real blockchain, you should use the second method and connect geth with the mainnet.
N.B: You could deploy a contract directly on Mist without using Truffle migration system, here is an example video of that process. Using Truffle is more interesting in a real development process though, since you’ll be able to include and import multiple other smart contracts and scripts if you use a modular approach to develop your smart contracts.
N.B.2: You could always write your contract code on a basic nodepad application, and deploy it to the mainnet using some trusted third party’s portal, but I advise you to not.
The repository for this tutorial can be found here:
In conclusion
We have seen 4 ways of developing and deploying our Smart Contracts:
- The first one is to use Truffle and Ganache. Since we copied the code from the last tutorial, I want to tell you that there are plugins available for the most popular text editors and IDEs. Some offer only syntax highlighting, while others help with other areas.
- The second one is to deploy code from Truffle to geth (and the GUI app Mist).
- The third, is to use Remix to write small, simple contract when you are just learning Solidity, and deploying the code in Mist like shown in the video previously linked.
- Or like a real cowboy, you could use a simple text editor to write and then deploy your untested contract using a nameless third party’s drag-and-drop deployment feature.
In the next part, we will discuss security since our “Wrestling” script is far from being ready to be launched in a real environment.
After, that we will see Token creation, and Initial Coin Offerings (ICOs).
If you liked this second part, you can find me on twitter @dev_zl.