wallet: Add version information to Orchard commitment tree data
We add both C++ client version information (enabling version-specific changes if necessary, matching the rest of the C++ wallet), and record version information (enabling local serialization format changes). Part of zcash/zcash#5784.
This commit is contained in:
parent
d651f22e86
commit
2d6cb93125
|
@ -1177,6 +1177,8 @@ pub extern "C" fn orchard_wallet_gc_note_commitment_tree(wallet: *mut Wallet) {
|
||||||
wallet.witness_tree.garbage_collect();
|
wallet.witness_tree.garbage_collect();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const NOTE_STATE_V1: u8 = 1;
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "C" fn orchard_wallet_write_note_commitment_tree(
|
pub extern "C" fn orchard_wallet_write_note_commitment_tree(
|
||||||
wallet: *const Wallet,
|
wallet: *const Wallet,
|
||||||
|
@ -1186,7 +1188,7 @@ pub extern "C" fn orchard_wallet_write_note_commitment_tree(
|
||||||
let wallet = unsafe { wallet.as_ref() }.expect("Wallet pointer may not be null.");
|
let wallet = unsafe { wallet.as_ref() }.expect("Wallet pointer may not be null.");
|
||||||
let mut writer = CppStreamWriter::from_raw_parts(stream, write_cb.unwrap());
|
let mut writer = CppStreamWriter::from_raw_parts(stream, write_cb.unwrap());
|
||||||
|
|
||||||
let mut write_all = move || -> io::Result<()> {
|
let write_v1 = move |mut writer: CppStreamWriter| -> io::Result<()> {
|
||||||
Optional::write(&mut writer, wallet.last_checkpoint, |w, h| {
|
Optional::write(&mut writer, wallet.last_checkpoint, |w, h| {
|
||||||
w.write_u32::<LittleEndian>(h.into())
|
w.write_u32::<LittleEndian>(h.into())
|
||||||
})?;
|
})?;
|
||||||
|
@ -1212,7 +1214,10 @@ pub extern "C" fn orchard_wallet_write_note_commitment_tree(
|
||||||
Ok(())
|
Ok(())
|
||||||
};
|
};
|
||||||
|
|
||||||
match write_all() {
|
match writer
|
||||||
|
.write_u8(NOTE_STATE_V1)
|
||||||
|
.and_then(|()| write_v1(writer))
|
||||||
|
{
|
||||||
Ok(()) => true,
|
Ok(()) => true,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
error!("Failure in writing Orchard note commitment tree: {}", e);
|
error!("Failure in writing Orchard note commitment tree: {}", e);
|
||||||
|
@ -1230,7 +1235,7 @@ pub extern "C" fn orchard_wallet_load_note_commitment_tree(
|
||||||
let wallet = unsafe { wallet.as_mut() }.expect("Wallet pointer may not be null.");
|
let wallet = unsafe { wallet.as_mut() }.expect("Wallet pointer may not be null.");
|
||||||
let mut reader = CppStreamReader::from_raw_parts(stream, read_cb.unwrap());
|
let mut reader = CppStreamReader::from_raw_parts(stream, read_cb.unwrap());
|
||||||
|
|
||||||
let mut read_all = move || -> io::Result<()> {
|
let mut read_v1 = move |mut reader: CppStreamReader| -> io::Result<()> {
|
||||||
let last_checkpoint = Optional::read(&mut reader, |r| {
|
let last_checkpoint = Optional::read(&mut reader, |r| {
|
||||||
r.read_u32::<LittleEndian>().map(BlockHeight::from)
|
r.read_u32::<LittleEndian>().map(BlockHeight::from)
|
||||||
})?;
|
})?;
|
||||||
|
@ -1257,7 +1262,15 @@ pub extern "C" fn orchard_wallet_load_note_commitment_tree(
|
||||||
Ok(())
|
Ok(())
|
||||||
};
|
};
|
||||||
|
|
||||||
match read_all() {
|
match reader.read_u8() {
|
||||||
|
Err(e) => {
|
||||||
|
error!(
|
||||||
|
"Failed to read Orchard note position serialization flag: {}",
|
||||||
|
e
|
||||||
|
);
|
||||||
|
false
|
||||||
|
}
|
||||||
|
Ok(NOTE_STATE_V1) => match read_v1(reader) {
|
||||||
Ok(_) => true,
|
Ok(_) => true,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
error!(
|
error!(
|
||||||
|
@ -1266,5 +1279,13 @@ pub extern "C" fn orchard_wallet_load_note_commitment_tree(
|
||||||
);
|
);
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
Ok(flag) => {
|
||||||
|
error!(
|
||||||
|
"Unrecognized Orchard note position serialization version: {}",
|
||||||
|
flag
|
||||||
|
);
|
||||||
|
false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -485,6 +485,10 @@ public:
|
||||||
|
|
||||||
template<typename Stream>
|
template<typename Stream>
|
||||||
void Serialize(Stream& s) const {
|
void Serialize(Stream& s) const {
|
||||||
|
int nVersion = s.GetVersion();
|
||||||
|
if (!(s.GetType() & SER_GETHASH)) {
|
||||||
|
::Serialize(s, nVersion);
|
||||||
|
}
|
||||||
RustStream rs(s);
|
RustStream rs(s);
|
||||||
if (!orchard_wallet_write_note_commitment_tree(
|
if (!orchard_wallet_write_note_commitment_tree(
|
||||||
wallet.inner.get(),
|
wallet.inner.get(),
|
||||||
|
@ -503,6 +507,10 @@ public:
|
||||||
|
|
||||||
template<typename Stream>
|
template<typename Stream>
|
||||||
void Unserialize(Stream& s) {
|
void Unserialize(Stream& s) {
|
||||||
|
int nVersion = s.GetVersion();
|
||||||
|
if (!(s.GetType() & SER_GETHASH)) {
|
||||||
|
::Unserialize(s, nVersion);
|
||||||
|
}
|
||||||
RustStream rs(s);
|
RustStream rs(s);
|
||||||
if (!orchard_wallet_load_note_commitment_tree(
|
if (!orchard_wallet_load_note_commitment_tree(
|
||||||
wallet.inner.get(),
|
wallet.inner.get(),
|
||||||
|
|
Loading…
Reference in New Issue