|
| 1 | +// Copyright (c) 2021-2025 RBB S.r.l |
| 2 | + |
| 3 | +// SPDX-License-Identifier: MIT |
| 4 | +// Licensed under the MIT License; |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// https://github.com/mintlayer/mintlayer-core/blob/master/LICENSE |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | + |
| 16 | +use std::collections::BTreeMap; |
| 17 | + |
| 18 | +use serialization::{Decode, Encode}; |
| 19 | + |
| 20 | +use crate::{ |
| 21 | + chain::{ |
| 22 | + output_value::OutputValue, |
| 23 | + signature::sighash::{self}, |
| 24 | + OrderId, PoolId, |
| 25 | + }, |
| 26 | + primitives::Amount, |
| 27 | +}; |
| 28 | + |
| 29 | +// Note: PoolAdditionalInfo and OrderAdditionalInfo below are identical to the corresponding |
| 30 | +// structs in `input_commitments/info_providers.rs` (except that those don't derive Encode/Decode) |
| 31 | +// and basically serve the same purpose. |
| 32 | +// We keep them separate because: |
| 33 | +// 1) Technically we may want to have even more info inside partially signed transaction in |
| 34 | +// the future, which may not be needed by the input commitments. |
| 35 | +// 2) We want to be able to refactor input commitments structs without worrying about breaking |
| 36 | +// PartiallySignedTransaction's backward compatibility. |
| 37 | + |
| 38 | +/// Pool additional info, which must be present for each ProduceBlockFromStake UTXO consumed by |
| 39 | +/// the transaction. Transaction's signature commits to this info since SighashInputCommitments::V1. |
| 40 | +#[derive(Debug, Eq, PartialEq, Clone, Encode, Decode, serde::Serialize)] |
| 41 | +pub struct PoolAdditionalInfo { |
| 42 | + pub staker_balance: Amount, |
| 43 | +} |
| 44 | + |
| 45 | +/// Order additional info, which must be present for each FillOrder and ConcludeOrder input consumed |
| 46 | +/// by the transaction. Transaction's signature commits to this info since SighashInputCommitments::V1. |
| 47 | +/// |
| 48 | +/// Note though that only ConcludeOrder commitments include both initial and current balances, |
| 49 | +/// while FillOrder commitments only include the initial ones. So this info representation |
| 50 | +/// is not ideal, as it forces the caller to provide additional info that will not actually |
| 51 | +/// be used. |
| 52 | +#[derive(Debug, Eq, PartialEq, Clone, Encode, Decode, serde::Serialize)] |
| 53 | +pub struct OrderAdditionalInfo { |
| 54 | + pub initially_asked: OutputValue, |
| 55 | + pub initially_given: OutputValue, |
| 56 | + pub ask_balance: Amount, |
| 57 | + pub give_balance: Amount, |
| 58 | +} |
| 59 | + |
| 60 | +#[derive(Debug, Eq, PartialEq, Clone, Encode, Decode, serde::Serialize)] |
| 61 | +pub struct TxAdditionalInfo { |
| 62 | + pool_info: BTreeMap<PoolId, PoolAdditionalInfo>, |
| 63 | + order_info: BTreeMap<OrderId, OrderAdditionalInfo>, |
| 64 | +} |
| 65 | + |
| 66 | +impl TxAdditionalInfo { |
| 67 | + pub fn new() -> Self { |
| 68 | + Self { |
| 69 | + pool_info: BTreeMap::new(), |
| 70 | + order_info: BTreeMap::new(), |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + pub fn with_pool_info(mut self, pool_id: PoolId, info: PoolAdditionalInfo) -> Self { |
| 75 | + self.pool_info.insert(pool_id, info); |
| 76 | + self |
| 77 | + } |
| 78 | + |
| 79 | + pub fn with_order_info(mut self, order_id: OrderId, info: OrderAdditionalInfo) -> Self { |
| 80 | + self.order_info.insert(order_id, info); |
| 81 | + self |
| 82 | + } |
| 83 | + |
| 84 | + pub fn add_pool_info(&mut self, pool_id: PoolId, info: PoolAdditionalInfo) { |
| 85 | + self.pool_info.insert(pool_id, info); |
| 86 | + } |
| 87 | + |
| 88 | + pub fn add_order_info(&mut self, order_id: OrderId, info: OrderAdditionalInfo) { |
| 89 | + self.order_info.insert(order_id, info); |
| 90 | + } |
| 91 | + |
| 92 | + pub fn join(mut self, other: Self) -> Self { |
| 93 | + self.pool_info.extend(other.pool_info); |
| 94 | + self.order_info.extend(other.order_info); |
| 95 | + Self { |
| 96 | + pool_info: self.pool_info, |
| 97 | + order_info: self.order_info, |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + pub fn get_pool_info(&self, pool_id: &PoolId) -> Option<&PoolAdditionalInfo> { |
| 102 | + self.pool_info.get(pool_id) |
| 103 | + } |
| 104 | + |
| 105 | + pub fn get_order_info(&self, order_id: &OrderId) -> Option<&OrderAdditionalInfo> { |
| 106 | + self.order_info.get(order_id) |
| 107 | + } |
| 108 | + |
| 109 | + pub fn pool_info_iter(&self) -> impl Iterator<Item = (&'_ PoolId, &'_ PoolAdditionalInfo)> { |
| 110 | + self.pool_info.iter() |
| 111 | + } |
| 112 | + |
| 113 | + pub fn order_info_iter(&self) -> impl Iterator<Item = (&'_ OrderId, &'_ OrderAdditionalInfo)> { |
| 114 | + self.order_info.iter() |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +impl sighash::input_commitments::PoolInfoProvider for TxAdditionalInfo { |
| 119 | + type Error = std::convert::Infallible; |
| 120 | + |
| 121 | + fn get_pool_info( |
| 122 | + &self, |
| 123 | + pool_id: &PoolId, |
| 124 | + ) -> Result<Option<sighash::input_commitments::PoolInfo>, Self::Error> { |
| 125 | + Ok( |
| 126 | + self.pool_info.get(pool_id).map(|info| sighash::input_commitments::PoolInfo { |
| 127 | + staker_balance: info.staker_balance, |
| 128 | + }), |
| 129 | + ) |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +impl sighash::input_commitments::OrderInfoProvider for TxAdditionalInfo { |
| 134 | + type Error = std::convert::Infallible; |
| 135 | + |
| 136 | + fn get_order_info( |
| 137 | + &self, |
| 138 | + order_id: &OrderId, |
| 139 | + ) -> Result<Option<sighash::input_commitments::OrderInfo>, Self::Error> { |
| 140 | + Ok( |
| 141 | + self.order_info.get(order_id).map(|info| sighash::input_commitments::OrderInfo { |
| 142 | + initially_asked: info.initially_asked.clone(), |
| 143 | + initially_given: info.initially_given.clone(), |
| 144 | + ask_balance: info.ask_balance, |
| 145 | + give_balance: info.give_balance, |
| 146 | + }), |
| 147 | + ) |
| 148 | + } |
| 149 | +} |
0 commit comments