Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Write beacon slot to summary chain #358

Merged
1 commit merged into from
Jun 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions lib/archethic/beacon_chain.ex
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,11 @@ defmodule Archethic.BeaconChain do
data: %TransactionData{content: content}
}
) do
with {%Slot{subset: subset} = slot, _} <- Slot.deserialize(content),
:ok <- validate_slot(tx, slot) do
with {%Slot{subset: subset, slot_time: slot_time} = slot, _} <- Slot.deserialize(content),
:ok <- validate_slot(tx, slot),
genesis_address <-
Crypto.derive_beacon_chain_address(subset, previous_summary_time(slot_time)),
:ok <- TransactionChain.write_transaction_at(tx, genesis_address) do
Logger.debug("New beacon transaction loaded - #{inspect(slot)}",
beacon_subset: Base.encode16(subset)
)
Expand Down
52 changes: 36 additions & 16 deletions lib/archethic/beacon_chain/subset.ex
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,17 @@ defmodule Archethic.BeaconChain.Subset do

# Avoid to store or dispatch an empty beacon's slot
unless Slot.empty?(current_slot) do
beacon_transaction = create_beacon_transaction(current_slot)

if summary_time?(time) do
genesis_address =
Crypto.derive_beacon_chain_address(subset, SummaryTimer.previous_summary(time))

TransactionChain.write_transaction_at(beacon_transaction, genesis_address)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, This is where we are caching as well as writing to the DB

SummaryCache.add_slot(subset, current_slot)
else
dispatch_slot_to_summary_nodes(current_slot, time, node_public_key)
next_time = SlotTimer.next_slot(time)
broadcast_beacon_transaction(subset, next_time, beacon_transaction, node_public_key)
end
end
end
Expand All @@ -199,13 +206,6 @@ defmodule Archethic.BeaconChain.Subset do

defp update_p2p_view?(_), do: true

defp dispatch_slot_to_summary_nodes(current_slot = %Slot{subset: subset}, time, node_public_key) do
beacon_transaction = create_beacon_transaction(current_slot)

next_time = SlotTimer.next_slot(time)
broadcast_beacon_transaction(subset, next_time, beacon_transaction, node_public_key)
end

defp next_state(state = %{subset: subset}, time) do
next_time = SlotTimer.next_slot(time)

Expand Down Expand Up @@ -239,9 +239,12 @@ defmodule Archethic.BeaconChain.Subset do
beacon_subset: Base.encode16(subset)
)

genesis_address =
Crypto.derive_beacon_chain_address(subset, SummaryTimer.previous_summary(time))

beacon_slots
|> create_summary_transaction(subset, time)
|> TransactionChain.write_transaction()
|> TransactionChain.write_transaction_at(genesis_address)
end
end

Expand Down Expand Up @@ -283,13 +286,30 @@ defmodule Archethic.BeaconChain.Subset do
{prev_pub, prev_pv} = Crypto.derive_beacon_keypair(subset, SlotTimer.previous_slot(slot_time))
{next_pub, _} = Crypto.derive_beacon_keypair(subset, slot_time)

Transaction.new_with_keys(
:beacon,
%TransactionData{content: Slot.serialize(slot) |> Utils.wrap_binary()},
prev_pv,
prev_pub,
next_pub
)
slot_content =
slot
|> Slot.serialize()
|> Utils.wrap_binary()

tx =
Transaction.new_with_keys(
:beacon,
%TransactionData{content: slot_content},
prev_pv,
prev_pub,
next_pub
)

stamp =
%ValidationStamp{
timestamp: slot_time,
proof_of_election: <<0::size(512)>>,
proof_of_integrity: TransactionChain.proof_of_integrity([tx]),
proof_of_work: Crypto.first_node_public_key()
}
|> ValidationStamp.sign()

%{tx | validation_stamp: stamp}
end

defp create_summary_transaction(beacon_slots, subset, summary_time) do
Expand Down