github.com/elastos/Elastos.ELA.SideChain.ETH@v0.2.2/README.md (about) 1 ## Elastos.ELA.SideChain.ESC 2 3 Elastos Sidechain implementation based on Go Ethereum. 4 5 6 ## Building the source 7 8 For prerequisites and detailed build instructions please read the 9 [Installation Instructions](https://github.com/elastos/Elastos.ELA.SideChain.ESC/wiki/Building-Ethereum) 10 on the wiki. 11 12 Building `geth` requires both a Go (version 1.16.5 or later) and a C compiler. You can install 13 them using your favourite package manager. Once the dependencies are installed, run 14 15 ```shell 16 export GO111MODULE=off 17 make geth 18 ``` 19 20 or, to build the full suite of utilities: 21 22 ```shell 23 export GO111MODULE=off 24 make all 25 ``` 26 27 ## Executables 28 29 The Elastos.ELA.SideChain.ESC project comes with several wrappers/executables found in the `cmd` 30 directory. 31 32 | Command | Description | 33 |:----------:|-------------| 34 | **`geth`** | Our main ELA-Ethereum CLI client. It is the entry point into the ELA-Ethereum network (main-, test- or private net), capable of running as a full node (default), archive node (retaining all historical state) or a light node (retrieving data live). It can be used by other processes as a gateway into the Ethereum network via JSON RPC endpoints exposed on top of HTTP, WebSocket and/or IPC transports. `geth --help` and the [CLI Wiki page](https://github.com/elastos/Elastos.ELA.SideChain.ESC/wiki/Command-Line-Options) for command line options. | 35 | `abigen` | Source code generator to convert Ethereum contract definitions into easy to use, compile-time type-safe Go packages. It operates on plain [Ethereum contract ABIs](https://github.com/ethereum/wiki/wiki/Ethereum-Contract-ABI) with expanded functionality if the contract bytecode is also available. However it also accepts Solidity source files, making development much more streamlined. Please see our [Native DApps](https://github.com/elastos/Elastos.ELA.SideChain.ESC/wiki/Native-DApps:-Go-bindings-to-Ethereum-contracts) wiki page for details. | 36 | `bootnode` | Stripped down version of our Ethereum client implementation that only takes part in the network node discovery protocol, but does not run any of the higher level application protocols. It can be used as a lightweight bootstrap node to aid in finding peers in private networks. | 37 | `evm` | Developer utility version of the EVM (Ethereum Virtual Machine) that is capable of running bytecode snippets within a configurable environment and execution mode. Its purpose is to allow isolated, fine-grained debugging of EVM opcodes (e.g. `evm --code 60ff60ff --debug`). | 38 | `gethrpctest` | Developer utility tool to support our [ethereum/rpc-test](https://github.com/ethereum/rpc-tests) test suite which validates baseline conformity to the [Ethereum JSON RPC](https://github.com/ethereum/wiki/wiki/JSON-RPC) specs. Please see the [test suite's readme](https://github.com/ethereum/rpc-tests/blob/master/README.md) for details. | 39 | `rlpdump` | Developer utility tool to convert binary RLP ([Recursive Length Prefix](https://github.com/ethereum/wiki/wiki/RLP)) dumps (data encoding used by the Ethereum protocol both network as well as consensus wise) to user friendlier hierarchical representation (e.g. `rlpdump --hex CE0183FFFFFFC4C304050583616263`). | 40 | `puppeth` | a CLI wizard that aids in creating a new Ethereum network. | 41 42 ## Running `geth` 43 44 Going through all the possible command line flags is out of scope here (please consult our 45 [CLI Wiki page](https://github.com/elastos/Elastos.ELA.SideChain.ESC/wiki/Command-Line-Options)), 46 but we've enumerated a few common parameter combos to get you up to speed quickly 47 on how you can run your own `geth` instance. 48 49 ### Full node on the main Ethereum network 50 51 By far the most common scenario is people wanting to simply interact with the Ethereum 52 network: create accounts; transfer funds; deploy and interact with contracts. For this 53 particular use-case the user doesn't care about years-old historical data, so we can 54 fast-sync quickly to the current state of the network. To do so: 55 56 ```shell 57 $ geth console 58 ``` 59 60 This command will: 61 * Start `geth` in fast sync mode (default, can be changed with the `--syncmode` flag), 62 causing it to download more data in exchange for avoiding processing the entire history 63 of the Ethereum network, which is very CPU intensive. 64 * Start up `geth`'s built-in interactive [JavaScript console](https://github.com/elastos/Elastos.ELA.SideChain.ESC/wiki/JavaScript-Console), 65 (via the trailing `console` subcommand) through which you can invoke all official [`web3` methods](https://github.com/elastos/wiki/wiki/JavaScript-API) 66 as well as `geth`'s own [management APIs](https://github.com/elastos/Elastos.ELA.SideChain.ESC/wiki/Management-APIs). 67 This tool is optional and if you leave it out you can always attach to an already running 68 `geth` instance with `geth attach`. 69 70 ### A Full node on the Ethereum test network 71 72 Transitioning towards developers, if you'd like to play around with creating Ethereum 73 contracts, you almost certainly would like to do that without any real money involved until 74 you get the hang of the entire system. In other words, instead of attaching to the main 75 network, you want to join the **test** network with your node, which is fully equivalent to 76 the main network, but with play-Ether only. 77 78 ```shell 79 $ geth --testnet console 80 ``` 81 82 The `console` subcommand has the exact same meaning as above and they are equally 83 useful on the testnet too. Please see above for their explanations if you've skipped here. 84 85 Specifying the `--testnet` flag, however, will reconfigure your `geth` instance a bit: 86 87 * Instead of using the default data directory (`~/.ela_ethereum` on Linux for example), `geth` 88 will nest itself one level deeper into a `testnet` subfolder (`~/.ela_ethereum/testnet` on 89 Linux). Note, on OSX and Linux this also means that attaching to a running testnet node 90 requires the use of a custom endpoint since `geth attach` will try to attach to a 91 production node endpoint by default. E.g. 92 `geth attach <datadir>/testnet/geth.ipc`. Windows users are not affected by 93 this. 94 * Instead of connecting the main Ethereum network, the client will connect to the test 95 network, which uses different P2P bootnodes, different network IDs and genesis states. 96 97 *Note: Although there are some internal protective measures to prevent transactions from 98 crossing over between the main network and test network, you should make sure to always 99 use separate accounts for play-money and real-money. Unless you manually move 100 accounts, `geth` will by default correctly separate the two networks and will not make any 101 accounts available between them.* 102 103 ### Full node on the Rinkeby test network 104 105 The above test network is a cross-client one based on the ethash proof-of-work consensus 106 algorithm. As such, it has certain extra overhead and is more susceptible to reorganization 107 attacks due to the network's low difficulty/security. Go Ethereum also supports connecting 108 to a proof-of-authority based test network called [*Rinkeby*](https://www.rinkeby.io) 109 (operated by members of the community). This network is lighter, more secure, but is only 110 supported by Elastos.ELA.SideChain.ESC. 111 112 ```shell 113 $ geth --rinkeby console 114 ``` 115 116 ### How to topup ELA to ETH 117 * use compile [ELA-Client](https://github.com/elastos/Elastos.ELA.Client) 118 * create topup transaction: `./ela-cli wallet -t create --deposit eth_address(ETH address) --amount recharge_value(amount ela units) --fee recharge_fee(fee ela units)` 119 * sign transaction: `./ela-cli wallet -t sign --file to_be_signed.txn -p yourpassword(your keystore password)` 120 * send transaction: `./ela-cli wallet -t send --file ready_to_send.txn` 121 122 ### How to withdraw ETH to ELA 123 * use node console: `node` 124 * use contract code: 125 126 ``` 127 Web3 = require("web3"); 128 // set web3 uri 129 web3 = new Web3("http://127.0.0.1:20636"); 130 // set withdraw contract 131 contract = new web3.eth.Contract([{"constant":false,"inputs":[{"name":"_addr","type":"string"},{"name":"_amount","type":"uint256"},{"name":"_fee","type":"uint256"}],"name":"receivePayload","outputs":[],"payable":true,"stateMutability":"payable","type":"function"},{"payable":true,"stateMutability":"payable","type":"fallback"},{"anonymous":false,"inputs":[{"indexed":false,"name":"_addr","type":"string"},{"indexed":false,"name":"_amount","type":"uint256"},{"indexed":false,"name":"_crosschainamount","type":"uint256"},{"indexed":true,"name":"_sender","type":"address"}],"name":"PayloadReceived","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"_sender","type":"address"},{"indexed":false,"name":"_amount","type":"uint256"},{"indexed":true,"name":"_black","type":"address"}],"name":"EtherDeposited","type":"event"}]); 132 // set eth account address 133 contract.options.address = deploy_contract_address; 134 // set account contract keystore info 135 acc = web3.eth.accounts.decrypt(keystore_content, ketstore_password); 136 // call receivePayload function,params:(ELA main chain address,amount(In ela up to convert wei 10000000000),fee) 137 cdata = contract.methods.receivePayload(ELA_address, withdraw_amount, fee).encodeABI(); 138 // gas minimum is 3000000,gasPrice is any value 139 tx = {data: cdata, to: contract.options.address, from: acc.address, gas: "3000000", gasPrice: "20000000000"}; 140 // send transaction amount(use receivePayload function amount) 141 tx.value = withdraw_amount; 142 acc.signTransaction(tx).then((res)=>{ 143 console.log("coming"); 144 stx = res; 145 console.log(stx.rawTransaction); 146 web3.eth.sendSignedTransaction(stx.rawTransaction).then(console)}); 147 ``` 148 *Note: ELA testnet server Deploy Contract Address: `0x491bC043672B9286fA02FA7e0d6A3E5A0384A31A` 149 ELA mainnet server Deploy Contract Address: `0xC445f9487bF570fF508eA9Ac320b59730e81e503`* 150 151 ### Configuration 152 153 As an alternative to passing the numerous flags to the `geth` binary, you can also pass a 154 configuration file via: 155 156 ```shell 157 $ geth --config /path/to/your_config.toml 158 ``` 159 160 To get an idea how the file should look like you can use the `dumpconfig` subcommand to 161 export your existing configuration: 162 163 ```shell 164 $ geth --your-favourite-flags dumpconfig 165 ``` 166 167 *Note: This works only with `geth` v1.6.0 and above.* 168 169 ### Programmatically interfacing `geth` nodes 170 171 As a developer, sooner rather than later you'll want to start interacting with Geth and the Ethereum 172 network via your own programs and not manually through the console. To aid this, Geth has built-in 173 support for a JSON-RPC based APIs ([standard APIs](https://github.com/ethereum/wiki/wiki/JSON-RPC) and 174 [Geth specific APIs](https://github.com/elastos/Elastos.ELA.SideChain.ESC/wiki/Management-APIs)). These can be 175 exposed via HTTP, WebSockets and IPC (unix sockets on unix based platforms, and named pipes on Windows). 176 177 The IPC interface is enabled by default and exposes all the APIs supported by Geth, whereas the HTTP 178 and WS interfaces need to manually be enabled and only expose a subset of APIs due to security reasons. 179 These can be turned on/off and configured as you'd expect. 180 181 HTTP based JSON-RPC API options: 182 183 * `--rpc` Enable the HTTP-RPC server 184 * `--rpcaddr` HTTP-RPC server listening interface (default: "localhost") 185 * `--rpcport` HTTP-RPC server listening port (default: 20636) 186 * `--rpcapi` API's offered over the HTTP-RPC interface (default: "eth,net,web3") 187 * `--rpccorsdomain` Comma separated list of domains from which to accept cross origin requests (browser enforced) 188 * `--ws` Enable the WS-RPC server 189 * `--wsaddr` WS-RPC server listening interface (default: "localhost") 190 * `--wsport` WS-RPC server listening port (default: 20635) 191 * `--wsapi` API's offered over the WS-RPC interface (default: "eth,net,web3") 192 * `--wsorigins` Origins from which to accept websockets requests 193 * `--ipcdisable` Disable the IPC-RPC server 194 * `--ipcapi` API's offered over the IPC-RPC interface (default: "admin,debug,eth,miner,net,personal,shh,txpool,web3") 195 * `--ipcpath` Filename for IPC socket/pipe within the datadir (explicit paths escape it) 196 197 You'll need to use your own programming environments' capabilities (libraries, tools, etc) to connect 198 via HTTP, WS or IPC to a Geth node configured with the above flags and you'll need to speak [JSON-RPC](http://www.jsonrpc.org/specification) 199 on all transports. You can reuse the same connection for multiple requests! 200 201 **Note: Please understand the security implications of opening up an HTTP/WS based 202 transport before doing so! Hackers on the internet are actively trying to subvert 203 Ethereum nodes with exposed APIs! Further, all browser tabs can access locally 204 running web servers, so malicious web pages could try to subvert locally available 205 APIs!** 206 207 ### Operating a private network 208 209 Maintaining your own private network is more involved as a lot of configurations taken for 210 granted in the official networks need to be manually set up. 211 212 #### Defining the private genesis state 213 214 First, you'll need to create the genesis state of your networks, which all nodes need to be 215 aware of and agree upon. This consists of a small JSON file (e.g. call it `genesis.json`): 216 217 ```json 218 { 219 "config": { 220 "chainId": <arbitrary positive integer>, 221 "homesteadBlock": 0, 222 "eip150Block": 0, 223 "eip155Block": 0, 224 "eip158Block": 0, 225 "byzantiumBlock": 0, 226 "constantinopleBlock": 0, 227 "petersburgBlock": 0 228 }, 229 "alloc": {}, 230 "coinbase": "0x0000000000000000000000000000000000000000", 231 "difficulty": "0x20000", 232 "extraData": "", 233 "gasLimit": "0x2fefd8", 234 "nonce": "0x0000000000000042", 235 "mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000", 236 "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000", 237 "timestamp": "0x00" 238 } 239 ``` 240 241 The above fields should be fine for most purposes, although we'd recommend changing 242 the `nonce` to some random value so you prevent unknown remote nodes from being able 243 to connect to you. If you'd like to pre-fund some accounts for easier testing, create 244 the accounts and populate the `alloc` field with their addresses. 245 246 ```json 247 "alloc": { 248 "0x0000000000000000000000000000000000000001": { 249 "balance": "111111111" 250 }, 251 "0x0000000000000000000000000000000000000002": { 252 "balance": "222222222" 253 } 254 } 255 ``` 256 257 With the genesis state defined in the above JSON file, you'll need to initialize **every** 258 `geth` node with it prior to starting it up to ensure all blockchain parameters are correctly 259 set: 260 261 ```shell 262 $ geth init path/to/genesis.json 263 ``` 264 265 #### Creating the rendezvous point 266 267 With all nodes that you want to run initialized to the desired genesis state, you'll need to 268 start a bootstrap node that others can use to find each other in your network and/or over 269 the internet. The clean way is to configure and run a dedicated bootnode: 270 271 ```shell 272 $ bootnode --genkey=boot.key 273 $ bootnode --nodekey=boot.key 274 ``` 275 276 With the bootnode online, it will display an [`enode` URL](https://github.com/ethereum/wiki/wiki/enode-url-format) 277 that other nodes can use to connect to it and exchange peer information. Make sure to replace the 278 displayed IP address information (most probably `[::]`) with your externally accessible IP to get the 279 actual `enode` URL. 280 281 *Note: You could also use a full fledged Geth node as a bootnode, but it's the less recommended way.* 282 283 #### Starting up your member nodes 284 285 With the bootnode operational and externally reachable (you can try 286 `telnet <ip> <port>` to ensure it's indeed reachable), start every subsequent `geth` 287 node pointed to the bootnode for peer discovery via the `--bootnodes` flag. It will 288 probably also be desirable to keep the data directory of your private network separated, so 289 do also specify a custom `--datadir` flag. 290 291 ```shell 292 $ geth --datadir=path/to/custom/data/folder --bootnodes=<bootnode-enode-url-from-above> 293 ``` 294 295 *Note: Since your network will be completely cut off from the main and test networks, you'll 296 also need to configure a miner to process transactions and create new blocks for you.* 297 298 #### Running a private miner 299 300 Mining on the public Ethereum network is a complex task as it's only feasible using GPUs, 301 requiring an OpenCL or CUDA enabled `ethminer` instance. For information on such a 302 setup, please consult the [EtherMining subreddit](https://www.reddit.com/r/EtherMining/) 303 and the [Genoil miner](https://github.com/Genoil/cpp-ethereum) repository. 304 305 In a private network setting, however a single CPU miner instance is more than enough for 306 practical purposes as it can produce a stable stream of blocks at the correct intervals 307 without needing heavy resources (consider running on a single thread, no need for multiple 308 ones either). To start a `geth` instance for mining, run it with all your usual flags, extended 309 by: 310 311 ```shell 312 $ geth <usual-flags> --mine --miner.threads=1 --etherbase=0x0000000000000000000000000000000000000000 313 ``` 314 315 Which will start mining blocks and transactions on a single CPU thread, crediting all 316 proceedings to the account specified by `--etherbase`. You can further tune the mining 317 by changing the default gas limit blocks converge to (`--targetgaslimit`) and the price 318 transactions are accepted at (`--gasprice`). 319 320 ## Contribution 321 322 Thank you for considering to help out with the source code! We welcome contributions 323 from anyone on the internet, and are grateful for even the smallest of fixes! 324 325 If you'd like to contribute to Elastos.ELA.SideChain.ESC, please fork, fix, commit and send a pull request 326 for the maintainers to review and merge into the main code base. If you wish to submit 327 more complex changes though, please check up with the core devs first on [our gitter channel](https://gitter.im/elastos/Elastos.ELA.SideChain.ESC) 328 to ensure those changes are in line with the general philosophy of the project and/or get 329 some early feedback which can make both your efforts much lighter as well as our review 330 and merge procedures quick and simple. 331 332 Please make sure your contributions adhere to our coding guidelines: 333 334 * Code must adhere to the official Go [formatting](https://golang.org/doc/effective_go.html#formatting) 335 guidelines (i.e. uses [gofmt](https://golang.org/cmd/gofmt/)). 336 * Code must be documented adhering to the official Go [commentary](https://golang.org/doc/effective_go.html#commentary) 337 guidelines. 338 * Pull requests need to be based on and opened against the `master` branch. 339 * Commit messages should be prefixed with the package(s) they modify. 340 * E.g. "eth, rpc: make trace configs optional" 341 342 Please see the [Developers' Guide](https://github.com/elastos/Elastos.ELA.SideChain.ESC/wiki/Developers'-Guide) 343 for more details on configuring your environment, managing project dependencies, and 344 testing procedures. 345 346 ## License 347 348 The Elastos.ELA.SideChain.ESC library (i.e. all code outside of the `cmd` directory) is licensed under the 349 [GNU Lesser General Public License v3.0](https://www.gnu.org/licenses/lgpl-3.0.en.html), 350 also included in our repository in the `COPYING.LESSER` file. 351 352 The Elastos.ELA.SideChain.ESC binaries (i.e. all code inside of the `cmd` directory) is licensed under the 353 [GNU General Public License v3.0](https://www.gnu.org/licenses/gpl-3.0.en.html), also 354 included in our repository in the `COPYING` file.