Skip to content
Snippets Groups Projects
Commit ac81f88a authored by Augustin Jaujay's avatar Augustin Jaujay
Browse files

Merge branch '219-gestion-bdd' into dev

parents 62fd8092 655621f7
No related branches found
No related tags found
No related merge requests found
Pipeline #4319 passed
......@@ -102,9 +102,10 @@ defmodule DB do
:dets.close(db)
if dbName == 'block' do
fill_with_block()
spawn(fn -> fill_with_block() end)
end
{:ok, {dbName, dbNumber}}
end
......@@ -233,17 +234,25 @@ defmodule DB do
defp fill_with_block() do
# 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')
# currentRemoteBlock = (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)["number"]
currentRemoteBlock = 49
dbSize = get_size('block')
currentLocalBlock = if dbSize > currentRemoteBlock do
currentRemoteBlock
else
dbSize
end
get_size('block')..50 # currentBlock["number"] # TODO : Fetch all the blocks
currentLocalBlock..currentRemoteBlock # TODO : Fetch all the blocks
|> Enum.chunk_every(5000)
|> Enum.map(fn chk ->
# Putting the new blocks in the local database
case :httpc.request(:get, {@base_url ++ '/blockchain/blocks/' ++ to_charlist(List.last(chk) - List.first(chk)) ++ '/' ++ to_charlist(List.first(chk)), []}, [], []) do
case :httpc.request(:get, {@base_url ++ '/blockchain/blocks/' ++ to_charlist(length(chk)) ++ '/' ++ to_charlist(List.first(chk)), []}, [], []) do
{:ok, {{_, 200, 'OK'}, _headers, '[]'}} ->
Logger.info('Filling DB : No blocks to get from remote g1-test node')
......@@ -254,11 +263,13 @@ defmodule DB do
++ to_charlist(List.last(chk))
++ ' from '
++ @base_url)
Poison.decode!(body) |> Enum.map(fn b -> DB.insert('block', b["number"], b) end)
Poison.decode!(body) |> Enum.map(fn b -> DB.insert('block', b["number"], b); :timer.sleep(50) end)
_ ->
Logger.info('---Failed when filling DataBase---')
end
:timer.sleep(1000)
end)
end
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment