Skip to content

Commit

Permalink
Support stellar ledger entries
Browse files Browse the repository at this point in the history
and update file names

Co-authored-by: norn <keliumJU@users.noreply.github.com>
  • Loading branch information
CristhianRodriguezMolina and keliumJU committed Oct 26, 2023
1 parent 8e02832 commit 677bcdc
Show file tree
Hide file tree
Showing 11 changed files with 117 additions and 114 deletions.
78 changes: 0 additions & 78 deletions lib/xdr/ledger_entries/expiration_entry.ex

This file was deleted.

6 changes: 3 additions & 3 deletions lib/xdr/ledger_entries/ledger_entry_data.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ defmodule StellarBase.XDR.LedgerEntryData do
ClaimableBalanceEntry,
LiquidityPoolEntry,
LedgerEntryType,
ExpirationEntry
TTLEntry
}

@behaviour XDR.Declaration
Expand All @@ -31,7 +31,7 @@ defmodule StellarBase.XDR.LedgerEntryData do
CONTRACT_DATA: ContractDataEntry,
CONTRACT_CODE: ContractCodeEntry,
CONFIG_SETTING: ConfigSettingEntry,
EXPIRATION: ExpirationEntry
TTL: TTLEntry
]

@type entry ::
Expand All @@ -44,7 +44,7 @@ defmodule StellarBase.XDR.LedgerEntryData do
| ContractDataEntry.t()
| ContractCodeEntry.t()
| ConfigSettingEntry.t()
| ExpirationEntry.t()
| TTLEntry.t()

@type t :: %__MODULE__{entry: entry(), type: LedgerEntryType.t()}

Expand Down
2 changes: 1 addition & 1 deletion lib/xdr/ledger_entries/ledger_entry_type.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ defmodule StellarBase.XDR.LedgerEntryType do
CONTRACT_DATA: 6,
CONTRACT_CODE: 7,
CONFIG_SETTING: 8,
EXPIRATION: 9
TTL: 9
]

@enum_spec %XDR.Enum{declarations: @declarations, identifier: nil}
Expand Down
6 changes: 3 additions & 3 deletions lib/xdr/ledger_entries/ledger_key.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ defmodule StellarBase.XDR.LedgerKey do
LedgerKeyContractData,
LedgerKeyContractCode,
LedgerKeyConfigSetting,
LedgerKeyExpiration
LedgerKeyTTL
}

@behaviour XDR.Declaration
Expand All @@ -30,7 +30,7 @@ defmodule StellarBase.XDR.LedgerKey do
CONTRACT_DATA: LedgerKeyContractData,
CONTRACT_CODE: LedgerKeyContractCode,
CONFIG_SETTING: LedgerKeyConfigSetting,
EXPIRATION: LedgerKeyExpiration
TTL: LedgerKeyTTL
]

@type entry ::
Expand All @@ -43,7 +43,7 @@ defmodule StellarBase.XDR.LedgerKey do
| LedgerKeyContractData.t()
| LedgerKeyContractCode.t()
| LedgerKeyConfigSetting.t()
| LedgerKeyExpiration.t()
| LedgerKeyTTL.t()

@type t :: %__MODULE__{entry: entry(), type: LedgerEntryType.t()}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
defmodule StellarBase.XDR.LedgerKeyExpiration do
defmodule StellarBase.XDR.LedgerKeyTTL do
@moduledoc """
Automatically generated by xdrgen
DO NOT EDIT or your changes may be overwritten
Target implementation: elixir_xdr at https://hex.pm/packages/elixir_xdr
Representation of Stellar `LedgerKeyExpiration` type.
Representation of Stellar `LedgerKeyTTL` type.
"""

@behaviour XDR.Declaration
Expand Down
78 changes: 78 additions & 0 deletions lib/xdr/ledger_entries/ttl_entry.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
defmodule StellarBase.XDR.TTLEntry do
@moduledoc """
Automatically generated by xdrgen
DO NOT EDIT or your changes may be overwritten
Target implementation: elixir_xdr at https://hex.pm/packages/elixir_xdr
Representation of Stellar `TTLEntry` type.
"""

@behaviour XDR.Declaration

alias StellarBase.XDR.{
Hash,
UInt32
}

@struct_spec XDR.Struct.new(
key_hash: Hash,
live_until_ledger_seq: UInt32
)

@type key_hash_type :: Hash.t()
@type live_until_ledger_seq_type :: UInt32.t()

@type t :: %__MODULE__{
key_hash: key_hash_type(),
live_until_ledger_seq: live_until_ledger_seq_type()
}

defstruct [:key_hash, :live_until_ledger_seq]

@spec new(key_hash :: key_hash_type(), live_until_ledger_seq :: live_until_ledger_seq_type()) ::
t()
def new(
%Hash{} = key_hash,
%UInt32{} = live_until_ledger_seq
),
do: %__MODULE__{key_hash: key_hash, live_until_ledger_seq: live_until_ledger_seq}

@impl true
def encode_xdr(%__MODULE__{key_hash: key_hash, live_until_ledger_seq: live_until_ledger_seq}) do
[key_hash: key_hash, live_until_ledger_seq: live_until_ledger_seq]
|> XDR.Struct.new()
|> XDR.Struct.encode_xdr()
end

@impl true
def encode_xdr!(%__MODULE__{key_hash: key_hash, live_until_ledger_seq: live_until_ledger_seq}) do
[key_hash: key_hash, live_until_ledger_seq: live_until_ledger_seq]
|> XDR.Struct.new()
|> XDR.Struct.encode_xdr!()
end

@impl true
def decode_xdr(bytes, struct \\ @struct_spec)

def decode_xdr(bytes, struct) do
case XDR.Struct.decode_xdr(bytes, struct) do
{:ok,
{%XDR.Struct{
components: [key_hash: key_hash, live_until_ledger_seq: live_until_ledger_seq]
}, rest}} ->
{:ok, {new(key_hash, live_until_ledger_seq), rest}}

error ->
error
end
end

@impl true
def decode_xdr!(bytes, struct \\ @struct_spec)

def decode_xdr!(bytes, struct) do
{%XDR.Struct{components: [key_hash: key_hash, live_until_ledger_seq: live_until_ledger_seq]},
rest} = XDR.Struct.decode_xdr!(bytes, struct)

{new(key_hash, live_until_ledger_seq), rest}
end
end
7 changes: 5 additions & 2 deletions test/xdr/ledger_entries/ledger_entry_type_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ defmodule StellarBase.XDR.LedgerEntryTypeTest do
:LIQUIDITY_POOL,
:CONTRACT_DATA,
:CONTRACT_CODE,
:CONFIG_SETTING
:CONFIG_SETTING,
:TTL
]

@binaries [
Expand All @@ -23,7 +24,9 @@ defmodule StellarBase.XDR.LedgerEntryTypeTest do
<<0, 0, 0, 4>>,
<<0, 0, 0, 5>>,
<<0, 0, 0, 6>>,
<<0, 0, 0, 7>>
<<0, 0, 0, 7>>,
<<0, 0, 0, 8>>,
<<0, 0, 0, 9>>
]

describe "LedgerEntryType" do
Expand Down
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
defmodule StellarBase.XDR.LedgerKeyExpirationTest do
defmodule StellarBase.XDR.LedgerKeyTTLTest do
use ExUnit.Case

alias StellarBase.XDR.{Hash, LedgerKeyExpiration}
alias StellarBase.XDR.{Hash, LedgerKeyTTL}

describe "LedgerKeyExpiration" do
describe "LedgerKeyTTL" do
setup do
hash = Hash.new("GCIZ3GSM5XL7OUS4UP64THMDZ7CZ3ZWN")

%{
hash: hash,
ledger_expiration: LedgerKeyExpiration.new(hash),
ledger_expiration: LedgerKeyTTL.new(hash),
binary: "GCIZ3GSM5XL7OUS4UP64THMDZ7CZ3ZWN"
}
end

test "new/1", %{hash: hash} do
%LedgerKeyExpiration{key_hash: ^hash} = LedgerKeyExpiration.new(hash)
%LedgerKeyTTL{key_hash: ^hash} = LedgerKeyTTL.new(hash)
end

test "encode_xdr/1", %{ledger_expiration: ledger_expiration, binary: binary} do
{:ok, ^binary} = LedgerKeyExpiration.encode_xdr(ledger_expiration)
{:ok, ^binary} = LedgerKeyTTL.encode_xdr(ledger_expiration)
end

test "encode_xdr!/1", %{ledger_expiration: ledger_expiration, binary: binary} do
^binary = LedgerKeyExpiration.encode_xdr!(ledger_expiration)
^binary = LedgerKeyTTL.encode_xdr!(ledger_expiration)
end

test "decode_xdr/2", %{ledger_expiration: ledger_expiration, binary: binary} do
{:ok, {^ledger_expiration, ""}} = LedgerKeyExpiration.decode_xdr(binary)
{:ok, {^ledger_expiration, ""}} = LedgerKeyTTL.decode_xdr(binary)
end

test "decode_xdr!/2", %{ledger_expiration: ledger_expiration, binary: binary} do
{^ledger_expiration, ^binary} = LedgerKeyExpiration.decode_xdr!(binary <> binary)
{^ledger_expiration, ^binary} = LedgerKeyTTL.decode_xdr!(binary <> binary)
end

test "decode_xdr/2 with invalid binary" do
{:error, :not_binary} = LedgerKeyExpiration.decode_xdr(123)
{:error, :not_binary} = LedgerKeyTTL.decode_xdr(123)
end
end
end
Original file line number Diff line number Diff line change
@@ -1,51 +1,51 @@
defmodule StellarBase.XDR.ExpirationEntryTest do
defmodule StellarBase.XDR.TTLEntryTest do
use ExUnit.Case
alias StellarBase.XDR.ExpirationEntry
alias StellarBase.XDR.TTLEntry
alias StellarBase.XDR.{Hash, UInt32}

describe "ExpirationEntry" do
describe "TTLEntry" do
setup do
key_hash = Hash.new("GCIZ3GSM5XL7OUS4UP64THMDZ7CZ3ZWN")
expiration_ledger_seq = UInt32.new(123)
expiration_entry = ExpirationEntry.new(key_hash, expiration_ledger_seq)
live_until_ledger_seq = UInt32.new(123)
expiration_entry = TTLEntry.new(key_hash, live_until_ledger_seq)

binary =
<<71, 67, 73, 90, 51, 71, 83, 77, 53, 88, 76, 55, 79, 85, 83, 52, 85, 80, 54, 52, 84, 72,
77, 68, 90, 55, 67, 90, 51, 90, 87, 78, 0, 0, 0, 123>>

%{
key_hash: key_hash,
expiration_ledger_seq: expiration_ledger_seq,
live_until_ledger_seq: live_until_ledger_seq,
expiration_entry: expiration_entry,
binary: binary
}
end

test "new/2", %{key_hash: key_hash, expiration_ledger_seq: expiration_ledger_seq} do
%StellarBase.XDR.ExpirationEntry{
test "new/2", %{key_hash: key_hash, live_until_ledger_seq: live_until_ledger_seq} do
%StellarBase.XDR.TTLEntry{
key_hash: ^key_hash,
expiration_ledger_seq: ^expiration_ledger_seq
} = ExpirationEntry.new(key_hash, expiration_ledger_seq)
live_until_ledger_seq: ^live_until_ledger_seq
} = TTLEntry.new(key_hash, live_until_ledger_seq)
end

test "encode_xdr/1", %{expiration_entry: expiration_entry, binary: binary} do
{:ok, ^binary} = ExpirationEntry.encode_xdr(expiration_entry)
{:ok, ^binary} = TTLEntry.encode_xdr(expiration_entry)
end

test "encode_xdr!/1", %{expiration_entry: expiration_entry, binary: binary} do
^binary = ExpirationEntry.encode_xdr!(expiration_entry)
^binary = TTLEntry.encode_xdr!(expiration_entry)
end

test "decode_xdr/2", %{binary: binary, expiration_entry: expiration_entry} do
{:ok, {^expiration_entry, ""}} = ExpirationEntry.decode_xdr(binary)
{:ok, {^expiration_entry, ""}} = TTLEntry.decode_xdr(binary)
end

test "decode_xdr!/2", %{binary: binary, expiration_entry: expiration_entry} do
{^expiration_entry, ""} = ExpirationEntry.decode_xdr!(binary)
{^expiration_entry, ""} = TTLEntry.decode_xdr!(binary)
end

test "decode_xdr/2 with an invalid binary" do
{:error, :not_binary} = ExpirationEntry.decode_xdr(123)
{:error, :not_binary} = TTLEntry.decode_xdr(123)
end
end
end

0 comments on commit 677bcdc

Please sign in to comment.