probe-lab / zikade

A Go implementation of the libp2p Kademlia DHT specification
Other
12 stars 3 forks source link

Datastore Key conversion mixes namespaces prefix #25

Open iand opened 1 year ago

iand commented 1 year ago

(Migrated from https://github.com/libp2p/go-libp2p-kad-dht/issues/869)

When we receive a PUT_VALUE RPC from a remote peer, the key will contain a namespace prefix (e.g., pk or ipns) followed by a binary key. In the case of IPNS the key follows the spec:

Key format: /ipns/BINARY_ID

This is in line with how the IPNS key gets generated here:

func (n Name) RoutingKey() []byte {
    var buffer bytes.Buffer
    buffer.WriteString(NamespacePrefix)
    buffer.Write(n.src) // Note: we append raw multihash bytes (no multibase)
    return buffer.Bytes()
}

NamespacePrefix is set to /ipns/. In the handlePutValue method the key parameter will be set to the above /ipns/BINARY_ID format. Later in that handler we're generating the datastore key via convertToDsKey(rec.GetKey()):

func convertToDsKey(s []byte) ds.Key {
    return ds.NewKey(base32.RawStdEncoding.EncodeToString(s))
}

This means we're taking the bytes of the string /ipns/BINARY_ID and encode them as base32. This means we're losing the capability of proper namespacing. The /ipns prefix will still be the same in base32 for all keys but still this seems like a coincidence. In the ProviderManager, we're properly encoding each component separately here.

Changing this key format will be incompatible with any persistent stores out there. We'd need to support both types of keys (everything base32 encoded and only BINARY_ID encoded) for some time.

With go-libp2p-kad-dht v2 we could take the liberty to introduce a breaking change here.