geth + wagon 环境下调试ewasm 合约的发布与调用

一、配置环境

请参照教程 《geth + Wagon环境运行ewasm》
注意,本文新建的节点目录名是geth-data0,与参考教程略有不同。

二、准备私有链

创建两个账户

$ ./geth --datadir geth-data0 account new
$ ./geth --datadir geth-data0 account new

启动节点

$ ./geth \
--datadir geth-data0 \ 
--networkid 66 \
--nodiscover \
--rpc \
--rpcport 8545 \
--port 30001 \
--ipcpath geth1.ipc

attach 节点

$ ./geth attach ipc:geth-data0/geth1.ipc
Welcome to the Geth JavaScript console!

instance: Geth/v1.8.17-unstable/darwin-amd64/go1.12.6
coinbase: 0x2d636f052802a791a4114036d2c5f91cf4549539
at block: 0 (Mon, 20 Aug 2018 05:16:36 CST)
 datadir: /Users/ff/go/src/github.com/ethereum/go-ethereum/geth-data0
 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

> eth.accounts
["0x2d636f052802a791a4114036d2c5f91cf4549539", "0xc5bff4b89d965e00a94827d12ccd8cc70b5e30a5"]

二、RPC 调试

建议使用Postman 来发送RPC 请求,目标地址为 http://127.0.0.1:8545 。

发布合约

请求参数param 中,data 是ewasm 合约,from 是发布账户。

{
    "jsonrpc": "2.0",
    "id": 171,
    "method": "eth_sendTransaction",
    "params": [
        {
            "data": "0x0061736d0100000001090260027f7f0060000002130108657468657265756d0666696e6973680000030201010503010001071102046d61696e0001066d656d6f727902000a0b010900410041a41a10000b0b961a010041010b8f1a61736d0100000001450c60037f7f7f017f60027f7f017f6000017f60037f7f7f0060027f7f0060000060017f0060017f017f60027e7f017f60017f017e60057f7f7f7f7f017f60047f7f7f7f017f02580408657468657265756d0f67657443616c6c4461746153697a65000208657468657265756d0c63616c6c44617461436f7079000308657468657265756d06726576657274000408657468657265756d0666696e6973680004031e1d050604030505060401010001050506050607060601040806090a0b00000405017001040405030100110619037f01418080c0000b7f0041f483c0000b7f0041f483c0000b071102066d656d6f72790200046d61696e00080909010041010b03181b1c0a8e141d02000b5301017f230041206b22012400200141086a410c10062001410036021820012001290308370310200141106a418080c000410c1007200041086a200128021836020020002001290310370200200141206a24000b3b01017f02402001417f4c0d00024002402001450d0020014101100f22020d0100000b410121020b20002001360204200020023602000f0b1010000baf0101037f02400240024002400240024020002802042203200028020822046b20024f0d00200420026a22052004490d0320034101742204200520052004491b22044100480d032003450d01200028020020032004101f2203450d020c040b200028020021030c040b20044101100f22030d020b00000b1011000b20002003360200200041046a2004360200200041086a28020021040b200041086a200420026a360200200320046a2001200210201a0bbd0101047f230041306b2200240020004195b1ef8c0436001c10002101200041106a10002202100620002802102203410020021001024002400240200141034d0d00200241034d0d01200041086a410410062000410036022820002000290308370320200041206a200341041007024020002802284104470d00200028022022022000411c6a460d032002280000200028001c460d030b200041306a24000f0b1009000b2002100a000b200041206a100520002802202000280228100b000b0900410041001002000b6f01017f230041306b2201240020012000360204200141043602002001412c6a41013602002001411c6a4102360200200141013602242001420237020c200141a482c0003602082001200141046a360228200120013602202001200141206a360218200141086a41b482c0001019000b0900200020011003000b080020002001100d0b920101037f4100210202402000450d002001450d002001200020017022026b410020021b20006a21013f0022034110742102024041002802e0834022000d00410020023602e08340200221000b0240200120006a220120024d0d004100210241808004200141ffff037122046b410020041b20016a41107620036b4000417f460d010b410020013602e08340200021020b20020b2501017f024020024101100d2203450d002003200020022001200120024b1b10201a0b20030b080020002001100c0b05001011000b0a00418c80c0001016000b6c01027f41012101024002400240024041002802e883404101470d00410041002802ec834041016a22013602ec834020014103490d010c020b41004281808080103703e883400b41002802f083402202417f4c0d00410020023602f0834020014102490d010b00000b1013000b040000000b3101017f230041106b22012400200028020810151a2001200041146a2902003703082001200029020c37030020011012000b150002402000450d0020000f0b418483c0001016000b6802017f037e230041306b22012400200029020821022000290210210320002902002104200141146a410036020020012004370318200141cc80c000360210200142013702042001200141186a36020020012003370328200120023703202001200141206a1019000b070020001014000b0b0020003502002001101a0b4a02017f017e230041206b2202240020012902002103200241146a20012902083702002002200337020c20022000360208200241cc80c000360204200241cc80c00036020020021017000bc80203037f017e027f230041306b22022400412721030240024020004290ce00540d00412721030340200241096a20036a2204417c6a200020004290ce0080220542f0b17f7e7ca7220641e4006e220741017441dc80c0006a2f00003b00002004417e6a2007419c7f6c20066a41017441dc80c0006a2f00003b00002003417c6a2103200042ffc1d72f5621042005210020040d000c020b0b200021050b02402005a7220441e3004c0d00200241096a2003417e6a22036a2005a7220641ffff037141e4006e2204419c7f6c20066a41ffff037141017441dc80c0006a2f00003b00000b02400240200441094a0d00200241096a2003417f6a22036a200441306a3a00000c010b200241096a2003417e6a22036a200441017441dc80c0006a2f00003b00000b200141cc80c0004100200241096a20036a412720036b101d2103200241306a240020030b02000b0c0042e0e8b4fdc7c6bdd8760bb30601077f20002802002205410171220620046a21070240024020054104710d00410021010c010b4100210802402002450d00200221092001210a03402008200a2d000041c00171418001466a2108200a41016a210a2009417f6a22090d000b0b200720026a20086b21070b412b418080c40020061b21060240024002400240024002400240024002400240024002400240024020002802084101470d002000410c6a280200220820074d0d0120054108710d02200820076b210a410120002d0030220820084103461b2208410371450d0320084102460d044100210b200a21080c050b4101210a2000200620012002101e0d0c2000280218200320042000411c6a28020028020c1100000f0b4101210a2000200620012002101e0d0b2000280218200320042000411c6a28020028020c1100000f0b4101210a200041013a0030200041303602042000200620012002101e0d0a200820076b210a4101200041306a2d0000220820084103461b2208410371450d0320084102460d0441002107200a21080c050b41002108200a210b0c010b200a4101762108200a41016a410176210b0b417f210a200041046a2109200041186a21072000411c6a210502400340200a41016a220a20084f0d01200728020020092802002005280200280210110100450d000c050b0b200041046a28020021094101210a2000200620012002101e0d07200041186a2208280200200320042000411c6a220228020028020c1100000d0720082802002100417f2108200228020041106a21020340200841016a2208200b4f0d064101210a200020092002280200110100450d000c080b0b41002108200a21070c010b200a4101762108200a41016a41017621070b417f210a200041046a2109200041186a21022000411c6a21010340200a41016a220a20084f0d02200228020020092802002001280200280210110100450d000b0b4101210a0c030b200041046a28020021094101210a200041186a2208280200200320042000411c6a220228020028020c1100000d0220082802002100417f2108200228020041106a21020340200841016a220820074f0d024101210a200020092002280200110100450d000c030b0b41000f0b41000f0b200a0b5501017f0240024002402001418080c400460d0041012104200028021820012000411c6a2802002802101101000d010b2002450d012000280218200220032000411c6a28020028020c11000021040b20040f0b41000b0a00200020012002100e0b3601017f02402002450d00200021030340200320012d00003a0000200341016a2103200141016a21012002417f6a22020d000b0b20000b0b82040200418080c0000bdc0348656c6c6f20576f726c642124001000110000003500100017000000ea020000050000006361706163697479206f766572666c6f777372632f6c6962616c6c6f632f7261775f7665632e72730200000000000000010000000300000030303031303230333034303530363037303830393130313131323133313431353136313731383139323032313232323332343235323632373238323933303331333233333334333533363337333833393430343134323433343434353436343734383439353035313532353335343535353635373538353936303631363236333634363536363637363836393730373137323733373437353736373737383739383038313832383338343835383638373838383939303931393239333934393539363937393839395c0110000600000062011000220000004401100018000000090a0000050000007372632f6c6962636f72652f736c6963652f6d6f642e7273696e64657820206f7574206f662072616e676520666f7220736c696365206f66206c656e677468209c0110002b000000c7011000150000005b0100001500000063616c6c656420604f7074696f6e3a3a756e77726170282960206f6e206120604e6f6e65602076616c75657372632f6c6962636f72652f6f7074696f6e2e72730041e083c0000b1400360970726f647563657273010c70726f6365737365642d6279020677616c72757305302e362e31097761736d2d736e697005302e312e35",
            "from": "0x2d636f052802a791a4114036d2c5f91cf4549539",
            "gas": "0x47b760"
        }
    ]
}

提示"authentication needed: password or unlock",说明需要解锁账户。

> personal.unlockAccount("0x2d636f052802a791a4114036d2c5f91cf4549539")
Unlock account 0x2d636f052802a791a4114036d2c5f91cf4549539
Passphrase:
true

提示"insufficient funds for gas * price + value",说明当前账户没有足够的eth,挖一次矿即可。

> miner.start(); admin.sleepBlocks(1); miner.stop();

再次发起,得到交易hash "0x0e0f23a12e8871212b614feef16042643db6bb181a832220a8b6ac08afc8b276"。

{
    "jsonrpc": "2.0",
    "id": 171,
    "result": "0x0e0f23a12e8871212b614feef16042643db6bb181a832220a8b6ac08afc8b276"
}

获取交易票据

params 中放的是发布合约时得到的交易hash

{
    "jsonrpc": "2.0",
    "id": 2192787296,
    "method": "eth_getTransactionReceipt",
    "params": [
        "0x0e0f23a12e8871212b614feef16042643db6bb181a832220a8b6ac08afc8b276"
    ]
}

没有得到结果。

{
    "jsonrpc": "2.0",
    "id": 2192787296,
    "result": null
}

原因是,广播交易和确认交易各需要一个区块,挖矿两次即可。

> miner.start(); admin.sleepBlocks(2); miner.stop();

再次发起请求,返回结果中可以得到合约地址"0xee65abc1547a2592dc31a68cc03cafee72bbf56d"

{
    "jsonrpc": "2.0",
    "id": 2192787296,
    "result": {
        "blockHash": "0xc91396e7ec9efcaed26d6044d95b001c8cd6c6d1cbd8691e54c0e41096c16f53",
        "blockNumber": "0x3",
        "contractAddress": "0xee65abc1547a2592dc31a68cc03cafee72bbf56d",
        "cumulativeGasUsed": "0xe46f0",
        "from": "0x2d636f052802a791a4114036d2c5f91cf4549539",
        "gasUsed": "0xe46f0",
        "logs": [],
        "logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
        "status": "0x1",
        "to": null,
        "transactionHash": "0x0e0f23a12e8871212b614feef16042643db6bb181a832220a8b6ac08afc8b276",
        "transactionIndex": "0x0"
    }
}

调用合约

本文合约函数不需要消耗gas,所以用eth_call 方法,否则需要用eth_sendTransaction 方法。
params 中,data 是合约函数签名,from 是调用方地址,to 是合约地址。

{
    "jsonrpc": "2.0",
    "id": 171,
    "method": "eth_call",
    "params": [
        {
            "data": "0x95d89b41",
            "from": "0x2d636f052802a791a4114036d2c5f91cf4549539",
            "to": "0xee65abc1547a2592dc31a68cc03cafee72bbf56d"
        },
        "latest"
    ]
}

得到返回值,result 实际上是"Hello World!" 的ascii 码。

{
    "jsonrpc": "2.0",
    "id": 171,
    "result": "0x48656c6c6f20576f726c6421"
}

查看合约代码

只有合约地址,我们也有办法查看合约代码。
请求参数params 放的是合约地址。

{
    "jsonrpc": "2.0",
    "id": 1111,
    "method": "eth_getCode",
    "params": [
        "0xee65abc1547a2592dc31a68cc03cafee72bbf56d",
        "latest"
    ]
}

返回结果中是合约代码。

{
    "jsonrpc": "2.0",
    "id": 1111,
    "result": "0x0061736d0100000001450c60037f7f7f017f60027f7f017f6000017f60037f7f7f0060027f7f0060000060017f0060017f017f60027e7f017f60017f017e60057f7f7f7f7f017f60047f7f7f7f017f02580408657468657265756d0f67657443616c6c4461746153697a65000208657468657265756d0c63616c6c44617461436f7079000308657468657265756d06726576657274000408657468657265756d0666696e6973680004031e1d050604030505060401010001050506050607060601040806090a0b00000405017001040405030100110619037f01418080c0000b7f0041f483c0000b7f0041f483c0000b071102066d656d6f72790200046d61696e00080909010041010b03181b1c0a8e141d02000b5301017f230041206b22012400200141086a410c10062001410036021820012001290308370310200141106a418080c000410c1007200041086a200128021836020020002001290310370200200141206a24000b3b01017f02402001417f4c0d00024002402001450d0020014101100f22020d0100000b410121020b20002001360204200020023602000f0b1010000baf0101037f02400240024002400240024020002802042203200028020822046b20024f0d00200420026a22052004490d0320034101742204200520052004491b22044100480d032003450d01200028020020032004101f2203450d020c040b200028020021030c040b20044101100f22030d020b00000b1011000b20002003360200200041046a2004360200200041086a28020021040b200041086a200420026a360200200320046a2001200210201a0bbd0101047f230041306b2200240020004195b1ef8c0436001c10002101200041106a10002202100620002802102203410020021001024002400240200141034d0d00200241034d0d01200041086a410410062000410036022820002000290308370320200041206a200341041007024020002802284104470d00200028022022022000411c6a460d032002280000200028001c460d030b200041306a24000f0b1009000b2002100a000b200041206a100520002802202000280228100b000b0900410041001002000b6f01017f230041306b2201240020012000360204200141043602002001412c6a41013602002001411c6a4102360200200141013602242001420237020c200141a482c0003602082001200141046a360228200120013602202001200141206a360218200141086a41b482c0001019000b0900200020011003000b080020002001100d0b920101037f4100210202402000450d002001450d002001200020017022026b410020021b20006a21013f0022034110742102024041002802e0834022000d00410020023602e08340200221000b0240200120006a220120024d0d004100210241808004200141ffff037122046b410020041b20016a41107620036b4000417f460d010b410020013602e08340200021020b20020b2501017f024020024101100d2203450d002003200020022001200120024b1b10201a0b20030b080020002001100c0b05001011000b0a00418c80c0001016000b6c01027f41012101024002400240024041002802e883404101470d00410041002802ec834041016a22013602ec834020014103490d010c020b41004281808080103703e883400b41002802f083402202417f4c0d00410020023602f0834020014102490d010b00000b1013000b040000000b3101017f230041106b22012400200028020810151a2001200041146a2902003703082001200029020c37030020011012000b150002402000450d0020000f0b418483c0001016000b6802017f037e230041306b22012400200029020821022000290210210320002902002104200141146a410036020020012004370318200141cc80c000360210200142013702042001200141186a36020020012003370328200120023703202001200141206a1019000b070020001014000b0b0020003502002001101a0b4a02017f017e230041206b2202240020012902002103200241146a20012902083702002002200337020c20022000360208200241cc80c000360204200241cc80c00036020020021017000bc80203037f017e027f230041306b22022400412721030240024020004290ce00540d00412721030340200241096a20036a2204417c6a200020004290ce0080220542f0b17f7e7ca7220641e4006e220741017441dc80c0006a2f00003b00002004417e6a2007419c7f6c20066a41017441dc80c0006a2f00003b00002003417c6a2103200042ffc1d72f5621042005210020040d000c020b0b200021050b02402005a7220441e3004c0d00200241096a2003417e6a22036a2005a7220641ffff037141e4006e2204419c7f6c20066a41ffff037141017441dc80c0006a2f00003b00000b02400240200441094a0d00200241096a2003417f6a22036a200441306a3a00000c010b200241096a2003417e6a22036a200441017441dc80c0006a2f00003b00000b200141cc80c0004100200241096a20036a412720036b101d2103200241306a240020030b02000b0c0042e0e8b4fdc7c6bdd8760bb30601077f20002802002205410171220620046a21070240024020054104710d00410021010c010b4100210802402002450d00200221092001210a03402008200a2d000041c00171418001466a2108200a41016a210a2009417f6a22090d000b0b200720026a20086b21070b412b418080c40020061b21060240024002400240024002400240024002400240024002400240024020002802084101470d002000410c6a280200220820074d0d0120054108710d02200820076b210a410120002d0030220820084103461b2208410371450d0320084102460d044100210b200a21080c050b4101210a2000200620012002101e0d0c2000280218200320042000411c6a28020028020c1100000f0b4101210a2000200620012002101e0d0b2000280218200320042000411c6a28020028020c1100000f0b4101210a200041013a0030200041303602042000200620012002101e0d0a200820076b210a4101200041306a2d0000220820084103461b2208410371450d0320084102460d0441002107200a21080c050b41002108200a210b0c010b200a4101762108200a41016a410176210b0b417f210a200041046a2109200041186a21072000411c6a210502400340200a41016a220a20084f0d01200728020020092802002005280200280210110100450d000c050b0b200041046a28020021094101210a2000200620012002101e0d07200041186a2208280200200320042000411c6a220228020028020c1100000d0720082802002100417f2108200228020041106a21020340200841016a2208200b4f0d064101210a200020092002280200110100450d000c080b0b41002108200a21070c010b200a4101762108200a41016a41017621070b417f210a200041046a2109200041186a21022000411c6a21010340200a41016a220a20084f0d02200228020020092802002001280200280210110100450d000b0b4101210a0c030b200041046a28020021094101210a200041186a2208280200200320042000411c6a220228020028020c1100000d0220082802002100417f2108200228020041106a21020340200841016a220820074f0d024101210a200020092002280200110100450d000c030b0b41000f0b41000f0b200a0b5501017f0240024002402001418080c400460d0041012104200028021820012000411c6a2802002802101101000d010b2002450d012000280218200220032000411c6a28020028020c11000021040b20040f0b41000b0a00200020012002100e0b3601017f02402002450d00200021030340200320012d00003a0000200341016a2103200141016a21012002417f6a22020d000b0b20000b0b82040200418080c0000bdc0348656c6c6f20576f726c642124001000110000003500100017000000ea020000050000006361706163697479206f766572666c6f777372632f6c6962616c6c6f632f7261775f7665632e72730200000000000000010000000300000030303031303230333034303530363037303830393130313131323133313431353136313731383139323032313232323332343235323632373238323933303331333233333334333533363337333833393430343134323433343434353436343734383439353035313532353335343535353635373538353936303631363236333634363536363637363836393730373137323733373437353736373737383739383038313832383338343835383638373838383939303931393239333934393539363937393839395c0110000600000062011000220000004401100018000000090a0000050000007372632f6c6962636f72652f736c6963652f6d6f642e7273696e64657820206f7574206f662072616e676520666f7220736c696365206f66206c656e677468209c0110002b000000c7011000150000005b0100001500000063616c6c656420604f7074696f6e3a3a756e77726170282960206f6e206120604e6f6e65602076616c75657372632f6c6962636f72652f6f7074696f6e2e72730041e083c0000b140000000000000000000000000000000000000000"
}

你可能感兴趣的:(geth + wagon 环境下调试ewasm 合约的发布与调用)