orbs-network / orbs-spec

Orbs platform protocol and service specifications, network architecture, etc
MIT License
30 stars 6 forks source link

Merkle tree calculation in VM #48

Open ronnno opened 6 years ago

ronnno commented 6 years ago

Is it worthwhile to let the VM output a Merkle tree diff (including the merkle root) as well as state diff in the process of preparing a new block?

pros:

cons:

Optimization:

OdedWx commented 5 years ago

Proposal:

  1. Rename the interface to: {contract_name, hashkeys} a. hashkeys instead of keys
  2. hashkey = hash(contract_name.key), for example hash(token.transfer) a. calculated by the VM, used directly by the state storage (without additional hashing)
  3. Even though the address is unique across contracts, the state is managed per contract (allowing to delete / copy on upgrade)
  4. Hash (address length): SHA256 (32B) Discussion: TODO a. Length extension attack - 2 hash functions. RIPEMD160 is often used after SHA256 to prevent length extension attack: derive H(m||x) from H(m) without knowing x. This should not a problem for the state storage where the data is public. b. Consider LSB20B(SHA256) to reduce the size to 20B
  5. Future feature - add a merkle hash table variable type, allowing to store smart contract data under a separate merkle tree.
ronnno commented 5 years ago

Agree. To recap, you propose:

  1. state will retain ability to iterate all contract values, drop, extract and import contract state in isolation from other contracts.
  2. contract name will be included in the pre-image of the key hash to prevent collisions. this will be done by the VM and transparent to the state-storage module
  3. No double hashing needed

Am I correct? Also, about dropping contract state, however, i think if we want to drop a contract as part of a transaction we would have to iterate all contract keys and clear the merkle tree one by one. there is no way to quickly drop a set of contract keys from the merkle tree unless we build contract merkle trees in hierarchy. Can I assume we don't need to isolate contract merkle trees for now?