Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix contract type detection: get sighashes from all basicblocks #413

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 44 additions & 21 deletions ethereumetl/service/eth_contract_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,13 @@ def get_function_sighashes(self, bytecode):
evm_code.disassemble(bytecode)
basic_blocks = evm_code.basicblocks
if basic_blocks and len(basic_blocks) > 0:
init_block = basic_blocks[0]
instructions = init_block.instructions
push4_instructions = [inst for inst in instructions if inst.name == 'PUSH4']
return sorted(list(set('0x' + inst.operand for inst in push4_instructions)))
push4_instructions = set()
for block in basic_blocks:
instructions = block.instructions
block_push4_instructions = [inst for inst in instructions if inst.name == 'PUSH4']
push4_instructions.update(block_push4_instructions)

return sorted(list({'0x' + inst.operand for inst in push4_instructions}))
else:
return []
else:
Expand All @@ -46,28 +49,48 @@ def get_function_sighashes(self, bytecode):
# https://github.com/OpenZeppelin/openzeppelin-solidity/blob/master/contracts/token/ERC20/ERC20.sol
def is_erc20_contract(self, function_sighashes):
c = ContractWrapper(function_sighashes)
return c.implements('totalSupply()') and \
c.implements('balanceOf(address)') and \
c.implements('transfer(address,uint256)') and \
c.implements('transferFrom(address,address,uint256)') and \
c.implements('approve(address,uint256)') and \
c.implements('allowance(address,address)')
return all([
c.implements('totalSupply()'),
c.implements('balanceOf(address)'),
c.implements('transfer(address,uint256)'),
c.implements('transferFrom(address,address,uint256)'),
c.implements('approve(address,uint256)'),
c.implements('allowance(address,address)')
])

# https://github.com/ethereum/EIPs/blob/master/EIPS/eip-721.md
# https://github.com/OpenZeppelin/openzeppelin-solidity/blob/master/contracts/token/ERC721/ERC721Basic.sol
# Doesn't check the below ERC721 methods to match CryptoKitties contract
# getApproved(uint256)
# setApprovalForAll(address,bool)
# isApprovedForAll(address,address)
# transferFrom(address,address,uint256)
# safeTransferFrom(address,address,uint256)
# safeTransferFrom(address,address,uint256,bytes)
# CryptoKitties contracts doesn't strictly implement erc721 interface
# so we have to check for it's sighashes explicitly
def is_erc721_contract(self, function_sighashes):
c = ContractWrapper(function_sighashes)
return c.implements('balanceOf(address)') and \
c.implements('ownerOf(uint256)') and \
c.implements_any_of('transfer(address,uint256)', 'transferFrom(address,address,uint256)') and \
c.implements('approve(address,uint256)')
return all([
c.implements('balanceOf(address)'),
c.implements('ownerOf(uint256)'),
c.implements_any_of('transfer(address,uint256)', 'transferFrom(address,address,uint256)'),
c.implements('approve(address,uint256)'),
c.implements('getApproved(uint256)'),
c.implements('setApprovalForAll(address,bool)'),
c.implements('isApprovedForAll(address,address)'),
c.implements('transferFrom(address,address,uint256)'),
c.implements('safeTransferFrom(address,address,uint256)'),
c.implements('safeTransferFrom(address,address,uint256,bytes)'),
]) or self.is_crypto_kitties_contract(function_sighashes)

# https://etherscan.io/address/0x06012c8cf97bead5deae237070f9587f8e7a266d#code#L52
def is_crypto_kitties_contract(self, function_sighashes):
return function_sighashes == [
'0x01ffc9a7', '0x0519ce79', '0x0560ff44', '0x05e45546', '0x06fdde03', '0x095ea7b3','0x0a0f8168',
'0x0d9f5aed', '0x0e583df0', '0x14001f4c', '0x18160ddd', '0x183a7947', '0x1940a936', '0x19c2f201',
'0x21717ebf', '0x23b872dd', '0x24e7a38a', '0x27d7874c', '0x27ebe40a', '0x2ba73c15', '0x3d7d3f5a',
'0x3f4ba83a', '0x454a2ab3', '0x46116e6f', '0x46d22c70', '0x481af3d3', '0x4ad8c938', '0x4b85fd55',
'0x4dfff04f', '0x4e0a3379', '0x54c15b82', '0x56129134', '0x5663896e', '0x5c975abb', '0x5fd8c710',
'0x6352211e', '0x680eba27', '0x6af04a57', '0x6fbde40d', '0x70a08231', '0x71587988', '0x76190f8f',
'0x7a7d4937', '0x8456cb59', '0x8462151c', '0x85b86188', '0x88c2a0bf', '0x91876e57', '0x95d89b41',
'0x9d6fac6f', '0xa45f4bfc', '0xa9059cbb', '0xb047fb50', '0xb0c35c05', '0xbc4006f5', '0xc3bea9af',
'0xc55d0f56', '0xcb4799f2', '0xd3e6f49f', '0xdefb9584', '0xe17b25af', '0xe6cbe351', '0xe98b7f4d',
'0xeac9d94c', '0xed60ade6', '0xf1ca9410', '0xf2b47d52', '0xf7d8c883', '0xffffffff'
]


def clean_bytecode(bytecode):
Expand Down
72 changes: 54 additions & 18 deletions tests/ethereumetl/service/test_eth_contract_service.py

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"type": "contract", "address": "0xdbdacfc9eb9d42559ac1efbdb40460c728139e6a", "bytecode": "0x6060604052361561008d5760e060020a600035046306fdde03811461008f578063095ea7b3146100a557806318160ddd1461012457806323b872dd1461012f578063313ce567146101dc578063475a9fa9146101f057806370a0823114610215578063721a37d21461024357806395d89b411461008f578063a9059cbb14610268578063dd62ed3e146102e7575b005b61031d6040805160208101909152600081525b90565b61038b60043560243560007319ee743d2e356d5f0e4d97cc09b96d06e933d0db63c6605267600160005085856040518460e060020a0281526004018084815260200183600160a060020a0316815260200182815260200193505050506020604051808303818660325a03f4156100025750506040515191506103179050565b6102316003546100a2565b61038b60043560243560443560008054604080517fa00bfa1100000000000000000000000000000000000000000000000000000000815260016004820152600160a060020a038781166024830152868116604483015260648201869052929092166084830152517319ee743d2e356d5f0e4d97cc09b96d06e933d0db9163a00bfa119160a482810192602092919082900301818660325a03f4156100025750506040515195945050505050565b604080516000815290519081900360200190f35b61038b6004356024356000805433600160a060020a0390811691161461039f57610002565b600160a060020a03600435166000908152600160205260409020545b60408051918252519081900360200190f35b61038b6004356024356000805433600160a060020a039081169116146103ce57610002565b61038b60043560243560007319ee743d2e356d5f0e4d97cc09b96d06e933d0db6388d5fecb600160005085856040518460e060020a0281526004018084815260200183600160a060020a0316815260200182815260200193505050506020604051808303818660325a03f4156100025750506040515191506103179050565b610231600435602435600160a060020a038281166000908152600260209081526040808320938516835292905220545b92915050565b60405180806020018281038252838181518152602001915080519060200190808383829060006004602084601f0104600302600f01f150905090810190601f16801561037d5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b604080519115158252519081900360200190f35b50600160a060020a03821660009081526001602081905260409091208054830190556003805483019055610317565b600160a060020a038316600090815260016020526040902054821161040a57506040600020805482900390556003805482900390556001610317565b50600061031756", "function_sighashes": ["0x06fdde03", "0x095ea7b3", "0x18160ddd", "0x23b872dd", "0x313ce567", "0x475a9fa9", "0x70a08231", "0x721a37d2", "0x95d89b41", "0xa9059cbb", "0xdd62ed3e"], "is_erc20": true, "is_erc721": false, "block_number": 2112234, "block_timestamp": 1471774428, "block_hash": "0xd279067d9852394d6b6c00b13c49696503c9618d3ed3b23c6b1b1321857ddd92", "item_id": "contract_2112234_0xdbdacfc9eb9d42559ac1efbdb40460c728139e6a", "item_timestamp": "2016-08-21T10:13:48Z"}
{"type": "contract", "address": "0xdbdacfc9eb9d42559ac1efbdb40460c728139e6a", "bytecode": "0x6060604052361561008d5760e060020a600035046306fdde03811461008f578063095ea7b3146100a557806318160ddd1461012457806323b872dd1461012f578063313ce567146101dc578063475a9fa9146101f057806370a0823114610215578063721a37d21461024357806395d89b411461008f578063a9059cbb14610268578063dd62ed3e146102e7575b005b61031d6040805160208101909152600081525b90565b61038b60043560243560007319ee743d2e356d5f0e4d97cc09b96d06e933d0db63c6605267600160005085856040518460e060020a0281526004018084815260200183600160a060020a0316815260200182815260200193505050506020604051808303818660325a03f4156100025750506040515191506103179050565b6102316003546100a2565b61038b60043560243560443560008054604080517fa00bfa1100000000000000000000000000000000000000000000000000000000815260016004820152600160a060020a038781166024830152868116604483015260648201869052929092166084830152517319ee743d2e356d5f0e4d97cc09b96d06e933d0db9163a00bfa119160a482810192602092919082900301818660325a03f4156100025750506040515195945050505050565b604080516000815290519081900360200190f35b61038b6004356024356000805433600160a060020a0390811691161461039f57610002565b600160a060020a03600435166000908152600160205260409020545b60408051918252519081900360200190f35b61038b6004356024356000805433600160a060020a039081169116146103ce57610002565b61038b60043560243560007319ee743d2e356d5f0e4d97cc09b96d06e933d0db6388d5fecb600160005085856040518460e060020a0281526004018084815260200183600160a060020a0316815260200182815260200193505050506020604051808303818660325a03f4156100025750506040515191506103179050565b610231600435602435600160a060020a038281166000908152600260209081526040808320938516835292905220545b92915050565b60405180806020018281038252838181518152602001915080519060200190808383829060006004602084601f0104600302600f01f150905090810190601f16801561037d5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b604080519115158252519081900360200190f35b50600160a060020a03821660009081526001602081905260409091208054830190556003805483019055610317565b600160a060020a038316600090815260016020526040902054821161040a57506040600020805482900390556003805482900390556001610317565b50600061031756", "function_sighashes": ["0x06fdde03", "0x095ea7b3", "0x18160ddd", "0x23b872dd", "0x313ce567", "0x475a9fa9", "0x70a08231", "0x721a37d2", "0x88d5fecb", "0x95d89b41", "0xa00bfa11", "0xa9059cbb", "0xc6605267", "0xdd62ed3e"], "is_erc20": true, "is_erc721": false, "block_number": 2112234, "block_timestamp": 1471774428, "block_hash": "0xd279067d9852394d6b6c00b13c49696503c9618d3ed3b23c6b1b1321857ddd92", "item_id": "contract_2112234_0xdbdacfc9eb9d42559ac1efbdb40460c728139e6a", "item_timestamp": "2016-08-21T10:13:48Z"}
Loading