Skip to content

Commit

Permalink
Error appropriately when putting invalid typed value in DHT (#48)
Browse files Browse the repository at this point in the history
* Fixes #46

* Remove some ambiguity in the way IStorage works in comment

* Make documentation addition more clear

* Remove unneeded line

* Remove documentation example
  • Loading branch information
bretttjohnson1 authored and bmuller committed Apr 18, 2018
1 parent ecde2c9 commit 3035876
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .gitignore
Expand Up @@ -6,4 +6,4 @@ apidoc
build
dist
kademlia.egg-info
docs/_build
docs/_build
21 changes: 21 additions & 0 deletions kademlia/network.py
Expand Up @@ -157,6 +157,10 @@ async def set(self, key, value):
"""
Set the given string key to the given value in the network.
"""
if not check_dht_value_type(value):
raise TypeError(
"Value must be of type int, float, bool, str, or bytes"
)
log.info("setting '%s' = '%s' on network", key, value)
dkey = digest(key)
return await self.set_digest(dkey, value)
Expand Down Expand Up @@ -235,3 +239,20 @@ def saveStateRegularly(self, fname, frequency=600):
self.saveStateRegularly,
fname,
frequency)


def check_dht_value_type(value):
"""
Checks to see if the type of the value is a valid type for
placing in the dht.
"""
typeset = set(
[
int,
float,
bool,
str,
bytes,
]
)
return type(value) in typeset
1 change: 1 addition & 0 deletions kademlia/storage.py
Expand Up @@ -7,6 +7,7 @@
class IStorage:
"""
Local storage for this node.
IStorage implementations of get must return the same type as put in by set
"""

def __setitem__(self, key, value):
Expand Down

0 comments on commit 3035876

Please sign in to comment.