Skip to content

Commit

Permalink
Remove reward flag from an unspent output
Browse files Browse the repository at this point in the history
  • Loading branch information
Samuel committed Mar 14, 2022
1 parent 59cb1e7 commit be6f67e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -390,8 +390,6 @@ defmodule ArchEthic.TransactionChain.Transaction.ValidationStamp.LedgerOperation
# Unspent output amount (2 UCO)
0, 0, 0, 0, 11, 235, 194, 0,
# Unspent output type (UCO)
0,
# Unspent output reward?
0
>>
"""
Expand Down Expand Up @@ -422,7 +420,7 @@ defmodule ArchEthic.TransactionChain.Transaction.ValidationStamp.LedgerOperation
...> 0, 0, 0, 0, 60, 203, 247, 0, 0,
...> 1, 0, 0, 34, 118, 242, 194, 93, 131, 130, 195, 9, 97, 237,
...> 220, 195, 112, 1, 54, 221, 86, 154, 234, 96, 217, 149, 84, 188, 63, 242, 166, 47, 158, 139, 207,
...> 0, 0, 0, 0, 11, 235, 194, 0, 0, 0>>
...> 0, 0, 0, 0, 11, 235, 194, 0, 0>>
...> |> LedgerOperations.deserialize()
{
%LedgerOperations{
Expand All @@ -440,8 +438,7 @@ defmodule ArchEthic.TransactionChain.Transaction.ValidationStamp.LedgerOperation
from: <<0, 0, 34, 118, 242, 194, 93, 131, 130, 195, 9, 97, 237, 220, 195, 112, 1, 54, 221,
86, 154, 234, 96, 217, 149, 84, 188, 63, 242, 166, 47, 158, 139, 207>>,
amount: 200_000_000,
type: :UCO,
reward?: false
type: :UCO
}
]
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ defmodule ArchEthic.TransactionChain.Transaction.ValidationStamp.LedgerOperation
@type t :: %__MODULE__{
amount: non_neg_integer(),
from: Crypto.versioned_hash(),
type: TransactionMovementType.t(),
reward?: boolean()
type: TransactionMovementType.t()
}

@doc """
Expand All @@ -39,8 +38,6 @@ defmodule ArchEthic.TransactionChain.Transaction.ValidationStamp.LedgerOperation
# Amount
0, 0, 0, 0, 62, 149, 186, 128,
# UCO Unspent Output
0,
# Reward?
0
>>
Expand All @@ -64,16 +61,12 @@ defmodule ArchEthic.TransactionChain.Transaction.ValidationStamp.LedgerOperation
1,
# NFT address
0, 49, 101, 72, 154, 152, 3, 174, 47, 2, 35, 7, 92, 122, 206, 185, 71, 140, 74,
197, 46, 99, 117, 89, 96, 100, 20, 0, 34, 181, 215, 143, 175,
# Reward?
0
197, 46, 99, 117, 89, 96, 100, 20, 0, 34, 181, 215, 143, 175
>>
"""
@spec serialize(__MODULE__.t()) :: <<_::64, _::_*8>>
def serialize(%__MODULE__{from: from, amount: amount, type: type, reward?: reward?}) do
reward_bit = if reward?, do: 1, else: 0

<<from::binary, amount::64, TransactionMovementType.serialize(type)::binary, reward_bit::8>>
def serialize(%__MODULE__{from: from, amount: amount, type: type}) do
<<from::binary, amount::64, TransactionMovementType.serialize(type)::binary>>
end

@doc """
Expand All @@ -83,23 +76,22 @@ defmodule ArchEthic.TransactionChain.Transaction.ValidationStamp.LedgerOperation
iex> <<0, 0, 214, 107, 17, 107, 227, 11, 17, 43, 204, 48, 78, 129, 145, 126, 45, 68, 194,
...> 159, 19, 92, 240, 29, 37, 105, 183, 232, 56, 42, 163, 236, 251, 186,
...> 0, 0, 0, 0, 62, 149, 186, 128, 0, 0>>
...> 0, 0, 0, 0, 62, 149, 186, 128, 0>>
...> |> UnspentOutput.deserialize()
{
%UnspentOutput{
from: <<0, 0, 214, 107, 17, 107, 227, 11, 17, 43, 204, 48, 78, 129, 145, 126, 45, 68, 194,
159, 19, 92, 240, 29, 37, 105, 183, 232, 56, 42, 163, 236, 251, 186>>,
amount: 1_050_000_000,
type: :UCO,
reward?: false
type: :UCO
},
""
}
iex> <<0, 0, 214, 107, 17, 107, 227, 11, 17, 43, 204, 48, 78, 129, 145, 126, 45, 68, 194,
...> 159, 19, 92, 240, 29, 37, 105, 183, 232, 56, 42, 163, 236, 251, 186,
...> 0, 0, 0, 0, 62, 149, 186, 128, 1, 0, 0, 49, 101, 72, 154, 152, 3, 174, 47, 2, 35, 7, 92, 122, 206, 185, 71, 140, 74,
...> 197, 46, 99, 117, 89, 96, 100, 20, 0, 34, 181, 215, 143, 175, 0
...> 197, 46, 99, 117, 89, 96, 100, 20, 0, 34, 181, 215, 143, 175
...> >>
...> |> UnspentOutput.deserialize()
{
Expand All @@ -108,25 +100,21 @@ defmodule ArchEthic.TransactionChain.Transaction.ValidationStamp.LedgerOperation
159, 19, 92, 240, 29, 37, 105, 183, 232, 56, 42, 163, 236, 251, 186>>,
amount: 1_050_000_000,
type: {:NFT, <<0, 0, 49, 101, 72, 154, 152, 3, 174, 47, 2, 35, 7, 92, 122, 206, 185, 71, 140, 74,
197, 46, 99, 117, 89, 96, 100, 20, 0, 34, 181, 215, 143, 175>>},
reward?: false
197, 46, 99, 117, 89, 96, 100, 20, 0, 34, 181, 215, 143, 175>>}
},
""
}
"""
@spec deserialize(bitstring()) :: {__MODULE__.t(), bitstring}
def deserialize(data) when is_bitstring(data) do
{address, <<amount::64, rest::bitstring>>} = Utils.deserialize_address(data)
{type, <<reward_bit::8, rest::bitstring>>} = TransactionMovementType.deserialize(rest)

reward? = if reward_bit == 1, do: true, else: false
{type, rest} = TransactionMovementType.deserialize(rest)

{
%__MODULE__{
from: address,
amount: amount,
type: type,
reward?: reward?
type: type
},
rest
}
Expand All @@ -136,8 +124,7 @@ defmodule ArchEthic.TransactionChain.Transaction.ValidationStamp.LedgerOperation
def from_map(unspent_output = %{}) do
res = %__MODULE__{
from: Map.get(unspent_output, :from),
amount: Map.get(unspent_output, :amount),
reward?: Map.get(unspent_output, :reward)
amount: Map.get(unspent_output, :amount)
}

case Map.get(unspent_output, :type) do
Expand All @@ -150,12 +137,11 @@ defmodule ArchEthic.TransactionChain.Transaction.ValidationStamp.LedgerOperation
end

@spec to_map(t()) :: map()
def to_map(%__MODULE__{from: from, amount: amount, type: :UCO, reward?: reward?}) do
def to_map(%__MODULE__{from: from, amount: amount, type: :UCO}) do
%{
from: from,
amount: amount,
type: "UCO",
reward: reward?
type: "UCO"
}
end

Expand All @@ -164,8 +150,7 @@ defmodule ArchEthic.TransactionChain.Transaction.ValidationStamp.LedgerOperation
from: from,
amount: amount,
type: "NFT",
nft_address: nft_address,
reward: false
nft_address: nft_address
}
end
end
3 changes: 1 addition & 2 deletions priv/migrations/2_create_validation_stamp_type.cql
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ CREATE TYPE IF NOT EXISTS archethic.ledger_operations_unspent_output(
"from" blob,
amount bigint,
type varchar,
nft_address blob,
reward boolean
nft_address blob
);

CREATE TYPE IF NOT EXISTS archethic.ledger_operations(
Expand Down

0 comments on commit be6f67e

Please sign in to comment.