Skip to content
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

Orders accounting #1740

Merged
merged 37 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from 34 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
317d51c
Add basic types
azarovh Apr 28, 2024
7e4f886
Pass PoSAccountingView to ConstraitsAccumulator
azarovh Apr 29, 2024
2e202e8
Add storage in orders-accounting
azarovh Apr 29, 2024
6e28bed
Withdraw command initial impl
azarovh Apr 29, 2024
2b2c7a9
Move OrderData to common
azarovh Apr 30, 2024
93287c3
FillOrder command initial impl
azarovh Apr 30, 2024
b9951e9
Add TxOutput::CreateOrder
azarovh Apr 30, 2024
121b3f6
Complete order operations
azarovh Apr 30, 2024
2e81d52
Add tests for order constraints
azarovh May 1, 2024
b6b361c
Initial chainstate integration
azarovh May 1, 2024
0124113
Integrate into tx-verifier
azarovh May 2, 2024
815be09
Impl signature getter
azarovh May 2, 2024
3132483
Support chainstate storage
azarovh May 2, 2024
1b3a33a
More chainstate tests
azarovh May 2, 2024
9dea16e
Fix purposes tests
azarovh May 5, 2024
7fdf792
Fix bunch of todos
azarovh May 5, 2024
e61f50f
Rename to TxOutput::AnyoneCanTake
azarovh May 5, 2024
ae37959
Stub api-server and wallet
azarovh May 5, 2024
6493c3d
Fix clippy
azarovh May 5, 2024
036d37a
Check for activation and same currencies
azarovh May 7, 2024
8c83480
Rename withdraw and add some docs
azarovh May 7, 2024
ec02d7c
Avoid panicing on tokens v0
azarovh May 9, 2024
3a5d960
Add tests and minor fixes
azarovh May 10, 2024
fabea97
Support orders in RandomTxMaker
azarovh May 17, 2024
53bf148
Disable orders for api-server simulation
azarovh May 17, 2024
eeabbd4
Fix mint limit in tests
azarovh May 20, 2024
93400fa
Improve simulation test
azarovh May 21, 2024
2d9c9e7
WIP: tranlate to mintscript
azarovh Jun 21, 2024
3c8de65
Add translation tests
azarovh Jun 23, 2024
fc25b9b
Add additional tests
azarovh Jun 23, 2024
ddbecb6
Isolate output_value_amount function
azarovh Jun 23, 2024
03da404
Solve Some(0) problem
azarovh Jun 23, 2024
7282604
Fix api simulation test
azarovh Jun 24, 2024
8fe0684
Fix clippy
azarovh Jun 24, 2024
e5c4702
Rename cancel order to conclude
azarovh Jun 26, 2024
2ba828d
Fix complete order fill
azarovh Jun 26, 2024
323a569
Fix review comments
azarovh Jun 27, 2024
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
29 changes: 29 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ members = [
"networking", # Pure networking implementations
"node-gui", # Node GUI binary.
"node-lib", # Node lib; the common library between daemon, tui and gui node executables.
"orders-accounting", # Orders accounting
"p2p", # P2p communication interfaces and protocols.
"p2p/backend-test-suite", # P2p backend agnostic tests.
"p2p/types", # P2p support types with minimal dependencies.
Expand Down
1 change: 1 addition & 0 deletions api-server/scanner-lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ constraints-value-accumulator = { path = "../../chainstate/constraints-value-acc
logging = { path = "../../logging" }
mempool = { path = "../../mempool" }
node-comm = { path = "../../wallet/wallet-node-client" }
orders-accounting = { path = "../../orders-accounting" }
pos-accounting = { path = "../../pos-accounting" }
randomness = { path = "../../randomness" }
utils = { path = "../../utils" }
Expand Down
108 changes: 84 additions & 24 deletions api-server/scanner-lib/src/blockchain_state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,8 @@ async fn update_tables_from_block_reward<T: ApiServerStorageWrite>(
| TxOutput::DelegateStaking(_, _)
| TxOutput::IssueFungibleToken(_)
| TxOutput::IssueNft(_, _, _)
| TxOutput::Htlc(_, _) => {}
| TxOutput::Htlc(_, _)
| TxOutput::AnyoneCanTake(_) => {}
TxOutput::ProduceBlockFromStake(_, _) => {
set_utxo(
outpoint,
Expand Down Expand Up @@ -571,7 +572,8 @@ async fn calculate_fees<T: ApiServerStorageWrite>(
| TxOutput::DelegateStaking(_, _)
| TxOutput::CreateDelegationId(_, _)
| TxOutput::ProduceBlockFromStake(_, _)
| TxOutput::Htlc(_, _) => None,
| TxOutput::Htlc(_, _)
| TxOutput::AnyoneCanTake(_) => None,
})
})
.collect();
Expand Down Expand Up @@ -604,7 +606,8 @@ async fn calculate_fees<T: ApiServerStorageWrite>(
| TxOutput::CreateDelegationId(_, _)
| TxOutput::IssueFungibleToken(_)
| TxOutput::ProduceBlockFromStake(_, _)
| TxOutput::Htlc(_, _) => None,
| TxOutput::Htlc(_, _)
| TxOutput::AnyoneCanTake(_) => None,
},
TxInput::Account(_) => None,
TxInput::AccountCommand(_, cmd) => match cmd {
Expand All @@ -614,6 +617,7 @@ async fn calculate_fees<T: ApiServerStorageWrite>(
| AccountCommand::UnfreezeToken(token_id)
| AccountCommand::LockTokenSupply(token_id)
| AccountCommand::ChangeTokenAuthority(token_id, _) => Some(*token_id),
AccountCommand::CancelOrder(_) | AccountCommand::FillOrder(_, _, _) => None,
},
})
.collect();
Expand Down Expand Up @@ -672,6 +676,56 @@ async fn token_decimals<T: ApiServerStorageRead>(
Ok((token_id, decimals))
}

struct PoSAccountingAdapterToCheckFees {
pools: BTreeMap<PoolId, PoolData>,
}

impl PoSAccountingView for PoSAccountingAdapterToCheckFees {
type Error = pos_accounting::Error;

fn pool_exists(&self, _pool_id: PoolId) -> Result<bool, Self::Error> {
unimplemented!()
}

fn get_pool_balance(&self, _pool_id: PoolId) -> Result<Option<Amount>, Self::Error> {
unimplemented!()
}

fn get_pool_data(&self, pool_id: PoolId) -> Result<Option<PoolData>, Self::Error> {
Ok(self.pools.get(&pool_id).cloned())
}

fn get_pool_delegations_shares(
&self,
_pool_id: PoolId,
) -> Result<Option<BTreeMap<DelegationId, Amount>>, Self::Error> {
unimplemented!()
}

fn get_delegation_balance(
&self,
_delegation_id: DelegationId,
) -> Result<Option<Amount>, Self::Error> {
// only used for checks for attempted to print money but we don't need to check that here
Ok(Some(Amount::MAX))
}

fn get_delegation_data(
&self,
_delegation_id: DelegationId,
) -> Result<Option<pos_accounting::DelegationData>, Self::Error> {
unimplemented!()
}

fn get_pool_delegation_share(
&self,
_pool_id: PoolId,
_delegation_id: DelegationId,
) -> Result<Option<Amount>, Self::Error> {
unimplemented!()
}
}

async fn tx_fees<T: ApiServerStorageWrite>(
chain_config: &ChainConfig,
block_height: BlockHeight,
Expand All @@ -680,17 +734,18 @@ async fn tx_fees<T: ApiServerStorageWrite>(
new_outputs: &BTreeMap<UtxoOutPoint, &TxOutput>,
) -> Result<AccumulatedFee, ApiServerStorageError> {
let inputs_utxos = collect_inputs_utxos(db_tx, tx.inputs(), new_outputs).await?;
let pools = prefetch_pool_amounts(&inputs_utxos, db_tx).await?;
let pools = prefetch_pool_data(&inputs_utxos, db_tx).await?;
let pos_accounting_adapter = PoSAccountingAdapterToCheckFees { pools };

let staker_balance_getter = |pool_id: PoolId| Ok(pools.get(&pool_id).cloned());
// only used for checks for attempted to print money but we don't need to check that here
let delegation_balance_getter = |_delegation_id: DelegationId| Ok(Some(Amount::MAX));
// TODO(orders)
let orders_store = orders_accounting::InMemoryOrdersAccounting::new();
let orders_db = orders_accounting::OrdersAccountingDB::new(&orders_store);

let inputs_accumulator = ConstrainedValueAccumulator::from_inputs(
chain_config,
block_height,
staker_balance_getter,
delegation_balance_getter,
&orders_db,
&pos_accounting_adapter,
tx.inputs(),
&inputs_utxos,
)
Expand All @@ -703,23 +758,18 @@ async fn tx_fees<T: ApiServerStorageWrite>(
Ok(consumed_accumulator)
}

async fn prefetch_pool_amounts<T: ApiServerStorageWrite>(
async fn prefetch_pool_data<T: ApiServerStorageWrite>(
inputs_utxos: &Vec<Option<TxOutput>>,
db_tx: &mut T,
) -> Result<BTreeMap<PoolId, Amount>, ApiServerStorageError> {
) -> Result<BTreeMap<PoolId, PoolData>, ApiServerStorageError> {
let mut pools = BTreeMap::new();
for output in inputs_utxos {
match output {
Some(
TxOutput::CreateStakePool(pool_id, _) | TxOutput::ProduceBlockFromStake(_, pool_id),
) => {
let amount = db_tx
.get_pool_data(*pool_id)
.await?
.expect("should exist")
.staker_balance()
.expect("no overflow");
pools.insert(*pool_id, amount);
let data = db_tx.get_pool_data(*pool_id).await?.expect("should exist");
pools.insert(*pool_id, data);
}
Some(
TxOutput::Burn(_)
Expand All @@ -730,7 +780,8 @@ async fn prefetch_pool_amounts<T: ApiServerStorageWrite>(
| TxOutput::DelegateStaking(_, _)
| TxOutput::IssueNft(_, _, _)
| TxOutput::IssueFungibleToken(_)
| TxOutput::Htlc(_, _),
| TxOutput::Htlc(_, _)
| TxOutput::AnyoneCanTake(_),
) => {}
None => {}
}
Expand Down Expand Up @@ -1055,6 +1106,9 @@ async fn update_tables_from_transaction_inputs<T: ApiServerStorageWrite>(
)
.await;
}
AccountCommand::CancelOrder(_) | AccountCommand::FillOrder(_, _, _) => {
// TODO(orders)
}
},
TxInput::Account(outpoint) => {
match outpoint.account() {
Expand Down Expand Up @@ -1108,7 +1162,8 @@ async fn update_tables_from_transaction_inputs<T: ApiServerStorageWrite>(
| TxOutput::CreateDelegationId(_, _)
| TxOutput::DelegateStaking(_, _)
| TxOutput::IssueFungibleToken(_)
| TxOutput::Htlc(_, _) => {}
| TxOutput::Htlc(_, _)
| TxOutput::AnyoneCanTake(_) => {}
TxOutput::CreateStakePool(pool_id, _)
| TxOutput::ProduceBlockFromStake(_, pool_id) => {
let pool_data = db_tx
Expand Down Expand Up @@ -1161,8 +1216,9 @@ async fn update_tables_from_transaction_inputs<T: ApiServerStorageWrite>(
| TxOutput::CreateDelegationId(_, _)
| TxOutput::DelegateStaking(_, _)
| TxOutput::DataDeposit(_)
| TxOutput::IssueFungibleToken(_) => {}
| TxOutput::CreateStakePool(pool_id, _)
| TxOutput::IssueFungibleToken(_)
| TxOutput::AnyoneCanTake(_) => {}
TxOutput::CreateStakePool(pool_id, _)
| TxOutput::ProduceBlockFromStake(_, pool_id) => {
let pool_data = db_tx
.get_pool_data(pool_id)
Expand Down Expand Up @@ -1464,7 +1520,7 @@ async fn update_tables_from_transaction_outputs<T: ApiServerStorageWrite>(
.expect("Unable to encode address");
address_transactions.entry(staker_address).or_default().insert(transaction_id);
}
| TxOutput::DelegateStaking(amount, delegation_id) => {
TxOutput::DelegateStaking(amount, delegation_id) => {
// Update delegation pledge

let delegation = db_tx
Expand Down Expand Up @@ -1617,6 +1673,9 @@ async fn update_tables_from_transaction_outputs<T: ApiServerStorageWrite>(
}
}
TxOutput::Htlc(_, _) => {} // TODO(HTLC)
TxOutput::AnyoneCanTake(_) => {
// TODO(orders)
}
}
}

Expand Down Expand Up @@ -1815,7 +1874,8 @@ fn get_tx_output_destination(txo: &TxOutput) -> Option<&Destination> {
TxOutput::IssueFungibleToken(_)
| TxOutput::Burn(_)
| TxOutput::DelegateStaking(_, _)
| TxOutput::DataDeposit(_) => None,
| TxOutput::DataDeposit(_)
| TxOutput::AnyoneCanTake(_) => None,
TxOutput::Htlc(_, _) => None, // TODO(HTLC)
}
}
Loading
Loading