项目作者: begmaroman

项目描述 :
Simple Implementation of Modified Merkle Patricia Tries
高级语言: Go
项目地址: git://github.com/begmaroman/mpt.git
创建时间: 2018-10-16T13:28:07Z
项目社区:https://github.com/begmaroman/mpt

开源协议:

下载


MPT - Modified Merkle Patricia Tries

Simple implementation of Modified Merkle Patricia Tries on GoLang.

Modified Merkle Patricia Trie

Usage

  1. Create the Trie:

    • With empty root node:

      1. trie := mpt.NewEmptyTrie()
    • With branch root node:

      1. nd := node.NewBranchNode()
      2. trie := mpt.NewTrie(nd)
    • With extension root node:

      1. nd := node.NewExtensionNode(nil, nil)
      2. trie := mpt.NewTrie(nd)
  2. Add key/value pair:

  1. key := []byte("key")
  2. val := []byte("val")
  3. // create new trie and try add key/value pair
  4. trie := mpt.NewEmptyTrie()
  5. if ok := trie.Put(key, val); !ok {
  6. // key/pair not added
  7. }
  1. Get data by key:
  1. key := []byte("key")
  2. val := []byte("val")
  3. // create new trie and try add key/value pair
  4. trie := mpt.NewEmptyTrie()
  5. if ok := trie.Put(key, val); !ok {
  6. // key/pair not added
  7. }
  8. // some logic...
  9. // try to get data by key
  10. getVal, ok := trie.Get(key)
  11. if !ok {
  12. // key/pair not added
  13. }
  1. Update data by key:
  1. // initialize data
  2. key := []byte("key")
  3. val := []byte("val")
  4. // create new trie and try add key/value pair
  5. trie := mpt.NewEmptyTrie()
  6. if ok := trie.Put(key, val); !ok {
  7. // key/pair not added
  8. }
  9. // some logic...
  10. // try to update value by key
  11. newVal := []byte("updated value")
  12. if ok := trie.Update(key, newVal); !ok {
  13. // data not updated
  14. }
  1. Delete data by key:
  1. // initialize data
  2. key := []byte("key")
  3. val := []byte("val")
  4. // create new trie and try add key/value pair
  5. trie := mpt.NewEmptyTrie()
  6. if ok := trie.Put(key, val); !ok {
  7. // key/pair not added
  8. }
  9. // some logic...
  10. // try to delete data by key
  11. if ok := trie.Delete(key, newVal); !ok {
  12. // data not deleted
  13. }
  1. Get hash of the trie:
  1. // initialize data
  2. key := []byte("key")
  3. val := []byte("val")
  4. // create new trie and try add key/value pair
  5. trie := mpt.NewEmptyTrie()
  6. if ok := trie.Put(key, val); !ok {
  7. // key/pair not added
  8. }
  9. // some logic...
  10. // try to get hash of the trie
  11. h, err := trie.Hash()
  12. if err != nil {
  13. // error while hash calculation
  14. }
  15. fmt.Println("hash of the trie:", h)

Benchmarks

Name Iterations Time Description
BenchmarkTrie_Get/FromFullTrie100000-4 1000000 3463 ns/op Get data from the tree which have 100000 key/value pairs
BenchmarkTrie_Get/FromEmptyTrie-4 1000000 1262 ns/op Get data from the empty tree
BenchmarkTrie_Put/ToFullTrie100000-4 3000000 513 ns/op Put data to the tree which have 100000 key/value pairs
BenchmarkTrie_Put/ToEmptyTrie-4 3000000 524 ns/op Put data to empty tree
BenchmarkTrie_Update/FullTrie100000-4 1000000 3083 ns/op Update data in the tree which have 100000 key/value pairs
BenchmarkTrie_Update/EmptyTrie-4 2000000 674 ns/op Update data in empty tree

Backlog

  • Implement Database layer - need for store tree