Commit 7aaf1da5 authored by Cédric F.'s avatar Cédric F.

Use getbestblock RPC call to save one request

parent 439857fc
Pipeline #13972943 (#) passed with stage
in 1 minute and 43 seconds
module Lib.WebSocket exposing (send, isSuccess, newBlock)
module Lib.WebSocket exposing (send, isSuccess, isMethod)
import WebSocket
import Json.Decode as Decode
......@@ -38,21 +38,8 @@ isSuccess jsonString =
maybeError == Nothing
newBlock : String -> Maybe Int
newBlock jsonString =
if filterMethod [ "blockconnected", "blockdisconnected" ] jsonString then
-- decodeNewBlock jsonString
Nothing
else
Nothing
-- write tests
filterMethod : List String -> String -> Bool
filterMethod methods jsonString =
isMethod : List String -> String -> Bool
isMethod methods jsonString =
let
decoder =
Decode.maybe <| Decode.field "method" <| Decode.string
......@@ -66,12 +53,3 @@ filterMethod methods jsonString =
Ok maybeMethod ->
List.any (\method -> Just method == maybeMethod) methods
-- == "{\"result\":null,\"error\":null,\"id\":0}"
-- decode : String -> Json.Decode.Decoder Notification
-- decode jsonString =
-- Json.Decode.map2 JsonModel
-- (Json.Decode.at [ "result", "blocks" ] Json.Decode.int)
-- (Json.Decode.at [ "result", "connections" ] Json.Decode.int)
......@@ -39,8 +39,9 @@ type alias Model =
, wsEndpoint : String
, vimMode : Bool
, debug : Bool
, time : Time
, now : Time
, lastWebSocketPong : Time
, lastBlockHash : String
, lastBlockHeight : Int
, fetching : Bool
, webSocketConnected : Bool
......@@ -61,8 +62,9 @@ initialModel coin wsEndpoint query =
, wsEndpoint = wsEndpoint
, vimMode = False
, debug = False
, time = -1
, now = -1
, lastWebSocketPong = -1
, lastBlockHash = ""
, lastBlockHeight = -1
, fetching = False
, webSocketConnected = False
......@@ -80,8 +82,12 @@ type Msg
| Resize Window.Size
| Tick Time
| WSMsg String
| GetInfo
| GetInfoResult (Result Http.Error Int)
| GetBestBlock
| GetBestBlockResult (Result Http.Error BestBlock)
type alias BestBlock =
{ hash : String, height : Int }
type alias Keys =
......
......@@ -59,7 +59,7 @@ init flags location =
( updatedModel, msg ) =
if String.isEmpty query then
update GetInfo model
( model, Cmd.none )
else
update (Query query) model
......@@ -73,6 +73,7 @@ init flags location =
, Cmd.batch
[ elmToJs [ "focus" ]
, msg
, getBestBlock updatedModel
, notifyBlocks
, notifyNewTransactions
, Task.perform Tick Time.now
......@@ -96,17 +97,17 @@ subscriptions model =
update : Msg -> Model -> ( Model, Cmd Msg )
update action model =
case action of
Tick time ->
Tick now ->
let
ping =
Lib.WebSocket.send model.wsEndpoint "session" []
timeout =
model.time - model.lastWebSocketPong >= webSocketTTL
model.now - model.lastWebSocketPong >= webSocketTTL
updatedModel =
{ model
| time = time
| now = now
, webSocketConnected = not timeout
}
in
......@@ -120,22 +121,19 @@ update action model =
connected =
Lib.WebSocket.isSuccess message
lastBlockHeight =
case Lib.WebSocket.newBlock message of
Nothing ->
model.lastBlockHeight
Just height ->
height
updatedModel =
{ model
| webSocketConnected = connected
, lastWebSocketPong = model.time
, lastBlockHeight = lastBlockHeight
, lastWebSocketPong = model.now
}
cmd =
if Lib.WebSocket.isMethod [ "blockconnected", "blockdisconnected" ] message then
getBestBlock updatedModel
else
Cmd.none
in
( updatedModel, Cmd.none )
( updatedModel, cmd )
NewUrl location ->
( { model | query = extractQuery location }, Cmd.none )
......@@ -189,18 +187,19 @@ update action model =
)
|> updateUrl
GetInfo ->
GetBestBlock ->
let
updatedModel =
{ model | fetching = True }
in
( updatedModel, getInfo updatedModel )
( updatedModel, getBestBlock updatedModel )
GetInfoResult result ->
GetBestBlockResult result ->
case result of
Ok blocks ->
Ok bestBlock ->
( { model
| lastBlockHeight = blocks
| lastBlockHash = bestBlock.hash
, lastBlockHeight = bestBlock.height
, fetching = False
, error = Nothing
}
......@@ -427,15 +426,17 @@ updateUrl ( model, cmd ) =
( model, Cmd.batch <| commands ++ [ cmd ] )
getInfo : Model -> Cmd Msg
getInfo model =
getBestBlock : Model -> Cmd Msg
getBestBlock model =
let
params =
Encode.list []
in
JsonRpc.post "getinfo" params GetInfoResult decodeStatusFetch
JsonRpc.post "getbestblock" params GetBestBlockResult decodeGetBestBlock
decodeStatusFetch : Decode.Decoder Int
decodeStatusFetch =
Decode.at [ "result", "blocks" ] Decode.int
decodeGetBestBlock : Decode.Decoder BestBlock
decodeGetBestBlock =
Decode.map2 BestBlock
(Decode.at [ "result", "hash" ] Decode.string)
(Decode.at [ "result", "height" ] Decode.int)
......@@ -79,11 +79,7 @@ searchView model =
]
[]
]
, div
[ class "col-2"
, style [ ( "background-color", "rgba(255,255,255,0.5)" ) ]
]
[ statusView model ]
, div [ class "col-2" ] [ statusView model ]
]
vim =
......@@ -97,9 +93,7 @@ searchView model =
logo model =
div [ class "row" ]
[ div
[ class "col text-center"
, style [ ( "background-color", "rgba(255,255,255,0.5)" ) ]
]
[ class "col text-center" ]
[ a [ href "/" ]
[ img
[ class
......@@ -132,7 +126,7 @@ statusView model =
if model.lastBlockHeight < 0 then
span [] [ text "??????" ]
else
queryLink (toString model.lastBlockHeight) (toString model.lastBlockHeight) []
queryLink model.lastBlockHash (toString model.lastBlockHeight) []
( wsClass, wsStatus ) =
if model.webSocketConnected then
......@@ -142,12 +136,11 @@ statusView model =
in
div [ class "text-center" ]
[ span
[ class "badge badge-info" ]
[ class "badge badge-dark" ]
[ h5 []
[ span [] [ text "Last block: " ]
, lastBlock
[ span [] [ text "Last block:" ]
, br [] []
, span [] [ text "N minutes ago" ]
, lastBlock
]
]
, span
......@@ -158,17 +151,17 @@ statusView model =
addressView : Model -> Html Msg
addressView model =
Html.map AddressMsg (AddressComponent.view model.addressModel model.time)
Html.map AddressMsg (AddressComponent.view model.addressModel model.now)
blockView : Model -> Html Msg
blockView model =
Html.map BlockMsg (BlockComponent.view model.blockModel model.time)
Html.map BlockMsg (BlockComponent.view model.blockModel model.now)
transactionView : Model -> Html Msg
transactionView model =
Html.map TransactionMsg (TransactionComponent.view model.transactionModel model.time)
Html.map TransactionMsg (TransactionComponent.view model.transactionModel model.now)
view : Model -> Html Msg
......
......@@ -5,6 +5,7 @@ import Fuzz exposing (Fuzzer, int, list, string)
import Test exposing (..)
import String
import Json.Decode as Decode
import Lib.WebSocket as WebSocket
import Components.Address as Address
import Components.Block as Block
import Components.Transaction as Transaction
......@@ -93,6 +94,30 @@ suite =
in
Expect.equal "v4" <| Transaction.computeVote jsonModel
]
, describe "WebSocket"
[ test "isSuccess" <|
\() ->
let
connectFixture =
"{\"result\":null,\"error\":null,\"id\":0}"
sessionFixture =
"{\"result\":{\"sessionid\":14497963617325762052},\"error\":null,\"id\":0}"
in
List.map (\fixture -> WebSocket.isSuccess fixture)
[ connectFixture, sessionFixture ]
|> Expect.equal [ True, True ]
, test "isMethod" <|
\() ->
let
blockConnectedFixture =
"{\"jsonrpc\":\"1.0\",\"method\":\"blockconnected\",\"params\":[\"050000003cc1d839eda0a63e40b2103ff9dccc0ce9fc85f35b64c752510000000000000058075ab9a9d12268beaea7ca86f08e06c20672d530a1fead3010c4ddaf0e1a181547f9c847bf2dff1601d0aca79a6a5e1a526c8620a7fa1736dd12d38d1c436f0100f1dfbad173c404000401639f0000b2c2001a21564fce010000005ed80200fe1c00006b330c5a00a22593153298ba57a0006393587581000000000000000000000000000000000000000005000000\",null],\"id\":null}"
result =
WebSocket.isMethod [ "blockconnected", "blockdisconnected" ] blockConnectedFixture
in
Expect.equal result True
]
, describe "Fuzz test examples, using randomly generated input"
-- XXX: keping them as examples
[ fuzz (list int) "Lists always have positive length" <|
......
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