Skip to content

Commit 1bfc6dd

Browse files
authored
Merge pull request #1740 from mintlayer/feat/orders_accounting
Orders accounting
2 parents c008a99 + 323a569 commit 1bfc6dd

File tree

168 files changed

+7268
-686
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

168 files changed

+7268
-686
lines changed

Cargo.lock

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ members = [
3434
"networking", # Pure networking implementations
3535
"node-gui", # Node GUI binary.
3636
"node-lib", # Node lib; the common library between daemon, tui and gui node executables.
37+
"orders-accounting", # Orders accounting
3738
"p2p", # P2p communication interfaces and protocols.
3839
"p2p/backend-test-suite", # P2p backend agnostic tests.
3940
"p2p/types", # P2p support types with minimal dependencies.

api-server/scanner-lib/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ constraints-value-accumulator = { path = "../../chainstate/constraints-value-acc
1515
logging = { path = "../../logging" }
1616
mempool = { path = "../../mempool" }
1717
node-comm = { path = "../../wallet/wallet-node-client" }
18+
orders-accounting = { path = "../../orders-accounting" }
1819
pos-accounting = { path = "../../pos-accounting" }
1920
randomness = { path = "../../randomness" }
2021
utils = { path = "../../utils" }

api-server/scanner-lib/src/blockchain_state/mod.rs

Lines changed: 84 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,8 @@ async fn update_tables_from_block_reward<T: ApiServerStorageWrite>(
396396
| TxOutput::DelegateStaking(_, _)
397397
| TxOutput::IssueFungibleToken(_)
398398
| TxOutput::IssueNft(_, _, _)
399-
| TxOutput::Htlc(_, _) => {}
399+
| TxOutput::Htlc(_, _)
400+
| TxOutput::AnyoneCanTake(_) => {}
400401
TxOutput::ProduceBlockFromStake(_, _) => {
401402
set_utxo(
402403
outpoint,
@@ -571,7 +572,8 @@ async fn calculate_fees<T: ApiServerStorageWrite>(
571572
| TxOutput::DelegateStaking(_, _)
572573
| TxOutput::CreateDelegationId(_, _)
573574
| TxOutput::ProduceBlockFromStake(_, _)
574-
| TxOutput::Htlc(_, _) => None,
575+
| TxOutput::Htlc(_, _)
576+
| TxOutput::AnyoneCanTake(_) => None,
575577
})
576578
})
577579
.collect();
@@ -604,7 +606,8 @@ async fn calculate_fees<T: ApiServerStorageWrite>(
604606
| TxOutput::CreateDelegationId(_, _)
605607
| TxOutput::IssueFungibleToken(_)
606608
| TxOutput::ProduceBlockFromStake(_, _)
607-
| TxOutput::Htlc(_, _) => None,
609+
| TxOutput::Htlc(_, _)
610+
| TxOutput::AnyoneCanTake(_) => None,
608611
},
609612
TxInput::Account(_) => None,
610613
TxInput::AccountCommand(_, cmd) => match cmd {
@@ -614,6 +617,7 @@ async fn calculate_fees<T: ApiServerStorageWrite>(
614617
| AccountCommand::UnfreezeToken(token_id)
615618
| AccountCommand::LockTokenSupply(token_id)
616619
| AccountCommand::ChangeTokenAuthority(token_id, _) => Some(*token_id),
620+
AccountCommand::ConcludeOrder(_) | AccountCommand::FillOrder(_, _, _) => None,
617621
},
618622
})
619623
.collect();
@@ -672,6 +676,56 @@ async fn token_decimals<T: ApiServerStorageRead>(
672676
Ok((token_id, decimals))
673677
}
674678

679+
struct PoSAccountingAdapterToCheckFees {
680+
pools: BTreeMap<PoolId, PoolData>,
681+
}
682+
683+
impl PoSAccountingView for PoSAccountingAdapterToCheckFees {
684+
type Error = pos_accounting::Error;
685+
686+
fn pool_exists(&self, _pool_id: PoolId) -> Result<bool, Self::Error> {
687+
unimplemented!()
688+
}
689+
690+
fn get_pool_balance(&self, _pool_id: PoolId) -> Result<Option<Amount>, Self::Error> {
691+
unimplemented!()
692+
}
693+
694+
fn get_pool_data(&self, pool_id: PoolId) -> Result<Option<PoolData>, Self::Error> {
695+
Ok(self.pools.get(&pool_id).cloned())
696+
}
697+
698+
fn get_pool_delegations_shares(
699+
&self,
700+
_pool_id: PoolId,
701+
) -> Result<Option<BTreeMap<DelegationId, Amount>>, Self::Error> {
702+
unimplemented!()
703+
}
704+
705+
fn get_delegation_balance(
706+
&self,
707+
_delegation_id: DelegationId,
708+
) -> Result<Option<Amount>, Self::Error> {
709+
// only used for checks for attempted to print money but we don't need to check that here
710+
Ok(Some(Amount::MAX))
711+
}
712+
713+
fn get_delegation_data(
714+
&self,
715+
_delegation_id: DelegationId,
716+
) -> Result<Option<pos_accounting::DelegationData>, Self::Error> {
717+
unimplemented!()
718+
}
719+
720+
fn get_pool_delegation_share(
721+
&self,
722+
_pool_id: PoolId,
723+
_delegation_id: DelegationId,
724+
) -> Result<Option<Amount>, Self::Error> {
725+
unimplemented!()
726+
}
727+
}
728+
675729
async fn tx_fees<T: ApiServerStorageWrite>(
676730
chain_config: &ChainConfig,
677731
block_height: BlockHeight,
@@ -680,17 +734,18 @@ async fn tx_fees<T: ApiServerStorageWrite>(
680734
new_outputs: &BTreeMap<UtxoOutPoint, &TxOutput>,
681735
) -> Result<AccumulatedFee, ApiServerStorageError> {
682736
let inputs_utxos = collect_inputs_utxos(db_tx, tx.inputs(), new_outputs).await?;
683-
let pools = prefetch_pool_amounts(&inputs_utxos, db_tx).await?;
737+
let pools = prefetch_pool_data(&inputs_utxos, db_tx).await?;
738+
let pos_accounting_adapter = PoSAccountingAdapterToCheckFees { pools };
684739

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

689744
let inputs_accumulator = ConstrainedValueAccumulator::from_inputs(
690745
chain_config,
691746
block_height,
692-
staker_balance_getter,
693-
delegation_balance_getter,
747+
&orders_db,
748+
&pos_accounting_adapter,
694749
tx.inputs(),
695750
&inputs_utxos,
696751
)
@@ -703,23 +758,18 @@ async fn tx_fees<T: ApiServerStorageWrite>(
703758
Ok(consumed_accumulator)
704759
}
705760

706-
async fn prefetch_pool_amounts<T: ApiServerStorageWrite>(
761+
async fn prefetch_pool_data<T: ApiServerStorageWrite>(
707762
inputs_utxos: &Vec<Option<TxOutput>>,
708763
db_tx: &mut T,
709-
) -> Result<BTreeMap<PoolId, Amount>, ApiServerStorageError> {
764+
) -> Result<BTreeMap<PoolId, PoolData>, ApiServerStorageError> {
710765
let mut pools = BTreeMap::new();
711766
for output in inputs_utxos {
712767
match output {
713768
Some(
714769
TxOutput::CreateStakePool(pool_id, _) | TxOutput::ProduceBlockFromStake(_, pool_id),
715770
) => {
716-
let amount = db_tx
717-
.get_pool_data(*pool_id)
718-
.await?
719-
.expect("should exist")
720-
.staker_balance()
721-
.expect("no overflow");
722-
pools.insert(*pool_id, amount);
771+
let data = db_tx.get_pool_data(*pool_id).await?.expect("should exist");
772+
pools.insert(*pool_id, data);
723773
}
724774
Some(
725775
TxOutput::Burn(_)
@@ -730,7 +780,8 @@ async fn prefetch_pool_amounts<T: ApiServerStorageWrite>(
730780
| TxOutput::DelegateStaking(_, _)
731781
| TxOutput::IssueNft(_, _, _)
732782
| TxOutput::IssueFungibleToken(_)
733-
| TxOutput::Htlc(_, _),
783+
| TxOutput::Htlc(_, _)
784+
| TxOutput::AnyoneCanTake(_),
734785
) => {}
735786
None => {}
736787
}
@@ -1055,6 +1106,9 @@ async fn update_tables_from_transaction_inputs<T: ApiServerStorageWrite>(
10551106
)
10561107
.await;
10571108
}
1109+
AccountCommand::ConcludeOrder(_) | AccountCommand::FillOrder(_, _, _) => {
1110+
// TODO(orders)
1111+
}
10581112
},
10591113
TxInput::Account(outpoint) => {
10601114
match outpoint.account() {
@@ -1108,7 +1162,8 @@ async fn update_tables_from_transaction_inputs<T: ApiServerStorageWrite>(
11081162
| TxOutput::CreateDelegationId(_, _)
11091163
| TxOutput::DelegateStaking(_, _)
11101164
| TxOutput::IssueFungibleToken(_)
1111-
| TxOutput::Htlc(_, _) => {}
1165+
| TxOutput::Htlc(_, _)
1166+
| TxOutput::AnyoneCanTake(_) => {}
11121167
TxOutput::CreateStakePool(pool_id, _)
11131168
| TxOutput::ProduceBlockFromStake(_, pool_id) => {
11141169
let pool_data = db_tx
@@ -1161,8 +1216,9 @@ async fn update_tables_from_transaction_inputs<T: ApiServerStorageWrite>(
11611216
| TxOutput::CreateDelegationId(_, _)
11621217
| TxOutput::DelegateStaking(_, _)
11631218
| TxOutput::DataDeposit(_)
1164-
| TxOutput::IssueFungibleToken(_) => {}
1165-
| TxOutput::CreateStakePool(pool_id, _)
1219+
| TxOutput::IssueFungibleToken(_)
1220+
| TxOutput::AnyoneCanTake(_) => {}
1221+
TxOutput::CreateStakePool(pool_id, _)
11661222
| TxOutput::ProduceBlockFromStake(_, pool_id) => {
11671223
let pool_data = db_tx
11681224
.get_pool_data(pool_id)
@@ -1464,7 +1520,7 @@ async fn update_tables_from_transaction_outputs<T: ApiServerStorageWrite>(
14641520
.expect("Unable to encode address");
14651521
address_transactions.entry(staker_address).or_default().insert(transaction_id);
14661522
}
1467-
| TxOutput::DelegateStaking(amount, delegation_id) => {
1523+
TxOutput::DelegateStaking(amount, delegation_id) => {
14681524
// Update delegation pledge
14691525

14701526
let delegation = db_tx
@@ -1617,6 +1673,9 @@ async fn update_tables_from_transaction_outputs<T: ApiServerStorageWrite>(
16171673
}
16181674
}
16191675
TxOutput::Htlc(_, _) => {} // TODO(HTLC)
1676+
TxOutput::AnyoneCanTake(_) => {
1677+
// TODO(orders)
1678+
}
16201679
}
16211680
}
16221681

@@ -1815,7 +1874,8 @@ fn get_tx_output_destination(txo: &TxOutput) -> Option<&Destination> {
18151874
TxOutput::IssueFungibleToken(_)
18161875
| TxOutput::Burn(_)
18171876
| TxOutput::DelegateStaking(_, _)
1818-
| TxOutput::DataDeposit(_) => None,
1877+
| TxOutput::DataDeposit(_)
1878+
| TxOutput::AnyoneCanTake(_) => None,
18191879
TxOutput::Htlc(_, _) => None, // TODO(HTLC)
18201880
}
18211881
}

0 commit comments

Comments
 (0)