天天看點

【以太坊】Debugging Ethereum-cppDependsBuildInstallDebuggingWalletDataDirKeyIdGenesisConsolePrivateChain

# Source

Depends

$ cd cpp-ethereum 
$ sh scripts/install_deps.sh
           

Build

NOTE: remove GUI, MINIUPNPC, VMJIT, TESTS etc. from CMakeLists.txt, to accelerate building:
$ mkdir build
$ cd build
$ cmake -DCMAKE_BUILD_TYPE=Debug ../
           

or configure as following:

-- The C compiler identification is GNU .
-- The CXX compiler identification is GNU .
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done

------------------------------------------------------------------------
-- Configuring cpp-ethereum
------------------------------------------------------------------------
--                  CMake Version                            .
-- CMAKE_BUILD_TYPE Build type                               Debug
-- TARGET_PLATFORM  Target platform                          Linux
--------------------------------------------------------------- features
--                  Hardware identification support          
--                  HTTP Request support                     
-- VMTRACE          VM execution tracing                     OFF
-- PROFILING        Profiling support                        OFF
-- FATDB            Full database exploring                  ON
-- ROCKSDB          Prefer rocksdb to leveldb                OFF
-- OLYMPIC          Default to the Olympic network           OFF
-- PARANOID         -                                        OFF
------------------------------------------------------------- components
-- ETHASHCL         Build OpenCL components                  ON
-- SOLIDITY         Build Solidity                           ON
------------------------------------------------------------------------

-- Found jsoncpp: /usr/lib/x86_64-linux-gnu/libjsoncpp.so  
-- JsonCpp headers: /usr/include/jsoncpp
-- JsonCpp lib   : /usr/lib/x86_64-linux-gnu/libjsoncpp.so
-- Found json_rpc_cpp: /usr/local/include (found suitable version "0.6.0", minimum required is "0.4") 
-- json-rpc-cpp headers: /usr/local/include
-- json-rpc-cpp lib   : /usr/local/lib/libjsonrpccpp-common.so;/usr/local/lib/libjsonrpccpp-server.so;/usr/local/lib/libjsonrpccpp-client.so
-- Found CURL: /usr/lib/x86_64-linux-gnu/libcurl.so  
-- curl headers: /usr/include
-- curl lib   : /usr/lib/x86_64-linux-gnu/libcurl.so
-- Found leveldb: /usr/lib/x86_64-linux-gnu/libleveldb.so  
-- Found CryptoPP: /usr/include (found suitable version "5.6.2", minimum required is "5.6.2") 
-- CryptoPP headers: /usr/include
-- CryptoPP lib   : /usr/lib/libcryptopp.so
-- Cpuid headers: CPUID_INCLUDE_DIR-NOTFOUND
-- Cpuid lib   : CPUID_LIBRARY-NOTFOUND
-- Looking for CL_VERSION_2_0
-- Looking for CL_VERSION_2_0 - found
-- Found OpenCL: /usr/lib/x86_64-linux-gnu/libOpenCL.so. (found version "2.0") 
-- OpenCL headers: /usr/include
-- OpenCL lib   : /usr/lib/x86_64-linux-gnu/libOpenCL.so.
-- Found miniupnpc: /usr/lib/x86_64-linux-gnu/libminiupnpc.so  
-- miniupnpc headers: /usr/include
-- miniupnpc lib   : /usr/lib/x86_64-linux-gnu/libminiupnpc.so
-- Found mhd: /usr/lib/x86_64-linux-gnu/libmicrohttpd.so  
-- microhttpd headers: /usr/include
-- microhttpd lib   : /usr/lib/x86_64-linux-gnu/libmicrohttpd.so
-- jsonrpcstub location    : /usr/local/bin/jsonrpcstub
-- test: ClientBase |  --eth_testfile=BlockTests/bcJS_API_Test --eth_threads=; --eth_testfile=BlockTests/bcJS_API_Test --eth_threads=; --eth_testfile=BlockTests/bcJS_API_Test --eth_threads=; --eth_testfile=BlockTests/bcValidBlockTest --eth_threads=; --eth_testfile=BlockTests/bcValidBlockTest --eth_threads=; --eth_testfile=BlockTests/bcValidBlockTest --eth_threads=
-- test: JsonRpc |  --eth_testfile=BlockTests/bcJS_API_Test; --eth_testfile=BlockTests/bcValidBlockTest
-- Found gmp: /usr/lib/x86_64-linux-gnu/libgmp.so  
-- Gmp headers: /usr/include/x86_64-linux-gnu
-- Gmp lib   : /usr/lib/x86_64-linux-gnu/libgmp.so
-- Configuring done
-- Generating done
-- Build files have been written to: /root/workspace/github/cpp-ethereum/build

           
$ make
Scanning dependencies of target cpp-ethereum_BuildInfo.h
[  %] Built target cpp-ethereum_BuildInfo.h
Scanning dependencies of target secp256k1
[  %] Building C object utils/secp256k1/CMakeFiles/secp256k1.dir/secp256k1.c.o
Linking C static library libsecp256k1.a
[  %] Built target secp256k1
Scanning dependencies of target devcore
...
[ %] Building CXX object libp2p/CMakeFiles/p2p.dir/Common.cpp.o
Linking CXX shared library libp2p.so
[ %] Built target p2p
Scanning dependencies of target ethereum
[ %] Building CXX object libethereum/CMakeFiles/ethereum.dir/ClientTest.cpp.o
[ %] Building CXX object libethereum/CMakeFiles/ethereum.dir/Utility.cpp.o
[ %] Building CXX object libethereum/CMakeFiles/ethereum.dir/GasPricer.cpp.o
[ %] Building CXX object libethereum/CMakeFiles/ethereum.dir/Account.cpp.o
[ %] Building CXX object libethereum/CMakeFiles/ethereum.dir/CachedAddressState.cpp.o
[ %] Building CXX object libethereum/CMakeFiles/ethereum.dir/LogFilter.cpp.o
[ %] Building CXX object libethereum/CMakeFiles/ethereum.dir/Client.cpp.o

...

[ %] Building CXX object test/libweb3core/test/CMakeFiles/testweb3core.dir/libp2p/rlpx.cpp.o
[ %] Building CXX object test/libweb3core/test/CMakeFiles/testweb3core.dir/libp2p/eip-cpp.o
[ %] Building CXX object test/libweb3core/test/CMakeFiles/testweb3core.dir/libp2p/capability.cpp.o
Linking CXX executable testweb3core
[ %] Built target testweb3core
Scanning dependencies of target testweb3
[ %] Building CXX object test/webthree/test/CMakeFiles/testweb3.dir/test.cpp.o
[ %] Building CXX object test/webthree/test/CMakeFiles/testweb3.dir/libwhisper/shhrpc.cpp.o
[ %] Building CXX object test/webthree/test/CMakeFiles/testweb3.dir/libwhisper/whisperMessage.cpp.o
[ %] Building CXX object test/webthree/test/CMakeFiles/testweb3.dir/libwhisper/whisperTopic.cpp.o
[ %] Building CXX object test/webthree/test/CMakeFiles/testweb3.dir/libwhisper/bloomFilter.cpp.o
[ %] Building CXX object test/webthree/test/CMakeFiles/testweb3.dir/libwhisper/whisperDB.cpp.o
[%] Building CXX object test/webthree/test/CMakeFiles/testweb3.dir/libweb3jsonrpc/Client.cpp.o
[%] Building CXX object test/webthree/test/CMakeFiles/testweb3.dir/libweb3jsonrpc/AccountHolder.cpp.o
[%] Building CXX object test/webthree/test/CMakeFiles/testweb3.dir/libweb3jsonrpc/jsonrpc.cpp.o
Linking CXX executable testweb3
[%] Built target testweb3
           

Install

Note: default install into /usr/local/bin
$ make install
$ cd /usr/local/bin
$ ls
bench  eth  ethkey  ethminer  ethvm  jsonrpcstub  lllc  rlp  solc
           

Debugging

Note: not all help entries are displayed(bug?)

Usage eth [OPTIONS]
Options:

Wallet usage:
    account list  List all keys available in wallet.
    account new Create a new key and add it to the wallet.
    account update [<uuid>|<address> , ... ]  Decrypt and re-encrypt given keys.
    account import [<uuid>|<file>|<secret-hex>] Import keys from given source and place in wallet.
    wallet import <file>    Import a presale wallet.

Client mode (default):
    --olympic  Use the Olympic () protocol.
    --frontier  Use the Frontier () protocol.
    --morden  Use the Morden testnet.
    --private <name>  Use a private chain.
    --test  Testing mode: Disable PoW and provide test rpc interface.
    --config <file>  Configure specialised blockchain using given JSON information.
    --oppose-dao-fork  Ignore DAO hard fork (default is to participate).

    -o,--mode <full/peer>  Start a full node or a peer node (default: full).

    -j,--json-rpc  Enable JSON-RPC server (default: off).
    --ipc  Enable IPC server (default: on).
    --ipcpath Set .ipc socket path (default: data directory)
    --admin-via-http  Expose admin interface via http - UNSAFE! (default: off).
    --no-ipc  Disable IPC server.
    --json-rpc-port <n>  Specify JSON-RPC server port (implies '-j', default: ).
    --rpccorsdomain <domain>  Domain on which to send Access-Control-Allow-Origin header.
    --admin <password>  Specify admin session key for JSON-RPC (default: auto-generated and printed at start-up).
    -K,--kill  Kill the blockchain first.
    -R,--rebuild  Rebuild the blockchain from the existing database.
    --rescue  Attempt to rescue a corrupt database.

    --import-presale <file>  Import a pre-sale key; you ll need to specify the password to this key.
    -s,--import-secret <secret>  Import a secret key into the key store and use as the default.
    -S,--import-session-secret <secret>  Import a secret key into the key store and use as the default for this session only.
    --sign-key <address>  Sign all transactions with the key of the given address.
    --session-sign-key <address>  Sign all transactions with the key of the given address for this session only.
    --master <password>  Give the master password for the key store. Use --master "" to show a prompt.
    --password <password>  Give a password for a private key.

Client transacting:
    --ask <wei>  Set the minimum ask gas price under which no transaction will be mined (default  ).
    --bid <wei>  Set the bid gas price to pay for transactions (default  ).
    --unsafe-transactions  Allow all transactions to proceed without verification. EXTREMELY UNSAFE.
Client mining:
    -a,--address <addr>  Set the author (mining payout) address to given address (default: auto).
    -m,--mining <on/off/number>  Enable mining, optionally for a specified number of blocks (default: off).
    -f,--force-mining  Mine even when there are no transactions to mine (default: off).
    -C,--cpu  When mining, use the CPU.
    -G,--opencl  When mining, use the GPU via OpenCL.
    --opencl-platform <n>  When mining using -G/--opencl, use OpenCL platform n (default: ).
    --opencl-device <n>  When mining using -G/--opencl, use OpenCL device n (default: ).
    -t, --mining-threads <n>  Limit number of CPU/GPU miners to n (default: use everything available on selected platform).

Client networking:
    --client-name <name>  Add a name to your client s version string (default: blank).
    --bootstrap  Connect to the default Ethereum peer servers (default unless --no-discovery used).
    --no-bootstrap  Do not connect to the default Ethereum peer servers (default only when --no-discovery is used).
    -x,--peers <number>  Attempt to connect to a given number of peers (default: ).
    --peer-stretch <number>  Give the accepted connection multiplier (default: ).
    --public-ip <ip>  Force advertised public IP to the given IP (default: auto).
    --listen-ip <ip>(:<port>)  Listen on the given IP for incoming connections (default: .).
    --listen <port>  Listen on the given port for incoming connections (default: ).
    -r,--remote <host>(:<port>)  Connect to the given remote host (default: none).
    --port <port>  Connect to the given remote port (default: ).
    --network-id <n>  Only connect to other hosts with this network id.
    --upnp <on/off>  Use UPnP for NAT (default: on).
    --peerset <list>  Space delimited list of peers; element format: type:[email protected][:port].
        Types:
        default     Attempt connection when no other peers are available and pinning is disabled.
        required        Keep connected at all times.
    --no-discovery  Disable node discovery, implies --no-bootstrap.
    --pin  Only accept or connect to trusted peers.
    --hermit  Equivalent to --no-discovery --pin.
    --sociable  Force discovery and no pinning.

Work farming mode:
    -F,--farm <url>  Put into mining farm mode with the work server at URL (default: http://.:)
    --farm-recheck <n>  Leave n ms between checks for changed work (default: ).
    --no-precompute  Don t precompute the next epoch s DAG.
Ethash verify mode:
    -w,--check-pow <headerHash> <seedHash> <difficulty> <nonce>  Check PoW credentials for validity.

Benchmarking mode:
    -M,--benchmark  Benchmark for mining and exit; use with --cpu and --opencl.
    --benchmark-warmup <seconds>  Set the duration of warmup for the benchmark tests (default: ).
    --benchmark-trial <seconds>  Set the duration for each trial for the benchmark tests (default: ).
    --benchmark-trials <n>  Set the number of trials for the benchmark tests (default: ).
    --phone-home <on/off>  When benchmarking, publish results (default: on)
DAG creation mode:
    -D,--create-dag <number>  Create the DAG in preparation for mining on given block and exit.
Mining configuration:
    -C,--cpu  When mining, use the CPU.
    -G,--opencl  When mining use the GPU via OpenCL.
    --opencl-platform <n>  When mining using -G/--opencl use OpenCL platform n (default: ).
    --opencl-device <n>  When mining using -G/--opencl use OpenCL device n (default: ).
    -t, --mining-threads <n> Limit number of CPU/GPU miners to n (default: use everything available on selected platform)
    --allow-opencl-cpu Allows CPU to be considered as an OpenCL device if the OpenCL platform supports it.
    --list-devices List the detected OpenCL devices and exit.
    --current-block Let the miner know the current block number at configuration time. Will help determine DAG size and required GPU memory.
    --disable-submit-hashrate  When mining, don t submit hashrate to node.
    --cl-extragpu-mem Set the memory (in MB) you believe your GPU requires for stuff other than mining. Windows rendering e.t.c..
    --cl-local-work Set the OpenCL local work size. Default is 
    --cl-global-work Set the OpenCL global work size as a multiple of the local work size. Default is  * 
    --cl-ms-per-batch Set the OpenCL target milliseconds per batch (global workgroup size). Default is . If  is given then no autoadjustment of global work size will happen
Import/export modes:
    --from <n>  Export only from block n; n may be a decimal, a '0x' prefixed hash, or 'latest'.
    --to <n>  Export only to block n (inclusive); n may be a decimal, a '0x' prefixed hash, or 'latest'.
    --only <n>  Equivalent to --export-from n --export-to n.
    --dont-check  Prevent checking some block aspects. Faster importing, but to apply only when the data is known to be valid.

General Options:
    -d,--db-path,--datadir <path>  Load database from path (default: /root/.ethereum).
    -v,--verbosity < - >  Set the log verbosity from  to  (default: ).
    -V,--version  Show the version and exit.
    -h,--help  Show this help message and exit.

Experimental / Proof of Concept:
    --shh  Enable Whisper.
           

Wallet

  • Create wallet
$ cd build/ethkey
$ ./ethkey createwallet
Please enter a MASTER passphrase to protect your key store (make it strong!): 
Please confirm the passphrase by entering it again: 
           
  • Create key
./ethkey new testkey0
Please enter your MASTER passphrase: 
Enter a passphrase with which to secure this account (or nothing to use the master passphrase): 
Please confirm the passphrase by entering it again: 
Created key a2e93a4d--e723-a677-be540d95
  Name: testkey0
  Password hint: 
  ICAP: XE2113T1643JT73UYHLYN4FH8V5CRZS0KJ
  Raw hex: f6e288a801aa7415a21690c535ff40e3
           
  • List keys
$ ./ethkey list
Please enter your MASTER passphrase: 
c1f9e-e806-dbd4-ac0f-e3395 c56f8b… XE1038R6CAKY4CFOPAODNNHGSOSKP1IP7P  
a1b56307-c3-e0e-e09-fa0a7 ed… XE5900O0YWPKSTYTWDZBLS66VKLAY515P6 
           
  • Or list keys by: eth account list
$ build/eth/eth account list
...  ::|eth  Reading /root/.web3/keys/a1b56307-c3-e0e-e09-fa0a7.json
...  ::|eth  Reading /root/.web3/keys/c1f9e-e806-dbd4-ac0f-e3395.json
Please enter your MASTER passphrase: 
Account #0: {00c56f8b9ac4e59be3b194d534ebe736d9c53525}
Account #1: {000120ed08b4d93b0242d31c18ed7e90e10ca01a}
           

DataDir

Default datadir is ~/.ethereum, and wallet, account options will ignore –datadir option.

Note: datadir may not work correctly. (bug?)

KeyId

$ ls -l ~/.web3/keys
-rw-------  root root   月   : c1f9e-e806-dbd4-ac0f-e3395.json
-rw-------  root root   月   : a1b56307-c3-e0e-e09-fa0a7.json
           
$ cat c1f9e-e806-dbd4-ac0f-e3395.json
           
{
    "address" : "00c56f8b9ac4e59be3b194d534ebe736d9c53525",
    "crypto" : {
        "cipher" : "aes-128-ctr",
        "cipherparams" : {
            "iv" : "670d27f6652969655c04fe533d8eb864"
        },
        "ciphertext" : "d39907d8901a7c582afeeac0e9dc1a2a5c83c61a5f5ba0525d813640d9a34508",
        "kdf" : "scrypt",
        "kdfparams" : {
            "dklen" : ,
            "n" : ,
            "p" : ,
            "r" : ,
            "salt" : "3f48c6759cb2b15dc7c9a52c346e710cc88a41ebf0a9f1cd456df005992ac309"
        },
        "mac" : "f95b7713470decba82370292bfc19d1bd3d51b458aa914508542290281b86452"
    },
    "id" : "764c1f9e-e806-dbd4-ac0f-8856050e3395",
    "version" : 
}
           
Note: see the address field in json

bool AccountManager::openWallet() will prompt for master key passphrase

bool AccountManager::openWallet()
 {
     if (!m_keyManager)
     {
         m_keyManager.reset(new KeyManager());
         if (m_keyManager->exists())
         {
             if (m_keyManager->load(std::string()) || m_keyManager->load(getPassword("Please enter your MASTER passphrase: ")))
                 return true;
             else
           

Genesis

The default frontier genesis info

cpp-ethereum/libethereum/ChainParams.cpp:55

dev::eth::ChainParams::loadConfig (this=, _json=..., _stateRoot=...)
           
dump binary memory
(gdb) p /x _json

$10 = (const std::__cxx11::string &) @0x7ffff722c440: {static npos = , 
  _M_dataplus = {<std::allocator<char>> = {<__gnu_cxx::new_allocator<char>> = {<No data fields>}, <No data fields>}, _M_p = }, 
  _M_string_length = , {_M_local_buf = {, , ,  <repeats  times>}, _M_allocated_capacity = }}

(gdb) dump binary memory result.txt  +
           
$ cat result.txt
{
    "sealEngine": "Ethash",
    "params": {
        "accountStartNonce": "0x00",
        "frontierCompatibilityModeLimit": "0x118c30",
        "daoHardforkBlock": "0x1d4c00",
        "maximumExtraDataSize": "0x20",
        "tieBreakingGas": false,
        "minGasLimit": "0x1388",
        "maxGasLimit": "7fffffffffffffff",
        "gasLimitBoundDivisor": "0x0400",
        "minimumDifficulty": "0x020000",
        "difficultyBoundDivisor": "0x0800",
        "durationLimit": "0x0d",
        "blockReward": "0x4563918244F40000",
        "registrar" : "0xc6d9d2cd449a754c494264e1809c50e34d64562b",
        "networkID" : "0x1"
    },
    "genesis": {
        "nonce": "0x0000000000000042",
        "difficulty": "0x400000000",
        "mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
        "author": "0x0000000000000000000000000000000000000000",
        "timestamp": "0x00",
        "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
        "extraData": "0x11bbe8db4e347b4e8c937c1c8370e4b5ed33adb3db69cbdb7a38e1e50b1b82fa",
        "gasLimit": "0x1388"
    },
    "accounts": {
        "0000000000000000000000000000000000000001": { "precompiled": { "name": "ecrecover", "linear": { "base": , "word":  } } },
        "0000000000000000000000000000000000000002": { "precompiled": { "name": "sha256", "linear": { "base": , "word":  } } },
        "0000000000000000000000000000000000000003": { "precompiled": { "name": "ripemd160", "linear": { "base": , "word":  } } },
        "0000000000000000000000000000000000000004": { "precompiled": { "name": "identity", "linear": { "base": , "word":  } } },
        "3282791d6fd713f1e94f4bfd565eaa78b3a0599d": {
            "balance": "1337000000000000000000"
        }
........
}
           

Console

Interacting with your node can be done using either geth or the ethereum console:

Note:

$ npm install -g ethereum-console web3 repl process

$ ethconsole

Fixed: main.js

repl.start('>');
           

Usage:

Connects to an ethereum node via ipc in order to control it remotely, through global variable web3 (web3.admin is also present). If no arguments are given, connects to the default ipc socket and drops into interactive mode.

Arguments:

  • ipc socket

    connect to the given ipc socket (use ipc:// if it does not end with .ipc)

  • javascript file

    execute the given javascript file that has to end in .js non-interactively. The script has to call process.exit() in order to terminate the console.

Web3 Console API Callback

Issue

>web3.eth.hashrate
Error: You tried to send "eth_hashrate" synchronously. Synchronous requests are not supported by the IPC provider.
    at IpcProvider.send (/usr/local/lib/node_modules/ethereum-console/node_modules/web3/lib/web3/ipcprovider.js::)
    at RequestManager.send (/usr/local/lib/node_modules/ethereum-console/node_modules/web3/lib/web3/requestmanager.js::)
    at Eth.get [as hashrate] (/usr/local/lib/node_modules/ethereum-console/node_modules/web3/lib/web3/property.js::)
    at repl::
    at REPLServer.self.eval (repl.js::)
    at Interface.<anonymous> (repl.js::)
    at Interface.EventEmitter.emit (events.js::)
    at Interface._onLine (readline.js::)
    at Interface._line (readline.js::)
    at Interface._ttyWrite (readline.js::)
           
>web3.eth.getMining(console.log);
undefined
>null false
           

PrivateChain

Section.1.5.3

$ ./ethkey –wallet-path

pwd

/dat createwallet

$ ./ethkey –wallet-path

pwd

/dat list

$ ./ethkey –wallet-path

pwd

/dat new testkey1

$ ./eth –datadir

pwd

/dat –genesis

pwd

/genesis.json –peers 0 –no-discovery -j –ipc

繼續閱讀