Commit 8e7b3702 authored by Cédric F.'s avatar Cédric F.

Get best block upon reconnection

parent 210f3459
Pipeline #14998701 passed with stage
in 57 seconds
......@@ -12,7 +12,7 @@
<meta name="application-name" content="Trappisto">
<!-- <meta property="og:url" content="https://localhost"> -->
<meta property="og:title" content="Trappisto">
<meta property="og:title" content="Trappisto - Interactive Block Explorer">
<meta property="og:description" content="Trappisto - Interactive Block Explorer">
<meta property="og:image" content="assets/images/apple-touch-icon.png">
......
module Lib.WebSocket exposing (listen, send, isSuccess, isMethod)
module Lib.WebSocket exposing (listen, send, isSuccess, isMethod, getSessionId)
{-
This allows to send web socket messages to the `/ws` endpoint and parse notification messages.
......@@ -62,3 +62,12 @@ isMethod methods jsonString =
Ok maybeMethod ->
List.any (\method -> Just method == maybeMethod) methods
getSessionId : String -> Maybe Int
getSessionId jsonString =
let
decoder =
Decode.at [ "result", "sessionid" ] <| Decode.int
in
Decode.decodeString decoder jsonString |> Result.toMaybe
......@@ -38,7 +38,8 @@ type alias Model =
, now : Time
, fetching : Bool
, webSocketConnected : Bool
, lastWebSocketPong : Time
, webSocketSessionId : Maybe Int
, webSocketLastPong : Time
, lastBlockHash : String
, lastBlockHeight : Int
, lastTransactions : List BasicTransaction
......@@ -58,10 +59,11 @@ initialModel config query =
, error = Nothing
, vimMode = False
, debug = False
, now = -1
, fetching = False
, webSocketConnected = False
, now = -1
, lastWebSocketPong = -1
, webSocketSessionId = Nothing
, webSocketLastPong = -1
, lastBlockHash = ""
, lastBlockHeight = -1
, lastTransactions = []
......
......@@ -41,20 +41,14 @@ init flags location =
( model, Cmd.none )
else
update (Query query) model
notifyBlocks =
WebSocket.send model.config.wsEndpoint "notifyblocks" []
notifyNewTransactions =
WebSocket.send model.config.wsEndpoint "notifynewtransactions" []
in
( updatedModel
, Cmd.batch
[ elmToJs [ "focus" ]
, msg
, getBestBlock updatedModel
, notifyBlocks
, notifyNewTransactions
, notifyBlocks model.config.wsEndpoint
, notifyNewTransactions model.config.wsEndpoint
, Task.perform Tick Time.now
, Task.perform Resize Window.size
]
......@@ -78,32 +72,30 @@ update action model =
case action of
Tick now ->
let
ping =
WebSocket.send model.config.wsEndpoint "session" []
timeout =
model.now - model.lastWebSocketPong >= webSocketTTL
model.now - model.webSocketLastPong >= webSocketTTL
updatedModel =
{ model
| now = now
, webSocketConnected = not timeout
}
ping =
WebSocket.send model.config.wsEndpoint "session" []
in
( updatedModel, ping )
WebSocketMsg message ->
let
-- _ =
-- Debug.log "WebSocketMsg" message
connected =
WebSocket.isSuccess message
txAccepted =
( txAccepted, debug ) =
if WebSocket.isMethod [ "txaccepted" ] message then
Decoder.decodeTxAccepted message
( Decoder.decodeTxAccepted message, "" )
else
Nothing
( Nothing, Debug.log "WebSocketMsg" message )
newLastTransactions =
case txAccepted of
......@@ -113,20 +105,49 @@ update action model =
Just tx ->
List.take 15 (tx :: model.lastTransactions)
sessionId =
WebSocket.getSessionId message
newSessionId =
case sessionId of
Nothing ->
model.webSocketSessionId
Just _ ->
sessionId
updatedModel =
{ model
| webSocketConnected = connected
, lastWebSocketPong = model.now
, webSocketSessionId = newSessionId
, webSocketLastPong = model.now
, lastTransactions = newLastTransactions
}
cmd =
if WebSocket.isMethod [ "blockconnected", "blockdisconnected" ] message then
getBestBlock updatedModel
else
Cmd.none
hasNewSessionId =
(model.webSocketSessionId /= Nothing)
&& (model.webSocketSessionId /= newSessionId)
hasNewBlock =
WebSocket.isMethod
[ "blockconnected", "blockdisconnected" ]
message
commands =
case ( hasNewSessionId, hasNewBlock ) of
( True, _ ) ->
[ getBestBlock updatedModel
, notifyBlocks model.config.wsEndpoint
, notifyNewTransactions model.config.wsEndpoint
]
( False, True ) ->
[ getBestBlock updatedModel ]
( False, False ) ->
[ Cmd.none ]
in
( updatedModel, cmd )
( updatedModel, Cmd.batch commands )
NewUrl location ->
( { model | query = extractQuery location }, Cmd.none )
......@@ -435,6 +456,16 @@ getBestBlock model =
Decoder.decodeGetBestBlock
notifyBlocks : String -> Cmd Msg
notifyBlocks endpoint =
WebSocket.send endpoint "notifyblocks" []
notifyNewTransactions : String -> Cmd Msg
notifyNewTransactions endpoint =
WebSocket.send endpoint "notifynewtransactions" []
webSocketTTL : Time
webSocketTTL =
Time.second * 10
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment