pchain-org / pchain

Pchain client-in-Go
https://plian.org
GNU General Public License v3.0
24 stars 12 forks source link

optimize the "save the whole block from child chain to main chain" process #17

Closed simplyzhao closed 5 years ago

simplyzhao commented 6 years ago

Instead of saving the whole block, try optimize it with Merkle proof.

simplyzhao commented 6 years ago

For Ethereum, the TxHash in Block Header is calculated as:

b.header.TxHash = DeriveSha(Transactions(txs))
func DeriveSha(list DerivableList) common.Hash {
    keybuf := new(bytes.Buffer)
    trie := new(trie.Trie)
    for i := 0; i < list.Len(); i++ {
        keybuf.Reset()
        rlp.Encode(keybuf, uint(i))
        trie.Update(keybuf.Bytes(), list.GetRlp(i))
    }
    return trie.Hash()
}

Here the key in MPT is the index, and the value is the rlp encoding of the tx.

Prove/VerifyProof(go-ethereum/trie/proof.go) indicates that the Merkle Proof is only for key.

simplyzhao commented 6 years ago

For tendermint, the DataHash(TxHash) is calculated as:

func (txs Txs) Hash() []byte {
    // Recursive impl.
    // Copied from go-merkle to avoid allocations
    switch len(txs) {
    case 0:
        return nil
    case 1:
        return txs[0].Hash()
    default:
        left := Txs(txs[:(len(txs)+1)/2]).Hash()
        right := Txs(txs[(len(txs)+1)/2:]).Hash()
        return merkle.SimpleHashFromTwoHashes(left, right)
    }
}

Here it's seems like a normal Merkle tree. (NOTE: this is the hash of the go-wire encoded Tx)