Contract-Development

通過 Geth RPC 使用建構子參數部署合約

  • December 23, 2016

如何使用 geth rpc 部署帶有建構子參數的編譯合約?

我已經測試了我的呼叫部署編譯的合約字節碼就好了,我只是不明白如何通過 rpc 呼叫以如下形式傳遞建構子參數:

{'jsonrpc': '2.0', 'method': 'eth_sendTransaction', 'params': [{'from': account, 'contract': contract}], 'id': 1}

謝謝

正確解決這個問題非常複雜,但這裡有一些關於如何解決的資訊。

我以一個簡單的契約為例:

pragma solidity ^0.4.2;

contract Test {
   uint256 public v1;
   string public v2;

   function Test(uint256 _v1, string _v2) {
       v1 = _v1;
       v2 = _v2;
   }
}

這是我--dev使用 Browser Solidity 對區塊鏈的部署:

在此處輸入圖像描述

這是來自 Browser Solidity 的字節碼:

6060604052604051610235380380610235833981016040528051608051909101600082815581516001805492819052916020601f60026000196101008588161502019093169290920482018190047fb10e2d527612073b26eecdfd717e6a320cf44b4afac2b0732d9fcbe2b7fa0cf69081019392909186019083901060b657805160ff19168380011785555b5060a49291505b8082111560e357600081556001016092565b5050505061014e806100e76000396000f35b82800160010185558215608b579182015b82811115608b57825182600050559160200191906001019060c7565b509056606060405260e060020a60003504636854171d8114610029578063f3acae3a14610037575b610002565b346100025761009b60005481565b34610002576100ad60018054604080516020601f6002600019610100878916150201909516949094049384018190048102820181019092528281529291908301828280156101465780601f1061011b57610100808354040283529160200191610146565b60408051918252519081900360200190f35b60405180806020018281038252838181518152602001915080519060200190808383829060006004602084601f0104600302600f01f150905090810190601f16801561010d5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b820191906000526020600020905b81548152906001019060200180831161012957829003601f168201915b50505050508156

在將合約部署到--dev區塊鏈之前,我debug.verbosity(4)geth控制台命令行中執行了命令。Browser Solidity 在部署合約時,會顯示如下資訊geth

I1222 14:09:50.670663 core/tx_pool.go:542] 促進排隊交易:

TX(92a1308637d6ad0a63b181194701114780afdaacfcd73604a1f7f3b6985b9755)

契約:假

發件人:000d1009bd8f0b1301cc5edc28ed1222a3ce671e

到:

$$ contract creation $$ 隨機數:457

天然氣價格:20000000000

氣體限制 198696

值:0

數據:0x6060604052604051610235380380610235833981016040528051608051909101600082815581516001805492819052916020601f60026000196101008588161502019093169290920482018190047fb10e2d527612073b26eecdfd717e6a320cf44b4afac2b0732d9fcbe2b7fa0cf69081019392909186019083901060b657805160ff19168380011785555b5060a49291505b8082111560e357600081556001016092565b5050505061014e806100e76000396000f35b82800160010185558215608b579182015b82811115608b57825182600050559160200191906001019060c7565b509056606060405260e060020a60003504636854171d8114610029578063f3acae3a14610037575b610002565b346100025761009b60005481565b34610002576100ad60018054604080516020601f6002600019610100878916150201909516949094049384018190048102820181019092528281529291908301828280156101465780601f1061011b57610100808354040283529160200191610146565b60408051918252519081900360200190f35b60405180806020018281038252838181518152602001915080519060200190808383829060006004602084601f0104600302600f01f150905090810190601f16801561010d5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b820191906000526020600020905b81548152906001019060200180831161012957829003601f168201915b50505050508156000000000000000000000000000000000000000000000000000000007b0000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000d48656c6c6f2c20576f726c6421000000000000000000000000000000000000000000

五:0x1c

R:0xe84786ff0c0ed7564a972d3e037ef74d9adada759f845f36246e818f987d2945

S: 0x38ce5aa5b1a283f3270acd4668e99d410cd7f65ac74f4ae72416b0068935bd51

十六進制:f9030a8201c98504a817c800830308288080b902b56060604052604051610235380380610235833981016040528051608051909101600082815581516001805492819052916020601f60026000196101008588161502019093169290920482018190047fb10e2d527612073b26eecdfd717e6a320cf44b4afac2b0732d9fcbe2b7fa0cf69081019392909186019083901060b657805160ff19168380011785555b5060a49291505b8082111560e357600081556001016092565b5050505061014e806100e76000396000f35b82800160010185558215608b579182015b82811115608b57825182600050559160200191906001019060c7565b509056606060405260e060020a60003504636854171d8114610029578063f3acae3a14610037575b610002565b346100025761009b60005481565b34610002576100ad60018054604080516020601f6002600019610100878916150201909516949094049384018190048102820181019092528281529291908301828280156101465780601f1061011b57610100808354040283529160200191610146565b60408051918252519081900360200190f35b60405180806020018281038252838181518152602001915080519060200190808383829060006004602084601f0104600302600f01f150905090810190601f16801561010d5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b820191906000526020600020905b81548152906001019060200180831161012957829003601f168201915b50505050508156000000000000000000000000000000000000000000000000000000000000007b0000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000d48656c6c6f2c20576f726c6421000000000000000000000000000000000000001ca0e84786ff0c0ed7564a972d3e037ef74d9adada759f845f36246e818f987d2945a038ce5aa5b1a283f3270acd4668e99d410cd7f65ac74f4ae72416b0068935bd51000a031916815260200191505b509250505060405180910390f35b820191906000526020600020905b81548152906001019060200180831161012957829003601f168201915b50505050508156000000000000000000000000000000000000000000000000000000000000007b0000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000d48656c6c6f2c20576f726c6421000000000000000000000000000000000000001ca0e84786ff0c0ed7564a972d3e037ef74d9adada759f845f36246e818f987d2945a038ce5aa5b1a283f3270acd4668e99d410cd7f65ac74f4ae72416b0068935bd51000a031916815260200191505b509250505060405180910390f35b820191906000526020600020905b81548152906001019060200180831161012957829003601f168201915b50505050508156000000000000000000000000000000000000000000000000000000000000007b0000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000d48656c6c6f2c20576f726c6421000000000000000000000000000000000000001ca0e84786ff0c0ed7564a972d3e037ef74d9adada759f845f36246e818f987d2945a038ce5aa5b1a283f3270acd4668e99d410cd7f65ac74f4ae72416b0068935bd5100000000000000000000000000000d48656c6c6f2c20576f726c6421000000000000000000000000000000000000001ca0e84786ff0c0ed7564a972d3e037ef74d9adada759f845f36246e818f987d2945a038ce5aa5b1a283f3270acd4668e99d410cd7f65ac74f4ae72416b0068935bd5100000000000000000000000000000d48656c6c6f2c20576f726c6421000000000000000000000000000000000000001ca0e84786ff0c0ed7564a972d3e037ef74d9adada759f845f36246e818f987d2945a038ce5aa5b1a283f3270acd4668e99d410cd7f65ac74f4ae72416b0068935bd51

Data:將上面的部分作為data:JSON-RPC eth_sendTransaction事務中的參數發送將使用相同的建構子參數實現將合約部署到區塊鏈上的相同操作。

您可以看到欄位的第一部分Data:(非粗體)與合約字節碼匹配,欄位的第二部分Data:(粗體)表示建構子參數。

建構子參數是:

// _v1: 0x7b = 123
000000000000000000000000000000000000000000000000000000000000007b
// 0x40 = 64. This is the offset from the beginning of _v1
// directly above to the start of the next set of hex strings
// ending below
0000000000000000000000000000000000000000000000000000000000000040
// 0xd = 13 - the length of the "Hello, World!" string
000000000000000000000000000000000000000000000000000000000000000d
// In geth, 
// > web3.toUtf8("48656c6c6f2c20576f726c642100000000000000000000000000000000000000")
// "Hello, World!"
48656c6c6f2c20576f726c642100000000000000000000000000000000000000

更多參考資料:


回復下面的評論

我添加了另外兩個範例來展示偏移量的計算。

範例 2

pragma solidity ^0.4.2;

contract Test {
   uint256 public v1;
   uint256 public v2;
   string public v3;

   function Test(uint256 _v1, uint256 _v2, string _v3) {
       v1 = _v1;
       v2 = _v2;
       v3 = _v3;
   }
}

上面部署的合約具有以下參數:

123,456,"thequickbrownfoxjumpsoverthelazydog"

這是使用建構子部署合約的數據,參數以粗體顯示:

0x60606040526040516102de3803806102de833981016040528080519060200190919080519060200190919080518201919060200150505b82600060005081905550816001600050819055508060026000509080519060200190828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061009957805160ff19168380011785556100ca565b828001600101855582156100ca579182015b828111156100c95782518260005055916020019190600101906100ab565b5b5090506100f591906100d7565b808211156100f157600081815060009055506001016100d7565b5090565b50505b5050506101d5806101096000396000f360606040526000357c0100000000000000000000000000000000000000000000000000000000900480636257a38e146100525780636854171d146100d2578063f3acae3a146100fa5761004d565b610002565b34610002576100646004805050610122565b60405180806020018281038252838181518152602001915080519060200190808383829060006004602084601f0104600302600f01f150905090810190601f1680156100c45780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b34610002576100e460048050506101c3565b6040518082815260200191505060405180910390f35b346100025761010c60048050506101cc565b6040518082815260200191505060405180910390f35b60026000508054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156101bb5780601f10610190576101008083540402835291602001916101bb565b820191906000526020600020905b81548152906001019060200180831161019e57829003601f168201915b505050505081565b60006000505481565b6001600050548156f35b60026000508054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156101bb5780601f10610190576101008083540402835291602001916101bb565b820191906000526020600020905b81548152906001019060200180831161019e57829003601f168201915b505050505081565b60006000505481565b6001600050548156f35b60026000508054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156101bb5780601f10610190576101008083540402835291602001916101bb565b820191906000526020600020905b81548152906001019060200180831161019e57829003601f168201915b505050505081565b60006000505481565b6001600050548156000000000000000000000000000000000000000000000000000000000000007b00000000000000000000000000000000000000000000000000000001c80000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000023746865717569636b62726f776e666f786a756d70736f7665727468656c617a79646f67000000000000000000000000000000

// _v1: 0x7b = 123
000000000000000000000000000000000000000000000000000000000000007b
// _v2: 0x1c8 = 456
00000000000000000000000000000000000000000000000000000000000001c8
// 0x60 = 96. This is the offset from the start of _v1 to the
// start of v3. 32 x 3 = 96
0000000000000000000000000000000000000000000000000000000000000060
// _v3: 0x23 = 35
0000000000000000000000000000000000000000000000000000000000000023
// In geth:
// > web3.toUtf8("746865717569636b62726f776e666f786a756d70736f7665727468656c617a79646f67")
// "thequickbrownfoxjumpsoverthelazydog" (35 chars)
746865717569636b62726f776e666f786a756d70736f7665727468656c617a79
646f670000000000000000000000000000000000000000000000000000000000

範例 3

pragma solidity ^0.4.2;

contract Test {
   uint256 public v1;
   uint256 public v2;
   string public v3;
   string public v4;

   function Test(uint256 _v1, uint256 _v2, string _v3, string _v4) {
       v1 = _v1;
       v2 = _v2;
       v3 = _v3;
       v4 = _v4;
   }
}

上面部署的合約具有以下參數:

123,456,"thequickbrownfoxjumpsoverthelazydog","shesellsseashellsontheseashore"

這是使用建構子部署合約的數據,參數以粗體顯示:

0x60606040526040516104c13803806104c1833981016040528080519060200190919080519060200190919080518201919060200180518201919060200150505b83600060005081905550826001600050819055508160026000509080519060200190828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106100a257805160ff19168380011785556100d3565b828001600101855582156100d3579182015b828111156100d25782518260005055916020019190600101906100b4565b5b5090506100fe91906100e0565b808211156100fa57600081815060009055506001016100e0565b5090565b50508060036000509080519060200190828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061014f57805160ff1916838001178555610180565b82800160010185558215610180579182015b8281111561017f578251826000505591602001919060010190610161565b5b5090506101ab919061018d565b808211156101a7576000818150600090555060010161018d565b5090565b50505b50505050610301806101c06000396000f360606040526000357c0100000000000000000000000000000000000000000000000000000000900480636257a38e1461005d5780636854171d146100dd578063bfa5750314610105578063f3acae3a1461018557610058565b610002565b346100025761006f60048050506101ad565b60405180806020018281038252838181518152602001915080519060200190808383829060006004602084601f0104600302600f01f150905090810190601f1680156100cf5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b34610002576100ef600480505061024e565b6040518082815260200191505060405180910390f35b34610002576101176004805050610257565b60405180806020018281038252838181518152602001915080519060200190808383829060006004602084601f0104600302600f01f150905090810190601f1680156101775780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b346100025761019760048050506102f8565b6040518082815260200191505060405180910390f35b60026000508054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156102465780601f1061021b57610100808354040283529160200191610246565b820191906000526020600020905b81548152906001019060200180831161022957829003601f168201915b505050505081565b60006000505481565b60036000508054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156102f05780601f106102c5576101008083540402835291602001916102f0565b820191906000526020600020905b8154815290600101906020018083116102d357829003601f168201915b505050505081565b6001600050548156602001828054600181600116156101000203166002900480156102465780601f1061021b57610100808354040283529160200191610246565b820191906000526020600020905b81548152906001019060200180831161022957829003601f168201915b505050505081565b60006000505481565b60036000508054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156102f05780601f106102c5576101008083540402835291602001916102f0565b820191906000526020600020905b8154815290600101906020018083116102d357829003601f168201915b505050505081565b6001600050548156602001828054600181600116156101000203166002900480156102465780601f1061021b57610100808354040283529160200191610246565b820191906000526020600020905b81548152906001019060200180831161022957829003601f168201915b505050505081565b60006000505481565b60036000508054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156102f05780601f106102c5576101008083540402835291602001916102f0565b820191906000526020600020905b8154815290600101906020018083116102d357829003601f168201915b505050505081565b60016000505481560160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156102f05780601f106102c5576101008083540402835291602001916102f0565b820191906000526020600020905b8154815290600101906020018083116102d357829003601f168201915b505050505081565b60016000505481560160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156102f05780601f106102c5576101008083540402835291602001916102f0565b820191906000526020600020905b8154815290600101906020018083116102d357829003601f168201915b505050505081565b6001600050548156000000000000000000000000000000000000000000000000000000000000007b00000000000000000000000000000000000000000000000000000000000001c8000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000e00000000000000000000000000000000000000000000000000000000000000023746865717569636b62726f776e666f786a756d70736f7665727468656c617a79646f670000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001e73686573656c6c737365617368656c6c736f6e74686573656173686f72650000

這是參數的解碼:

// _v1: 0x7b = 123
000000000000000000000000000000000000000000000000000000000000007b
// _v2: 0x1c8 = 456
00000000000000000000000000000000000000000000000000000000000001c8
// 0x80 = 128. This is the offset from the start of _v1 to the
// start of v3. 32 + 32 + 32 + 32 = 128
0000000000000000000000000000000000000000000000000000000000000080
// 0xe0 = 224. This is the offset from the start of _v1 to the
// start of v4. 32 x 7 = 224
00000000000000000000000000000000000000000000000000000000000000e0
// _v3: 0x23 = 35
0000000000000000000000000000000000000000000000000000000000000023
// In geth:
// > web3.toUtf8("746865717569636b62726f776e666f786a756d70736f7665727468656c617a79646f67")
// "thequickbrownfoxjumpsoverthelazydog" (35 chars)
746865717569636b62726f776e666f786a756d70736f7665727468656c617a79
646f670000000000000000000000000000000000000000000000000000000000
// v4: 0x1e = 30
000000000000000000000000000000000000000000000000000000000000001e
// In geth:
// > web3.toUtf8("73686573656c6c737365617368656c6c736f6e74686573656173686f72650000")
// "shesellsseashellsontheseashore" (30 chars)
73686573656c6c737365617368656c6c736f6e74686573656173686f72650000

引用自:https://ethereum.stackexchange.com/questions/10862