Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
E
ece598pv-sp2022
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
qh11
ece598pv-sp2022
Commits
678722eb
Commit
678722eb
authored
3 years ago
by
qh11
Browse files
Options
Downloads
Patches
Plain Diff
Update mod.rs
parent
afc206f4
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/blockchain/mod.rs
+41
-8
41 additions, 8 deletions
src/blockchain/mod.rs
with
41 additions
and
8 deletions
src/blockchain/mod.rs
+
41
−
8
View file @
678722eb
use
crate
::
types
::
block
::
Block
;
use
crate
::
types
::
hash
::
H256
;
use
crate
::
types
::
block
::{
Block
,
generate_genesis_block
};
use
crate
::
types
::
hash
::{
H256
,
Hashable
};
use
std
::
collections
::
HashMap
;
pub
struct
Blockchain
{
pub
blocks
:
HashMap
<
H256
,
Block
>
,
pub
blocks_lens
:
HashMap
<
H256
,
usize
>
,
pub
latest
:
H256
,
}
impl
Blockchain
{
/// Create a new blockchain, only containing the genesis block
pub
fn
new
()
->
Self
{
Self
{}
let
genesis_block
:
Block
=
generate_genesis_block
();
let
mut
blocks
=
HashMap
::
new
();
let
mut
blocks_lens
=
HashMap
::
new
();
let
latest
=
genesis_block
.hash
();
blocks
.insert
(
latest
,
genesis_block
);
blocks_lens
.insert
(
latest
,
0
);
Blockchain
{
blocks
:
blocks
,
latest
:
latest
,
blocks_lens
:
blocks_lens
,
}
}
/// Insert a block into blockchain
pub
fn
insert
(
&
mut
self
,
block
:
&
Block
)
{
unimplemented!
()
pub
fn
insert
(
&
mut
self
,
block
:
&
Block
)
->
bool
{
let
parent
=
block
.get_parent
();
let
block_hash
=
block
.hash
();
if
self
.blocks
.contains_key
(
&
parent
)
{
self
.blocks
.insert
(
block_hash
,
block
.clone
());
let
clen
=
self
.blocks_lens
[
&
parent
]
+
1
;
self
.blocks_lens
.insert
(
block_hash
,
clen
);
if
self
.blocks_lens
[
&
self
.latest
]
<
clen
{
self
.latest
=
block_hash
;
}
true
}
else
{
false
}
}
/// Get the last block's hash of the longest chain
pub
fn
tip
(
&
self
)
->
H256
{
unimplemented!
()
self
.latest
}
/// Get all blocks' hashes of the longest chain, ordered from genesis to the tip
pub
fn
all_blocks_in_longest_chain
(
&
self
)
->
Vec
<
H256
>
{
// unimplemented!()
vec!
[]
let
gen_parent
=
H256
::
from
([
0
;
32
]);
// gen_block_parent
let
mut
result
=
Vec
::
new
();
let
mut
curr
=
self
.latest
;
while
curr
!=
gen_parent
{
result
.push
(
curr
);
curr
=
self
.blocks
[
&
curr
]
.get_parent
();
}
return
result
;
}
}
...
...
@@ -46,4 +79,4 @@ mod tests {
}
}
// DO NOT CHANGE THIS COMMENT, IT IS FOR AUTOGRADER. AFTER TEST
\ No newline at end of file
// DO NOT CHANGE THIS COMMENT, IT IS FOR AUTOGRADER. AFTER TEST
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment