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

Orders accounting #1740

merged 37 commits into from
Jun 28, 2024

Conversation

azarovh
Copy link
Member

@azarovh azarovh commented Apr 30, 2024

Introduces new TxOutput::AnyoneCanTake which essentially represents a buy/sell order. This output creates an account with respective balances which can be filled by anyone partially/fully or withdrawn by an authority specified in the original output. Filling an order means creating a transaction that "burns" a currency that is being bought by the order and producing corresponding amount of other currency from the order account.

Allows to exchange a token for other token, coin for any token or vice versa. Given that the exchange is arbitrary and the atoms amount not divisible price arithmetics has it's restrictions. Currently it's implemented with integers arithmetics in a way that 3 tokens sold for 100 coins one by one will cost [33; 33; 34] coins each.

Suggestions for better arithmetics as well as naming are welcome.

Api-server and wallet are stubbed in this PR.

@azarovh azarovh force-pushed the feat/orders_accounting branch 2 times, most recently from ea193a6 to 1feed11 Compare May 5, 2024 15:14
@azarovh azarovh marked this pull request as ready for review May 5, 2024 15:33
Comment on lines 1520 to 1353
// TODO: support orders
AccountCommand::WithdrawOrder(_) => unimplemented!(),
AccountCommand::FillOrder(_, _, _) => unimplemented!(),
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Must be implemented before merging

Comment on lines 2107 to 1834
// TODO: support orders
AccountCommand::WithdrawOrder(_) => unimplemented!(),
AccountCommand::FillOrder(_, _, _) => unimplemented!(),
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Must be implemented before merging

@@ -2459,6 +2468,8 @@ fn group_preselected_inputs(
output.clone(),
)))
}
// TODO: support orders
TxOutput::AnyoneCanTake(_) => unimplemented!(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Must be implemented before merging

Comment on lines 2516 to 2245
// TODO: support orders
AccountCommand::WithdrawOrder(_) => unimplemented!(),
AccountCommand::FillOrder(_, _, _) => unimplemented!(),
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Must be implemented before merging

@@ -716,6 +719,8 @@ impl OutputCache {
| TxOutput::CreateDelegationId(_, _)
| TxOutput::IssueFungibleToken(_)
| TxOutput::ProduceBlockFromStake(_, _) => false,
// TODO: support orders
TxOutput::AnyoneCanTake(_) => unimplemented!(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Must be implemented before merging

Comment on lines 734 to 738
AccountCommand::WithdrawOrder(_) => unimplemented!(),
AccountCommand::FillOrder(_, _, _) => unimplemented!(),
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Must be implemented before merging

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Many other unimplemented!() instances

@@ -57,6 +57,8 @@ pub(crate) fn group_outputs<T, Grouped: Clone>(
get_tx_output(&output).clone(),
)))
}
// TODO: support orders
TxOutput::AnyoneCanTake(_) => unimplemented!(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Must be implemented before merging

}

impl<P: OrdersAccountingView> OrdersAccountingOperations for OrdersAccountingCache<P> {
fn create_order(&mut self, id: OrderId, data: OrderData) -> Result<OrdersAccountingUndo> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reminder: Order id calculation should be automated + it should be enforced to use the automated results. We don't want people to be able to use any id they want.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is calculated in the tx-verifier based on tx input.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure it's not possible to use a custom id? This is manually enforced for staking pools.

Copy link
Member Author

@azarovh azarovh May 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is possible to pass custom id to this function, e.g. in tests. But in transaction verifier it is called with id generated from utxo input.
For staking pool the situation is different because TxInput::CreateStakePool(PoolId, _) has pool id in it. So manual enforcement is required. But not for tokens and orders it is not the case.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please ensure this is tested, because abusing it can cause issues.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checked, it is tested in chainstate tests that an order is created with id produced from input0.

Comment on lines 127 to +129
tokens_accounting_cache: TokensAccountingCache<T>,
tokens_accounting_block_undo: AccountingBlockUndoCache<TokenAccountingUndo>,

orders_accounting_cache: OrdersAccountingCache<O>,
orders_accounting_block_undo: AccountingBlockUndoCache<OrdersAccountingUndo>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All these should be somehow unified in the future.

@azarovh azarovh marked this pull request as ready for review June 25, 2024 10:41
chain_config,
block_height,
command,
orders_accounting_view,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't we use the tracker here? I'm not very comfortable with this half and half approach

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe tracker's purpose is to track overspends from accounts across multiple inputs. Here I need orders_view to calculate actual amounts being spend. Am I missing something?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What prevents multiple commands from executing the same fill?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the rule that there can only be single Command per tx

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will be reworked in a separate pr

orders-accounting/src/price_calculation.rs Outdated Show resolved Hide resolved
orders-accounting/src/price_calculation.rs Outdated Show resolved Hide resolved
orders-accounting/src/error.rs Outdated Show resolved Hide resolved
orders-accounting/src/error.rs Outdated Show resolved Hide resolved
Comment on lines 508 to 517
Error::InvariantOrderDataNotFoundForUndo(_) => 100,
Error::InvariantOrderAskBalanceNotFoundForUndo(_) => 100,
Error::InvariantOrderAskBalanceChangedForUndo(_) => 100,
Error::InvariantOrderGiveBalanceNotFoundForUndo(_) => 100,
Error::InvariantOrderGiveBalanceChangedForUndo(_) => 100,
Error::InvariantOrderDataExistForCancelUndo(_) => 100,
Error::InvariantOrderAskBalanceExistForCancelUndo(_) => 100,
Error::InvariantOrderGiveBalanceExistForCancelUndo(_) => 100,
Error::InvariantNonzeroAskBalanceForMissingOrder(_) => 100,
Error::InvariantNonzeroGiveBalanceForMissingOrder(_) => 100,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not that it's a big deal, but IMO it's better for "invariant" errors to have zero ban score, because they can't be peer's fault.
Same for chainstate/src/detail/ban_score.rs

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe, not sure, it's just how it's done for every other invariant error

common/src/chain/tokens/issuance.rs Outdated Show resolved Hide resolved
Comment on lines +144 to 146
#[codec(index = 11)]
AnyoneCanTake(Box<OrderData>),
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not that I insist on renaming this, but I wonder why this name was chosen. Shouldn't it be "CreateOrder" or something similar?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea is that the output names describe a use case rather then specific operation performed. Like Transfer which doesn't transfer anything per se.

@azarovh azarovh marked this pull request as draft June 27, 2024 09:36
@azarovh azarovh marked this pull request as ready for review June 27, 2024 10:19
Copy link
Contributor

@TheQuantumPhysicist TheQuantumPhysicist left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Everything seems fine except for the accumulator. Stuff need to be discussed further.

@azarovh azarovh merged commit 1bfc6dd into master Jun 28, 2024
23 checks passed
@azarovh azarovh deleted the feat/orders_accounting branch June 28, 2024 10:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants