3 ms·
So as someone who is working in this space (on distributed applications using Ethereum), I want to call everyone's attention to a very important API that Cloudf
by saurik 7y ago
So as someone who is working in this space (on distributed applications using Ethereum), I want to call everyone's attention to a very important API that Cloudflare thankfully did implement: eth_getProof. As far as I am concerned, this is the most important API offered (and if anything, it is mostly sad no one has built out a few more APIs designed to make getting to the point where this is useful).
For those who don't know much about the inner workings of Ethereum, every account has state associated with it: not just its balance, but in the case of a contract its memory and its code; the memory of a contract is pretty much a sparse 256-bit addressable hash table, with everything from a single contract hashed into the same address space. This makes directly working with the memory of a contract really trivial; and, if/once you know of a specific storage slot you want, you can get it using eth_getStorageAt.
Now, what most people know is that each block contains a hash of the previous block, and "the contents of the block"; but, the setup is actually way more useful than that: each block "contains" (by hash reference) the entire current state. So, given a block header you trust, you can work off of a merkle tree of the entire Ethereum state that is rooted at that block and obtain a proof that some specific account state value is valid.
This means that light clients can opt to sync only block headers and then request proofs of the specific tiny bits of information they need from other nodes. Instead of using eth_getStorageAt to pull a storage slot from the memory of a contract, or eth_getBalance to find out how much money the account is storing, you can use eth_getProof to get a merkle path demonstration that "given the block headers you trust, you can now trust this random value you wanted to pull".
A really light client might alternatively sync some subset of block headers to some total amount of difficulty into the past (similar to people who wait for "confirmations" on Bitcoin blocks), to say "someone who is trying to fool me about this event having happened would have had to spend a million dollars of electricity to target me with fake information here, which wouldn't be worth it given the relatively small amount of my transaction I am verifying".
Another way of doing this is to correlate the current block hash from multiple providers to make sure that it is the same, and then use that opaque agreement as the root of trust for calls to eth_getProof. (An important detail: doing it like this, as opposed to correlating the thing you wanted to check, happens before any of the providers know what you are looking for, so if they are colluding they wouldn't be able to target you and the information you want to read with corrupt data.)
Essentially, since Cloudflare has implemented this specific key API (which is notable as it is a newer one that has greater resource usage on their end than other APIs, and so I have seen providers decide not to bother as they just don't get it), you don't actually have to trust them all that much when you are using this API: it feels more like working with IPFS, where "well, I asked for the file with this hash and they gave me a file that in fact does have this hash... I guess I would be shocked if it weren't the right file".