forked from joshuavial/bridge.hc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scratch.rs
138 lines (114 loc) · 4.22 KB
/
scratch.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
// -AuthorityList
// - list of authorities (holo_pubkey, eth_pubkey)
// - initial list provided by the properties of the dht
// - validations
// at least 50% sign an update to the list
struct AuthorityList {
authorities: Vec<(AgentPubKey, String)>
percentage_for_consensus: u32,
}
// need validation for:
// done // - percentage for consensus must be at least 50%
// - authorities list must be of a minimum length
// Build functionality for adding new agents to the authoritylist
// guillem's entries...
use hdk::prelude::holo_hash::*;
use hdk::prelude::*;
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct TransactionParty {
pub agent_pub_key: AgentPubKeyB64,
pub previous_transaction_hash: Option<ActionHashB64>,
pub resulting_balance: f64,
}
#[hdk_entry_helper]
#[derive(Clone)]
#[serde(rename_all = "camelCase")]
pub struct Transaction {
pub spender: TransactionParty,
pub recipient: TransactionParty,
pub amount: f64,
pub info: SerializedBytes,
}
impl Transaction {
pub fn try_from_entry(entry: Entry) -> ExternResult<Transaction> {
match entry {
Entry::CounterSign(_session_data, entry_bytes) => {
let transaction = Transaction::try_from(entry_bytes.into_sb())?;
Ok(transaction)
}
_ => Err(wasm_error!(String::from("Malformed entry"))),
}
}
pub fn from_previous_transactions(
spender: AgentPubKey,
recipient: AgentPubKey,
previous_spender_transaction: Option<(ActionHashB64, Transaction)>,
previous_recipient_transaction: Option<(ActionHashB64, Transaction)>,
amount: f64,
info: SerializedBytes,
) -> ExternResult<Transaction> {
let previous_spender_balance = balance_from_previous_transaction(
spender.clone(),
previous_spender_transaction.clone().map(|(_, t)| t),
)?;
let previous_recipient_balance = balance_from_previous_transaction(
recipient.clone(),
previous_recipient_transaction.clone().map(|(_, t)| t),
)?;
let resulting_spender_balance = previous_spender_balance - amount;
let resulting_recipient_balance = previous_recipient_balance + amount;
let spender = TransactionParty {
agent_pub_key: spender.into(),
previous_transaction_hash: previous_spender_transaction.map(|(h, _)| h),
resulting_balance: resulting_spender_balance,
};
let recipient = TransactionParty {
agent_pub_key: recipient.into(),
previous_transaction_hash: previous_recipient_transaction.map(|(h, _)| h),
resulting_balance: resulting_recipient_balance,
};
let transaction = Transaction {
spender,
recipient,
amount,
info,
};
Ok(transaction)
}
pub fn get_party(&self, agent_pub_key: &AgentPubKey) -> ExternResult<TransactionParty> {
if AgentPubKey::from(self.spender.agent_pub_key.clone()).eq(agent_pub_key) {
Ok(self.spender.clone())
} else if AgentPubKey::from(self.recipient.agent_pub_key.clone()).eq(agent_pub_key) {
Ok(self.recipient.clone())
} else {
Err(wasm_error!(String::from(
"This agent did not participate in the transaction",
)))
}
}
pub fn get_counterparty(&self) -> ExternResult<TransactionParty> {
let my_pub_key: AgentPubKeyB64 = agent_info()?.agent_initial_pubkey.into();
if my_pub_key.eq(&self.spender.agent_pub_key) {
Ok(self.spender.clone())
} else if my_pub_key.eq(&self.recipient.agent_pub_key) {
Ok(self.spender.clone())
} else {
Err(wasm_error!(String::from(
"I don't participate in this Transaction",
)))
}
}
}
fn balance_from_previous_transaction(
for_agent: AgentPubKey,
previous_transaction: Option<Transaction>,
) -> ExternResult<f64> {
match previous_transaction {
None => Ok(0.0),
Some(txn) => {
let party = txn.get_party(&for_agent)?;
Ok(party.resulting_balance)
}
}
}