unify connection states

This commit is contained in:
Evgeny Poberezkin
2020-05-07 17:19:17 +01:00
parent de706b9d23
commit f4c4dde30f
+107 -57
View File
@@ -2,11 +2,12 @@ module Simplex.Messaging
data Participant = Recipient | Sender | Broker data Participant = Recipient | Sender | Broker
Key : Type
Key = String
data Conn : Type where data Conn : Type where
MkConn : (id : String) -> (key : Key) -> Conn MkConn : (id : String) -> (key : Key) -> Conn
type Key = String
record ClientConn where record ClientConn where
constructor MkClientConn constructor MkClientConn
label : String label : String
@@ -14,76 +15,125 @@ record ClientConn where
conn : Conn conn : Conn
senderKey : Key -- public key for sender to encrypt messages senderKey : Key -- public key for sender to encrypt messages
record RecipientConnData where newClientConn : ClientConn
constructor MkRecipientConnData newClientConn = MkClientConn "" "" (MkConn "" "") ""
conn : ClientConn
record RCData where -- recipient connection data
constructor MkRCData
conn : ClientConn
privateBrokerKey : Key privateBrokerKey : Key
privateSenderKey : Key privateSenderKey : Key
record SenderConnData where newRCData : RCData
constructor MkSenderConnData newRCData = MkRCData newClientConn "" ""
record SCData where -- sender connection data
constructor MkSCData
conn : ClientConn conn : ClientConn
privateBrokerKey : Key privateBrokerKey : Key
data BrokerConnState = -- broker connection state newSCData : SCData
New -- connection created by the receiver but not secured yet newSCData = MkSCData newClientConn ""
| Secured -- connection is secured and active
| Disabled -- connection is disabled (by receiver)
| Drained -- connection is disabled and has no messages
| NoRecipient -- connection for given receiver ID does not exist
| NoSender -- connection for given sender ID does not exist
data BrokerConn : BrokerConnState -> Type where
BCNew : (recipient : Conn) -> (senderId : String) -> BrokerConn New data ConnectionState = -- connection states for all participants
-- three constructors below can probably be merged into one New -- (participants: all) connection created (or received from sender)
MkBrokerConn : (recipient : Conn) -> (sender : Conn) -> BrokerConn state | Pending -- (recipient) sent to sender out-of-band
-- with some restriction on the state | Confirmed -- (recipient) confirmed by sender with the broker
BCSecured : (recipient : Conn) -> (sender : Conn) -> BrokerConn Secured | Secured -- (all) secured with the broker
BCDisabled : (recipient : Conn) -> (sender : Conn) -> BrokerConn Disabled | Disabled -- (broker, recipient) disabled with the broker by recipient
BCDrained : (recipient : Conn) -> (sender : Conn) -> BrokerConn Drained | Drained -- (broker, recipient) drained (no messages)
| Null -- (all) not available or removed from the broker
-- broker connection states
data BrokerCS : ConnectionState -> Type where
BNew : BrokerCS New
BSecured : BrokerCS Secured
BDisabled : BrokerCS Disabled
BDrained : BrokerCS Drained
BNull : BrokerCS Null
-- sender connection states
data SenderCS : ConnectionState -> Type where
SNew : SenderCS New
SConfirmed : SenderCS Confirmed
SSecured : SenderCS Secured
SNull : SenderCS Null
-- established connection states (used by broker and recipient)
data EstablishedCS : ConnectionState -> Type where
ESecured : EstablishedCS Secured
EDisabled : EstablishedCS Disabled
EDrained : EstablishedCS Drained
data BrokerConn : (state : ConnectionState) -> {auto prf : BrokerCS state} -> Type where
BCNew : (recipient : Conn) -> (senderId : String) -> BrokerConn New
MkBrkConn : (state : ConnectionState)
-> (recipient : Conn)
-> (sender : Conn)
-> {auto prf : BrokerCS state}
-> {auto prf : EstablishedCS state}
-> BrokerConn state
-- 3 constructors below are equivalent to MkBrkConn with some state
BCSecured : (recipient : Conn) -> (sender : Conn) -> BrokerConn Secured
BCDisabled : (recipient : Conn) -> (sender : Conn) -> BrokerConn Disabled
BCDrained : (recipient : Conn) -> (sender : Conn) -> BrokerConn Drained
-- --
BCNoRecipient : (id : String) -> BrokerConn NoRecipient BCNull : (id : String) -> BrokerConn Null
BCNoSender : (senderId : String) -> BrokerConn NoSender
data RecipientConnState = -- recipient connection state -- good broker connection sample
RNew -- connection created with the broker goodBrkConn : BrokerConn Secured
| Pending -- pending: connection informaiton sent to sender out-of-band goodBrkConn = MkBrkConn Secured (MkConn "1" "1") (MkConn "2" "2")
| Confirmed -- sender confirmation received from the broker
| RSecured -- connection secured with the broker
| RDisabled -- connection is disabled with the broker
| RDrained -- connection is disabled with the broker and drained
| Deleted -- connection is deleted from the broker
data RecipientConn : (state : RecipientConnState) -> Type where -- bad broker connection sample - does not type check
RCRcvNew : (conn : RecipientConnData) -> (senderId : String) -> RecipientConn RNew -- badBrkConn : BrokerConn Null
RCPending : (conn : RecipientConnData) -> (senderId : String) -> RecipientConn Pending -- badBrkConn = BCEstablished Null (MkConn "1" "1") (MkConn "2" "2")
RCConfirmed : (conn : RecipientConnData) -> (sender : Conn) -> RecipientConn Confirmed
-- 4 constructors below can probably be merged into this one
MkRecipientConn : (conn : RecipientConnData) -> RecipientConn state data RecipientConn : (state : ConnectionState) -> Type where
-- with some restriction on the state RCNew : (conn : RCData) -> (senderId : String) -> RecipientConn New
RCSecured : (conn : RecipientConnData) -> RecipientConn RSecured RCPending : (conn : RCData) -> (senderId : String) -> RecipientConn Pending
RCDisabled : (conn : RecipientConnData) -> RecipientConn RDisabled RCConfirmed : (conn : RCData) -> (sender : Conn) -> RecipientConn Confirmed
RCDrained : (conn : RecipientConnData) -> RecipientConn RDrained MkRcpConn : (state : ConnectionState)
RCDeleted : (conn : RecipientConnData) -> RecipientConn Deleted -> (conn : RCData)
-> {auto prf : EstablishedCS state}
-> RecipientConn state
-- 3 constructors below are equivalent to MkRcpConn with some state
RCSecured : (conn : RCData) -> RecipientConn Secured
RCDisabled : (conn : RCData) -> RecipientConn Disabled
RCDrained : (conn : RCData) -> RecipientConn Drained
-- --
RCNull : (conn : RCData) -> RecipientConn Null
-- recipient connection sample
goodRcpConn : RecipientConn Secured
goodRcpConn = MkRcpConn Secured (record
{ conn = record
{ label = "label"
, broker = "broker"
, conn = MkConn "1" "1"
, senderKey = "2" } newClientConn
, privateBrokerKey = "3"
, privateSenderKey = "4" } newRCData)
data SenderConnState = -- sender connection state data SenderConn : (state : ConnectionState) -> {auto prf : SenderCS state} -> Type where
Received -- connection received from the recipient SCNew : (conn : ClientConn) -> SenderConn New
| Failed -- failed to send confirmation message to broker SCConfirmed : (conn : SCData) -> SenderConn Confirmed
| SConfirmed -- sent confirmation message to broker SCSecured : (conn : SCData) -> SenderConn Secured
| SSecured -- succeeded sending the message after sending confirmation message SCNull : (conn : SCData) -> SenderConn Null
| Unavailable -- connection is no longer available with the broker
data SenderConn : (state : SenderConnState) -> Type where -- sender connection sample
SCReceived : (conn : ClientConn) -> SenderConn Received goodSndConn : SenderConn Secured
SCFailed : (conn : SenderConnData) -> SenderConn Failed goodSndConn = SCSecured (record
SCConfirmed : (conn : SenderConnData) -> SenderConn SConfirmed { conn = record
SCSecured : (conn : SenderConnData) -> SenderConn SSecured { label = "label"
SCUnavailable : (conn : SenderConnData) -> SenderConn Unavailable , broker = "broker"
, conn = MkConn "1" "1"
-- RDisabled , senderKey = "2" } newClientConn
, privateBrokerKey = "3" } newSCData)