A pithy primer of Polykey
#777
Replies: 6 comments 15 replies
-
This might function better as a discussion. Maybe I'll convert it. |
Beta Was this translation helpful? Give feedback.
-
Comments please @aryanjassal @brynblack. |
Beta Was this translation helpful? Give feedback.
-
I'm unable to understand how the |
Beta Was this translation helpful? Give feedback.
-
I've taken a look at the Kademlia paper and even asked ChatGPT to explain it to me, but I am struggling to grasp how the distance metric works in relation to a binary tree. In this image, the physical distance from Similarly, |
Beta Was this translation helpful? Give feedback.
-
The discovery domain requires a bit of improvement. It's supposed to act as a decentralized crawler. Lots of optimisation required here... as well as wasm based plugins to deal with various third party platforms. |
Beta Was this translation helpful? Give feedback.
-
Polykey
is a kind of a complex program. It is made up groups of functionality we calldomains
These are a collection of classes, utilities and types, some are made up of a single class.For example, we have a domain called
keys
. This handles the base level password, keys and certificate information that the rest ofPolykey
makes use of. It contains 2 main classes, theCertManager
and theKeyRing
. What they each do is self explanatory and I'll leave it to you to dig deeper.Keep in mind that for the most part each of these classes are decorated with an
async-init
giving the class a life-cycle. So theKeyRing
is aCreateDestroyStartStop
structure. Which just means that it has implemented aCreateKeyRing
static factory function for creating the class. And contains astart
,stop
anddestroy
method. Any method insideKeyRing
that is decorated with@ready
can only be called when theKeyRing
is in the running state. Callingstop
will cause it to stop and active state and callingdestroy
will clear any persistent state. That's all you really need to know for now. You will see this pattern everywhere inPolykey
and even other projects.Polykey
can be subdivided into a few aspects.keys
,claims
andsigChain
domainsnodes
and discovery domains.gestalts
,sigchain
andACL
domains.vaults
domain.client
domain.There are some others but those are the main ones. If you look into
Polykey/src/PolykeyAgent.ts
Line 244 in 2a88416
CreatePolykeyAgent
method inside the maintry {} catch
block.Arguably the most important domain for
Polykey
as a product is thevaults
domain. It's where all of the secret handling is and maybe one of the easier domains to read through. For that you'll want to start at theVaultManager
https://github.com/MatrixAI/Polykey/blob/staging/src/vaults/VaultManager.ts. Get a feel for that and the dig down intoVaultInternal
and how that handles the data. Keep an eye out for thewithF
pattern. You'll see that all over the place.After that the
nodes
domain is the next important. It handles all of the logic for managing connections in theNodeConnectionManager
. Notably tracking data about other nodes in theNodeGraph
and handling all of the logic for finding nodes you want to connect to and establishing that connection in theNodeManager
. All of the connection related stuff is pretty complex so just skim over this. Dig into how theNodeConnectionManager
https://github.com/MatrixAI/Polykey/blob/staging/src/nodes/NodeConnectionManager.ts handles creating connections and how theNodeConnection
https://github.com/MatrixAI/Polykey/blob/staging/src/nodes/NodeConnection.ts wraps them. It uses an object map and locking to ensure that we don't create duplicateNodeConnection
s. Skim over whatNodeManager
handles, especially the logic for finding nodes andNodeConnectionQueue
, its pretty complex. Besides that, some good related reading is thekademlia
spec https://pdos.csail.mit.edu/~petar/papers/maymounkov-kademlia-lncs.pdf. Get an idea of howcloseness
works as a concept in Kademlia.Polykey
uses a lot of persistent state, this will be stored in theDB
as encrypted data. So to get a good feel of what domains need data persistence just trace what depends on theDB
. Along side this thekeys
domain maintains most of the information used for they cryptography functions. It stores the private and public keys. But also manages encryption keys for the DB and vaults domain.Polykey
needs to track and maintain relationships and permissions between nodes. There are a few parts to this. One of the main ones areclaims
. These in essences are a claim that are signed by one or more nodes. usually to state that two nodes own each other in a way that forms a gestalt. Other claims will be made and they can come later. These are stored in theSigChain
which functions similar to a block chain where immutability of the chain is enforced by a claim including a hash of it's parent within it. So the history can't be modified without breaking the chain.The
ACL
tracks permissions we give to other nodes. The main example of this is the permission to see and clone vaults from our node. It is a simple access control list that maps a permission to aNodeID
The Gestalts domain manages keeping track of claims between nodes within a
gestalt
. AGestalt
is a graph formed of all nodes that hold claims between each other. So a collection of nodes can be considered a group of a whole. given how the structure works, you can't really reference a gestalt as a whole, you can only really reference it by a member or check if two nodes are part of a gestalt. TheGestaltGraph
tracks our own gestalt but also other nodes gestalts. For the most part we only care about our own, or gestalts we have a first order relationship with via social or permission links.Following on from gestalts we have the
discovery
domain. This handles the logic for exploring and mapping these claims and links between nodes. It fills in theGestaltGraph
with this information and makes sure it's maintained.Have a read over this @aryanjassal @brynblack if you have any questions just ask them in the comments here. They'll be a great reference for later.
Beta Was this translation helpful? Give feedback.
All reactions