Skip to content

libs/db: prefix Database Key Collision #6

@liamsi

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")))
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    StaleT:enhancementType: Enhancement, New feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions