aboutsummaryrefslogtreecommitdiffstats
path: root/src/routes.rs
diff options
context:
space:
mode:
authorYigit Sever2021-04-12 05:32:53 +0300
committerYigit Sever2021-04-12 05:32:53 +0300
commit44d21b676f90a2fc8b255eb9c1393e53f40c9daa (patch)
treeedfadfd279dc9fcfaa6c27f819c7f0e69d14599c /src/routes.rs
parent6c0345ecda5e46da88bc6ca513a28c648c29833c (diff)
downloadgradecoin-44d21b676f90a2fc8b255eb9c1393e53f40c9daa.tar.gz
gradecoin-44d21b676f90a2fc8b255eb9c1393e53f40c9daa.tar.bz2
gradecoin-44d21b676f90a2fc8b255eb9c1393e53f40c9daa.zip
Implement proof-of-work
Using blacke2s: https://docs.rs/blake2/0.9.1/blake2/ Using this guy's hash checker https://gist.github.com/gkbrk/2e4835e3a17b3fb6e1e7 blacke2s with 5 bits 0 can mine a block between 20 seconds to 359 during my tests, hope it'll be fun
Diffstat (limited to 'src/routes.rs')
-rw-r--r--src/routes.rs80
1 files changed, 70 insertions, 10 deletions
diff --git a/src/routes.rs b/src/routes.rs
index 9f0adc5..03a2569 100644
--- a/src/routes.rs
+++ b/src/routes.rs
@@ -2,7 +2,7 @@ use warp::{Filter, Rejection, Reply};
2 2
3use crate::custom_filters; 3use crate::custom_filters;
4use crate::handlers; 4use crate::handlers;
5use crate::schema::Db; 5use gradecoin::schema::Db;
6 6
7/// Root, all routes combined 7/// Root, all routes combined
8pub fn consensus_routes(db: Db) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone { 8pub fn consensus_routes(db: Db) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
@@ -65,12 +65,11 @@ mod tests {
65 // use std::sync::Arc; 65 // use std::sync::Arc;
66 use warp::http::StatusCode; 66 use warp::http::StatusCode;
67 67
68 use crate::schema; 68 use gradecoin::schema::{create_database, AuthRequest, Block, Transaction};
69 use crate::schema::{AuthRequest, Block, Transaction};
70 69
71 /// Create a mock database to be used in tests 70 /// Create a mock database to be used in tests
72 fn mocked_db() -> Db { 71 fn mocked_db() -> Db {
73 let db = schema::create_database(); 72 let db = create_database();
74 73
75 db.pending_transactions.write().insert( 74 db.pending_transactions.write().insert(
76 "hash_value".to_owned(), 75 "hash_value".to_owned(),
@@ -88,7 +87,7 @@ mod tests {
88 "old_transaction_hash_2".to_owned(), 87 "old_transaction_hash_2".to_owned(),
89 "old_transaction_hash_3".to_owned(), 88 "old_transaction_hash_3".to_owned(),
90 ], 89 ],
91 nonce: "not_a_thing_yet".to_owned(), 90 nonce: 0,
92 timestamp: chrono::NaiveDate::from_ymd(2021, 04, 08).and_hms(12, 30, 30), 91 timestamp: chrono::NaiveDate::from_ymd(2021, 04, 08).and_hms(12, 30, 30),
93 hash: "not_a_thing_yet".to_owned(), 92 hash: "not_a_thing_yet".to_owned(),
94 }; 93 };
@@ -122,6 +121,26 @@ mod tests {
122 } 121 }
123 } 122 }
124 123
124 /// Create a mock block with a correct mined hash to be used in tests
125 fn mocked_block() -> Block {
126 Block {
127 transaction_list: vec!["hash_value".to_owned()],
128 nonce: 560108,
129 timestamp: chrono::NaiveDate::from_ymd(2021, 04, 08).and_hms(12, 30, 30),
130 hash: "c7d053f3e5b056ba948db3f5c0d30408fb0c29a328a0c3c1cf435fb68d700000".to_owned(),
131 }
132 }
133
134 /// Create a mock block with a wrong hash and nonce
135 fn mocked_wrong_block() -> Block {
136 Block {
137 transaction_list: vec!["foobarbaz".to_owned(), "dazsaz".to_owned()],
138 nonce: 1000, // can you imagine
139 timestamp: chrono::NaiveDate::from_ymd(2021, 04, 12).and_hms(05, 29, 30),
140 hash: "tnarstnarsuthnarsthlarjstk".to_owned(),
141 }
142 }
143
125 /// Test simple GET request to /transaction, resource that exists 144 /// Test simple GET request to /transaction, resource that exists
126 /// https://tools.ietf.org/html/rfc7231#section-6.3.1 145 /// https://tools.ietf.org/html/rfc7231#section-6.3.1
127 /// We should get the only pending transaction available in the database as json 146 /// We should get the only pending transaction available in the database as json
@@ -160,7 +179,7 @@ mod tests {
160 179
161 assert_eq!(res.status(), StatusCode::OK); 180 assert_eq!(res.status(), StatusCode::OK);
162 181
163 let expected_json_body = r#"{"transaction_list":["old_transaction_hash_1","old_transaction_hash_2","old_transaction_hash_3"],"nonce":"not_a_thing_yet","timestamp":"2021-04-08T12:30:30","hash":"not_a_thing_yet"}"#; 182 let expected_json_body = r#"{"transaction_list":["old_transaction_hash_1","old_transaction_hash_2","old_transaction_hash_3"],"nonce":0,"timestamp":"2021-04-08T12:30:30","hash":"not_a_thing_yet"}"#;
164 assert_eq!(res.body(), expected_json_body); 183 assert_eq!(res.body(), expected_json_body);
165 } 184 }
166 185
@@ -201,7 +220,48 @@ mod tests {
201 assert_eq!(db.pending_transactions.read().len(), 2); 220 assert_eq!(db.pending_transactions.read().len(), 2);
202 } 221 }
203 222
204 /// TEST a POST request to /transaction, an endpoint that exists 223 /// Test a POST request to /block, a resource that exists
224 /// https://tools.ietf.org/html/rfc7231#section-6.3.2
225 /// Should accept the json request, create
226 /// the block
227 #[tokio::test]
228 async fn post_block_201() {
229 let db = mocked_db();
230 let filter = consensus_routes(db.clone());
231
232 let res = warp::test::request()
233 .method("POST")
234 .json(&mocked_block())
235 .path("/block")
236 .reply(&filter)
237 .await;
238
239 assert_eq!(res.status(), StatusCode::CREATED);
240 assert_eq!(
241 *db.blockchain.read().hash,
242 "c7d053f3e5b056ba948db3f5c0d30408fb0c29a328a0c3c1cf435fb68d700000".to_owned()
243 );
244 }
245
246 /// Test a POST request to /block, a resource that exists
247 /// https://tools.ietf.org/html/rfc7231#section-6.3.2
248 /// Should reject the block because of the wrong hash
249 #[tokio::test]
250 async fn post_block_wrong_hash() {
251 let db = mocked_db();
252 let filter = consensus_routes(db.clone());
253
254 let res = warp::test::request()
255 .method("POST")
256 .json(&mocked_wrong_block())
257 .path("/block")
258 .reply(&filter)
259 .await;
260
261 assert_eq!(res.status(), StatusCode::BAD_REQUEST);
262 }
263
264 /// Test a POST request to /register, an endpoint that exists
205 /// https://tools.ietf.org/html/rfc7231#section-6.3.2 265 /// https://tools.ietf.org/html/rfc7231#section-6.3.2
206 /// Should accept the json request, create a new user and 266 /// Should accept the json request, create a new user and
207 /// add it to the user hashmap in the db 267 /// add it to the user hashmap in the db
@@ -221,9 +281,10 @@ mod tests {
221 assert_eq!(res.status(), StatusCode::CREATED); 281 assert_eq!(res.status(), StatusCode::CREATED);
222 assert_eq!(db.users.read().len(), 1); 282 assert_eq!(db.users.read().len(), 1);
223 } 283 }
224 /// TEST a POST request to /transaction, an endpoint that exists 284
285 /// Test a POST request to /transaction, an endpoint that exists
225 /// https://tools.ietf.org/html/rfc7231#section-6.3.2 286 /// https://tools.ietf.org/html/rfc7231#section-6.3.2
226 /// Should NOT accept the json request 287 /// Should NOT accept the json request as the user is unpriviliged
227 #[tokio::test] 288 #[tokio::test]
228 async fn post_register_unpriviliged_user() { 289 async fn post_register_unpriviliged_user() {
229 let db = mocked_db(); 290 let db = mocked_db();
@@ -261,6 +322,5 @@ mod tests {
261 } 322 }
262} 323}
263 324
264// TODO: POST block test <09-04-21, yigit> //
265// TODO: POST block without correct transactions test <09-04-21, yigit> // 325// TODO: POST block without correct transactions test <09-04-21, yigit> //
266// TODO: POST transaction while that source has pending transaction test <09-04-21, yigit> // 326// TODO: POST transaction while that source has pending transaction test <09-04-21, yigit> //