Skip to content

Commit

Permalink
Check ConcludeOrder amounts
Browse files Browse the repository at this point in the history
  • Loading branch information
azarovh committed Mar 4, 2025
1 parent 7ac5b97 commit fcf7fc6
Show file tree
Hide file tree
Showing 19 changed files with 173 additions and 99 deletions.
12 changes: 6 additions & 6 deletions api-server/scanner-lib/src/blockchain_state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -651,8 +651,8 @@ async fn calculate_tx_fee_and_collect_token_info<T: ApiServerStorageWrite>(
OrderAccountCommand::FillOrder(order_id, _, _)
| OrderAccountCommand::ConcludeOrder {
order_id,
ask_balance: _,
give_balance: _,
filled_amount: _,
remaining_give_amount: _,
} => {
let order = db_tx.get_order(*order_id).await?.expect("must exist");
match order.ask_currency {
Expand Down Expand Up @@ -934,8 +934,8 @@ async fn prefetch_orders<T: ApiServerStorageRead>(
OrderAccountCommand::FillOrder(order_id, _, _)
| OrderAccountCommand::ConcludeOrder {
order_id,
ask_balance: _,
give_balance: _,
filled_amount: _,
remaining_give_amount: _,
} => {
let order = db_tx.get_order(*order_id).await?.expect("must be present ");
let order_data = to_order_data(&order);
Expand Down Expand Up @@ -1328,8 +1328,8 @@ async fn update_tables_from_transaction_inputs<T: ApiServerStorageWrite>(
}
OrderAccountCommand::ConcludeOrder {
order_id,
ask_balance: _,
give_balance: _,
filled_amount: _,
remaining_give_amount: _,
} => {
let order = db_tx.get_order(*order_id).await?.expect("must exist");
let order = order.conclude();
Expand Down
4 changes: 2 additions & 2 deletions api-server/stack-test-suite/tests/v2/orders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ async fn create_fill_conclude_order(#[case] seed: Seed) {
.add_input(
TxInput::OrderAccountCommand(OrderAccountCommand::ConcludeOrder {
order_id,
ask_balance: Amount::from_atoms(1),
give_balance: Amount::from_atoms(9),
filled_amount: Amount::from_atoms(1),
remaining_give_amount: Amount::from_atoms(9),
}),
InputWitness::NoSignature(None),
)
Expand Down
8 changes: 4 additions & 4 deletions api-server/web-server/src/api/json_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,15 +330,15 @@ pub fn tx_input_to_json(inp: &TxInput, chain_config: &ChainConfig) -> serde_json
}
OrderAccountCommand::ConcludeOrder {
order_id,
ask_balance,
give_balance,
filled_amount,
remaining_give_amount,
} => {
json!({
"input_type": "OrderAccountCommand",
"command": "ConcludeOrder",
"order_id": Address::new(chain_config, *order_id).expect("addressable").to_string(),
"ask_atoms": json!({"atoms": ask_balance.into_atoms().to_string()}),
"give_atoms": json!({"atoms": give_balance.into_atoms().to_string()}),
"filled_atoms": json!({"atoms": filled_amount.into_atoms().to_string()}),
"remaining_give_atoms": json!({"atoms": remaining_give_amount.into_atoms().to_string()}),
})
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -338,8 +338,8 @@ impl ConstrainedValueAccumulator {
),
OrderAccountCommand::ConcludeOrder {
order_id,
ask_balance: _,
give_balance: _,
filled_amount: _,
remaining_give_amount: _,
} => self.process_conclude_order_command(*order_id, orders_accounting_delta),
}
}
Expand Down
1 change: 1 addition & 0 deletions chainstate/src/detail/ban_score.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ impl BanScore for ConnectTransactionError {
ConnectTransactionError::InputCheck(e) => e.ban_score(),
ConnectTransactionError::OrdersAccountingError(err) => err.ban_score(),
ConnectTransactionError::AttemptToCreateOrderFromAccounts => 100,
ConnectTransactionError::ConcludeInputAmountsDoesntMatch(_, _) => 100,
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion chainstate/src/detail/error_classification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,8 @@ impl BlockProcessingErrorClassification for ConnectTransactionError {
| ConnectTransactionError::IOPolicyError(_, _)
| ConnectTransactionError::TotalFeeRequiredOverflow
| ConnectTransactionError::InsufficientCoinsFee(_, _)
| ConnectTransactionError::AttemptToSpendFrozenToken(_) => {
| ConnectTransactionError::AttemptToSpendFrozenToken(_)
| ConnectTransactionError::ConcludeInputAmountsDoesntMatch(_, _) => {
BlockProcessingErrorClass::BadBlock
}

Expand Down
17 changes: 10 additions & 7 deletions chainstate/src/rpc/types/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ impl RpcAccountCommand {
pub enum RpcOrderAccountCommand {
ConcludeOrder {
order_id: RpcAddress<OrderId>,
ask_balance: RpcAmountOut,
give_balance: RpcAmountOut,
filled_amount: RpcAmountOut,
remaining_give_amount: RpcAmountOut,
},
FillOrder {
order_id: RpcAddress<OrderId>,
Expand All @@ -159,13 +159,16 @@ impl RpcOrderAccountCommand {
let result = match command {
OrderAccountCommand::ConcludeOrder {
order_id,
ask_balance,
give_balance,
filled_amount,
remaining_give_amount,
} => RpcOrderAccountCommand::ConcludeOrder {
order_id: RpcAddress::new(chain_config, *order_id)?,
ask_balance: RpcAmountOut::from_amount(*ask_balance, chain_config.coin_decimals()),
give_balance: RpcAmountOut::from_amount(
*give_balance,
filled_amount: RpcAmountOut::from_amount(
*filled_amount,
chain_config.coin_decimals(),
),
remaining_give_amount: RpcAmountOut::from_amount(
*remaining_give_amount,
chain_config.coin_decimals(),
),
},
Expand Down
4 changes: 2 additions & 2 deletions chainstate/test-framework/src/random_tx_maker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -536,8 +536,8 @@ impl<'a> RandomTxMaker<'a> {
result_inputs.push(TxInput::OrderAccountCommand(
OrderAccountCommand::ConcludeOrder {
order_id,
ask_balance: current_ask_balance,
give_balance: filled_amount,
filled_amount,
remaining_give_amount: available_give_balance,
},
));

Expand Down
4 changes: 2 additions & 2 deletions chainstate/test-framework/src/signature_destination_getter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,8 @@ impl<'a> SignatureDestinationGetter<'a> {
OrderAccountCommand::FillOrder(_, _, d) => Ok(d.clone()),
OrderAccountCommand::ConcludeOrder {
order_id,
ask_balance: _,
give_balance: _,
filled_amount: _,
remaining_give_amount: _,
} => {
let order_data = orders_view
.get_order_data(order_id)
Expand Down
91 changes: 53 additions & 38 deletions chainstate/test-suite/src/tests/orders_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -487,8 +487,8 @@ fn conclude_order_check_storage(#[case] seed: Seed, #[case] version: OrdersVersi
),
OrdersVersion::V1 => TxInput::OrderAccountCommand(OrderAccountCommand::ConcludeOrder {
order_id,
ask_balance: ask_amount,
give_balance: give_amount,
filled_amount: Amount::ZERO,
remaining_give_amount: give_amount,
}),
};
let tx = TransactionBuilder::new()
Expand Down Expand Up @@ -549,8 +549,8 @@ fn conclude_order_multiple_txs(#[case] seed: Seed, #[case] version: OrdersVersio
),
OrdersVersion::V1 => TxInput::OrderAccountCommand(OrderAccountCommand::ConcludeOrder {
order_id,
ask_balance: ask_amount,
give_balance: give_amount,
filled_amount: Amount::ZERO,
remaining_give_amount: give_amount,
}),
};
let tx1 = TransactionBuilder::new()
Expand All @@ -568,8 +568,8 @@ fn conclude_order_multiple_txs(#[case] seed: Seed, #[case] version: OrdersVersio
),
OrdersVersion::V1 => TxInput::OrderAccountCommand(OrderAccountCommand::ConcludeOrder {
order_id,
ask_balance: (ask_amount - Amount::from_atoms(1)).unwrap(), // avoid duplicating input in a block
give_balance: give_amount,
filled_amount: Amount::ZERO,
remaining_give_amount: give_amount,
}),
};
let tx2 = TransactionBuilder::new()
Expand All @@ -581,22 +581,37 @@ fn conclude_order_multiple_txs(#[case] seed: Seed, #[case] version: OrdersVersio
.build();
let tx2_id = tx2.transaction().get_id();

let res = tf
.make_block_builder()
.with_transactions(vec![tx1, tx2])
.build_and_process(&mut rng);
let block = tf.make_block_builder().with_transactions(vec![tx1, tx2]).build(&mut rng);
let block_id = block.get_id();
let res = tf.process_block(block, chainstate::BlockSource::Local);

assert_eq!(
res.unwrap_err(),
chainstate::ChainstateError::ProcessBlockError(
chainstate::BlockError::StateUpdateFailed(
ConnectTransactionError::ConstrainedValueAccumulatorError(
orders_accounting::Error::OrderDataNotFound(order_id).into(),
tx2_id.into()
match version {
OrdersVersion::V0 => {
assert_eq!(
res.unwrap_err(),
chainstate::ChainstateError::ProcessBlockError(
chainstate::BlockError::StateUpdateFailed(
ConnectTransactionError::ConstrainedValueAccumulatorError(
orders_accounting::Error::OrderDataNotFound(order_id).into(),
tx2_id.into()
)
)
)
)
)
);
);
}
OrdersVersion::V1 => {
assert_eq!(
res.unwrap_err(),
chainstate::ChainstateError::ProcessBlockError(
chainstate::BlockError::CheckBlockFailed(
chainstate::CheckBlockError::CheckTransactionFailed(
CheckBlockTransactionsError::DuplicateInputInBlock(block_id)
)
)
)
);
}
}
});
}

Expand Down Expand Up @@ -814,8 +829,8 @@ fn fill_partially_then_conclude(#[case] seed: Seed, #[case] version: OrdersVersi
OrdersVersion::V1 => {
TxInput::OrderAccountCommand(OrderAccountCommand::ConcludeOrder {
order_id,
ask_balance: ask_amount,
give_balance: give_amount,
filled_amount: fill_amount,
remaining_give_amount: (give_amount - filled_amount).unwrap(),
})
}
};
Expand Down Expand Up @@ -859,8 +874,8 @@ fn fill_partially_then_conclude(#[case] seed: Seed, #[case] version: OrdersVersi
OrdersVersion::V1 => {
TxInput::OrderAccountCommand(OrderAccountCommand::ConcludeOrder {
order_id,
ask_balance: ask_amount,
give_balance: give_amount,
filled_amount: fill_amount,
remaining_give_amount: (give_amount - filled_amount).unwrap(),
})
}
};
Expand Down Expand Up @@ -897,8 +912,8 @@ fn fill_partially_then_conclude(#[case] seed: Seed, #[case] version: OrdersVersi
),
OrdersVersion::V1 => TxInput::OrderAccountCommand(OrderAccountCommand::ConcludeOrder {
order_id,
ask_balance: ask_amount,
give_balance: give_amount,
filled_amount: fill_amount,
remaining_give_amount: (give_amount - filled_amount).unwrap(),
}),
};
let tx = TransactionBuilder::new()
Expand Down Expand Up @@ -1232,8 +1247,8 @@ fn fill_completely_then_conclude(#[case] seed: Seed, #[case] version: OrdersVers
OrdersVersion::V1 => {
TxInput::OrderAccountCommand(OrderAccountCommand::ConcludeOrder {
order_id,
ask_balance: ask_amount,
give_balance: give_amount,
filled_amount: ask_amount,
remaining_give_amount: Amount::ZERO,
})
}
};
Expand Down Expand Up @@ -1266,8 +1281,8 @@ fn fill_completely_then_conclude(#[case] seed: Seed, #[case] version: OrdersVers
),
OrdersVersion::V1 => TxInput::OrderAccountCommand(OrderAccountCommand::ConcludeOrder {
order_id,
ask_balance: ask_amount,
give_balance: give_amount,
filled_amount: ask_amount,
remaining_give_amount: Amount::ZERO,
}),
};
let tx = TransactionBuilder::new()
Expand Down Expand Up @@ -1333,8 +1348,8 @@ fn conclude_order_check_signature(#[case] seed: Seed, #[case] version: OrdersVer
OrdersVersion::V1 => {
TxInput::OrderAccountCommand(OrderAccountCommand::ConcludeOrder {
order_id,
ask_balance: ask_amount,
give_balance: give_amount,
filled_amount: Amount::ZERO,
remaining_give_amount: give_amount,
})
}
};
Expand Down Expand Up @@ -1370,8 +1385,8 @@ fn conclude_order_check_signature(#[case] seed: Seed, #[case] version: OrdersVer
OrdersVersion::V1 => {
TxInput::OrderAccountCommand(OrderAccountCommand::ConcludeOrder {
order_id,
ask_balance: ask_amount,
give_balance: give_amount,
filled_amount: Amount::ZERO,
remaining_give_amount: give_amount,
})
}
};
Expand Down Expand Up @@ -1428,8 +1443,8 @@ fn conclude_order_check_signature(#[case] seed: Seed, #[case] version: OrdersVer
),
OrdersVersion::V1 => TxInput::OrderAccountCommand(OrderAccountCommand::ConcludeOrder {
order_id,
ask_balance: ask_amount,
give_balance: give_amount,
filled_amount: Amount::ZERO,
remaining_give_amount: give_amount,
}),
};
let tx = TransactionBuilder::new()
Expand Down Expand Up @@ -1645,8 +1660,8 @@ fn reorg_after_create(#[case] seed: Seed, #[case] version: OrdersVersion) {
),
OrdersVersion::V1 => TxInput::OrderAccountCommand(OrderAccountCommand::ConcludeOrder {
order_id,
ask_balance: ask_amount,
give_balance: give_amount,
filled_amount: fill_amount,
remaining_give_amount: (give_amount - filled_amount).unwrap(),
}),
};
tf.make_block_builder()
Expand Down
2 changes: 2 additions & 0 deletions chainstate/tx-verifier/src/transaction_verifier/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ pub enum ConnectTransactionError {
OrdersAccountingError(#[from] orders_accounting::Error),
#[error(transparent)]
InputCheck(#[from] InputCheckError),
#[error("Transaction {0} has conclude order input {1} with amounts that doesn't match the db")]
ConcludeInputAmountsDoesntMatch(Id<Transaction>, OrderId),
}

impl From<std::convert::Infallible> for ConnectTransactionError {
Expand Down
Loading

0 comments on commit fcf7fc6

Please sign in to comment.