Skip to content

Trie Proofs Not Working as Expected #2240

@notbdu

Description

@notbdu

Describe the bug

Inserting two unique keys with a mismatching final character results in non functioning proofs.

Expected Behavior

Expect the following to run successfully:

package main

import (
	"io/ioutil"
	"log"
	"os"

	"github.com/ChainSafe/chaindb"
	"github.com/ChainSafe/gossamer/lib/trie"
)

var (
	value = []byte("somevalue")
	key1  = []byte("worlda")
	key2  = []byte("worldb")
)

func main() {
	tmp, err := ioutil.TempDir("", "trietest")
	if err != nil {
		log.Fatal(err)
	}
	defer func() {
		if err := os.RemoveAll(tmp); err != nil {
			log.Fatal(err)
		}
	}()
	memdb, err := chaindb.NewBadgerDB(&chaindb.Config{
		InMemory: true,
		DataDir:  tmp,
	})
	if err != nil {
		log.Fatal(err)
	}

	t := trie.NewEmptyTrie()
	t.Put(key1, value)
	t.Put(key2, value)

	err = t.Store(memdb)
	if err != nil {
		log.Fatal(err)
	}

	hash, err := t.Hash()
	if err != nil {
		log.Fatal(err)
	}

	proof, err := trie.GenerateProof(hash.ToBytes(), [][]byte{key1, key2}, memdb)
	if err != nil {
		log.Fatal(err)
	}

	pairs := []trie.Pair{
		{Key: key1, Value: value},
		{Key: key2, Value: value},
	}
	v, err := trie.VerifyProof(proof, hash.ToBytes(), pairs)
	if err != nil {
		log.Fatal(err)
	}
	log.Println("verified proof:", v)
}

Current Behavior

Instead, we are seeing the following error on trie.VerifyProof():
expected key not found in the trie

Possible Solution

To Reproduce

Steps to reproduce the behaviour:

  1. Run the provided example.
  2. Confirm the results.

Log output

2022/01/26 21:09:47 expected key not found in the trie       
exit status 1                                                

Specification

  • go version: 1.16.4
  • gossamer version: development
  • gossamer commit tag: NA
  • gossamer commit hash: 0f78be36386db424669267c40b5146bf5962d651
  • operating system: 5.13.0-7620-generic #20~1634827117~21.04~874b071-Ubuntu SMP
  • additional links: NA

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions