script: fix Debug impl bugs in Script, PUSHDATA1 not displayed correctly

This commit is contained in:
Andrew Poelstra 2016-06-24 00:20:49 +00:00
parent 1e47019221
commit 698a23e32d
2 changed files with 7 additions and 4 deletions

View File

@ -1,7 +1,7 @@
[package]
name = "bitcoin"
version = "0.7.1"
version = "0.7.2"
authors = ["Andrew Poelstra <apoelstra@wpsoftware.net>"]
license = "CC0-1.0"
homepage = "https://github.com/apoelstra/rust-bitcoin/"

View File

@ -44,6 +44,7 @@ impl fmt::Debug for Script {
try!(f.write_str("Script("));
while index < self.0.len() {
let opcode = opcodes::All::from(self.0[index]);
index += 1;
let data_len = if let opcodes::Class::PushBytes(n) = opcode.classify() {
n as usize
@ -83,18 +84,17 @@ impl fmt::Debug for Script {
}
};
if index > 0 { try!(f.write_str(" ")); }
if index > 1 { try!(f.write_str(" ")); }
// Write the opcode
if opcode == opcodes::All::OP_PUSHBYTES_0 {
try!(f.write_str("OP_0"));
} else {
try!(write!(f, "{:?}", opcode));
}
index += 1;
// Write any pushdata
if data_len > 0 {
try!(f.write_str(" "));
if index + data_len < self.0.len() {
if index + data_len <= self.0.len() {
for ch in &self.0[index..index + data_len] {
try!(write!(f, "{:02x}", ch));
}
@ -610,6 +610,9 @@ mod test {
"Script(OP_IF OP_IF OP_IF OP_IF OP_IF OP_ENDIF OP_ENDIF OP_ENDIF OP_ENDIF OP_ENDIF OP_0)");
assert_eq!(format!("{}", hex_script!("2102715e91d37d239dea832f1460e91e368115d8ca6cc23a7da966795abad9e3b699ac")),
"Script(OP_PUSHBYTES_33 02715e91d37d239dea832f1460e91e368115d8ca6cc23a7da966795abad9e3b699 OP_CHECKSIG)");
// Elements Alpha peg-out transaction with some signatures removed for brevity. Mainly to test PUSHDATA1
assert_eq!(format!("{}", hex_script!("0047304402202457e78cc1b7f50d0543863c27de75d07982bde8359b9e3316adec0aec165f2f02200203fd331c4e4a4a02f48cf1c291e2c0d6b2f7078a784b5b3649fca41f8794d401004cf1552103244e602b46755f24327142a0517288cebd159eccb6ccf41ea6edf1f601e9af952103bbbacc302d19d29dbfa62d23f37944ae19853cf260c745c2bea739c95328fcb721039227e83246bd51140fe93538b2301c9048be82ef2fb3c7fc5d78426ed6f609ad210229bf310c379b90033e2ecb07f77ecf9b8d59acb623ab7be25a0caed539e2e6472103703e2ed676936f10b3ce9149fa2d4a32060fb86fa9a70a4efe3f21d7ab90611921031e9b7c6022400a6bb0424bbcde14cff6c016b91ee3803926f3440abf5c146d05210334667f975f55a8455d515a2ef1c94fdfa3315f12319a14515d2a13d82831f62f57ae")),
"Script(OP_0 OP_PUSHBYTES_71 304402202457e78cc1b7f50d0543863c27de75d07982bde8359b9e3316adec0aec165f2f02200203fd331c4e4a4a02f48cf1c291e2c0d6b2f7078a784b5b3649fca41f8794d401 OP_0 OP_PUSHDATA1 552103244e602b46755f24327142a0517288cebd159eccb6ccf41ea6edf1f601e9af952103bbbacc302d19d29dbfa62d23f37944ae19853cf260c745c2bea739c95328fcb721039227e83246bd51140fe93538b2301c9048be82ef2fb3c7fc5d78426ed6f609ad210229bf310c379b90033e2ecb07f77ecf9b8d59acb623ab7be25a0caed539e2e6472103703e2ed676936f10b3ce9149fa2d4a32060fb86fa9a70a4efe3f21d7ab90611921031e9b7c6022400a6bb0424bbcde14cff6c016b91ee3803926f3440abf5c146d05210334667f975f55a8455d515a2ef1c94fdfa3315f12319a14515d2a13d82831f62f57ae)");
}
}