Skip to content

Commit 7247d89

Browse files
committed
new receipt format
1 parent 62b849f commit 7247d89

File tree

5 files changed

+35
-84
lines changed

5 files changed

+35
-84
lines changed

ex/lib/api/api_tx.ex

Lines changed: 7 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ defmodule API.TX do
99
entry_hash = if byte_size(entry_hash) != 32, do: Base58.decode(entry_hash), else: entry_hash
1010
case DB.Entry.by_hash(entry_hash) do
1111
nil -> nil
12-
%{hash: entry_hash, header: %{slot: slot}, txs: txs} ->
12+
%{hash: entry_hash, header: %{height: height}, txs: txs} ->
1313
Enum.map(txs, fn(txu)->
1414
txu = TX.unpack(txu)
15-
|> Map.put(:metadata, %{entry_hash: entry_hash, entry_slot: slot})
15+
|> Map.put(:metadata, %{entry_hash: entry_hash, entry_height: height})
1616
format_tx_for_client(txu)
1717
end)
1818
end
@@ -110,23 +110,8 @@ defmodule API.TX do
110110
result = TX.validate(tx_packed |> TX.unpack())
111111
if result[:error] == :ok do
112112
txu = result.txu
113-
if tx_packed =~ "deploy" do
114-
action = TX.action(txu)
115-
if action.contract == "Contract" and action.function == "deploy" do
116-
case BIC.Contract.validate(List.first(action.args)) do
117-
%{error: :ok} ->
118-
TXPool.insert_and_broadcast(txu)
119-
%{error: :ok, hash: Base58.encode(result.txu.hash)}
120-
error -> error
121-
end
122-
else
123-
TXPool.insert_and_broadcast(txu)
124-
%{error: :ok, hash: Base58.encode(result.txu.hash)}
125-
end
126-
else
127-
TXPool.insert_and_broadcast(txu)
128-
%{error: :ok, hash: Base58.encode(result.txu.hash)}
129-
end
113+
TXPool.insert_and_broadcast(txu)
114+
%{error: :ok, hash: Base58.encode(result.txu.hash)}
130115
else
131116
%{error: result.error}
132117
end
@@ -136,26 +121,9 @@ defmodule API.TX do
136121
result = TX.validate(tx_packed |> TX.unpack())
137122
if result[:error] == :ok do
138123
txu = result.txu
139-
if tx_packed =~ "deployyy" do
140-
action = TX.action(txu)
141-
if action.contract == "Contract" and action.function == "deploy" do
142-
case BIC.Contract.validate(List.first(action.args)) do
143-
%{error: :ok} ->
144-
if broadcast do TXPool.insert_and_broadcast(txu) else TXPool.insert(txu) end
145-
txres = submit_and_wait_1(result.txu.hash)
146-
%{error: :ok, hash: Base58.encode(result.txu.hash), entry_hash: txres.metadata.entry_hash, result: txres[:result], receipt: txres[:receipt]}
147-
error -> error
148-
end
149-
else
150-
if broadcast do TXPool.insert_and_broadcast(txu) else TXPool.insert(txu) end
151-
txres = submit_and_wait_1(result.txu.hash)
152-
%{error: :ok, hash: Base58.encode(result.txu.hash), entry_hash: txres.metadata.entry_hash, result: txres[:result], receipt: txres[:receipt]}
153-
end
154-
else
155-
if broadcast do TXPool.insert_and_broadcast(txu) else TXPool.insert(txu) end
156-
txres = submit_and_wait_1(result.txu.hash)
157-
%{error: :ok, hash: Base58.encode(result.txu.hash), entry_hash: txres.metadata.entry_hash, result: txres[:result], receipt: txres[:receipt]}
158-
end
124+
if broadcast do TXPool.insert_and_broadcast(txu) else TXPool.insert(txu) end
125+
txres = submit_and_wait_1(result.txu.hash)
126+
%{error: :ok, hash: Base58.encode(result.txu.hash), metadata: txres.metadata, receipt: txres.receipt}
159127
else
160128
%{error: result.error}
161129
end

ex/lib/api/db_chain.ex

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,17 @@ defmodule DB.Chain do
5454
entry_bytes = RocksDB.get(map.entry_hash, db_handle(db_opts, :entry, %{}))
5555
entry = DB.Entry.by_hash(map.entry_hash, db_opts)
5656
tx_bytes = binary_part(entry_bytes, map.index_start, map.index_size)
57-
receipt = if map[:result] do map[:result] else
58-
r = map.receipt
59-
result = RocksDB.ascii_dump(r.result)
60-
logs = Enum.map(r.logs, & RocksDB.ascii_dump(&1))
61-
Map.merge(r, %{result: result, logs: logs})
57+
58+
receipt = if map[:result] do map.result else
59+
receipt = map.receipt
60+
result = RocksDB.ascii_dump(receipt.result)
61+
logs = Enum.map(receipt.logs, & RocksDB.ascii_dump(&1))
62+
Map.merge(receipt, %{result: result, logs: logs})
6263
end
64+
6365
TX.unpack(tx_bytes)
64-
|> Map.put(:result, receipt)
6566
|> Map.put(:receipt, receipt)
66-
|> Map.put(:metadata, %{entry_hash: map.entry_hash, entry_height: entry.header.height, entry_slot: entry.header.slot})
67+
|> Map.put(:metadata, %{entry_hash: map.entry_hash, entry_height: entry.header.height})
6768
end
6869
end
6970

ex/lib/api/db_entry.ex

Lines changed: 18 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ defmodule DB.Entry do
8888
db_opts[:rtx_commit] && RocksDB.transaction_commit(db_opts.rtx)
8989
end
9090

91-
def apply_into_main_chain(entry, muts_hash, muts_rev, {logs, receipts}, root_receipts, root_contractstate, db_opts = %{rtx: _}) do
91+
def apply_into_main_chain(entry, muts_hash, muts_rev, receipts, root_receipts, root_contractstate, db_opts = %{rtx: _}) do
9292
entry_packed = Entry.pack_for_db(entry)
9393
RocksDB.put(entry.hash, entry_packed, db_handle(db_opts, :entry, %{}))
9494
RocksDB.put("by_height:#{pad_integer(entry.header.height)}:#{entry.hash}", entry.hash, db_handle(db_opts, :entry_meta, %{}))
@@ -102,41 +102,23 @@ defmodule DB.Entry do
102102
RocksDB.put("entry:#{entry.hash}:root_receipts", root_receipts, db_handle(db_opts, :entry_meta, %{}))
103103
RocksDB.put("entry:#{entry.hash}:root_contractstate", root_contractstate, db_handle(db_opts, :entry_meta, %{}))
104104

105-
if entry.header.height >= RDBProtocol.forkheight() do
106-
receipts_by_txid = Map.new(receipts, fn r -> {r.txid, Map.drop(r, [:txid])} end)
107-
Enum.each(entry.txs, fn(txu)->
108-
receipt = Map.fetch!(receipts_by_txid, txu.hash)
109-
case :binary.match(entry_packed, TX.pack(txu)) do
110-
{index_start, index_size} ->
111-
tx_ptr = %{entry_hash: entry.hash, receipt: receipt, index_start: index_start, index_size: index_size}
112-
|> RDB.vecpak_encode()
113-
RocksDB.put(txu.hash, tx_ptr, db_handle(db_opts, :tx, %{}))
114-
115-
nonce_padded = pad_integer_20(txu.tx.nonce)
116-
RocksDB.put("#{txu.tx.signer}:#{nonce_padded}", txu.hash, db_handle(db_opts, :tx_account_nonce, %{}))
117-
TX.known_receivers(txu)
118-
|> Enum.each(fn(receiver)->
119-
RocksDB.put("#{receiver}:#{nonce_padded}", txu.hash, db_handle(db_opts, :tx_receiver_nonce, %{}))
120-
end)
121-
end
122-
end)
123-
else
124-
Enum.each(Enum.zip(entry.txs, logs), fn({txu, result})->
125-
case :binary.match(entry_packed, TX.pack(txu)) do
126-
{index_start, index_size} ->
127-
tx_ptr = %{entry_hash: entry.hash, result: result, index_start: index_start, index_size: index_size}
128-
|> RDB.vecpak_encode()
129-
RocksDB.put(txu.hash, tx_ptr, db_handle(db_opts, :tx, %{}))
130-
131-
nonce_padded = pad_integer_20(txu.tx.nonce)
132-
RocksDB.put("#{txu.tx.signer}:#{nonce_padded}", txu.hash, db_handle(db_opts, :tx_account_nonce, %{}))
133-
TX.known_receivers(txu)
134-
|> Enum.each(fn(receiver)->
135-
RocksDB.put("#{receiver}:#{nonce_padded}", txu.hash, db_handle(db_opts, :tx_receiver_nonce, %{}))
136-
end)
137-
end
138-
end)
139-
end
105+
receipts_by_txid = Map.new(receipts, fn r -> {r.txid, Map.drop(r, [:txid])} end)
106+
Enum.each(entry.txs, fn(txu)->
107+
receipt = Map.fetch!(receipts_by_txid, txu.hash)
108+
case :binary.match(entry_packed, TX.pack(txu)) do
109+
{index_start, index_size} ->
110+
tx_ptr = %{entry_hash: entry.hash, receipt: receipt, index_start: index_start, index_size: index_size}
111+
|> RDB.vecpak_encode()
112+
RocksDB.put(txu.hash, tx_ptr, db_handle(db_opts, :tx, %{}))
113+
114+
nonce_padded = pad_integer_20(txu.tx.nonce)
115+
RocksDB.put("#{txu.tx.signer}:#{nonce_padded}", txu.hash, db_handle(db_opts, :tx_account_nonce, %{}))
116+
TX.known_receivers(txu)
117+
|> Enum.each(fn(receiver)->
118+
RocksDB.put("#{receiver}:#{nonce_padded}", txu.hash, db_handle(db_opts, :tx_receiver_nonce, %{}))
119+
end)
120+
end
121+
end)
140122
end
141123

142124
def apply_into_main_chain_muts(hash, muts, db_opts = %{rtx: _}) do

ex/lib/consensus/fabric_gen.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ defmodule FabricGen do
347347

348348
RocksDB.put("temporal_tip", next_entry.hash, %{rtx: rtx, cf: cf.sysconf})
349349

350-
DB.Entry.apply_into_main_chain(next_entry, mutations_hash, m_rev, {l, receipts}, root_receipts, root_contractstate, %{rtx: rtx})
350+
DB.Entry.apply_into_main_chain(next_entry, mutations_hash, m_rev, receipts, root_receipts, root_contractstate, %{rtx: rtx})
351351
if Application.fetch_env!(:ama, :archival_node) do
352352
DB.Entry.apply_into_main_chain_muts(next_entry.hash, m, %{rtx: rtx})
353353
end

ex/lib/consensus/models/entry_genesis.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ defmodule EntryGenesis do
182182
pop = BlsEx.sign!(sk, pk, BLS12AggSig.dst_pop())
183183

184184
DB.Entry.insert(entry_signed, %{rtx: rtx})
185-
DB.Entry.apply_into_main_chain(entry_signed, mutations_hash, [], {[], []}, "", "", %{rtx: rtx})
185+
DB.Entry.apply_into_main_chain(entry_signed, mutations_hash, [], [], "", "", %{rtx: rtx})
186186
RocksDB.put("temporal_tip", entry_signed.hash, %{rtx: rtx, cf: cf.sysconf})
187187
RocksDB.put("rooted_tip", entry_signed.hash, %{rtx: rtx, cf: cf.sysconf})
188188

0 commit comments

Comments
 (0)