Rejig tests and ensure authorities are addressed consistently.

This commit is contained in:
Gav
2018-01-14 23:41:32 +01:00
parent bd8f67a20d
commit adbeecbdcf
2 changed files with 43 additions and 37 deletions
+19 -23
View File
@@ -114,13 +114,9 @@ impl OverlayedChanges {
pub trait Error: 'static + fmt::Debug + fmt::Display + Send {}
impl<E> Error for E where E: 'static + fmt::Debug + fmt::Display + Send {}
fn value_vec(mut value: usize, initial: Vec<u8>) -> Vec<u8> {
let mut acc = initial;
while value > 0 {
acc.push(value as u8);
value /= 256;
}
acc
fn to_keyed_vec(value: u32, mut prepend: Vec<u8>) -> Vec<u8> {
prepend.extend((0..::std::mem::size_of::<u32>()).into_iter().map(|i| (value >> (i * 8)) as u8));
prepend
}
/// Externalities: pinned to specific active address.
@@ -134,12 +130,12 @@ pub trait Externalities {
/// Set storage of current contract being called (effective immediately).
fn set_storage(&mut self, key: Vec<u8>, value: Vec<u8>);
/// Get the current set of validators.
fn validators(&self) -> Result<Vec<&[u8]>, Self::Error> {
(0..self.storage(b"\0validator_count")?.into_iter()
/// Get the current set of authorities.
fn authorities(&self) -> Result<Vec<&[u8]>, Self::Error> {
(0..self.storage(b"con\0aut\0len")?.into_iter()
.rev()
.fold(0, |acc, &i| (acc << 8) + (i as usize)))
.map(|i| self.storage(&value_vec(i, b"\0validator".to_vec())))
.fold(0, |acc, &i| (acc << 8) + (i as u32)))
.map(|i| self.storage(&to_keyed_vec(i, b"con\0aut\0".to_vec())))
.collect()
}
}
@@ -248,22 +244,22 @@ mod tests {
}
#[test]
fn validators_call_works() {
fn authorities_call_works() {
let mut ext = TestExternalities::default();
assert_eq!(ext.validators(), Ok(vec![]));
assert_eq!(ext.authorities(), Ok(vec![]));
ext.set_storage(b"\0validator_count".to_vec(), vec![]);
assert_eq!(ext.validators(), Ok(vec![]));
ext.set_storage(b"con\0aut\0len".to_vec(), vec![0u8; 4]);
assert_eq!(ext.authorities(), Ok(vec![]));
ext.set_storage(b"\0validator_count".to_vec(), vec![1]);
assert_eq!(ext.validators(), Ok(vec![&[][..]]));
ext.set_storage(b"con\0aut\0len".to_vec(), vec![1u8, 0, 0, 0]);
assert_eq!(ext.authorities(), Ok(vec![&[][..]]));
ext.set_storage(b"\0validator".to_vec(), b"first".to_vec());
assert_eq!(ext.validators(), Ok(vec![&b"first"[..]]));
ext.set_storage(b"con\0aut\0\0\0\0\0".to_vec(), b"first".to_vec());
assert_eq!(ext.authorities(), Ok(vec![&b"first"[..]]));
ext.set_storage(b"\0validator_count".to_vec(), vec![2]);
ext.set_storage(b"\0validator\x01".to_vec(), b"second".to_vec());
assert_eq!(ext.validators(), Ok(vec![&b"first"[..], &b"second"[..]]));
ext.set_storage(b"con\0aut\0len".to_vec(), vec![2u8, 0, 0, 0]);
ext.set_storage(b"con\0aut\0\x01\0\0\0".to_vec(), b"second".to_vec());
assert_eq!(ext.authorities(), Ok(vec![&b"first"[..], &b"second"[..]]));
}
}