Solidity

為什麼solidity編譯的字節碼與創建交易的數據不匹配?

  • February 2, 2022

我正在嘗試獨立驗證solidity合約的原始碼,並偶然發現了合約原始碼的編譯字節碼與創建合約的交易數據不匹配的情況。根據https://etherchain.org/account_verify/info和我讀過的其他內容,通過使用正確的solidity 版本和優化的標誌編譯合約收到的字節碼應該與創建交易的輸入欄位匹配。

契約可以在這裡找到:https ://etherscan.io/address/0x9ffa45c5943a0203ee462c48c8b97064abbe28f3#code

通過奇偶校驗使用 web3.eth.getTransaction,我看到事務的數據(0xf991503a9e798e2c8e53c865b09e985f59100a1561f9a5fad546f59e8a4096db)為:

606060405234610000575b60008054600160a060020a031916730807a2d6a675e7196a3d9b1910700cae9795b72a1790555b5b6104fb806100416000396000f300606060405236156100675763ffffffff60e060020a6000350416633ccfd60b81146101115780633feb5f2b146101205780635d4522011461014c57806370a082311461017557806372ea4b8c146101a05780637975ce28146101bf578063cee2a9cf146101d4575b61010f5b61007433610201565b15156100ee57600180548060010182818154818355818115116100bc576000838152602090206100bc9181019083015b808211156100b857600081556001016100a4565b5090565b5b505050916000526020600020900160005b8154600160a060020a033381166101009390930a92830292021916179055505b600160a060020a03331660009081526002602052604090208054340190555b565b005b346100005761010f610271565b005b34610000576101306004356102a3565b60408051600160a060020a039092168252519081900360200190f35b34610000576101306102d3565b60408051600160a060020a039092168252519081900360200190f35b346100005761018e600160a060020a03600435166102e2565b60408051918252519081900360200190f35b346100005761018e6102f4565b60408051918252519081900360200190f35b346100005761010f6004356024356102fb565b005b34610000576101ed600160a060020a0360043516610201565b604080519115158252519081900360200190f35b6000805b6001548110156102665782600160a060020a0316600182815481101561000057906000526020600020900160005b9054906101000a9004600160a060020a0316600160a060020a0316141561025d576001915061026b565b5b600101610205565b600091505b50919050565b600160a060020a03331660008181526002602052604080822054905181156108fc0292818181858888f150505050505b565b600181815481101561000057906000526020600020900160005b915054906101000a9004600160a060020a031681565b600054600160a060020a031681565b60026020526000908152604090205481565b6001545b90565b60015460009081908311156103105760015492505b50825b828110156104c857600060026000600184815481101561000057906000526020600020900160005b9054906101000a9004600160a060020a0316600160a060020a0316600160a060020a031681526020019081526020016000205411156104bf5760026000600183815481101561000057906000526020600020900160005b9054906101000a9004600160a060020a0316600160a060020a0316600160a060020a0316815260200190815260200160002054915060026000600183815481101561000057906000526020600020900160005b9054906101000a9004600160a060020a0316600160a060020a0316600160a060020a0316815260200190815260200160002060009055600060009054906101000a9004600160a060020a0316600160a060020a03166303f9c79383600184815481101561000057906000526020600020900160005b9054906101000a9004600160a060020a03166040518363ffffffff1660e060020a0281526004018082600160a060020a0316600160a060020a031681526020019150506000604051808303818588803b156100005761235a5a03f11561000057505050505b5b600101610313565b5b505050505600a165627a7a72305820e7c797ee36d8a2e4c28e3fa53150f5fac2d95456b0610d4b7e2a312b5249c2480029

我正在使用 solc-js 編譯器(具有相同的 Solidity 版本,v0.4.8+commit.60cc1668,已優化),並編譯 etherscan 連結中顯示的相同原始碼給出:

606060405234610000575b60008054600160a060020a031916730807a2d6a675e7196a3d9b1910700cae9795b72a1790555b5b6104fb806100416000396000f300606060405236156100675763ffffffff60e060020a6000350416633ccfd60b81146101115780633feb5f2b146101205780635d4522011461014c57806370a082311461017557806372ea4b8c146101a05780637975ce28146101bf578063cee2a9cf146101d4575b61010f5b61007433610201565b15156100ee57600180548060010182818154818355818115116100bc576000838152602090206100bc9181019083015b808211156100b857600081556001016100a4565b5090565b5b505050916000526020600020900160005b8154600160a060020a033381166101009390930a92830292021916179055505b600160a060020a03331660009081526002602052604090208054340190555b565b005b346100005761010f610271565b005b34610000576101306004356102a3565b60408051600160a060020a039092168252519081900360200190f35b34610000576101306102d3565b60408051600160a060020a039092168252519081900360200190f35b346100005761018e600160a060020a03600435166102e2565b60408051918252519081900360200190f35b346100005761018e6102f4565b60408051918252519081900360200190f35b346100005761010f6004356024356102fb565b005b34610000576101ed600160a060020a0360043516610201565b604080519115158252519081900360200190f35b6000805b6001548110156102665782600160a060020a0316600182815481101561000057906000526020600020900160005b9054906101000a9004600160a060020a0316600160a060020a0316141561025d576001915061026b565b5b600101610205565b600091505b50919050565b600160a060020a03331660008181526002602052604080822054905181156108fc0292818181858888f150505050505b565b600181815481101561000057906000526020600020900160005b915054906101000a9004600160a060020a031681565b600054600160a060020a031681565b60026020526000908152604090205481565b6001545b90565b60015460009081908311156103105760015492505b50825b828110156104c857600060026000600184815481101561000057906000526020600020900160005b9054906101000a9004600160a060020a0316600160a060020a0316600160a060020a031681526020019081526020016000205411156104bf5760026000600183815481101561000057906000526020600020900160005b9054906101000a9004600160a060020a0316600160a060020a0316600160a060020a0316815260200190815260200160002054915060026000600183815481101561000057906000526020600020900160005b9054906101000a9004600160a060020a0316600160a060020a0316600160a060020a0316815260200190815260200160002060009055600060009054906101000a9004600160a060020a0316600160a060020a03166303f9c79383600184815481101561000057906000526020600020900160005b9054906101000a9004600160a060020a03166040518363ffffffff1660e060020a0281526004018082600160a060020a0316600160a060020a031681526020019150506000604051808303818588803b156100005761235a5a03f11561000057505050505b5b600101610313565b5b505050505600a165627a7a723058202529b66753f69883ab5e76eebe3048611454a5662624196e7cac88f862841a990029

此外,當我嘗試使用 remix 線上編譯器進行編譯時,它給了我:

606060405234610000575b60008054600160a060020a031916730807a2d6a675e7196a3d9b1910700cae9795b72a1790555b5b6104fb806100416000396000f300606060405236156100675763ffffffff60e060020a6000350416633ccfd60b81146101115780633feb5f2b146101205780635d4522011461014c57806370a082311461017557806372ea4b8c146101a05780637975ce28146101bf578063cee2a9cf146101d4575b61010f5b61007433610201565b15156100ee57600180548060010182818154818355818115116100bc576000838152602090206100bc9181019083015b808211156100b857600081556001016100a4565b5090565b5b505050916000526020600020900160005b8154600160a060020a033381166101009390930a92830292021916179055505b600160a060020a03331660009081526002602052604090208054340190555b565b005b346100005761010f610271565b005b34610000576101306004356102a3565b60408051600160a060020a039092168252519081900360200190f35b34610000576101306102d3565b60408051600160a060020a039092168252519081900360200190f35b346100005761018e600160a060020a03600435166102e2565b60408051918252519081900360200190f35b346100005761018e6102f4565b60408051918252519081900360200190f35b346100005761010f6004356024356102fb565b005b34610000576101ed600160a060020a0360043516610201565b604080519115158252519081900360200190f35b6000805b6001548110156102665782600160a060020a0316600182815481101561000057906000526020600020900160005b9054906101000a9004600160a060020a0316600160a060020a0316141561025d576001915061026b565b5b600101610205565b600091505b50919050565b600160a060020a03331660008181526002602052604080822054905181156108fc0292818181858888f150505050505b565b600181815481101561000057906000526020600020900160005b915054906101000a9004600160a060020a031681565b600054600160a060020a031681565b60026020526000908152604090205481565b6001545b90565b60015460009081908311156103105760015492505b50825b828110156104c857600060026000600184815481101561000057906000526020600020900160005b9054906101000a9004600160a060020a0316600160a060020a0316600160a060020a031681526020019081526020016000205411156104bf5760026000600183815481101561000057906000526020600020900160005b9054906101000a9004600160a060020a0316600160a060020a0316600160a060020a0316815260200190815260200160002054915060026000600183815481101561000057906000526020600020900160005b9054906101000a9004600160a060020a0316600160a060020a0316600160a060020a0316815260200190815260200160002060009055600060009054906101000a9004600160a060020a0316600160a060020a03166303f9c79383600184815481101561000057906000526020600020900160005b9054906101000a9004600160a060020a03166040518363ffffffff1660e060020a0281526004018082600160a060020a0316600160a060020a031681526020019150506000604051808303818588803b156100005761235a5a03f11561000057505050505b5b600101610313565b5b505050505600a165627a7a723058209af8a6ef7fce5c06c5d80234a6a37e819ab91c3c3950d09db1c3ab478cbff2670029

奇怪的是,solc-js 和 remix 編譯後的字節碼不匹配,也不匹配鏈上合約初始化數據。仔細觀察編譯後的字節碼,我注意到所有的開頭都是:

606060405234610000575b60008054600160a060020a031916730807a2d6a675e7196a3d9b1910700cae9795b72a1790555b5b6104fb806100416000396000f300606060405236156100675763ffffffff60e060020a6000350416633ccfd60b81146101115780633feb5f2b146101205780635d4522011461014c57806370a082311461017557806372ea4b8c146101a05780637975ce28146101bf578063cee2a9cf146101d4575b61010f5b61007433610201565b15156100ee57600180548060010182818154818355818115116100bc576000838152602090206100bc9181019083015b808211156100b857600081556001016100a4565b5090565b5b505050916000526020600020900160005b8154600160a060020a033381166101009390930a92830292021916179055505b600160a060020a03331660009081526002602052604090208054340190555b565b005b346100005761010f610271565b005b34610000576101306004356102a3565b60408051600160a060020a039092168252519081900360200190f35b34610000576101306102d3565b60408051600160a060020a039092168252519081900360200190f35b346100005761018e600160a060020a03600435166102e2565b60408051918252519081900360200190f35b346100005761018e6102f4565b60408051918252519081900360200190f35b346100005761010f6004356024356102fb565b005b34610000576101ed600160a060020a0360043516610201565b604080519115158252519081900360200190f35b6000805b6001548110156102665782600160a060020a0316600182815481101561000057906000526020600020900160005b9054906101000a9004600160a060020a0316600160a060020a0316141561025d576001915061026b565b5b600101610205565b600091505b50919050565b600160a060020a03331660008181526002602052604080822054905181156108fc0292818181858888f150505050505b565b600181815481101561000057906000526020600020900160005b915054906101000a9004600160a060020a031681565b600054600160a060020a031681565b60026020526000908152604090205481565b6001545b90565b60015460009081908311156103105760015492505b50825b828110156104c857600060026000600184815481101561000057906000526020600020900160005b9054906101000a9004600160a060020a0316600160a060020a0316600160a060020a031681526020019081526020016000205411156104bf5760026000600183815481101561000057906000526020600020900160005b9054906101000a9004600160a060020a0316600160a060020a0316600160a060020a0316815260200190815260200160002054915060026000600183815481101561000057906000526020600020900160005b9054906101000a9004600160a060020a0316600160a060020a0316600160a060020a0316815260200190815260200160002060009055600060009054906101000a9004600160a060020a0316600160a060020a03166303f9c79383600184815481101561000057906000526020600020900160005b9054906101000a9004600160a060020a03166040518363ffffffff1660e060020a0281526004018082600160a060020a0316600160a060020a031681526020019150506000604051808303818588803b156100005761235a5a03f11561000057505050505b5b600101610313565b5b505050505600a165627a7a72305820

但之後有不同的結局。有誰知道為什麼會這樣?

驗證原始碼並不容易。您需要“完全相同的編譯器版本”。

請參閱:https ://ethereum.stackexchange.com/a/221/852

如果你只是想知道地址是否存在合約,最簡單的方法是包含一個簡單的函式。

function contractExists () constant returns (bool result){
 return true;
}

安東尼奧,我看到了完全相同的問題,我選擇了相同的混音和相同的非常comp版本..一切都很好並且完全相同並且它驗證了..但是當我在混音中選擇優化並在etherscan中優化驗證..然後字節碼是非常不一樣。5 行簡單狀態合約,無構造參數和 np 庫

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