-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathlock_controller.rs
More file actions
112 lines (102 loc) · 4.27 KB
/
Copy pathlock_controller.rs
File metadata and controls
112 lines (102 loc) · 4.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
//! Runtime interface for protocol-level lock controllers.
use concordium_base::contracts_common::AccountAddress;
use concordium_base::protocol_level_tokens::meta_operations::{
MetaLockCancelDetails, MetaLockFundDetails, MetaLockReturnDetails, MetaLockSendDetails,
};
use plt_block_state::block_state_interface::BlockStateQuery;
use plt_block_state::external::AccountNotFoundByIndexError;
use plt_block_state::persistent::protocol_level_locks::p11::{
LockControllerConfig, LockControllerSimpleV0,
};
use plt_scheduler_types::types::reject_reasons::TransactionRejectReason;
/// Runtime lock operation model. This corresponds to the "fund", "send", "return", and "cancel"
/// CBOR operations for interacting with locks from concordium-base.
#[derive(Debug, Clone, Eq, PartialEq)]
#[allow(dead_code)] // FIXME: remove this when all operations are implemented.
pub enum LockOperation {
Fund(MetaLockFundDetails),
Send(MetaLockSendDetails),
Return(MetaLockReturnDetails),
Cancel(MetaLockCancelDetails),
}
/// Runtime interface implemented by protocol-level locks.
pub trait LockController {
/// Approve or reject a lock operation. Returns `Ok(())` if the operation is authorized, or
/// a `TransactionRejectReason` if it is not.
///
/// * `bsq`: the block state to query on
/// * `sender`: the transaction sender reference
/// * `operation`: the lock operation to approve/reject.
fn validate_operation<BSQ: BlockStateQuery>(
&self,
bsq: &BSQ,
sender_address: AccountAddress,
sender: &BSQ::Account,
operation: &LockOperation,
) -> Result<(), TransactionRejectReason>;
/// Convert this controller configuration to its canonical CBOR
/// [`concordium_base::protocol_level_locks::LockController`] representation, used by the
/// `lock-info` payload returned from `query_lock_info`.
///
/// Resolves any block-state `AccountIndex` references (e.g. grant accounts) to their
/// canonical [`CborHolderAccount`] form via `bsq`. Surfaces an
/// [`AccountNotFoundByIndexError`] if a recorded `AccountIndex` cannot be
/// looked up — that signals corrupted block state, since lock configurations are only
/// allowed to reference accounts that exist at creation time.
fn to_cbor_controller<BSQ: BlockStateQuery>(
&self,
bsq: &BSQ,
) -> Result<concordium_base::protocol_level_locks::LockController, AccountNotFoundByIndexError>;
/// Controller configuration type used for constructing this controller.
/// This is expected to be decoded CBOR derived from the `lockCreate`
/// operation payload.
type ControllerConfig;
/// Construct this lock controller from the given configuration.
fn new<BSQ: BlockStateQuery>(
bsq: &BSQ,
config: Self::ControllerConfig,
) -> Result<Self, TransactionRejectReason>
where
Self: Sized;
}
impl LockController for LockControllerConfig {
fn validate_operation<BSQ: BlockStateQuery>(
&self,
bsq: &BSQ,
sender_address: AccountAddress,
sender: &BSQ::Account,
operation: &LockOperation,
) -> Result<(), TransactionRejectReason> {
match self {
LockControllerConfig::SimpleV0(lock_controller_simple_v0) => {
lock_controller_simple_v0.validate_operation(bsq, sender_address, sender, operation)
}
}
}
fn to_cbor_controller<BSQ: BlockStateQuery>(
&self,
bsq: &BSQ,
) -> Result<concordium_base::protocol_level_locks::LockController, AccountNotFoundByIndexError>
{
match self {
LockControllerConfig::SimpleV0(lock_controller_simple_v0) => {
lock_controller_simple_v0.to_cbor_controller(bsq)
}
}
}
type ControllerConfig = concordium_base::protocol_level_locks::LockController;
fn new<BSQ: BlockStateQuery>(
bsq: &BSQ,
config: Self::ControllerConfig,
) -> Result<Self, TransactionRejectReason>
where
Self: Sized,
{
use concordium_base::protocol_level_locks::LockController::*;
match config {
SimpleV0(lock_controller_simple_v0) => Ok(LockControllerConfig::SimpleV0(
LockControllerSimpleV0::new(bsq, lock_controller_simple_v0)?,
)),
}
}
}