Replace UTXO::is_internal with script_type

This means less conversion and logic mapping from bool to ScriptType and
back again.
This commit is contained in:
LLFourn
2020-12-04 10:37:58 +11:00
parent a89dd85833
commit 8dcb75dfa4
10 changed files with 54 additions and 75 deletions

View File

@@ -55,7 +55,7 @@ macro_rules! impl_batch_operations {
let key = MapKey::UTXO(Some(&utxo.outpoint)).as_map_key();
let value = json!({
"t": utxo.txout,
"i": utxo.is_internal,
"i": utxo.script_type,
});
self.insert(key, serde_json::to_vec(&value)?)$($after_insert)*;
@@ -130,9 +130,9 @@ macro_rules! impl_batch_operations {
Some(b) => {
let mut val: serde_json::Value = serde_json::from_slice(&b)?;
let txout = serde_json::from_value(val["t"].take())?;
let is_internal = serde_json::from_value(val["i"].take())?;
let script_type = serde_json::from_value(val["i"].take())?;
Ok(Some(UTXO { outpoint: outpoint.clone(), txout, is_internal }))
Ok(Some(UTXO { outpoint: outpoint.clone(), txout, script_type }))
}
}
}
@@ -243,12 +243,12 @@ impl Database for Tree {
let mut val: serde_json::Value = serde_json::from_slice(&v)?;
let txout = serde_json::from_value(val["t"].take())?;
let is_internal = serde_json::from_value(val["i"].take())?;
let script_type = serde_json::from_value(val["i"].take())?;
Ok(UTXO {
outpoint,
txout,
is_internal,
script_type,
})
})
.collect()
@@ -311,12 +311,12 @@ impl Database for Tree {
.map(|b| -> Result<_, Error> {
let mut val: serde_json::Value = serde_json::from_slice(&b)?;
let txout = serde_json::from_value(val["t"].take())?;
let is_internal = serde_json::from_value(val["i"].take())?;
let script_type = serde_json::from_value(val["i"].take())?;
Ok(UTXO {
outpoint: *outpoint,
txout,
is_internal,
script_type,
})
})
.transpose()

View File

@@ -160,7 +160,7 @@ impl BatchOperations for MemoryDatabase {
fn set_utxo(&mut self, utxo: &UTXO) -> Result<(), Error> {
let key = MapKey::UTXO(Some(&utxo.outpoint)).as_map_key();
self.map
.insert(key, Box::new((utxo.txout.clone(), utxo.is_internal)));
.insert(key, Box::new((utxo.txout.clone(), utxo.script_type)));
Ok(())
}
@@ -231,11 +231,11 @@ impl BatchOperations for MemoryDatabase {
match res {
None => Ok(None),
Some(b) => {
let (txout, is_internal) = b.downcast_ref().cloned().unwrap();
let (txout, script_type) = b.downcast_ref().cloned().unwrap();
Ok(Some(UTXO {
outpoint: *outpoint,
txout,
is_internal,
script_type,
}))
}
}
@@ -322,11 +322,11 @@ impl Database for MemoryDatabase {
.range::<Vec<u8>, _>((Included(&key), Excluded(&after(&key))))
.map(|(k, v)| {
let outpoint = deserialize(&k[1..]).unwrap();
let (txout, is_internal) = v.downcast_ref().cloned().unwrap();
let (txout, script_type) = v.downcast_ref().cloned().unwrap();
Ok(UTXO {
outpoint,
txout,
is_internal,
script_type,
})
})
.collect()
@@ -385,11 +385,11 @@ impl Database for MemoryDatabase {
fn get_utxo(&self, outpoint: &OutPoint) -> Result<Option<UTXO>, Error> {
let key = MapKey::UTXO(Some(outpoint)).as_map_key();
Ok(self.map.get(&key).map(|b| {
let (txout, is_internal) = b.downcast_ref().cloned().unwrap();
let (txout, script_type) = b.downcast_ref().cloned().unwrap();
UTXO {
outpoint: *outpoint,
txout,
is_internal,
script_type,
}
}))
}
@@ -507,7 +507,7 @@ impl MemoryDatabase {
txid,
vout: vout as u32,
},
is_internal: false,
script_type: ScriptType::External,
})
.unwrap();
}

View File

@@ -301,7 +301,7 @@ pub mod test {
let utxo = UTXO {
txout,
outpoint,
is_internal: false,
script_type: ScriptType::External,
};
tree.set_utxo(&utxo).unwrap();