ethereum / go-ethereum

Go implementation of the Ethereum protocol
https://geth.ethereum.org
GNU Lesser General Public License v3.0
47.24k stars 20k forks source link

Calling a method on a contract gives "0x" for address, 0 for uint #16743

Closed quantumproducer closed 6 years ago

quantumproducer commented 6 years ago

System information

Geth version: 1.8.8 OS & Version: Ubuntu

Expected behaviour

Read data from contract

Actual behaviour

0x is returned from calling a method

Steps to reproduce the behaviour

Running geth:

geth --syncmode "fast" --cache=1024 --datadir "mnt/tryit" --rpc
geth attach ipc:///mnt/tryit/geth.ipc

abi = [{"constant":true,"inputs":[{"name":"","type":"uint256"}],"name":"owners","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"ownersNum","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[],"name":"payout","outputs":[{"name":"success","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"developer","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[],"name":"buyKey","outputs":[{"name":"success","type":"bool"}],"payable":true,"stateMutability":"payable","type":"function"},{"anonymous":false,"inputs":[{"indexed":true,"name":"buyer","type":"address"}],"name":"QuantumPilotKeyPurchased","type":"event"}]

keyaddress = "0xFc7e86dBD205D02F97316995d8AcE5D0AFB9fe62"

cABI = web3.eth.contract(abi)
c = cABI.at(keyaddress)

c.developer.call()
>>>0x

EXPECTED: From https://etherscan.io/address/0xFc7e86dBD205D02F97316995d8AcE5D0AFB9fe62#readContract , I expected this call to give: ownersNum 1 uint256

  1. developer 0x2c3b0f6e40d61feb9def9deb1811ea66485b83e7 address

as the result, NOT 0x.

eth.syncing:

eth.syncing
{
  currentBlock: 5617672,
  highestBlock: 5617742,
  knownStates: 127516821,
  pulledStates: 127508634,
  startingBlock: 5617664
}

I also tried c.ownersNum.call() and c.ownersNum(), expecting 1, and both yield 0.

The contract was created at block 5581564.

I first thought this was a web3 issue, but upon further testing this isn't an issue in the Metamask injected web3, which makes me suspect this is a problem with the geth datastorage.

karalabe commented 6 years ago

Syncing Ethereum is a pain point for many people, so I'll try to detail what's happening behind the scenes so there might be a bit less confusion.

The current default mode of sync for Geth is called fast sync. Instead of starting from the genesis block and reprocessing all the transactions that ever occurred (which could take weeks), fast sync downloads the blocks, and only verifies the associated proof-of-works. Downloading all the blocks is a straightforward and fast procedure and will relatively quickly reassemble the entire chain.

Many people falsely assume that because they have the blocks, they are in sync. Unfortunately this is not the case, since no transaction was executed, so we do not have any account state available (ie. balances, nonces, smart contract code and data). These need to be downloaded separately and cross checked with the latest blocks. This phase is called the state trie download and it actually runs concurrently with the block downloads; alas it take a lot longer nowadays than downloading the blocks.

So, what's the state trie? In the Ethereum mainnet, there are a ton of accounts already, which track the balance, nonce, etc of each user/contract. The accounts themselves are however insufficient to run a node, they need to be cryptographically linked to each block so that nodes can actually verify that the account's are not tampered with. This cryptographic linking is done by creating a tree data structure above the accounts, each level aggregating the layer below it into an ever smaller layer, until you reach the single root. This gigantic data structure containing all the accounts and the intermediate cryptographic proofs is called the state trie.

Ok, so why does this pose a problem? This trie data structure is an intricate interlink of hundreds of millions of tiny cryptographic proofs (trie nodes). To truly have a synchronized node, you need to download all the account data, as well as all the tiny cryptographic proofs to verify that noone in the network is trying to cheat you. This itself is already a crazy number of data items. The part where it gets even messier is that this data is constantly morphing: at every block (15s), about 1000 nodes are deleted from this trie and about 2000 new ones are added. This means your node needs to synchronize a dataset that is changing 200 times per second. The worst part is that while you are synchronizing, the network is moving forward, and state that you begun to download might disappear while you're downloading, so your node needs to constantly follow the network while trying to gather all the recent data. But until you actually do gather all the data, your local node is not usable since it cannot cryptographically prove anything about any accounts.

If you see that you are 64 blocks behind mainnet, you aren't yet synchronized, not even close. You are just done with the block download phase and still running the state downloads. You can see this yourself via the seemingly endless Imported state entries [...] stream of logs. You'll need to wait that out too before your node comes truly online.


Q: The node just hangs on importing state enties?!

A: The node doesn't hang, it just doesn't know how large the state trie is in advance so it keeps on going and going and going until it discovers and downloads the entire thing.

The reason is that a block in Ethereum only contains the state root, a single hash of the root node. When the node begins synchronizing, it knows about exactly 1 node and tries to download it. That node, can refer up to 16 new nodes, so in the next step, we'll know about 16 new nodes and try to download those. As we go along the download, most of the nodes will reference new ones that we didn't know about until then. This is why you might be tempted to think it's stuck on the same numbers. It is not, rather it's discovering and downloading the trie as it goes along.

Q: I'm stuck at 64 blocks behind mainnet?!

A: As explained above, you are not stuck, just finished with the block download phase, waiting for the state download phase to complete too. This latter phase nowadays take a lot longer than just getting the blocks.

Q: Why does downloading the state take so long, I have good bandwidth?

A: State sync is mostly limited by disk IO, not bandwidth.

The state trie in Ethereum contains hundreds of millions of nodes, most of which take the form of a single hash referencing up to 16 other hashes. This is a horrible way to store data on a disk, because there's almost no structure in it, just random numbers referencing even more random numbers. This makes any underlying database weep, as it cannot optimize storing and looking up the data in any meaningful way.

Not only is storing the data very suboptimal, but due to the 200 modification / second and pruning of past data, we cannot even download it is a properly pre-processed way to make it import faster without the underlying database shuffling it around too much. The end result is that even a fast sync nowadays incurs a huge disk IO cost, which is too much for a mechanical hard drive.

Q: Wait, so I can't run a full node on an HDD?

A: Unfortunately not. Doing a fast sync on an HDD will take more time than you're willing to wait with the current data schema. Even if you do wait it out, an HDD will not be able to keep up with the read/write requirements of transaction processing on mainnet.

You however should be able to run a light client on an HDD with minimal impact on system resources. If you wish to run a full node however, an SSD is your only option.

quantumproducer commented 6 years ago

Thank for the detailed writeup, @karalabe . What is the estimated time & storage space of a light node (instead of fast), if there were actual peers? ( https://github.com/ethereum/go-ethereum/issues/16649 )

What is your recommendation for syncing with the blockchain on a rented server? I am currently trying out parity, but that's had its own issues.

holiman commented 6 years ago

The time and storage of a light node is in a totally different order of magnitude. With les, you could be "synced" in a matter of minutes, and the storage is very small.

I would personally recommend doing a fast-sync on the rented server, however, after the fast-sync completes (which could take anything from a couple of hours to a day), it will import blocks one by one. This means that it will be quite resource intensive if you keep it running. So if you leave it running for two weeks without using it, it may be costly.

One naive alternative is to shut it down after you're done for the moment. However, if you start it up two weeks later, it will have a backlong of two weeks of blocks to slog through before it's at head again. Doing so may take more time than doing a new fast-sync. So, YMMV depending on how you intend to use the node, and what you need it for.

Ethereum, unlike bitcoin, does not require the historical context of an account in order to create a new transaction. Bitcoin needs the outputs to combine, ethereum just needs the correct nonce to use, which can be looked up in a block explorer. So if you just want to make a transcation, there's no need to actually keep a synced node.

quantumproducer commented 6 years ago

@holiman thanks for the information.

I tried running a light node, there were 0 peers. There is an issue open about this, apparently no one has incentive to run light nodes, but it's nice to know in theory it would be significantly faster.

cryptohazard commented 6 years ago

@karalabe thanks for the detailed answer. Can we save this somewhere(wiki?) for future reference?