Skip to content

libs/db: prefix Database Key Collision #6

Closed

Description

@liamsi

We have to make sure, that keys will not collide when different prefixes are used. When setting or getting a value from the db, the keys are simply appended:
https://github.com/tendermint/tendermint/blob/40d6dc2ee55a34a075e7b7e73c3525c84ae705ea/libs/db/prefix_db.go#L228-L230

This leads to the following behaviour (a value of the namespace "prefix-key" can be accessed from 2 different prefix dbs):

package main
import tdb "github.com/tendermint/tendermint/libs/db"
import "fmt"
func bz(v string) []byte {
	return []byte(v)
}
func main() {
	db := tdb.NewMemDB()
	db.Set(bz("prefix-key"), bz("100"))

	pdb1 := tdb.NewPrefixDB(db, bz("prefix-"))
	fmt.Printf("%s\n",pdb1.Get(bz("key")))

	pdb2 := tdb.NewPrefixDB(db, bz("pre"))
	fmt.Printf("%s\n",pdb2.Get(bz("fix-key")))
}

Activity

  1. melekes commented on Sep 18, 2019

    @melekes
    Contributor

    I think this is super important.

  2. added
    T:enhancement
    on Feb 25, 2020
  3. erikgrinaker commented on May 18, 2020

    @erikgrinaker
    Contributor

    I think this is sort of working as intended, given that the backend is named PrefixDB - it is the user's responsibility to use non-overlapping prefixes if necessary, which is simple enough by appending a reserved separator to the prefix, e.g. 0x00.

    We could add a separator parameter to the constructor (to maintain backwards compatibility), but it is kind of redundant - the following examples do exactly the same:

    // With separator parameter
    NewPrefixDB(db, []byte("prefix"), []byte("/"))
    
    // Without separator parameter
    NewPrefixDB(db, []byte("prefix/"))

    It would also require the user to use the same separator consistently across PrefixDB instances, otherwise they would have the same problem.

  4. added a commit that references this issue on May 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    StaleT:enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions