Skip to content
Snippets Groups Projects

Resolve "Récupération des blocs"

Merged JAUJAY Augustin requested to merge 186-recuperation-des-blocs into dev
2 files
+ 27
9
Compare changes
  • Side-by-side
  • Inline
Files
2
+ 27
9
@@ -28,23 +28,41 @@ defmodule BDD do
def fill_with_block() do
{:ok, :block} = :dets.open_file(:block, [{:file, 'data/block'}, {:type, :set}])
# Getting current block from the remote node of the blockchain
currentBlock = case :httpc.request(:get, {@base_url ++ '/blockchain/current', []}, [], []) do
{:ok, {{_, 200, 'OK'}, _headers, body}} -> Poison.decode!(body)
_ -> Logger.info('Filling DB : Failing when getting current block')
end
case :httpc.request(:get, {@base_url ++ '/blockchain/blocks/' ++ to_charlist(50) ++ '/0', []}, [], []) do
{:ok, {{_, 200, 'OK'}, _headers, '[]'}} ->
Logger.info('Filling DB : No blocks to get from remote g1-test node')
# Getting current block in local database
lastLocalBlock = case :dets.first(:block) do
:"$end_of_table" -> 0
n -> n
end
{:ok, {{_, 200, 'OK'}, _headers, body}} ->
Logger.info('Filling : DB : Successfully got blocks from remote g1-test node')
Poison.decode!(body) |> Enum.map(fn b -> :dets.insert(:block, {b["number"], b}) end)
lastLocalBlock..currentBlock["number"] # TODO : gérer le cas où la bdd est vide
|> Enum.chunk_every(1000)
|> Enum.map(fn chk ->
{List.first(chk), List.last(chk)} |> IO.inspect
# Putting the new blocks in the local database
case :httpc.request(:get, {@base_url ++ '/blockchain/blocks/' ++ to_charlist(1000) ++ '/' ++ to_charlist(List.first(chk)), []}, [], []) do
{:ok, {{_, 200, 'OK'}, _headers, '[]'}} ->
Logger.info('Filling DB : No blocks to get from remote g1-test node')
_ ->
Logger.info('---Failed when filling DataBase---')
end
{:ok, {{_, 200, 'OK'}, _headers, body}} ->
Logger.info('Filling : DB : Successfully got blocks'
++ List.first(chk)
++ 'to'
++ List.last(chk)
++ 'from remote g1-test node')
Poison.decode!(body) |> Enum.map(fn b -> :dets.insert(:block, {b["number"], b}) end)
error ->
error |> IO.inspect
Logger.info('---Failed when filling DataBase---')
end
end)
end
end
Loading