improve connection data structures

This commit is contained in:
Evgeny Poberezkin 2020-05-07 17:58:42 +01:00
parent f4c4dde30f
commit e7550f026c

View file

@ -2,39 +2,61 @@ module Simplex.Messaging
data Participant = Recipient | Sender | Broker data Participant = Recipient | Sender | Broker
data Client : Participant -> Type where
CRecipient : Client Recipient
CSender : Client Sender
Key : Type Key : Type
Key = String Key = String
data Conn : Type where PrivateKey : Type
MkConn : (id : String) -> (key : Key) -> Conn PrivateKey = String
-- Data structures for participants to store and pass connection information
data Conn : Type where -- connection info shared between a client and broker
MkConn : (id : String) -- connection ID to identify it with the broker
-> (key : Key) -- public key for broker to verify commands
-> Conn
record ClientConn where record ClientConn where
constructor MkClientConn constructor MkClientConn
label : String conn : Conn -- same info that broker has for this client
broker : String label : String -- label for the client to identify connection
conn : Conn broker : String -- broker URI
senderKey : Key -- public key for sender to encrypt messages brokerPrivateKey : PrivateKey -- private key to sign commands to broker
newClientConn : ClientConn newClientConn : ClientConn
newClientConn = MkClientConn "" "" (MkConn "" "") "" newClientConn = MkClientConn (MkConn "" "") "" "" ""
record RCData where -- recipient connection data record RcpConn where -- recipient connection data
constructor MkRCData constructor MkRcpConn
conn : ClientConn clientConn : ClientConn
privateBrokerKey : Key senderPrivateKey : PrivateKey -- private key to decrypt sender messages
privateSenderKey : Key
newRCData : RCData newRcpConn : RcpConn
newRCData = MkRCData newClientConn "" "" newRcpConn = MkRcpConn newClientConn ""
record SCData where -- sender connection data record Invitation where -- out of band message to sender inviting to connect
constructor MkSCData constructor MkInvitation
conn : ClientConn conn : Conn
privateBrokerKey : Key broker : String
senderKey : Key -- public key for sender to encrypt messages
newSCData : SCData newInvitation : Invitation
newSCData = MkSCData newClientConn "" newInvitation = MkInvitation (MkConn "" "") "" ""
record SndConn where -- sender connection data
constructor MkSndConn
clientConn : ClientConn
senderKey : Key -- public key for sender to encrypt messages
newSndConn : SndConn
newSndConn = MkSndConn newClientConn ""
-- connection states for all participants
data ConnectionState = -- connection states for all participants data ConnectionState = -- connection states for all participants
New -- (participants: all) connection created (or received from sender) New -- (participants: all) connection created (or received from sender)
@ -67,6 +89,8 @@ data EstablishedCS : ConnectionState -> Type where
EDrained : EstablishedCS Drained EDrained : EstablishedCS Drained
-- state-dependent types to represent connections for all participants
data BrokerConn : (state : ConnectionState) -> {auto prf : BrokerCS state} -> Type where data BrokerConn : (state : ConnectionState) -> {auto prf : BrokerCS state} -> Type where
BCNew : (recipient : Conn) -> (senderId : String) -> BrokerConn New BCNew : (recipient : Conn) -> (senderId : String) -> BrokerConn New
MkBrkConn : (state : ConnectionState) MkBrkConn : (state : ConnectionState)
@ -93,47 +117,50 @@ goodBrkConn = MkBrkConn Secured (MkConn "1" "1") (MkConn "2" "2")
data RecipientConn : (state : ConnectionState) -> Type where data RecipientConn : (state : ConnectionState) -> Type where
RCNew : (conn : RCData) -> (senderId : String) -> RecipientConn New RCNew : (conn : RcpConn) -> (sender : Invitation) -> RecipientConn New
RCPending : (conn : RCData) -> (senderId : String) -> RecipientConn Pending RCPending : (conn : RcpConn) -> (sender : Invitation) -> RecipientConn Pending
RCConfirmed : (conn : RCData) -> (sender : Conn) -> RecipientConn Confirmed RCConfirmed : (conn : RcpConn) -> (sender : Conn) -> RecipientConn Confirmed
MkRcpConn : (state : ConnectionState) MkRecipientConn : (state : ConnectionState)
-> (conn : RCData) -> (conn : RcpConn)
-> {auto prf : EstablishedCS state} -> {auto prf : EstablishedCS state}
-> RecipientConn state -> RecipientConn state
-- 3 constructors below are equivalent to MkRcpConn with some state -- 3 constructors below are equivalent to MkRcpConn with some state
RCSecured : (conn : RCData) -> RecipientConn Secured RCSecured : (conn : RcpConn) -> RecipientConn Secured
RCDisabled : (conn : RCData) -> RecipientConn Disabled RCDisabled : (conn : RcpConn) -> RecipientConn Disabled
RCDrained : (conn : RCData) -> RecipientConn Drained RCDrained : (conn : RcpConn) -> RecipientConn Drained
-- --
RCNull : (conn : RCData) -> RecipientConn Null RCNull : (conn : RcpConn) -> RecipientConn Null
-- recipient connection sample -- recipient connection sample
goodRcpConn : RecipientConn Secured goodRcpConn : RecipientConn Secured
goodRcpConn = MkRcpConn Secured (record goodRcpConn = MkRecipientConn Secured (record
{ conn = record { clientConn = record
{ label = "label" { conn = MkConn "1" "1"
, broker = "broker" , label = "label"
, conn = MkConn "1" "1" , broker = "broker"
, senderKey = "2" } newClientConn , brokerPrivateKey = "2" }
, privateBrokerKey = "3" newClientConn
, privateSenderKey = "4" } newRCData) , senderPrivateKey = "3" }
newRcpConn)
data SenderConn : (state : ConnectionState) -> {auto prf : SenderCS state} -> Type where data SenderConn : (state : ConnectionState) -> {auto prf : SenderCS state} -> Type where
SCNew : (conn : ClientConn) -> SenderConn New SCNew : (conn : Invitation) -> SenderConn New
SCConfirmed : (conn : SCData) -> SenderConn Confirmed SCConfirmed : (conn : SndConn) -> SenderConn Confirmed
SCSecured : (conn : SCData) -> SenderConn Secured SCSecured : (conn : SndConn) -> SenderConn Secured
SCNull : (conn : SCData) -> SenderConn Null SCNull : (conn : SndConn) -> SenderConn Null
-- sender connection sample -- sender connection sample
goodSndConn : SenderConn Secured goodSndConn : SenderConn Secured
goodSndConn = SCSecured (record goodSndConn = SCSecured (record
{ conn = record { clientConn = record
{ label = "label" { conn = MkConn "1" "1"
, broker = "broker" , label = "label"
, conn = MkConn "1" "1" , broker = "broker"
, senderKey = "2" } newClientConn , brokerPrivateKey = "2" }
, privateBrokerKey = "3" } newSCData) newClientConn
, senderKey = "3" }
newSndConn)