This repository was archived by the owner on Jun 2, 2024. It is now read-only.
forked from RGB-WG/rgb-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessages.rs
204 lines (167 loc) · 5.22 KB
/
messages.rs
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// RGB node providing smart contracts functionality for Bitcoin & Lightning.
//
// Written in 2022 by
// Dr. Maxim Orlovsky <orlovsky@lnp-bp.org>
//
// Copyright (C) 2022 by LNP/BP Standards Association, Switzerland.
//
// You should have received a copy of the MIT License along with this software.
// If not, see <https://opensource.org/licenses/MIT>.
use std::collections::BTreeSet;
use bitcoin::{OutPoint, Txid};
use internet2::addr::NodeAddr;
use internet2::presentation;
use lnpbp::chain::Chain;
use microservices::rpc;
use microservices::util::OptionDetails;
use psbt::Psbt;
use rgb::schema::TransitionType;
use rgb::{
seal, validation, ConsignmentType, Contract, ContractConsignment, ContractId, ContractState,
ContractStateMap, InmemConsignment, SealEndpoint, StateTransfer, TransferConsignment,
};
use crate::{FailureCode, Reveal};
/// We need this wrapper type to be compatible with RGB Node having multiple message buses
#[derive(Clone, Debug, Display, From, Api)]
#[api(encoding = "strict")]
#[non_exhaustive]
pub(crate) enum BusMsg {
#[api(type = 4)]
#[display(inner)]
#[from]
Rpc(RpcMsg),
}
impl rpc::Request for BusMsg {}
#[derive(Clone, Debug, Display, From)]
#[derive(NetworkEncode, NetworkDecode)]
#[display(inner)]
pub enum RpcMsg {
#[from]
Hello(HelloReq),
// Contract operations
// -------------------
#[display("list_contracts")]
ListContracts,
#[display("get_contract_state({0})")]
GetContractState(ContractId),
#[display("get_outpoint_state(...)")]
GetOutpointState(BTreeSet<OutPoint>),
#[display("consign_contract({0})")]
ConsignContract(ComposeReq),
#[display("consign_transfer({0})")]
ConsignTransfer(ComposeReq),
#[display(inner)]
ConsumeContract(AcceptReq<ContractConsignment>),
#[display("accept_transfer(...)")]
ConsumeTransfer(AcceptReq<TransferConsignment>),
#[display(inner)]
Transfer(TransferReq),
#[display("memorize_seal({0})")]
MemorizeSeal(seal::Revealed),
// Responses to CLI
// ----------------
#[display("contract_ids(...)")]
ContractIds(BTreeSet<ContractId>),
#[display("contract(...)")]
Contract(Contract),
#[display("contract_state(...)")]
ContractState(ContractState),
#[display("outpoint_state(...)")]
OutpointState(ContractStateMap),
#[display("state_transfer(...)")]
StateTransfer(StateTransfer),
#[display("state_transfer_finalize(...)")]
StateTransferFinalize(TransferFinalize),
#[display("progress(\"{0}\")")]
#[from]
Progress(String),
#[display("success{0}")]
Success(OptionDetails),
#[display("failure({0:#})")]
#[from]
Failure(rpc::Failure<FailureCode>),
#[display("unresolved_txids(...)")]
UnresolvedTxids(Vec<Txid>),
#[display("invalid(...)")]
Invalid(validation::Status),
}
impl From<presentation::Error> for RpcMsg {
fn from(err: presentation::Error) -> Self {
RpcMsg::Failure(rpc::Failure {
code: rpc::FailureCode::Presentation,
info: format!("{}", err),
})
}
}
impl RpcMsg {
pub fn success() -> Self { RpcMsg::Success(None.into()) }
pub fn failure(code: FailureCode, message: impl ToString) -> Self {
RpcMsg::Failure(rpc::Failure {
code: rpc::FailureCode::Other(code),
info: message.to_string(),
})
}
}
#[derive(Clone, Debug)]
#[derive(StrictEncode, StrictDecode)]
pub enum ContractValidity {
Valid,
Invalid(validation::Status),
UnknownTxids(Vec<Txid>),
}
#[derive(Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug)]
#[derive(StrictEncode, StrictDecode)]
pub enum OutpointFilter {
All,
Only(BTreeSet<OutPoint>),
}
impl OutpointFilter {
pub fn includes(&self, outpoint: OutPoint) -> bool {
match self {
OutpointFilter::All => true,
OutpointFilter::Only(set) => set.contains(&outpoint),
}
}
}
#[derive(Clone, PartialEq, Eq, Debug, Display)]
#[derive(NetworkEncode, NetworkDecode)]
#[display("accept(force: {force}, ...)")]
pub struct AcceptReq<T: ConsignmentType> {
pub consignment: InmemConsignment<T>,
pub force: bool,
pub reveal: Option<Reveal>,
}
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Display)]
#[derive(NetworkEncode, NetworkDecode)]
#[display("hello({network}, {user_agent})")]
pub struct HelloReq {
pub user_agent: String,
pub network: Chain,
}
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Display)]
#[derive(NetworkEncode, NetworkDecode)]
#[display("{contract_id}, ...")]
pub struct ComposeReq {
pub contract_id: ContractId,
pub include: BTreeSet<TransitionType>,
pub outpoints: OutpointFilter,
}
#[derive(Clone, PartialEq, Eq, Debug, Display)]
#[derive(NetworkEncode, NetworkDecode)]
#[display("transfer(...)")]
pub struct TransferReq {
pub consignment: StateTransfer,
pub endseals: Vec<SealEndpoint>,
pub psbt: Psbt,
pub beneficiary: Option<NodeAddr>,
}
impl From<&str> for RpcMsg {
fn from(s: &str) -> Self { RpcMsg::Progress(s.to_owned()) }
}
#[derive(Clone, PartialEq, Eq, Debug, Display)]
#[derive(NetworkEncode, NetworkDecode)]
#[display("transfer_complete(...)")]
pub struct TransferFinalize {
pub consignment: StateTransfer,
pub psbt: Psbt,
}