ethereum / ethereum-org-website

Ethereum.org is a primary online resource for the Ethereum community.
https://ethereum.org/
MIT License
5.08k stars 4.83k forks source link

Improvements to the code of Merkle Patricia Trie doc #12720

Closed croath closed 7 months ago

croath commented 7 months ago

The original code sample from https://github.com/ethereum/ethereum-org-website/blob/e7041d3db20c028f2bc93037eb13edb7a820396d/public/content/developers/docs/data-structures-and-encoding/patricia-merkle-trie/index.md?plain=1#L37-L48 is:

    def update(node,path,value):
        if path == '':
            curnode = db.get(node) if node else [ NULL ] * 17
            newnode = curnode.copy()
            newnode[-1] = value
        else:
            curnode = db.get(node) if node else [ NULL ] * 17
            newnode = curnode.copy()
            newindex = update(curnode[path[0]],path[1:],value)
            newnode[path[0]] = newindex
        db.put(hash(newnode),newnode)
        return hash(newnode)

Shall we streamline the code a little bit with reducing the repeating part, like:

    def update(node,path,value):
        curnode = db.get(node) if node else [ NULL ] * 17
        newnode = curnode.copy()
        if path == '':
            newnode[-1] = value
        else:
            newindex = update(curnode[path[0]],path[1:],value)
            newnode[path[0]] = newindex
        db.put(hash(newnode),newnode)
        return hash(newnode)
minimalsm commented 7 months ago

Closed by #12721