left-icon

The Kademlia Protocol Succinctly®
by Marc Clifton

Previous
Chapter

of
A
A
A

CHAPTER 6

Value Lookup

Value Lookup


From the spec: “FIND_VALUE behaves like FIND_NODE - returning (IP address, UDP port, Node ID) triples - with one exception. If the RPC recipient has received a STORE RPC for the key, it just returns the stored value.

That seems clear enough, but we must consider this part of the spec as well:

To find a (key,value) pair, a node starts by performing a lookup to find the k nodes with IDs closest to the key. However, value lookups use FIND_VALUE rather than FIND_NODE RPCS. Moreover, the procedure halts immediately when any node returns the value. For caching purposes, once a lookup succeeds, the requesting node stores the (key,value) pair at the closest node it observed to the key that did not return the value.

However, the spec says this: “Most operations are implemented in terms of the above lookup procedure.” When we’re performing a lookup, the initiator will be using the lookup call for both finding nodes and finding values. If it’s finding values, it needs to stop if/when the value is found.

This statement from the spec can be ambiguous: “For caching purposes, once a lookup succeeds, the requesting node stores the (key,value) pair at the closest node it observed to the key that did not return the value.” What does “requesting node” mean? Is it the node performing the lookup, or the node that made the GetValue request? It would seem to be the former, because “it observed to the key that did not return the value” would otherwise not make any sense.

Going back to the code:

Func<ID, Contact, (List<Contact> contacts, Contact foundBy, string val)> rpcCall

We can see now the reason for passing in the RPC, as we want to use the exact same lookup algorithm for FindNode as we do for FindValue.

Discussing value lookup tests doesn’t make sense outside of the context of the Dht wrapper, so the unit tests for the value lookup will be done in the Dht testing.

Implementation

First, we need to implement a simple virtual (in memory) storage mechanism. We don’t show the implementation of the full interface, as that isn’t required yet.

Code Listing 51: Basic Virtual Storage

public class VirtualStorage : IStorage

{

  protected ConcurrentDictionary<BigInteger, StoreValue> store;

  public VirtualStorage()

  {

    store = new ConcurrentDictionary<BigInteger, StoreValue>();

  }

  public bool Contains(ID key)

  {

    return store.ContainsKey(key.Value);

  }

  public string Get(ID key)

  {

    return store[key.Value].Value;

  }

  public string Get(BigInteger key)

  {

    return store[key].Value;

  }

}

We can also now implement Store and FindValue in the Node class.

Code Listing 52: Node Store

public void Store(
  Contact sender,
  ID key,
  string val,
  bool isCached = false,
  int expirationTimeSec = 0)

{

  Validate.IsFalse<SendingQueryToSelfException>(sender.ID == ourContact.ID,
    "Sender should not be ourself!");

  bucketList.AddContact(sender);

  if (isCached)

  {

    cacheStorage.Set(key, val, expirationTimeSec);

  }

  else

  {

    SendKeyValuesIfNewContact(sender);

    storage.Set(key, val, Constants.EXPIRATION_TIME_SECONDS);

  }

}

Code Listing 53: Node FindValue

public (List<Contact> contacts, string val) FindValue(Contact sender, ID key)

{

  Validate.IsFalse<SendingQueryToSelfException>(sender.ID == ourContact.ID,
    "Sender should not be ourself!");

  SendKeyValuesIfNewContact(sender);

  bucketList.AddContact(sender);

  if (storage.Contains(key))

  {

    return (null, storage.Get(key));

  }

  else if (CacheStorage.Contains(key))

  {

    return (null, CacheStorage.Get(key));

  }

  else

  {

    // Exclude sender.

    return (bucketList.GetCloseContacts(key, sender.ID), null);

  }

}

Scroll To Top
Disclaimer

DISCLAIMER: Web reader is currently in beta. Please report any issues through our support system. PDF and Kindle format files are also available for download.

Previous

Next



You are one step away from downloading ebooks from the Succinctly® series premier collection!
A confirmation has been sent to your email address. Please check and confirm your email subscription to complete the download.