8000 V0.12 fixes by dr-orlovsky · Pull Request #1 · bitlightlabs/rgb-tests · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

V0.12 fixes #1

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

Merged
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
179 changes: 123 additions & 56 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion bp-electrum-client
8000
Submodule bp-electrum-client updated 1 files
+2 −2 Cargo.toml
2 changes: 1 addition & 1 deletion bp-std
Submodule bp-std updated 2 files
+107 −32 Cargo.lock
+9 −15 Cargo.toml
2 changes: 1 addition & 1 deletion bp-wallet
2 changes: 1 addition & 1 deletion rgb
Submodule rgb updated 7 files
+163 −103 Cargo.lock
+12 −35 Cargo.toml
+9 −5 cli/src/cmd.rs
+31 −24 cli/src/exec.rs
+3 −0 src/lib.rs
+40 −0 src/payment.rs
+40 −18 src/runtime.rs
2 changes: 1 addition & 1 deletion rgb-core
Submodule rgb-core updated 4 files
+217 −96 Cargo.lock
+5 −12 Cargo.toml
+1 −1 src/lib.rs
+61 −52 src/verify.rs
2 changes: 1 addition & 1 deletion rgb-interfaces
Submodule rgb-interfaces updated 3 files
+13 −11 Cargo.lock
+5 −8 Cargo.toml
+14 −16 src/types.rs
2 changes: 1 addition & 1 deletion rgb-std
2 changes: 1 addition & 1 deletion rust-aluvm
2 changes: 1 addition & 1 deletion sonic
6D47
105 changes: 34 additions & 71 deletions tests/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ fn simple_transfer(wout: bool) {
// send asset to wlt2
// if `wout` is true (WitnessOut),
// wlt2 will have a 3000 Sats UTXO, which will be spent to transfer assets to wlt1 in the next step
let (consignment_1, tx) = wlt_1.transfer(invoice, Some(3000), Some(500), true, None);
let (consignment_1, tx, _) = wlt_1.transfer(invoice, Some(3000), Some(500), true, None);

// Receiver accepts the transfer
wlt_2.accept_transfer(&consignment_1, None).unwrap();
Expand All @@ -99,7 +99,7 @@ fn simple_transfer(wout: bool) {
wlt_2.runtime().wallet.coins().collect::<Vec<_>>()
);
// Sats cost: 500 fee + 2000 sats(default) = 2500
let (consignment_2, tx) = wlt_2.transfer(invoice, None, Some(500), true, None);
let (consignment_2, tx, _) = wlt_2.transfer(invoice, None, Some(500), true, None);
wlt_1.accept_transfer(&consignment_2, None).unwrap();
wlt_2.mine_tx(&tx.txid(), false);

Expand All @@ -120,11 +120,6 @@ fn simple_transfer(wout: bool) {
}

#[test]
// FIXME:
// called `Result::unwrap()` on an `Err` value: Fulfill(StateInsufficient)
//
// RBF transfer fails in the second asset transfer,
// Likely due to the inability to spend the same asset balance twice.
fn rbf_transfer() {
initialize();

Expand All @@ -146,8 +141,9 @@ fn rbf_transfer() {
stop_mining();
let initial_height = get_height();

// First transfer attempt - with lower fee
let (consignment_1, _) = wlt_1.transfer(invoice.clone(), None, Some(500), true, None);
// First transfer attempt - with a lower fee
let (consignment_1, _tx, payment) =
wlt_1.transfer(invoice.clone(), None, Some(500), true, None);

// Receiver accepts the transfer
wlt_2.accept_transfer(&consignment_1, None).unwrap();
Expand All @@ -156,8 +152,8 @@ fn rbf_transfer() {
let mid_height = get_height();
assert_eq!(initial_height, mid_height);

// Second transfer attempt - with higher fee for RBF
let (consignment_2, tx) = wlt_1.transfer(invoice, None, Some(1000), true, None);
// Second transfer attempt - with a higher fee for RBF
let (consignment_2, tx) = wlt_1.transfer_rbf(contract_id, payment, 1000, None);

// Verify block height still hasn't changed
let final_height = get_height();
Expand Down Expand Up @@ -556,19 +552,13 @@ fn collaborative_transfer() {
}

#[rstest]
#[should_panic(expected = "unknown seal definition for cell address")]
#[case(TransferType::Blinded)]
#[should_panic(expected = "Fulfill(StateInsufficient)")]
#[case(TransferType::Witness)]
fn same_transfer_twice_no_update_witnesses(#[case] transfer_type: TransferType) {
println!("transfer_type {transfer_type:?}");

// TODO: This test case aims to verify if asset transfers are handled correctly when using RBF (Replace-By-Fee) with the same invoice
// In RGB V0.11, there was an inflation attack vulnerability where using RBF with the same invoice would cause the receiver to record two receive states,
// while the sender only paid the assets once. This resulted in the total circulating assets exceeding the issued amount.
//
// In RGB V0.12, since it's not possible to use RBF with the same invoice, we cannot test for this inflation attack.
// NOTE: When paying the same invoice for the second time, the error `called `Result::unwrap()` on an `Err` value: Fulfill(StateInsufficient)` occurs
// Need to consult with Dr. Maxim on how to construct RBF asset transfer examples using the current API.

initialize();

let mut wlt_1 = get_wallet(&DescriptorType::Wpkh);
Expand All @@ -589,36 +579,18 @@ fn same_transfer_twice_no_update_witnesses(#[case] transfer_type: TransferType)
TransferType::Witness => true,
};
let invoice = wlt_2.invoice(contract_id, amount, wout, Some(0), None);
let _ = wlt_1.transfer(invoice.clone(), None, Some(500), false, None);

// dbg!(wlt_1
// .runtime()
// .state_all(None)
// .map(|(_, s)| { s.owned })
// .collect::<Vec<_>>());
// dbg!(wlt_1
// .runtime()
// .state_own(None)
// .map(|(_, s)| { s.owned })
// .collect::<Vec<_>>());

// dbg!(wlt_2
// .runtime()
// .state_all(None)
// .map(|(c, s)| { s.owned })
// .collect::<Vec<_>>());
// dbg!(wlt_2
// .runtime()
// .state_own(None)
// .map(|(c, s)| { s.owned })
// .collect::<Vec<_>>());
let (_, _tx, payment) = wlt_1.transfer(invoice.clone(), None, Some(500), false, None);

// TODO: called `Result::unwrap()` on an `Err` value: Fulfill(StateInsufficient)
let (consignment, _) = wlt_1.transfer(invoice, None, Some(1000), true, None);
let (consignment, _) = wlt_1.transfer_rbf(contract_id, payment, 1000, None);

wlt_2.accept_transfer(&consignment, None).unwrap();

wlt_2.check_allocations(contract_id, AssetSchema::RGB20, vec![amount]);
if transfer_type == TransferType::Blinded {
wlt_2.check_allocations(contract_id, AssetSchema::RGB20, vec![amount]);
} else {
// Since the receiver state is not updated, it treats the witness transaction as not mined and thus has no state
wlt_2.check_allocations(contract_id, AssetSchema::RGB20, vec![]);
}

wlt_2.send(
&mut wlt_1,
Expand All @@ -635,6 +607,7 @@ fn same_transfer_twice_no_update_witnesses(#[case] transfer_type: TransferType)
wlt_1.send_contract("TestAsset", &mut wlt_3);
wlt_3.reload_runtime();

// The receiver will fail to accept the consignment
wlt_1.send(
&mut wlt_3,
wout,
Expand Down Expand Up @@ -665,7 +638,7 @@ fn accept_0conf() {

let amt = 200;
let invoice = wlt_2.invoice(contract_id, amt, true, Some(0), None);
let (consignment, tx) = wlt_1.transfer(invoice.clone(), None, None, true, None);
let (consignment, tx, _) = wlt_1.transfer(invoice.clone(), None, None, true, None);
let txid = tx.txid();

wlt_2.accept_transfer(&consignment, None).unwrap();
Expand Down Expand Up @@ -740,7 +713,7 @@ fn check_fungible_history() {

// transfer
let amt = 200;
let (_, tx) = wlt_1.send(&mut wlt_2, true, contract_id, amt, 1000, None, None, None);
let (_, tx, _) = wlt_1.send(&mut wlt_2, true, contract_id, amt, 1000, None, None, None);
let _txid = tx.txid();

// debug contract state
Expand All @@ -767,7 +740,7 @@ fn send_to_oneself() {
// Transfer 200 to yourself
let amt = 200;
let invoice = wlt.invoice(contract_id, amt, true, Some(0), None);
let (consignment, tx) = wlt.transfer(invoice.clone(), None, None, true, None);
let (consignment, tx, _) = wlt.transfer(invoice.clone(), None, None, true, None);
wlt.mine_tx(&tx.txid(), false);
wlt.accept_transfer(&consignment, None).unwrap();
wlt.sync();
Expand Down Expand Up @@ -931,7 +904,7 @@ fn reorg_history(#[case] history_type: HistoryType, #[case] reorg_type: ReorgTyp
let amt_0 = 590;
// Create blinded invoice with specific UTXO
let invoice = wlt_2.invoice(contract_id, amt_0, false, Some(0), Some(utxo_wlt_2_1));
let (_, tx_0) = wlt_1.send_to_invoice(&mut wlt_2, invoice, Some(1000), None, None);
let (_, tx_0, _) = wlt_1.send_to_invoice(&mut wlt_2, invoice, Some(1000), None, None);
// dbg!(wlt_1
// .runtime()
// .state_own(Some(contract_id))
Expand All @@ -940,7 +913,7 @@ fn reorg_history(#[case] history_type: HistoryType, #[case] reorg_type: ReorgTyp

let amt_1 = 100;
let invoice = wlt_1.invoice(contract_id, amt_1, false, Some(0), Some(utxo_wlt_1_1));
let (_, tx_1) = wlt_2.send_to_invoice(&mut wlt_1, invoice, Some(1000), None, None);
let (_, tx_1, _) = wlt_2.send_to_invoice(&mut wlt_1, invoice, Some(1000), None, None);
// dbg!(wlt_1
// .runtime()
// .state_own(Some(contract_id))
Expand All @@ -949,37 +922,37 @@ fn reorg_history(#[case] history_type: HistoryType, #[case] reorg_type: ReorgTyp

let amt_2 = 80;
let invoice = wlt_2.invoice(contract_id, amt_2, false, Some(0), Some(utxo_wlt_2_2));
let (_, tx_2) = wlt_1.send_to_invoice(&mut wlt_2, invoice, Some(1000), None, None);
let (_, tx_2, _) = wlt_1.send_to_invoice(&mut wlt_2, invoice, Some(1000), None, None);

vec![tx_0, tx_1, tx_2]
}
HistoryType::Branching => {
let amt_0 = 600;
let invoice = wlt_2.invoice(contract_id, amt_0, false, Some(0), Some(utxo_wlt_2_1));
let (_, tx_0) = wlt_1.send_to_invoice(&mut wlt_2, invoice, Some(1000), None, None);
let (_, tx_0, _) = wlt_1.send_to_invoice(&mut wlt_2, invoice, Some(1000), None, None);

let amt_1 = 200;
let invoice = wlt_1.invoice(contract_id, amt_1, false, Some(0), Some(utxo_wlt_1_1));
let (_, tx_1) = wlt_2.send_to_invoice(&mut wlt_1, invoice, Some(1000), None, None);
let (_, tx_1, _) = wlt_2.send_to_invoice(&mut wlt_1, invoice, Some(1000), None, None);

let amt_2 = amt_0 - amt_1 - 1;
let invoice = wlt_1.invoice(contract_id, amt_2, false, Some(0), Some(utxo_wlt_1_2));
let (_, tx_2) = wlt_2.send_to_invoice(&mut wlt_1, invoice, Some(1000), None, None);
let (_, tx_2, _) = wlt_2.send_to_invoice(&mut wlt_1, invoice, Some(1000), None, None);

vec![tx_0, tx_1, tx_2]
}
HistoryType::Merging => {
let amt_0 = 400;
let invoice = wlt_2.invoice(contract_id, amt_0, false, Some(0), Some(utxo_wlt_2_1));
let (_, tx_0) = wlt_1.send_to_invoice(&mut wlt_2, invoice, None, None, None);
let (_, tx_0, _) = wlt_1.send_to_invoice(&mut wlt_2, invoice, None, None, None);

let amt_1 = 200;
let invoice = wlt_2.invoice(contract_id, amt_1, false, Some(0), Some(utxo_wlt_2_2));
let (_, tx_1) = wlt_1.send_to_invoice(&mut wlt_2, invoice, None, None, None);
let (_, tx_1, _) = wlt_1.send_to_invoice(&mut wlt_2, invoice, None, None, None);

let amt_2 = amt_0 + amt_1 - 1;
let invoice = wlt_1.invoice(contract_id, amt_2, false, Some(0), Some(utxo_wlt_1_1));
let (_, tx_2) = wlt_2.send_to_invoice(&mut wlt_1, invoice, None, None, None);
let (_, tx_2, _) = wlt_2.send_to_invoice(&mut wlt_1, invoice, None, None, None);

vec![tx_0, tx_1, tx_2]
}
Expand Down Expand Up @@ -1286,7 +1259,7 @@ fn mainnet_wlt_receiving_test_asset() {
.unwrap();
let invoice = wlt_2.invoice(contract_id, 150, false, None, Some(utxo));

let (consignment, tx) = wlt_1.transfer(invoice.clone(), None, Some(500), true, None);
let (consignment, tx, _) = wlt_1.transfer(invoice.clone(), None, Some(500), true, None);
wlt_1.mine_tx(&tx.txid(), false);
match wlt_2.accept_transfer(&consignment, None) {
Err(e) => {
Expand Down Expand Up @@ -1325,21 +1298,11 @@ fn invoice_reuse(#[case] transfer_type: TransferType) {
wlt_1.send_to_invoice(&mut wlt_2, invoice.clone(), Some(500), None, None);

// Second use same invoice
let (_, _) = wlt_1.send_to_invoice(&mut wlt_2, invoice, Some(600), None, None);

// FIXME: There is a question here,
// should the two transfers of the same invoice be processed as one transfer by RBF,
// or should they correspond to two transfers?
let (_, _, _) = wlt_1.send_to_invoice(&mut wlt_2, invoice, Some(600), None, None);

// Check asset allocations
wlt_1.check_allocations(contract_id, AssetSchema::RGB20, vec![100, 200]);
wlt_2.check_allocations(contract_id, AssetSchema::RGB20, vec![amount, amount]);

// FIXME: The following code needs to be redesigned in RGB v0.12
// Note: In RGB v0.12, the type of consignment has been changed to PathBuf, no longer directly containing the bundles field
// This test needs to be redesigned in RGB v0.12

// The original test assertion: assert_eq!(consignment.bundles.len(), 1);
}

#[test]
Expand Down Expand Up @@ -1401,7 +1364,7 @@ fn receive_from_unbroadcasted_transfer_to_blinded() {
let invoice = wlt_2.invoice(contract_id, 100, false, None, Some(utxo));

// Create transfer but do not broadcast its TX
let (consignment, _tx) = wlt_1.transfer(invoice.clone(), None, Some(500), false, None);
let (consignment, _tx, _) = wlt_1.transfer(invoice.clone(), None, Some(500), false, None);
wlt_2.accept_transfer(&consignment, None).unwrap();

// The following three lines are debug code,
Expand All @@ -1414,7 +1377,7 @@ fn receive_from_unbroadcasted_transfer_to_blinded() {
dbg!(wlt_2.runtime().state_own(contract_id).owned);

let invoice = wlt_3.invoice(contract_id, 50, true, None, None);
let (consignment, tx) = wlt_2.transfer(invoice, Some(2000), None, true, None);
let (consignment, tx, _) = wlt_2.transfer(invoice, Some(2000), None, true, None);
wlt_2.mine_tx(&tx.txid(), false);
wlt_2.sync();
wlt_1.sync();
Expand Down
4 changes: 2 additions & 2 deletions tests/transfer_rgb21.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ fn simple_fac_transfer(wout: bool) {
// Transfer some fractions to wallet 2
let transfer_amount = 1001;
let invoice = wlt_2.invoice(contract_id, transfer_amount, wout, Some(0), None);
let (consignment_1, tx) = wlt_1.transfer(invoice, Some(9000), Some(500), true, None);
let (consignment_1, tx, _) = wlt_1.transfer(invoice, Some(9000), Some(500), true, None);

// Receiver accepts the transfer
wlt_2.accept_transfer(&consignment_1, None).unwrap();
Expand Down Expand Up @@ -139,7 +139,7 @@ fn simple_fac_transfer(wout: bool) {
// Test transferring some fractions back to wallet 1
let return_amount = 1000;
let invoice = wlt_1.invoice(contract_id, return_amount, wout, Some(0), None);
540C let (consignment_2, tx) = wlt_2.transfer(invoice, Some(3000), Some(500), true, None);
let (consignment_2, tx, _) = wlt_2.transfer(invoice, Some(3000), Some(500), true, None);

// Wallet 1 accepts the transfer
wlt_1.accept_transfer(&consignment_2, None).unwrap();
Expand Down
Loading
0