Task3 Web3js


web3.js 简介

Web3 是一套和以太坊节点进行通信的 API,如果我们需要基于以太坊来开发去中心化应用,就可能需要使用 web3(或者使用 ethers.js ),例如需要通过 Web3 来获取节点状态,获取账号信息,调用合约、监听合约事件等等。

实验过程有点坎坷

记录下遇到的问题,以后有足够时间再来解决

bug

1. 挖过第一次矿就不能再挖了,然后重建一个子链也不大行

> eth.getBalance(eth.accounts[0])
0
> miner.start()
INFO [03-23|16:01:58.352] Updated mining threads                   threads=4
null
> miner.start(1)
INFO [03-23|16:02:01.197] Updated mining threads                   threads=1
null
> 

(刚才我想复现一下记录下代码,发现又可以了^_^

2.err=”invalid sender”

> eth.getBalance(eth.accounts[0])
610000000000000000000
> eth.getBalance(eth.accounts[1])
0
> personal.unlockAccount(eth.accounts[0])
Unlock account 0x22a95a40762d9f0580a68e2a878661dd0fd62e78
Passphrase: 
true
> amount = web3.toWei(5,'ether')
"5000000000000000000"
> eth.sendTransaction({from:eth.accounts[0],to:eth.accounts[1],value:amount})
WARN [03-23|16:32:01.748] Served eth_sendTransaction               reqid=19 t=1.326072ms err="invalid sender"
Error: invalid sender
	at web3.js:6347:37(47)
	at web3.js:5081:62(37)
	at <eval>:1:20(16)

> eth.sendTransaction({from:"0x22a95a40762d9f0580a68e2a878661dd0fd62e78",to:"0xf48509733ecc04fc874fbde1349eb1b260aeb6a1",value:amount})
WARN [03-23|16:35:07.159] Served eth_sendTransaction               reqid=21 t="802.013µs" err="invalid sender"
Error: invalid sender
	at web3.js:6347:37(47)
	at web3.js:5081:62(37)
	at <eval>:1:20(10)

> 

3.换一种启动方式

ERROR[03-23|16:46:39.776] Unavailable modules in HTTP API list unavailable=[shh] available=”[admin debug web3 eth txpool personal ethash miner net]”

terence@ccr:~/Desktop/DataWhale/OwnChain$ geth --networkid 150 --datadir "." --identity "kexin" --rpc --rpcport "8545" --rpcaddr "localhost" --port "30303" --nodiscover --allow-insecure-unlock --rpcapi "eth,net,web3,personal,admin,shh,txpool,debug,miner" console
INFO [03-23|16:46:39.753] Maximum peer count                       ETH=50 LES=0 total=50
WARN [03-23|16:46:39.753] The flag --rpc is deprecated and will be removed June 2021, please use --http 
WARN [03-23|16:46:39.753] The flag --rpcaddr is deprecated and will be removed June 2021, please use --http.addr 
WARN [03-23|16:46:39.753] The flag --rpcport is deprecated and will be removed June 2021, please use --http.port 
WARN [03-23|16:46:39.753] The flag --rpcapi is deprecated and will be removed June 2021, please use --http.api 
INFO [03-23|16:46:39.753] Smartcard socket not found, disabling    err="stat /run/pcscd/pcscd.comm: no such file or directory"
INFO [03-23|16:46:39.753] Set global gas cap                       cap=25000000
INFO [03-23|16:46:39.753] Allocated trie memory caches             clean=154.00MiB dirty=256.00MiB
INFO [03-23|16:46:39.753] Allocated cache and file handles         database=/home/terence/Desktop/DataWhale/OwnChain/geth/chaindata cache=512.00MiB handles=524288
INFO [03-23|16:46:39.764] Opened ancient database                  database=/home/terence/Desktop/DataWhale/OwnChain/geth/chaindata/ancient
INFO [03-23|16:46:39.764] Initialised chain configuration          config="{ChainID: 15 Homestead: <nil> DAO: <nil> DAOSupport: false EIP150: <nil> EIP155: <nil> EIP158: <nil> Byzantium: <nil> Constantinople: <nil> Petersburg: <nil> Istanbul: <nil>, Muir Glacier: <nil>, Berlin: <nil>, YOLO v3: <nil>, Engine: unknown}"
INFO [03-23|16:46:39.764] Disk storage enabled for ethash caches   dir=/home/terence/Desktop/DataWhale/OwnChain/geth/ethash count=3
INFO [03-23|16:46:39.764] Disk storage enabled for ethash DAGs     dir=/home/terence/.ethash count=2
INFO [03-23|16:46:39.764] Initialising Ethereum protocol           network=150 dbversion=8
INFO [03-23|16:46:39.766] Loaded most recent local header          number=122 hash="9198ca…30151e" td=16445520 age=15m19s
INFO [03-23|16:46:39.766] Loaded most recent local full block      number=122 hash="9198ca…30151e" td=16445520 age=15m19s
INFO [03-23|16:46:39.766] Loaded most recent local fast block      number=122 hash="9198ca…30151e" td=16445520 age=15m19s
INFO [03-23|16:46:39.769] Loaded local transaction journal         transactions=0 dropped=0
INFO [03-23|16:46:39.769] Regenerated local transaction journal    transactions=0 accounts=0
WARN [03-23|16:46:39.769] Switch sync mode from fast sync to full sync 
INFO [03-23|16:46:39.769] Starting peer-to-peer node               instance=Geth/kexin/v1.10.1-stable-c2d2f4ed/linux-amd64/go1.16
INFO [03-23|16:46:39.776] IPC endpoint opened                      url=/home/terence/Desktop/DataWhale/OwnChain/geth.ipc
ERROR[03-23|16:46:39.776] Unavailable modules in HTTP API list     unavailable=[shh] available="[admin debug web3 eth txpool personal ethash miner net]"
INFO [03-23|16:46:39.777] New local node record                    seq=12 id=0b52cc632063309f ip=127.0.0.1 udp=0 tcp=30303
INFO [03-23|16:46:39.777] Started P2P networking                   self="enode://307b4d41cb9dd38790819d73df6e0139c8f1105ed8ac4c209ee64d2c15f99ee7b7ea38000ac7a4225900b33418e5adc4ae5009e0fb45e6ed0185f72c3725295f@127.0.0.1:30303?discport=0"
INFO [03-23|16:46:39.777] HTTP server started                      endpoint=127.0.0.1:8545 prefix= cors= vhosts=localhost
INFO [03-23|16:46:39.814] Etherbase automatically configured       address=0x22A95a40762d9f0580A68e2A878661dd0fD62e78
Welcome to the Geth JavaScript console!

instance: Geth/kexin/v1.10.1-stable-c2d2f4ed/linux-amd64/go1.16
coinbase: 0x22a95a40762d9f0580a68e2a878661dd0fd62e78
at block: 122 (Tue Mar 23 2021 16:31:20 GMT+0800 (CST))
 datadir: /home/terence/Desktop/DataWhale/OwnChain
 modules: admin:1.0 debug:1.0 eth:1.0 ethash:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 txpool:1.0 web3:1.0

To exit, press ctrl-d
> 

4.web3命令

具体问题点

err=”the method eth_protocolVersion does not exist/is not available”

err=”the method shh_version does not exist/is not available”

version: WARN [03-23|16:37:42.155] Served eth_protocolVersion               reqid=70 t="4.89µs"    err="the method eth_protocolVersion does not exist/is not available"
WARN [03-23|16:37:42.156] Served shh_version                       reqid=71 t="5.39µs"    err="the method shh_version does not exist/is not available"
{
    api: "0.20.1",
WARN [03-23|16:37:42.156] Served eth_protocolVersion               reqid=72 t="8.99µs"    err="the method eth_protocolVersion does not exist/is not available"
    ethereum: undefined,
    network: "150",
    node: "Geth/v1.10.1-stable-c2d2f4ed/linux-amd64/go1.16",
WARN [03-23|16:37:42.156] Served shh_version                       reqid=75 t="4.98µs"    err="the method shh_version does not exist/is not available"

全部 log

> web3
{
  admin: {
    datadir: "/home/terence/Desktop/DataWhale/OwnChain",
    nodeInfo: {
      enode: "enode://307b4d41cb9dd38790819d73df6e0139c8f1105ed8ac4c209ee64d2c15f99ee7b7ea38000ac7a4225900b33418e5adc4ae5009e0fb45e6ed0185f72c3725295f@127.0.0.1:30303?discport=0",
      enr: "enr:-Ja4QDfN_iDtbTHSEp0nV5UjevhkZUGt9JNde1RCpgX2geaIBMfLqpLdG4qvSVcfbtIIhk_2Z7a53Yy_NEbZy5dAspgLg2V0aMfGhIj58RaAgmlkgnY0gmlwhH8AAAGJc2VjcDI1NmsxoQMwe01By53Th5CBnXPfbgE5yPEQXtisTCCe5k0sFfme54RzbmFwwIN0Y3CCdl8",
      id: "0b52cc632063309fde9524dfda2aaae2c551f47eaa4f66cd237c6948e524f6c6",
      ip: "127.0.0.1",
      listenAddr: "[::]:30303",
      name: "Geth/v1.10.1-stable-c2d2f4ed/linux-amd64/go1.16",
      ports: {
        discovery: 0,
        listener: 30303
      },
      protocols: {
        eth: {...},
        snap: {}
      }
    },
    peers: [],
    addPeer: function(),
    addTrustedPeer: function(),
    clearHistory: function(),
    exportChain: function(),
    getDatadir: function(callback),
    getNodeInfo: function(callback),
    getPeers: function(callback),
    importChain: function(),
    removePeer: function(),
    removeTrustedPeer: function(),
    sleep: function(),
    sleepBlocks: function(),
    startRPC: function(),
    startWS: function(),
    stopRPC: function(),
    stopWS: function()
  },
  bzz: WARN [03-23|16:37:42.148] Served bzz_hive                          reqid=28 t="104.991µs" err="the method bzz_hive does not exist/is not available"
WARN [03-23|16:37:42.148] Served bzz_info                          reqid=29 t="5.4µs"     err="the method bzz_info does not exist/is not available"
{
WARN [03-23|16:37:42.148] Served bzz_hive                          reqid=30 t="4.92µs"    err="the method bzz_hive does not exist/is not available"
    hive: undefined,
WARN [03-23|16:37:42.148] Served bzz_info                          reqid=31 t="4.18µs"    err="the method bzz_info does not exist/is not available"
    info: undefined,
    blockNetworkRead: function(),
    download: function(),
    get: function(),
    getHive: function(callback),
    getInfo: function(callback),
    modify: function(),
    put: function(),
    retrieve: function(),
    store: function(),
    swapEnabled: function(),
    syncEnabled: function(),
    upload: function()
  },
  currentProvider: {
    send: function(),
    sendAsync: function()
  },
  db: {
    getHex: function(),
    getString: function(),
    putHex: function(),
    putString: function()
  },
  debug: {
    accountRange: function(),
    backtraceAt: function(),
    blockProfile: function(),
    chaindbCompact: function(),
    chaindbProperty: function(),
    cpuProfile: function(),
    dumpBlock: function(),
    freeOSMemory: function(),
    freezeClient: function(),
    gcStats: function(),
    getBadBlocks: function(),
    getBlockRlp: function(),
    getModifiedAccountsByHash: function(),
    getModifiedAccountsByNumber: function(),
    goTrace: function(),
    memStats: function(),
    mutexProfile: function(),
    preimage: function(),
    printBlock: function(),
    seedHash: function(),
    setBlockProfileRate: function(),
    setGCPercent: function(),
    setHead: function(),
    setMutexProfileFraction: function(),
    stacks: function(),
    standardTraceBadBlockToFile: function(),
    standardTraceBlockToFile: function(),
    startCPUProfile: function(),
    startGoTrace: function(),
    stopCPUProfile: function(),
    stopGoTrace: function(),
    storageRangeAt: function(),
    testSignCliqueBlock: function(),
    traceBadBlock: function(),
    traceBlock: function(),
    traceBlockByHash: function(),
    traceBlockByNumber: function(),
    traceBlockFromFile: function(),
    traceCall: function(),
    traceTransaction: function(),
    verbosity: function(),
    vmodule: function(),
    writeBlockProfile: function(),
    writeMemProfile: function(),
    writeMutexProfile: function()
  },
  eth: WARN [03-23|16:37:42.150] Served eth_protocolVersion               reqid=39 t="6.45µs"    err="the method eth_protocolVersion does not exist/is not available"
{
    accounts: ["0x22a95a40762d9f0580a68e2a878661dd0fd62e78", "0xf48509733ecc04fc874fbde1349eb1b260aeb6a1"],
    blockNumber: 122,
    coinbase: "0x22a95a40762d9f0580a68e2a878661dd0fd62e78",
    compile: {
      lll: function(),
      serpent: function(),
      solidity: function()
    },
    defaultAccount: undefined,
    defaultBlock: "latest",
    gasPrice: 1000000000,
    hashrate: 24,
    mining: false,
    pendingTransactions: [],
WARN [03-23|16:37:42.151] Served eth_protocolVersion               reqid=48 t="6.9µs"     err="the method eth_protocolVersion does not exist/is not available"
    protocolVersion: undefined,
    syncing: false,
    call: function(),
    chainId: function(),
    contract: function(abi),
    estimateGas: function(),
    fillTransaction: function(),
    filter: function(options, callback, filterCreationErrorCallback),
    getAccounts: function(callback),
    getBalance: function(),
    getBlock: function(),
    getBlockByHash: function(),
    getBlockByNumber: function(),
    getBlockNumber: function(callback),
    getBlockTransactionCount: function(),
    getBlockUncleCount: function(),
    getCode: function(),
    getCoinbase: function(callback),
    getCompilers: function(),
    getGasPrice: function(callback),
    getHashrate: function(callback),
    getHeaderByHash: function(),
    getHeaderByNumber: function(),
    getMining: function(callback),
    getPendingTransactions: function(callback),
    getProof: function(),
    getProtocolVersion: function(callback),
    getRawTransaction: function(),
    getRawTransactionFromBlock: function(),
    getStorageAt: function(),
    getSyncing: function(callback),
    getTransaction: function(),
    getTransactionCount: function(),
    getTransactionFromBlock: function(),
    getTransactionReceipt: function(),
    getUncle: function(),
    getWork: function(),
    iban: function(iban),
    icapNamereg: function(),
    isSyncing: function(callback),
    namereg: function(),
    resend: function(),
    sendIBANTransaction: function(),
    sendRawTransaction: function(),
    sendTransaction: function(),
    sign: function(),
    signTransaction: function(),
    submitTransaction: function(),
    submitWork: function()
  },
  ethash: {
    getHashrate: function(),
    getWork: function(),
    submitHashRate: function(),
    submitWork: function()
  },
  isIBAN: undefined,
  miner: {
    getHashrate: function(),
    setEtherbase: function(),
    setExtra: function(),
    setGasPrice: function(),
    setRecommitInterval: function(),
    start: function(),
    stop: function()
  },
  net: {
    listening: true,
    peerCount: 0,
    version: "150",
    getListening: function(callback),
    getPeerCount: function(callback),
    getVersion: function(callback)
  },
  personal: {
    listAccounts: ["0x22a95a40762d9f0580a68e2a878661dd0fd62e78", "0xf48509733ecc04fc874fbde1349eb1b260aeb6a1"],
    listWallets: [{
        accounts: [...],
        status: "Locked",
        url: "keystore:///home/terence/Desktop/DataWhale/OwnChain/keystore/UTC--2021-03-22T12-15-05.415042974Z--22a95a40762d9f0580a68e2a878661dd0fd62e78"
    }, {
        accounts: [...],
        status: "Locked",
        url: "keystore:///home/terence/Desktop/DataWhale/OwnChain/keystore/UTC--2021-03-22T12-16-13.964571341Z--f48509733ecc04fc874fbde1349eb1b260aeb6a1"
    }],
    deriveAccount: function(),
    ecRecover: function(),
    getListAccounts: function(callback),
    getListWallets: function(callback),
    importRawKey: function(),
    initializeWallet: function(),
    lockAccount: function(),
    newAccount: function(),
    openWallet: function(),
    sendTransaction: function(),
    sign: function(),
    signTransaction: function(),
    unlockAccount: function(),
    unpair: function()
  },
  providers: {
    HttpProvider: function(host, timeout, user, password),
    IpcProvider: function(path, net)
  },
  rpc: {
    modules: {
      admin: "1.0",
      debug: "1.0",
      eth: "1.0",
      ethash: "1.0",
      miner: "1.0",
      net: "1.0",
      personal: "1.0",
      rpc: "1.0",
      txpool: "1.0",
      web3: "1.0"
    },
    getModules: function(callback)
  },
  settings: {
    defaultAccount: undefined,
    defaultBlock: "latest"
  },
  shh: {
    addPrivateKey: function(),
    addSymKey: function(),
    deleteKeyPair: function(),
    deleteSymKey: function(),
    generateSymKeyFromPassword: function(),
    getPrivateKey: function(),
    getPublicKey: function(),
    getSymKey: function(),
    hasKeyPair: function(),
    hasSymKey: function(),
    info: function(),
    markTrustedPeer: function(),
    newKeyPair: function(),
    newMessageFilter: function(options, callback, filterCreationErrorCallback),
    newSymKey: function(),
    post: function(),
    setMaxMessageSize: function(),
    setMinPoW: function(),
    version: function()
  },
  txpool: {
    content: {
      pending: {},
      queued: {}
    },
    inspect: {
      pending: {},
      queued: {}
    },
    status: {
      pending: 0,
      queued: 0
    },
    getContent: function(callback),
    getInspect: function(callback),
    getStatus: function(callback)
  },
  version: WARN [03-23|16:37:42.155] Served eth_protocolVersion               reqid=70 t="4.89µs"    err="the method eth_protocolVersion does not exist/is not available"
WARN [03-23|16:37:42.156] Served shh_version                       reqid=71 t="5.39µs"    err="the method shh_version does not exist/is not available"
{
    api: "0.20.1",
WARN [03-23|16:37:42.156] Served eth_protocolVersion               reqid=72 t="8.99µs"    err="the method eth_protocolVersion does not exist/is not available"
    ethereum: undefined,
    network: "150",
    node: "Geth/v1.10.1-stable-c2d2f4ed/linux-amd64/go1.16",
WARN [03-23|16:37:42.156] Served shh_version                       reqid=75 t="4.98µs"    err="the method shh_version does not exist/is not available"
    whisper: undefined,
    getEthereum: function(callback),
    getNetwork: function(callback),
    getNode: function(callback),
    getWhisper: function(callback)
  },
  BigNumber: function a(e,n),
  createBatch: function(),
  fromAscii: function(str),
  fromDecimal: function(value),
  fromICAP: function(icap),
  fromUtf8: function(str),
  fromWei: function(number, unit),
  isAddress: function(address),
  isChecksumAddress: function(address),
  isConnected: function(),
  padLeft: function(string, chars, sign),
  padRight: function(string, chars, sign),
  reset: function(keepIsSyncing),
  setProvider: function(provider),
  sha3: function(string, options),
  toAscii: function(hex),
  toBigNumber: function(number),
  toChecksumAddress: function(address),
  toDecimal: function(value),
  toHex: function(val),
  toUtf8: function(hex),
  toWei: function(number, unit)
}
> 


文章作者: Terence Cai
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Terence Cai !
  目录