Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
convert to safe but extremely verbose type conversion.
Browse files Browse the repository at this point in the history
@rphmeier any more concise way of doing this?
  • Loading branch information
gavofyork committed Dec 11, 2017
1 parent 13dcd89 commit 84b9f84
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions state_machine/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ keccak-hash = "0.1.0"
patricia-trie = "0.1.0"
memorydb = "0.1.1"
triehash = "0.1"
byteorder = "1.1"
8 changes: 6 additions & 2 deletions state_machine/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,14 @@ impl Backend for InMemory {
self.inner.update(changes);

// fully recalculate trie roots.
use std::mem::transmute;
let storage_roots = self.inner.storage.iter().map(|(object, storage)| {
let flat_trie = storage.iter().map(|(k, v)| (k.to_vec(), v.clone())).collect();
(unsafe { transmute::<u64, [u8; 8]>(object.to_be()) }.to_vec(), sec_trie_root(flat_trie).to_vec())
({
use byteorder::{BigEndian, ByteOrder};
let mut r = [0u8; 8];
BigEndian::write_u64(&mut r, *object);
r.to_vec()

This comment has been minimized.

Copy link
@rphmeier

rphmeier Dec 12, 2017

Contributor

this is about as concise as you can get, UFCS could get you to

<byteorder::BE as byteorder::ByteOrder>::write_u64(&mut r, *object) and save a line

This comment has been minimized.

Copy link
@rphmeier

rphmeier Dec 12, 2017

Contributor

i favor the verbosity over increased audit surface. the code is at least very readable and understandable here.

}, sec_trie_root(flat_trie).to_vec())
}).collect();

let storage_tree_root = H256(sec_trie_root(storage_roots).0);
Expand Down
2 changes: 2 additions & 0 deletions state_machine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ extern crate keccak_hash;
extern crate patricia_trie;
extern crate triehash;

extern crate byteorder;

use std::collections::HashMap;
use std::fmt;

Expand Down

0 comments on commit 84b9f84

Please sign in to comment.