Skip to content
Snippets Groups Projects
Commit 83c05d00 authored by ABDELGHANI Nassim's avatar ABDELGHANI Nassim Committed by ABDELGHANI Nassim
Browse files

check size before checking transaction rules

parent 69a9c2cb
No related branches found
No related tags found
1 merge request!46Resolve "Validation locale"
......@@ -36,62 +36,67 @@ defmodule IndexRules do
# Rule: For each OutputBase:
# if BaseDelta > 0, then it must be inferior or equal to the sum of all preceding BaseDelta
# Rule: The sum of all inputs in CommonBase must equal the sum of all outputs in CommonBase
commonBase =
:ets.match(local_sindex, {:_, %{base: :"$1"}})
|> Enum.map(fn [base] -> String.to_integer(base) end)
|> Enum.min()
if :ets.info(local_sindex)[:size] > 0 do
commonBase =
:ets.match(local_sindex, {:_, %{base: :"$1"}})
|> IO.inspect()
|> Enum.map(fn [base] -> String.to_integer(base) end)
|> Enum.min()
[inputSumCommonBase, outputSumCommonBase] =
:ets.match(local_sindex, {:_, %{amount: :"$1", base: :"$2", op: :"$3"}})
|> Enum.reduce([0, 0], fn
[amount, base, op], [inputSumCommonBase, outputSumCommonBase] ->
amountCommonBase =
String.to_integer(amount) * Integer.pow(10, String.to_integer(base) - commonBase)
[inputSumCommonBase, outputSumCommonBase] =
:ets.match(local_sindex, {:_, %{amount: :"$1", base: :"$2", op: :"$3"}})
|> Enum.reduce([0, 0], fn
[amount, base, op], [inputSumCommonBase, outputSumCommonBase] ->
amountCommonBase =
String.to_integer(amount) * Integer.pow(10, String.to_integer(base) - commonBase)
if op == "CREATE" do
# output
[inputSumCommonBase, outputSumCommonBase + amountCommonBase]
else
# input
[inputSumCommonBase + amountCommonBase, outputSumCommonBase]
end
end)
if op == "CREATE" do
# output
[inputSumCommonBase, outputSumCommonBase + amountCommonBase]
else
# input
[inputSumCommonBase + amountCommonBase, outputSumCommonBase]
end
end)
if inputSumCommonBase == outputSumCommonBase do
allBases =
:ets.match(local_sindex, {:_, %{base: :"$1"}})
|> Enum.map(fn [base] -> String.to_integer(base) end)
|> Enum.sort()
|> Enum.uniq()
if inputSumCommonBase == outputSumCommonBase do
allBases =
:ets.match(local_sindex, {:_, %{base: :"$1"}})
|> Enum.map(fn [base] -> String.to_integer(base) end)
|> Enum.sort()
|> Enum.uniq()
allBases
|> Enum.reduce_while(0, fn base, sumBaseDelta ->
[inputBaseSum, outputBaseSum] =
:ets.match(local_sindex, {:_, %{amount: :"$1", base: "#{base}", op: :"$2"}})
|> Enum.reduce([0, 0], fn
[amount, op], [inputBaseSum, outputBaseSum] ->
if op == "CREATE" do
# output
[inputBaseSum, outputBaseSum + String.to_integer(amount)]
else
# input
[inputBaseSum + String.to_integer(amount), outputBaseSum]
end
end)
allBases
|> Enum.reduce_while(0, fn base, sumBaseDelta ->
[inputBaseSum, outputBaseSum] =
:ets.match(local_sindex, {:_, %{amount: :"$1", base: "#{base}", op: :"$2"}})
|> Enum.reduce([0, 0], fn
[amount, op], [inputBaseSum, outputBaseSum] ->
if op == "CREATE" do
# output
[inputBaseSum, outputBaseSum + String.to_integer(amount)]
else
# input
[inputBaseSum + String.to_integer(amount), outputBaseSum]
end
end)
baseDelta = outputBaseSum - inputBaseSum
baseDelta = outputBaseSum - inputBaseSum
if baseDelta > 0 and baseDelta > -sumBaseDelta do
# Functionally: also, we cannot convert a superiod unit base into a lower one.
{:halt, false}
else
{:cont, sumBaseDelta + baseDelta}
end
end)
# enforce boolean return
|> then(fn check_base_delta -> if check_base_delta == false, do: false, else: true end)
if baseDelta > 0 and baseDelta > -sumBaseDelta do
# Functionally: also, we cannot convert a superiod unit base into a lower one.
{:halt, false}
else
{:cont, sumBaseDelta + baseDelta}
end
end)
# enforce boolean return
|> then(fn check_base_delta -> if check_base_delta == false, do: false, else: true end)
else
false
end
else
false
true
end
end
......@@ -119,11 +124,15 @@ defmodule IndexRules do
end
defp checkMaxTransactionChainingDepth(local_sindex) do
:ets.match(local_sindex, {:_, %{tx: :"$1"}})
|> Enum.map(fn [txHash] -> txHash end)
|> Enum.uniq()
|> Enum.map(fn txHash -> getTransactionDepth(local_sindex, txHash, 0) end)
|> Enum.max() <= 5
if :ets.info(local_sindex)[:size] > 0 do
:ets.match(local_sindex, {:_, %{tx: :"$1"}})
|> Enum.map(fn [txHash] -> txHash end)
|> Enum.uniq()
|> Enum.map(fn txHash -> getTransactionDepth(local_sindex, txHash, 0) end)
|> Enum.max() <= 5
else
true
end
end
def check(local_iindex, local_mindex, local_cindex, local_sindex) do
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment