From 2fe5dd568b10ba6043ca739db4915ef433cc66d8 Mon Sep 17 00:00:00 2001 From: duanyytop Date: Wed, 11 May 2022 14:13:19 +0800 Subject: [PATCH 01/10] feat: Add leaf keys and values to claim entries --- src/transfer.mol | 12 ++- src/transfer.rs | 262 ++++++++++++++++++++++++++++++++++------------- 2 files changed, 199 insertions(+), 75 deletions(-) diff --git a/src/transfer.mol b/src/transfer.mol index d1cad8d..a030e16 100644 --- a/src/transfer.mol +++ b/src/transfer.mol @@ -58,8 +58,13 @@ table ClaimCotaNFTV2Entries { claim_keys: ClaimCotaNFTKeyVec, claim_values: ClaimCotaNFTValueVec, proof: Bytes, - withdrawal_proof: Bytes, action: Bytes, + + // The leaf_keys and leaf_values are used to verify withdraw smt proof + leaf_keys: H256Vec, + leaf_values: H256Vec, + + withdrawal_proof: Bytes, raw_tx: RawTransaction, output_index: Uint32, tx_proof: TransactionProof, @@ -71,6 +76,11 @@ table TransferCotaNFTV2Entries { withdrawal_keys: WithdrawalCotaNFTKeyV1Vec, withdrawal_values: WithdrawalCotaNFTValueV1Vec, proof: Bytes, + + // The leaf_keys and leaf_values are used to verify withdraw smt proof + leaf_keys: H256Vec, + leaf_values: H256Vec, + withdrawal_proof: Bytes, action: Bytes, raw_tx: RawTransaction, diff --git a/src/transfer.rs b/src/transfer.rs index d7f9318..121b08d 100644 --- a/src/transfer.rs +++ b/src/transfer.rs @@ -2172,8 +2172,10 @@ impl ::core::fmt::Display for ClaimCotaNFTV2Entries { write!(f, ", {}: {}", "claim_keys", self.claim_keys())?; write!(f, ", {}: {}", "claim_values", self.claim_values())?; write!(f, ", {}: {}", "proof", self.proof())?; - write!(f, ", {}: {}", "withdrawal_proof", self.withdrawal_proof())?; write!(f, ", {}: {}", "action", self.action())?; + write!(f, ", {}: {}", "leaf_keys", self.leaf_keys())?; + write!(f, ", {}: {}", "leaf_values", self.leaf_values())?; + write!(f, ", {}: {}", "withdrawal_proof", self.withdrawal_proof())?; write!(f, ", {}: {}", "raw_tx", self.raw_tx())?; write!(f, ", {}: {}", "output_index", self.output_index())?; write!(f, ", {}: {}", "tx_proof", self.tx_proof())?; @@ -2187,19 +2189,20 @@ impl ::core::fmt::Display for ClaimCotaNFTV2Entries { impl ::core::default::Default for ClaimCotaNFTV2Entries { fn default() -> Self { let v: Vec = vec![ - 192, 0, 0, 0, 44, 0, 0, 0, 48, 0, 0, 0, 52, 0, 0, 0, 56, 0, 0, 0, 60, 0, 0, 0, 64, 0, - 0, 0, 68, 0, 0, 0, 72, 0, 0, 0, 124, 0, 0, 0, 128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 52, 0, 0, 0, 28, 0, 0, 0, 32, - 0, 0, 0, 36, 0, 0, 0, 40, 0, 0, 0, 44, 0, 0, 0, 48, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 64, 0, 0, 0, 12, 0, 0, 0, 44, + 208, 0, 0, 0, 52, 0, 0, 0, 56, 0, 0, 0, 60, 0, 0, 0, 64, 0, 0, 0, 68, 0, 0, 0, 72, 0, + 0, 0, 76, 0, 0, 0, 80, 0, 0, 0, 84, 0, 0, 0, 88, 0, 0, 0, 140, 0, 0, 0, 144, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 20, 0, 0, 0, 12, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 52, 0, 0, 0, 28, 0, 0, 0, 32, 0, 0, 0, 36, 0, 0, 0, 40, 0, 0, 0, + 44, 0, 0, 0, 48, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, + 4, 0, 0, 0, 0, 0, 0, 0, 64, 0, 0, 0, 12, 0, 0, 0, 44, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 20, 0, 0, 0, + 12, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ]; ClaimCotaNFTV2Entries::new_unchecked(v.into()) } } impl ClaimCotaNFTV2Entries { - pub const FIELD_COUNT: usize = 10; + pub const FIELD_COUNT: usize = 12; pub fn total_size(&self) -> usize { molecule::unpack_number(self.as_slice()) as usize @@ -2256,39 +2259,53 @@ impl ClaimCotaNFTV2Entries { Bytes::new_unchecked(self.0.slice(start..end)) } - pub fn withdrawal_proof(&self) -> Bytes { + pub fn action(&self) -> Bytes { let slice = self.as_slice(); let start = molecule::unpack_number(&slice[24..]) as usize; let end = molecule::unpack_number(&slice[28..]) as usize; Bytes::new_unchecked(self.0.slice(start..end)) } - pub fn action(&self) -> Bytes { + pub fn leaf_keys(&self) -> H256Vec { let slice = self.as_slice(); let start = molecule::unpack_number(&slice[28..]) as usize; let end = molecule::unpack_number(&slice[32..]) as usize; - Bytes::new_unchecked(self.0.slice(start..end)) + H256Vec::new_unchecked(self.0.slice(start..end)) } - pub fn raw_tx(&self) -> RawTransaction { + pub fn leaf_values(&self) -> H256Vec { let slice = self.as_slice(); let start = molecule::unpack_number(&slice[32..]) as usize; let end = molecule::unpack_number(&slice[36..]) as usize; - RawTransaction::new_unchecked(self.0.slice(start..end)) + H256Vec::new_unchecked(self.0.slice(start..end)) } - pub fn output_index(&self) -> Uint32 { + pub fn withdrawal_proof(&self) -> Bytes { let slice = self.as_slice(); let start = molecule::unpack_number(&slice[36..]) as usize; let end = molecule::unpack_number(&slice[40..]) as usize; + Bytes::new_unchecked(self.0.slice(start..end)) + } + + pub fn raw_tx(&self) -> RawTransaction { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[40..]) as usize; + let end = molecule::unpack_number(&slice[44..]) as usize; + RawTransaction::new_unchecked(self.0.slice(start..end)) + } + + pub fn output_index(&self) -> Uint32 { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[44..]) as usize; + let end = molecule::unpack_number(&slice[48..]) as usize; Uint32::new_unchecked(self.0.slice(start..end)) } pub fn tx_proof(&self) -> TransactionProof { let slice = self.as_slice(); - let start = molecule::unpack_number(&slice[40..]) as usize; + let start = molecule::unpack_number(&slice[48..]) as usize; if self.has_extra_fields() { - let end = molecule::unpack_number(&slice[44..]) as usize; + let end = molecule::unpack_number(&slice[52..]) as usize; TransactionProof::new_unchecked(self.0.slice(start..end)) } else { TransactionProof::new_unchecked(self.0.slice(start..)) @@ -2335,8 +2352,10 @@ impl molecule::prelude::Entity for ClaimCotaNFTV2Entries { .claim_keys(self.claim_keys()) .claim_values(self.claim_values()) .proof(self.proof()) - .withdrawal_proof(self.withdrawal_proof()) .action(self.action()) + .leaf_keys(self.leaf_keys()) + .leaf_values(self.leaf_values()) + .withdrawal_proof(self.withdrawal_proof()) .raw_tx(self.raw_tx()) .output_index(self.output_index()) .tx_proof(self.tx_proof()) @@ -2366,8 +2385,10 @@ impl<'r> ::core::fmt::Display for ClaimCotaNFTV2EntriesReader<'r> { write!(f, ", {}: {}", "claim_keys", self.claim_keys())?; write!(f, ", {}: {}", "claim_values", self.claim_values())?; write!(f, ", {}: {}", "proof", self.proof())?; - write!(f, ", {}: {}", "withdrawal_proof", self.withdrawal_proof())?; write!(f, ", {}: {}", "action", self.action())?; + write!(f, ", {}: {}", "leaf_keys", self.leaf_keys())?; + write!(f, ", {}: {}", "leaf_values", self.leaf_values())?; + write!(f, ", {}: {}", "withdrawal_proof", self.withdrawal_proof())?; write!(f, ", {}: {}", "raw_tx", self.raw_tx())?; write!(f, ", {}: {}", "output_index", self.output_index())?; write!(f, ", {}: {}", "tx_proof", self.tx_proof())?; @@ -2379,7 +2400,7 @@ impl<'r> ::core::fmt::Display for ClaimCotaNFTV2EntriesReader<'r> { } } impl<'r> ClaimCotaNFTV2EntriesReader<'r> { - pub const FIELD_COUNT: usize = 10; + pub const FIELD_COUNT: usize = 12; pub fn total_size(&self) -> usize { molecule::unpack_number(self.as_slice()) as usize @@ -2436,39 +2457,53 @@ impl<'r> ClaimCotaNFTV2EntriesReader<'r> { BytesReader::new_unchecked(&self.as_slice()[start..end]) } - pub fn withdrawal_proof(&self) -> BytesReader<'r> { + pub fn action(&self) -> BytesReader<'r> { let slice = self.as_slice(); let start = molecule::unpack_number(&slice[24..]) as usize; let end = molecule::unpack_number(&slice[28..]) as usize; BytesReader::new_unchecked(&self.as_slice()[start..end]) } - pub fn action(&self) -> BytesReader<'r> { + pub fn leaf_keys(&self) -> H256VecReader<'r> { let slice = self.as_slice(); let start = molecule::unpack_number(&slice[28..]) as usize; let end = molecule::unpack_number(&slice[32..]) as usize; - BytesReader::new_unchecked(&self.as_slice()[start..end]) + H256VecReader::new_unchecked(&self.as_slice()[start..end]) } - pub fn raw_tx(&self) -> RawTransactionReader<'r> { + pub fn leaf_values(&self) -> H256VecReader<'r> { let slice = self.as_slice(); let start = molecule::unpack_number(&slice[32..]) as usize; let end = molecule::unpack_number(&slice[36..]) as usize; - RawTransactionReader::new_unchecked(&self.as_slice()[start..end]) + H256VecReader::new_unchecked(&self.as_slice()[start..end]) } - pub fn output_index(&self) -> Uint32Reader<'r> { + pub fn withdrawal_proof(&self) -> BytesReader<'r> { let slice = self.as_slice(); let start = molecule::unpack_number(&slice[36..]) as usize; let end = molecule::unpack_number(&slice[40..]) as usize; + BytesReader::new_unchecked(&self.as_slice()[start..end]) + } + + pub fn raw_tx(&self) -> RawTransactionReader<'r> { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[40..]) as usize; + let end = molecule::unpack_number(&slice[44..]) as usize; + RawTransactionReader::new_unchecked(&self.as_slice()[start..end]) + } + + pub fn output_index(&self) -> Uint32Reader<'r> { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[44..]) as usize; + let end = molecule::unpack_number(&slice[48..]) as usize; Uint32Reader::new_unchecked(&self.as_slice()[start..end]) } pub fn tx_proof(&self) -> TransactionProofReader<'r> { let slice = self.as_slice(); - let start = molecule::unpack_number(&slice[40..]) as usize; + let start = molecule::unpack_number(&slice[48..]) as usize; if self.has_extra_fields() { - let end = molecule::unpack_number(&slice[44..]) as usize; + let end = molecule::unpack_number(&slice[52..]) as usize; TransactionProofReader::new_unchecked(&self.as_slice()[start..end]) } else { TransactionProofReader::new_unchecked(&self.as_slice()[start..]) @@ -2535,10 +2570,12 @@ impl<'r> molecule::prelude::Reader<'r> for ClaimCotaNFTV2EntriesReader<'r> { ClaimCotaNFTValueVecReader::verify(&slice[offsets[3]..offsets[4]], compatible)?; BytesReader::verify(&slice[offsets[4]..offsets[5]], compatible)?; BytesReader::verify(&slice[offsets[5]..offsets[6]], compatible)?; - BytesReader::verify(&slice[offsets[6]..offsets[7]], compatible)?; - RawTransactionReader::verify(&slice[offsets[7]..offsets[8]], compatible)?; - Uint32Reader::verify(&slice[offsets[8]..offsets[9]], compatible)?; - TransactionProofReader::verify(&slice[offsets[9]..offsets[10]], compatible)?; + H256VecReader::verify(&slice[offsets[6]..offsets[7]], compatible)?; + H256VecReader::verify(&slice[offsets[7]..offsets[8]], compatible)?; + BytesReader::verify(&slice[offsets[8]..offsets[9]], compatible)?; + RawTransactionReader::verify(&slice[offsets[9]..offsets[10]], compatible)?; + Uint32Reader::verify(&slice[offsets[10]..offsets[11]], compatible)?; + TransactionProofReader::verify(&slice[offsets[11]..offsets[12]], compatible)?; Ok(()) } } @@ -2549,14 +2586,16 @@ pub struct ClaimCotaNFTV2EntriesBuilder { pub(crate) claim_keys: ClaimCotaNFTKeyVec, pub(crate) claim_values: ClaimCotaNFTValueVec, pub(crate) proof: Bytes, - pub(crate) withdrawal_proof: Bytes, pub(crate) action: Bytes, + pub(crate) leaf_keys: H256Vec, + pub(crate) leaf_values: H256Vec, + pub(crate) withdrawal_proof: Bytes, pub(crate) raw_tx: RawTransaction, pub(crate) output_index: Uint32, pub(crate) tx_proof: TransactionProof, } impl ClaimCotaNFTV2EntriesBuilder { - pub const FIELD_COUNT: usize = 10; + pub const FIELD_COUNT: usize = 12; pub fn hold_keys(mut self, v: HoldCotaNFTKeyVec) -> Self { self.hold_keys = v; @@ -2583,13 +2622,23 @@ impl ClaimCotaNFTV2EntriesBuilder { self } - pub fn withdrawal_proof(mut self, v: Bytes) -> Self { - self.withdrawal_proof = v; + pub fn action(mut self, v: Bytes) -> Self { + self.action = v; self } - pub fn action(mut self, v: Bytes) -> Self { - self.action = v; + pub fn leaf_keys(mut self, v: H256Vec) -> Self { + self.leaf_keys = v; + self + } + + pub fn leaf_values(mut self, v: H256Vec) -> Self { + self.leaf_values = v; + self + } + + pub fn withdrawal_proof(mut self, v: Bytes) -> Self { + self.withdrawal_proof = v; self } @@ -2620,8 +2669,10 @@ impl molecule::prelude::Builder for ClaimCotaNFTV2EntriesBuilder { + self.claim_keys.as_slice().len() + self.claim_values.as_slice().len() + self.proof.as_slice().len() - + self.withdrawal_proof.as_slice().len() + self.action.as_slice().len() + + self.leaf_keys.as_slice().len() + + self.leaf_values.as_slice().len() + + self.withdrawal_proof.as_slice().len() + self.raw_tx.as_slice().len() + self.output_index.as_slice().len() + self.tx_proof.as_slice().len() @@ -2641,10 +2692,14 @@ impl molecule::prelude::Builder for ClaimCotaNFTV2EntriesBuilder { offsets.push(total_size); total_size += self.proof.as_slice().len(); offsets.push(total_size); - total_size += self.withdrawal_proof.as_slice().len(); - offsets.push(total_size); total_size += self.action.as_slice().len(); offsets.push(total_size); + total_size += self.leaf_keys.as_slice().len(); + offsets.push(total_size); + total_size += self.leaf_values.as_slice().len(); + offsets.push(total_size); + total_size += self.withdrawal_proof.as_slice().len(); + offsets.push(total_size); total_size += self.raw_tx.as_slice().len(); offsets.push(total_size); total_size += self.output_index.as_slice().len(); @@ -2659,8 +2714,10 @@ impl molecule::prelude::Builder for ClaimCotaNFTV2EntriesBuilder { writer.write_all(self.claim_keys.as_slice())?; writer.write_all(self.claim_values.as_slice())?; writer.write_all(self.proof.as_slice())?; - writer.write_all(self.withdrawal_proof.as_slice())?; writer.write_all(self.action.as_slice())?; + writer.write_all(self.leaf_keys.as_slice())?; + writer.write_all(self.leaf_values.as_slice())?; + writer.write_all(self.withdrawal_proof.as_slice())?; writer.write_all(self.raw_tx.as_slice())?; writer.write_all(self.output_index.as_slice())?; writer.write_all(self.tx_proof.as_slice())?; @@ -2698,6 +2755,8 @@ impl ::core::fmt::Display for TransferCotaNFTV2Entries { write!(f, ", {}: {}", "withdrawal_keys", self.withdrawal_keys())?; write!(f, ", {}: {}", "withdrawal_values", self.withdrawal_values())?; write!(f, ", {}: {}", "proof", self.proof())?; + write!(f, ", {}: {}", "leaf_keys", self.leaf_keys())?; + write!(f, ", {}: {}", "leaf_values", self.leaf_values())?; write!(f, ", {}: {}", "withdrawal_proof", self.withdrawal_proof())?; write!(f, ", {}: {}", "action", self.action())?; write!(f, ", {}: {}", "raw_tx", self.raw_tx())?; @@ -2713,19 +2772,20 @@ impl ::core::fmt::Display for TransferCotaNFTV2Entries { impl ::core::default::Default for TransferCotaNFTV2Entries { fn default() -> Self { let v: Vec = vec![ - 192, 0, 0, 0, 44, 0, 0, 0, 48, 0, 0, 0, 52, 0, 0, 0, 56, 0, 0, 0, 60, 0, 0, 0, 64, 0, - 0, 0, 68, 0, 0, 0, 72, 0, 0, 0, 124, 0, 0, 0, 128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 52, 0, 0, 0, 28, 0, 0, 0, 32, - 0, 0, 0, 36, 0, 0, 0, 40, 0, 0, 0, 44, 0, 0, 0, 48, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 64, 0, 0, 0, 12, 0, 0, 0, 44, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 20, 0, 0, 0, 12, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 208, 0, 0, 0, 52, 0, 0, 0, 56, 0, 0, 0, 60, 0, 0, 0, 64, 0, 0, 0, 68, 0, 0, 0, 72, 0, + 0, 0, 76, 0, 0, 0, 80, 0, 0, 0, 84, 0, 0, 0, 88, 0, 0, 0, 140, 0, 0, 0, 144, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 52, 0, 0, 0, 28, 0, 0, 0, 32, 0, 0, 0, 36, 0, 0, 0, 40, 0, 0, 0, + 44, 0, 0, 0, 48, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, + 4, 0, 0, 0, 0, 0, 0, 0, 64, 0, 0, 0, 12, 0, 0, 0, 44, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 20, 0, 0, 0, + 12, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ]; TransferCotaNFTV2Entries::new_unchecked(v.into()) } } impl TransferCotaNFTV2Entries { - pub const FIELD_COUNT: usize = 10; + pub const FIELD_COUNT: usize = 12; pub fn total_size(&self) -> usize { molecule::unpack_number(self.as_slice()) as usize @@ -2782,39 +2842,53 @@ impl TransferCotaNFTV2Entries { Bytes::new_unchecked(self.0.slice(start..end)) } - pub fn withdrawal_proof(&self) -> Bytes { + pub fn leaf_keys(&self) -> H256Vec { let slice = self.as_slice(); let start = molecule::unpack_number(&slice[24..]) as usize; let end = molecule::unpack_number(&slice[28..]) as usize; - Bytes::new_unchecked(self.0.slice(start..end)) + H256Vec::new_unchecked(self.0.slice(start..end)) } - pub fn action(&self) -> Bytes { + pub fn leaf_values(&self) -> H256Vec { let slice = self.as_slice(); let start = molecule::unpack_number(&slice[28..]) as usize; let end = molecule::unpack_number(&slice[32..]) as usize; - Bytes::new_unchecked(self.0.slice(start..end)) + H256Vec::new_unchecked(self.0.slice(start..end)) } - pub fn raw_tx(&self) -> RawTransaction { + pub fn withdrawal_proof(&self) -> Bytes { let slice = self.as_slice(); let start = molecule::unpack_number(&slice[32..]) as usize; let end = molecule::unpack_number(&slice[36..]) as usize; - RawTransaction::new_unchecked(self.0.slice(start..end)) + Bytes::new_unchecked(self.0.slice(start..end)) } - pub fn output_index(&self) -> Uint32 { + pub fn action(&self) -> Bytes { let slice = self.as_slice(); let start = molecule::unpack_number(&slice[36..]) as usize; let end = molecule::unpack_number(&slice[40..]) as usize; + Bytes::new_unchecked(self.0.slice(start..end)) + } + + pub fn raw_tx(&self) -> RawTransaction { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[40..]) as usize; + let end = molecule::unpack_number(&slice[44..]) as usize; + RawTransaction::new_unchecked(self.0.slice(start..end)) + } + + pub fn output_index(&self) -> Uint32 { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[44..]) as usize; + let end = molecule::unpack_number(&slice[48..]) as usize; Uint32::new_unchecked(self.0.slice(start..end)) } pub fn tx_proof(&self) -> TransactionProof { let slice = self.as_slice(); - let start = molecule::unpack_number(&slice[40..]) as usize; + let start = molecule::unpack_number(&slice[48..]) as usize; if self.has_extra_fields() { - let end = molecule::unpack_number(&slice[44..]) as usize; + let end = molecule::unpack_number(&slice[52..]) as usize; TransactionProof::new_unchecked(self.0.slice(start..end)) } else { TransactionProof::new_unchecked(self.0.slice(start..)) @@ -2862,6 +2936,8 @@ impl molecule::prelude::Entity for TransferCotaNFTV2Entries { .withdrawal_keys(self.withdrawal_keys()) .withdrawal_values(self.withdrawal_values()) .proof(self.proof()) + .leaf_keys(self.leaf_keys()) + .leaf_values(self.leaf_values()) .withdrawal_proof(self.withdrawal_proof()) .action(self.action()) .raw_tx(self.raw_tx()) @@ -2893,6 +2969,8 @@ impl<'r> ::core::fmt::Display for TransferCotaNFTV2EntriesReader<'r> { write!(f, ", {}: {}", "withdrawal_keys", self.withdrawal_keys())?; write!(f, ", {}: {}", "withdrawal_values", self.withdrawal_values())?; write!(f, ", {}: {}", "proof", self.proof())?; + write!(f, ", {}: {}", "leaf_keys", self.leaf_keys())?; + write!(f, ", {}: {}", "leaf_values", self.leaf_values())?; write!(f, ", {}: {}", "withdrawal_proof", self.withdrawal_proof())?; write!(f, ", {}: {}", "action", self.action())?; write!(f, ", {}: {}", "raw_tx", self.raw_tx())?; @@ -2906,7 +2984,7 @@ impl<'r> ::core::fmt::Display for TransferCotaNFTV2EntriesReader<'r> { } } impl<'r> TransferCotaNFTV2EntriesReader<'r> { - pub const FIELD_COUNT: usize = 10; + pub const FIELD_COUNT: usize = 12; pub fn total_size(&self) -> usize { molecule::unpack_number(self.as_slice()) as usize @@ -2963,39 +3041,53 @@ impl<'r> TransferCotaNFTV2EntriesReader<'r> { BytesReader::new_unchecked(&self.as_slice()[start..end]) } - pub fn withdrawal_proof(&self) -> BytesReader<'r> { + pub fn leaf_keys(&self) -> H256VecReader<'r> { let slice = self.as_slice(); let start = molecule::unpack_number(&slice[24..]) as usize; let end = molecule::unpack_number(&slice[28..]) as usize; - BytesReader::new_unchecked(&self.as_slice()[start..end]) + H256VecReader::new_unchecked(&self.as_slice()[start..end]) } - pub fn action(&self) -> BytesReader<'r> { + pub fn leaf_values(&self) -> H256VecReader<'r> { let slice = self.as_slice(); let start = molecule::unpack_number(&slice[28..]) as usize; let end = molecule::unpack_number(&slice[32..]) as usize; - BytesReader::new_unchecked(&self.as_slice()[start..end]) + H256VecReader::new_unchecked(&self.as_slice()[start..end]) } - pub fn raw_tx(&self) -> RawTransactionReader<'r> { + pub fn withdrawal_proof(&self) -> BytesReader<'r> { let slice = self.as_slice(); let start = molecule::unpack_number(&slice[32..]) as usize; let end = molecule::unpack_number(&slice[36..]) as usize; - RawTransactionReader::new_unchecked(&self.as_slice()[start..end]) + BytesReader::new_unchecked(&self.as_slice()[start..end]) } - pub fn output_index(&self) -> Uint32Reader<'r> { + pub fn action(&self) -> BytesReader<'r> { let slice = self.as_slice(); let start = molecule::unpack_number(&slice[36..]) as usize; let end = molecule::unpack_number(&slice[40..]) as usize; + BytesReader::new_unchecked(&self.as_slice()[start..end]) + } + + pub fn raw_tx(&self) -> RawTransactionReader<'r> { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[40..]) as usize; + let end = molecule::unpack_number(&slice[44..]) as usize; + RawTransactionReader::new_unchecked(&self.as_slice()[start..end]) + } + + pub fn output_index(&self) -> Uint32Reader<'r> { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[44..]) as usize; + let end = molecule::unpack_number(&slice[48..]) as usize; Uint32Reader::new_unchecked(&self.as_slice()[start..end]) } pub fn tx_proof(&self) -> TransactionProofReader<'r> { let slice = self.as_slice(); - let start = molecule::unpack_number(&slice[40..]) as usize; + let start = molecule::unpack_number(&slice[48..]) as usize; if self.has_extra_fields() { - let end = molecule::unpack_number(&slice[44..]) as usize; + let end = molecule::unpack_number(&slice[52..]) as usize; TransactionProofReader::new_unchecked(&self.as_slice()[start..end]) } else { TransactionProofReader::new_unchecked(&self.as_slice()[start..]) @@ -3061,11 +3153,13 @@ impl<'r> molecule::prelude::Reader<'r> for TransferCotaNFTV2EntriesReader<'r> { WithdrawalCotaNFTKeyV1VecReader::verify(&slice[offsets[2]..offsets[3]], compatible)?; WithdrawalCotaNFTValueV1VecReader::verify(&slice[offsets[3]..offsets[4]], compatible)?; BytesReader::verify(&slice[offsets[4]..offsets[5]], compatible)?; - BytesReader::verify(&slice[offsets[5]..offsets[6]], compatible)?; - BytesReader::verify(&slice[offsets[6]..offsets[7]], compatible)?; - RawTransactionReader::verify(&slice[offsets[7]..offsets[8]], compatible)?; - Uint32Reader::verify(&slice[offsets[8]..offsets[9]], compatible)?; - TransactionProofReader::verify(&slice[offsets[9]..offsets[10]], compatible)?; + H256VecReader::verify(&slice[offsets[5]..offsets[6]], compatible)?; + H256VecReader::verify(&slice[offsets[6]..offsets[7]], compatible)?; + BytesReader::verify(&slice[offsets[7]..offsets[8]], compatible)?; + BytesReader::verify(&slice[offsets[8]..offsets[9]], compatible)?; + RawTransactionReader::verify(&slice[offsets[9]..offsets[10]], compatible)?; + Uint32Reader::verify(&slice[offsets[10]..offsets[11]], compatible)?; + TransactionProofReader::verify(&slice[offsets[11]..offsets[12]], compatible)?; Ok(()) } } @@ -3076,6 +3170,8 @@ pub struct TransferCotaNFTV2EntriesBuilder { pub(crate) withdrawal_keys: WithdrawalCotaNFTKeyV1Vec, pub(crate) withdrawal_values: WithdrawalCotaNFTValueV1Vec, pub(crate) proof: Bytes, + pub(crate) leaf_keys: H256Vec, + pub(crate) leaf_values: H256Vec, pub(crate) withdrawal_proof: Bytes, pub(crate) action: Bytes, pub(crate) raw_tx: RawTransaction, @@ -3083,7 +3179,7 @@ pub struct TransferCotaNFTV2EntriesBuilder { pub(crate) tx_proof: TransactionProof, } impl TransferCotaNFTV2EntriesBuilder { - pub const FIELD_COUNT: usize = 10; + pub const FIELD_COUNT: usize = 12; pub fn claim_keys(mut self, v: ClaimCotaNFTKeyVec) -> Self { self.claim_keys = v; @@ -3110,6 +3206,16 @@ impl TransferCotaNFTV2EntriesBuilder { self } + pub fn leaf_keys(mut self, v: H256Vec) -> Self { + self.leaf_keys = v; + self + } + + pub fn leaf_values(mut self, v: H256Vec) -> Self { + self.leaf_values = v; + self + } + pub fn withdrawal_proof(mut self, v: Bytes) -> Self { self.withdrawal_proof = v; self @@ -3147,6 +3253,8 @@ impl molecule::prelude::Builder for TransferCotaNFTV2EntriesBuilder { + self.withdrawal_keys.as_slice().len() + self.withdrawal_values.as_slice().len() + self.proof.as_slice().len() + + self.leaf_keys.as_slice().len() + + self.leaf_values.as_slice().len() + self.withdrawal_proof.as_slice().len() + self.action.as_slice().len() + self.raw_tx.as_slice().len() @@ -3168,6 +3276,10 @@ impl molecule::prelude::Builder for TransferCotaNFTV2EntriesBuilder { offsets.push(total_size); total_size += self.proof.as_slice().len(); offsets.push(total_size); + total_size += self.leaf_keys.as_slice().len(); + offsets.push(total_size); + total_size += self.leaf_values.as_slice().len(); + offsets.push(total_size); total_size += self.withdrawal_proof.as_slice().len(); offsets.push(total_size); total_size += self.action.as_slice().len(); @@ -3186,6 +3298,8 @@ impl molecule::prelude::Builder for TransferCotaNFTV2EntriesBuilder { writer.write_all(self.withdrawal_keys.as_slice())?; writer.write_all(self.withdrawal_values.as_slice())?; writer.write_all(self.proof.as_slice())?; + writer.write_all(self.leaf_keys.as_slice())?; + writer.write_all(self.leaf_values.as_slice())?; writer.write_all(self.withdrawal_proof.as_slice())?; writer.write_all(self.action.as_slice())?; writer.write_all(self.raw_tx.as_slice())?; From c289c517b55fcd2e7d411be2324ca3cb7f8648da Mon Sep 17 00:00:00 2001 From: duanyytop Date: Wed, 11 May 2022 14:34:48 +0800 Subject: [PATCH 02/10] feat: Add leaf keys and values to claim update --- src/transfer.mol | 2 - src/transfer_update.mol | 8 ++ src/transfer_update.rs | 248 +++++++++++++++++++++++++++++----------- 3 files changed, 189 insertions(+), 69 deletions(-) diff --git a/src/transfer.mol b/src/transfer.mol index a030e16..b4617b5 100644 --- a/src/transfer.mol +++ b/src/transfer.mol @@ -63,7 +63,6 @@ table ClaimCotaNFTV2Entries { // The leaf_keys and leaf_values are used to verify withdraw smt proof leaf_keys: H256Vec, leaf_values: H256Vec, - withdrawal_proof: Bytes, raw_tx: RawTransaction, output_index: Uint32, @@ -80,7 +79,6 @@ table TransferCotaNFTV2Entries { // The leaf_keys and leaf_values are used to verify withdraw smt proof leaf_keys: H256Vec, leaf_values: H256Vec, - withdrawal_proof: Bytes, action: Bytes, raw_tx: RawTransaction, diff --git a/src/transfer_update.mol b/src/transfer_update.mol index 2bce460..a572a5d 100644 --- a/src/transfer_update.mol +++ b/src/transfer_update.mol @@ -40,6 +40,10 @@ table ClaimUpdateCotaNFTV2Entries { claim_keys: ClaimCotaNFTKeyVec, claim_infos: ClaimCotaNFTInfoVec, proof: Bytes, + + // The leaf_keys and leaf_values are used to verify withdraw smt proof + leaf_keys: H256Vec, + leaf_values: H256Vec, withdrawal_proof: Bytes, action: Bytes, raw_tx: RawTransaction, @@ -52,6 +56,10 @@ table TransferUpdateCotaNFTV2Entries { claim_infos: ClaimCotaNFTInfoVec, withdrawal_keys: WithdrawalCotaNFTKeyV1Vec, withdrawal_values: WithdrawalCotaNFTValueV1Vec, + + // The leaf_keys and leaf_values are used to verify withdraw smt proof + leaf_keys: H256Vec, + leaf_values: H256Vec, proof: Bytes, withdrawal_proof: Bytes, action: Bytes, diff --git a/src/transfer_update.rs b/src/transfer_update.rs index 7b29403..36965b8 100644 --- a/src/transfer_update.rs +++ b/src/transfer_update.rs @@ -1354,6 +1354,8 @@ impl ::core::fmt::Display for ClaimUpdateCotaNFTV2Entries { write!(f, ", {}: {}", "claim_keys", self.claim_keys())?; write!(f, ", {}: {}", "claim_infos", self.claim_infos())?; write!(f, ", {}: {}", "proof", self.proof())?; + write!(f, ", {}: {}", "leaf_keys", self.leaf_keys())?; + write!(f, ", {}: {}", "leaf_values", self.leaf_values())?; write!(f, ", {}: {}", "withdrawal_proof", self.withdrawal_proof())?; write!(f, ", {}: {}", "action", self.action())?; write!(f, ", {}: {}", "raw_tx", self.raw_tx())?; @@ -1369,19 +1371,20 @@ impl ::core::fmt::Display for ClaimUpdateCotaNFTV2Entries { impl ::core::default::Default for ClaimUpdateCotaNFTV2Entries { fn default() -> Self { let v: Vec = vec![ - 192, 0, 0, 0, 44, 0, 0, 0, 48, 0, 0, 0, 52, 0, 0, 0, 56, 0, 0, 0, 60, 0, 0, 0, 64, 0, - 0, 0, 68, 0, 0, 0, 72, 0, 0, 0, 124, 0, 0, 0, 128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 52, 0, 0, 0, 28, 0, 0, 0, 32, - 0, 0, 0, 36, 0, 0, 0, 40, 0, 0, 0, 44, 0, 0, 0, 48, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 64, 0, 0, 0, 12, 0, 0, 0, 44, + 208, 0, 0, 0, 52, 0, 0, 0, 56, 0, 0, 0, 60, 0, 0, 0, 64, 0, 0, 0, 68, 0, 0, 0, 72, 0, + 0, 0, 76, 0, 0, 0, 80, 0, 0, 0, 84, 0, 0, 0, 88, 0, 0, 0, 140, 0, 0, 0, 144, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 20, 0, 0, 0, 12, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 52, 0, 0, 0, 28, 0, 0, 0, 32, 0, 0, 0, 36, 0, 0, 0, 40, 0, 0, 0, + 44, 0, 0, 0, 48, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, + 4, 0, 0, 0, 0, 0, 0, 0, 64, 0, 0, 0, 12, 0, 0, 0, 44, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 20, 0, 0, 0, + 12, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ]; ClaimUpdateCotaNFTV2Entries::new_unchecked(v.into()) } } impl ClaimUpdateCotaNFTV2Entries { - pub const FIELD_COUNT: usize = 10; + pub const FIELD_COUNT: usize = 12; pub fn total_size(&self) -> usize { molecule::unpack_number(self.as_slice()) as usize @@ -1438,39 +1441,53 @@ impl ClaimUpdateCotaNFTV2Entries { Bytes::new_unchecked(self.0.slice(start..end)) } - pub fn withdrawal_proof(&self) -> Bytes { + pub fn leaf_keys(&self) -> H256Vec { let slice = self.as_slice(); let start = molecule::unpack_number(&slice[24..]) as usize; let end = molecule::unpack_number(&slice[28..]) as usize; - Bytes::new_unchecked(self.0.slice(start..end)) + H256Vec::new_unchecked(self.0.slice(start..end)) } - pub fn action(&self) -> Bytes { + pub fn leaf_values(&self) -> H256Vec { let slice = self.as_slice(); let start = molecule::unpack_number(&slice[28..]) as usize; let end = molecule::unpack_number(&slice[32..]) as usize; - Bytes::new_unchecked(self.0.slice(start..end)) + H256Vec::new_unchecked(self.0.slice(start..end)) } - pub fn raw_tx(&self) -> RawTransaction { + pub fn withdrawal_proof(&self) -> Bytes { let slice = self.as_slice(); let start = molecule::unpack_number(&slice[32..]) as usize; let end = molecule::unpack_number(&slice[36..]) as usize; - RawTransaction::new_unchecked(self.0.slice(start..end)) + Bytes::new_unchecked(self.0.slice(start..end)) } - pub fn output_index(&self) -> Uint32 { + pub fn action(&self) -> Bytes { let slice = self.as_slice(); let start = molecule::unpack_number(&slice[36..]) as usize; let end = molecule::unpack_number(&slice[40..]) as usize; + Bytes::new_unchecked(self.0.slice(start..end)) + } + + pub fn raw_tx(&self) -> RawTransaction { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[40..]) as usize; + let end = molecule::unpack_number(&slice[44..]) as usize; + RawTransaction::new_unchecked(self.0.slice(start..end)) + } + + pub fn output_index(&self) -> Uint32 { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[44..]) as usize; + let end = molecule::unpack_number(&slice[48..]) as usize; Uint32::new_unchecked(self.0.slice(start..end)) } pub fn tx_proof(&self) -> TransactionProof { let slice = self.as_slice(); - let start = molecule::unpack_number(&slice[40..]) as usize; + let start = molecule::unpack_number(&slice[48..]) as usize; if self.has_extra_fields() { - let end = molecule::unpack_number(&slice[44..]) as usize; + let end = molecule::unpack_number(&slice[52..]) as usize; TransactionProof::new_unchecked(self.0.slice(start..end)) } else { TransactionProof::new_unchecked(self.0.slice(start..)) @@ -1518,6 +1535,8 @@ impl molecule::prelude::Entity for ClaimUpdateCotaNFTV2Entries { .claim_keys(self.claim_keys()) .claim_infos(self.claim_infos()) .proof(self.proof()) + .leaf_keys(self.leaf_keys()) + .leaf_values(self.leaf_values()) .withdrawal_proof(self.withdrawal_proof()) .action(self.action()) .raw_tx(self.raw_tx()) @@ -1549,6 +1568,8 @@ impl<'r> ::core::fmt::Display for ClaimUpdateCotaNFTV2EntriesReader<'r> { write!(f, ", {}: {}", "claim_keys", self.claim_keys())?; write!(f, ", {}: {}", "claim_infos", self.claim_infos())?; write!(f, ", {}: {}", "proof", self.proof())?; + write!(f, ", {}: {}", "leaf_keys", self.leaf_keys())?; + write!(f, ", {}: {}", "leaf_values", self.leaf_values())?; write!(f, ", {}: {}", "withdrawal_proof", self.withdrawal_proof())?; write!(f, ", {}: {}", "action", self.action())?; write!(f, ", {}: {}", "raw_tx", self.raw_tx())?; @@ -1562,7 +1583,7 @@ impl<'r> ::core::fmt::Display for ClaimUpdateCotaNFTV2EntriesReader<'r> { } } impl<'r> ClaimUpdateCotaNFTV2EntriesReader<'r> { - pub const FIELD_COUNT: usize = 10; + pub const FIELD_COUNT: usize = 12; pub fn total_size(&self) -> usize { molecule::unpack_number(self.as_slice()) as usize @@ -1619,39 +1640,53 @@ impl<'r> ClaimUpdateCotaNFTV2EntriesReader<'r> { BytesReader::new_unchecked(&self.as_slice()[start..end]) } - pub fn withdrawal_proof(&self) -> BytesReader<'r> { + pub fn leaf_keys(&self) -> H256VecReader<'r> { let slice = self.as_slice(); let start = molecule::unpack_number(&slice[24..]) as usize; let end = molecule::unpack_number(&slice[28..]) as usize; - BytesReader::new_unchecked(&self.as_slice()[start..end]) + H256VecReader::new_unchecked(&self.as_slice()[start..end]) } - pub fn action(&self) -> BytesReader<'r> { + pub fn leaf_values(&self) -> H256VecReader<'r> { let slice = self.as_slice(); let start = molecule::unpack_number(&slice[28..]) as usize; let end = molecule::unpack_number(&slice[32..]) as usize; - BytesReader::new_unchecked(&self.as_slice()[start..end]) + H256VecReader::new_unchecked(&self.as_slice()[start..end]) } - pub fn raw_tx(&self) -> RawTransactionReader<'r> { + pub fn withdrawal_proof(&self) -> BytesReader<'r> { let slice = self.as_slice(); let start = molecule::unpack_number(&slice[32..]) as usize; let end = molecule::unpack_number(&slice[36..]) as usize; - RawTransactionReader::new_unchecked(&self.as_slice()[start..end]) + BytesReader::new_unchecked(&self.as_slice()[start..end]) } - pub fn output_index(&self) -> Uint32Reader<'r> { + pub fn action(&self) -> BytesReader<'r> { let slice = self.as_slice(); let start = molecule::unpack_number(&slice[36..]) as usize; let end = molecule::unpack_number(&slice[40..]) as usize; + BytesReader::new_unchecked(&self.as_slice()[start..end]) + } + + pub fn raw_tx(&self) -> RawTransactionReader<'r> { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[40..]) as usize; + let end = molecule::unpack_number(&slice[44..]) as usize; + RawTransactionReader::new_unchecked(&self.as_slice()[start..end]) + } + + pub fn output_index(&self) -> Uint32Reader<'r> { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[44..]) as usize; + let end = molecule::unpack_number(&slice[48..]) as usize; Uint32Reader::new_unchecked(&self.as_slice()[start..end]) } pub fn tx_proof(&self) -> TransactionProofReader<'r> { let slice = self.as_slice(); - let start = molecule::unpack_number(&slice[40..]) as usize; + let start = molecule::unpack_number(&slice[48..]) as usize; if self.has_extra_fields() { - let end = molecule::unpack_number(&slice[44..]) as usize; + let end = molecule::unpack_number(&slice[52..]) as usize; TransactionProofReader::new_unchecked(&self.as_slice()[start..end]) } else { TransactionProofReader::new_unchecked(&self.as_slice()[start..]) @@ -1717,11 +1752,13 @@ impl<'r> molecule::prelude::Reader<'r> for ClaimUpdateCotaNFTV2EntriesReader<'r> ClaimCotaNFTKeyVecReader::verify(&slice[offsets[2]..offsets[3]], compatible)?; ClaimCotaNFTInfoVecReader::verify(&slice[offsets[3]..offsets[4]], compatible)?; BytesReader::verify(&slice[offsets[4]..offsets[5]], compatible)?; - BytesReader::verify(&slice[offsets[5]..offsets[6]], compatible)?; - BytesReader::verify(&slice[offsets[6]..offsets[7]], compatible)?; - RawTransactionReader::verify(&slice[offsets[7]..offsets[8]], compatible)?; - Uint32Reader::verify(&slice[offsets[8]..offsets[9]], compatible)?; - TransactionProofReader::verify(&slice[offsets[9]..offsets[10]], compatible)?; + H256VecReader::verify(&slice[offsets[5]..offsets[6]], compatible)?; + H256VecReader::verify(&slice[offsets[6]..offsets[7]], compatible)?; + BytesReader::verify(&slice[offsets[7]..offsets[8]], compatible)?; + BytesReader::verify(&slice[offsets[8]..offsets[9]], compatible)?; + RawTransactionReader::verify(&slice[offsets[9]..offsets[10]], compatible)?; + Uint32Reader::verify(&slice[offsets[10]..offsets[11]], compatible)?; + TransactionProofReader::verify(&slice[offsets[11]..offsets[12]], compatible)?; Ok(()) } } @@ -1732,6 +1769,8 @@ pub struct ClaimUpdateCotaNFTV2EntriesBuilder { pub(crate) claim_keys: ClaimCotaNFTKeyVec, pub(crate) claim_infos: ClaimCotaNFTInfoVec, pub(crate) proof: Bytes, + pub(crate) leaf_keys: H256Vec, + pub(crate) leaf_values: H256Vec, pub(crate) withdrawal_proof: Bytes, pub(crate) action: Bytes, pub(crate) raw_tx: RawTransaction, @@ -1739,7 +1778,7 @@ pub struct ClaimUpdateCotaNFTV2EntriesBuilder { pub(crate) tx_proof: TransactionProof, } impl ClaimUpdateCotaNFTV2EntriesBuilder { - pub const FIELD_COUNT: usize = 10; + pub const FIELD_COUNT: usize = 12; pub fn hold_keys(mut self, v: HoldCotaNFTKeyVec) -> Self { self.hold_keys = v; @@ -1766,6 +1805,16 @@ impl ClaimUpdateCotaNFTV2EntriesBuilder { self } + pub fn leaf_keys(mut self, v: H256Vec) -> Self { + self.leaf_keys = v; + self + } + + pub fn leaf_values(mut self, v: H256Vec) -> Self { + self.leaf_values = v; + self + } + pub fn withdrawal_proof(mut self, v: Bytes) -> Self { self.withdrawal_proof = v; self @@ -1803,6 +1852,8 @@ impl molecule::prelude::Builder for ClaimUpdateCotaNFTV2EntriesBuilder { + self.claim_keys.as_slice().len() + self.claim_infos.as_slice().len() + self.proof.as_slice().len() + + self.leaf_keys.as_slice().len() + + self.leaf_values.as_slice().len() + self.withdrawal_proof.as_slice().len() + self.action.as_slice().len() + self.raw_tx.as_slice().len() @@ -1824,6 +1875,10 @@ impl molecule::prelude::Builder for ClaimUpdateCotaNFTV2EntriesBuilder { offsets.push(total_size); total_size += self.proof.as_slice().len(); offsets.push(total_size); + total_size += self.leaf_keys.as_slice().len(); + offsets.push(total_size); + total_size += self.leaf_values.as_slice().len(); + offsets.push(total_size); total_size += self.withdrawal_proof.as_slice().len(); offsets.push(total_size); total_size += self.action.as_slice().len(); @@ -1842,6 +1897,8 @@ impl molecule::prelude::Builder for ClaimUpdateCotaNFTV2EntriesBuilder { writer.write_all(self.claim_keys.as_slice())?; writer.write_all(self.claim_infos.as_slice())?; writer.write_all(self.proof.as_slice())?; + writer.write_all(self.leaf_keys.as_slice())?; + writer.write_all(self.leaf_values.as_slice())?; writer.write_all(self.withdrawal_proof.as_slice())?; writer.write_all(self.action.as_slice())?; writer.write_all(self.raw_tx.as_slice())?; @@ -1880,6 +1937,8 @@ impl ::core::fmt::Display for TransferUpdateCotaNFTV2Entries { write!(f, ", {}: {}", "claim_infos", self.claim_infos())?; write!(f, ", {}: {}", "withdrawal_keys", self.withdrawal_keys())?; write!(f, ", {}: {}", "withdrawal_values", self.withdrawal_values())?; + write!(f, ", {}: {}", "leaf_keys", self.leaf_keys())?; + write!(f, ", {}: {}", "leaf_values", self.leaf_values())?; write!(f, ", {}: {}", "proof", self.proof())?; write!(f, ", {}: {}", "withdrawal_proof", self.withdrawal_proof())?; write!(f, ", {}: {}", "action", self.action())?; @@ -1896,19 +1955,20 @@ impl ::core::fmt::Display for TransferUpdateCotaNFTV2Entries { impl ::core::default::Default for TransferUpdateCotaNFTV2Entries { fn default() -> Self { let v: Vec = vec![ - 192, 0, 0, 0, 44, 0, 0, 0, 48, 0, 0, 0, 52, 0, 0, 0, 56, 0, 0, 0, 60, 0, 0, 0, 64, 0, - 0, 0, 68, 0, 0, 0, 72, 0, 0, 0, 124, 0, 0, 0, 128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 52, 0, 0, 0, 28, 0, 0, 0, 32, - 0, 0, 0, 36, 0, 0, 0, 40, 0, 0, 0, 44, 0, 0, 0, 48, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 64, 0, 0, 0, 12, 0, 0, 0, 44, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 20, 0, 0, 0, 12, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 208, 0, 0, 0, 52, 0, 0, 0, 56, 0, 0, 0, 60, 0, 0, 0, 64, 0, 0, 0, 68, 0, 0, 0, 72, 0, + 0, 0, 76, 0, 0, 0, 80, 0, 0, 0, 84, 0, 0, 0, 88, 0, 0, 0, 140, 0, 0, 0, 144, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 52, 0, 0, 0, 28, 0, 0, 0, 32, 0, 0, 0, 36, 0, 0, 0, 40, 0, 0, 0, + 44, 0, 0, 0, 48, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, + 4, 0, 0, 0, 0, 0, 0, 0, 64, 0, 0, 0, 12, 0, 0, 0, 44, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 20, 0, 0, 0, + 12, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ]; TransferUpdateCotaNFTV2Entries::new_unchecked(v.into()) } } impl TransferUpdateCotaNFTV2Entries { - pub const FIELD_COUNT: usize = 10; + pub const FIELD_COUNT: usize = 12; pub fn total_size(&self) -> usize { molecule::unpack_number(self.as_slice()) as usize @@ -1958,46 +2018,60 @@ impl TransferUpdateCotaNFTV2Entries { WithdrawalCotaNFTValueV1Vec::new_unchecked(self.0.slice(start..end)) } - pub fn proof(&self) -> Bytes { + pub fn leaf_keys(&self) -> H256Vec { let slice = self.as_slice(); let start = molecule::unpack_number(&slice[20..]) as usize; let end = molecule::unpack_number(&slice[24..]) as usize; - Bytes::new_unchecked(self.0.slice(start..end)) + H256Vec::new_unchecked(self.0.slice(start..end)) } - pub fn withdrawal_proof(&self) -> Bytes { + pub fn leaf_values(&self) -> H256Vec { let slice = self.as_slice(); let start = molecule::unpack_number(&slice[24..]) as usize; let end = molecule::unpack_number(&slice[28..]) as usize; - Bytes::new_unchecked(self.0.slice(start..end)) + H256Vec::new_unchecked(self.0.slice(start..end)) } - pub fn action(&self) -> Bytes { + pub fn proof(&self) -> Bytes { let slice = self.as_slice(); let start = molecule::unpack_number(&slice[28..]) as usize; let end = molecule::unpack_number(&slice[32..]) as usize; Bytes::new_unchecked(self.0.slice(start..end)) } - pub fn raw_tx(&self) -> RawTransaction { + pub fn withdrawal_proof(&self) -> Bytes { let slice = self.as_slice(); let start = molecule::unpack_number(&slice[32..]) as usize; let end = molecule::unpack_number(&slice[36..]) as usize; - RawTransaction::new_unchecked(self.0.slice(start..end)) + Bytes::new_unchecked(self.0.slice(start..end)) } - pub fn output_index(&self) -> Uint32 { + pub fn action(&self) -> Bytes { let slice = self.as_slice(); let start = molecule::unpack_number(&slice[36..]) as usize; let end = molecule::unpack_number(&slice[40..]) as usize; + Bytes::new_unchecked(self.0.slice(start..end)) + } + + pub fn raw_tx(&self) -> RawTransaction { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[40..]) as usize; + let end = molecule::unpack_number(&slice[44..]) as usize; + RawTransaction::new_unchecked(self.0.slice(start..end)) + } + + pub fn output_index(&self) -> Uint32 { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[44..]) as usize; + let end = molecule::unpack_number(&slice[48..]) as usize; Uint32::new_unchecked(self.0.slice(start..end)) } pub fn tx_proof(&self) -> TransactionProof { let slice = self.as_slice(); - let start = molecule::unpack_number(&slice[40..]) as usize; + let start = molecule::unpack_number(&slice[48..]) as usize; if self.has_extra_fields() { - let end = molecule::unpack_number(&slice[44..]) as usize; + let end = molecule::unpack_number(&slice[52..]) as usize; TransactionProof::new_unchecked(self.0.slice(start..end)) } else { TransactionProof::new_unchecked(self.0.slice(start..)) @@ -2044,6 +2118,8 @@ impl molecule::prelude::Entity for TransferUpdateCotaNFTV2Entries { .claim_infos(self.claim_infos()) .withdrawal_keys(self.withdrawal_keys()) .withdrawal_values(self.withdrawal_values()) + .leaf_keys(self.leaf_keys()) + .leaf_values(self.leaf_values()) .proof(self.proof()) .withdrawal_proof(self.withdrawal_proof()) .action(self.action()) @@ -2075,6 +2151,8 @@ impl<'r> ::core::fmt::Display for TransferUpdateCotaNFTV2EntriesReader<'r> { write!(f, ", {}: {}", "claim_infos", self.claim_infos())?; write!(f, ", {}: {}", "withdrawal_keys", self.withdrawal_keys())?; write!(f, ", {}: {}", "withdrawal_values", self.withdrawal_values())?; + write!(f, ", {}: {}", "leaf_keys", self.leaf_keys())?; + write!(f, ", {}: {}", "leaf_values", self.leaf_values())?; write!(f, ", {}: {}", "proof", self.proof())?; write!(f, ", {}: {}", "withdrawal_proof", self.withdrawal_proof())?; write!(f, ", {}: {}", "action", self.action())?; @@ -2089,7 +2167,7 @@ impl<'r> ::core::fmt::Display for TransferUpdateCotaNFTV2EntriesReader<'r> { } } impl<'r> TransferUpdateCotaNFTV2EntriesReader<'r> { - pub const FIELD_COUNT: usize = 10; + pub const FIELD_COUNT: usize = 12; pub fn total_size(&self) -> usize { molecule::unpack_number(self.as_slice()) as usize @@ -2139,46 +2217,60 @@ impl<'r> TransferUpdateCotaNFTV2EntriesReader<'r> { WithdrawalCotaNFTValueV1VecReader::new_unchecked(&self.as_slice()[start..end]) } - pub fn proof(&self) -> BytesReader<'r> { + pub fn leaf_keys(&self) -> H256VecReader<'r> { let slice = self.as_slice(); let start = molecule::unpack_number(&slice[20..]) as usize; let end = molecule::unpack_number(&slice[24..]) as usize; - BytesReader::new_unchecked(&self.as_slice()[start..end]) + H256VecReader::new_unchecked(&self.as_slice()[start..end]) } - pub fn withdrawal_proof(&self) -> BytesReader<'r> { + pub fn leaf_values(&self) -> H256VecReader<'r> { let slice = self.as_slice(); let start = molecule::unpack_number(&slice[24..]) as usize; let end = molecule::unpack_number(&slice[28..]) as usize; - BytesReader::new_unchecked(&self.as_slice()[start..end]) + H256VecReader::new_unchecked(&self.as_slice()[start..end]) } - pub fn action(&self) -> BytesReader<'r> { + pub fn proof(&self) -> BytesReader<'r> { let slice = self.as_slice(); let start = molecule::unpack_number(&slice[28..]) as usize; let end = molecule::unpack_number(&slice[32..]) as usize; BytesReader::new_unchecked(&self.as_slice()[start..end]) } - pub fn raw_tx(&self) -> RawTransactionReader<'r> { + pub fn withdrawal_proof(&self) -> BytesReader<'r> { let slice = self.as_slice(); let start = molecule::unpack_number(&slice[32..]) as usize; let end = molecule::unpack_number(&slice[36..]) as usize; - RawTransactionReader::new_unchecked(&self.as_slice()[start..end]) + BytesReader::new_unchecked(&self.as_slice()[start..end]) } - pub fn output_index(&self) -> Uint32Reader<'r> { + pub fn action(&self) -> BytesReader<'r> { let slice = self.as_slice(); let start = molecule::unpack_number(&slice[36..]) as usize; let end = molecule::unpack_number(&slice[40..]) as usize; + BytesReader::new_unchecked(&self.as_slice()[start..end]) + } + + pub fn raw_tx(&self) -> RawTransactionReader<'r> { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[40..]) as usize; + let end = molecule::unpack_number(&slice[44..]) as usize; + RawTransactionReader::new_unchecked(&self.as_slice()[start..end]) + } + + pub fn output_index(&self) -> Uint32Reader<'r> { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[44..]) as usize; + let end = molecule::unpack_number(&slice[48..]) as usize; Uint32Reader::new_unchecked(&self.as_slice()[start..end]) } pub fn tx_proof(&self) -> TransactionProofReader<'r> { let slice = self.as_slice(); - let start = molecule::unpack_number(&slice[40..]) as usize; + let start = molecule::unpack_number(&slice[48..]) as usize; if self.has_extra_fields() { - let end = molecule::unpack_number(&slice[44..]) as usize; + let end = molecule::unpack_number(&slice[52..]) as usize; TransactionProofReader::new_unchecked(&self.as_slice()[start..end]) } else { TransactionProofReader::new_unchecked(&self.as_slice()[start..]) @@ -2243,12 +2335,14 @@ impl<'r> molecule::prelude::Reader<'r> for TransferUpdateCotaNFTV2EntriesReader< ClaimCotaNFTInfoVecReader::verify(&slice[offsets[1]..offsets[2]], compatible)?; WithdrawalCotaNFTKeyV1VecReader::verify(&slice[offsets[2]..offsets[3]], compatible)?; WithdrawalCotaNFTValueV1VecReader::verify(&slice[offsets[3]..offsets[4]], compatible)?; - BytesReader::verify(&slice[offsets[4]..offsets[5]], compatible)?; - BytesReader::verify(&slice[offsets[5]..offsets[6]], compatible)?; + H256VecReader::verify(&slice[offsets[4]..offsets[5]], compatible)?; + H256VecReader::verify(&slice[offsets[5]..offsets[6]], compatible)?; BytesReader::verify(&slice[offsets[6]..offsets[7]], compatible)?; - RawTransactionReader::verify(&slice[offsets[7]..offsets[8]], compatible)?; - Uint32Reader::verify(&slice[offsets[8]..offsets[9]], compatible)?; - TransactionProofReader::verify(&slice[offsets[9]..offsets[10]], compatible)?; + BytesReader::verify(&slice[offsets[7]..offsets[8]], compatible)?; + BytesReader::verify(&slice[offsets[8]..offsets[9]], compatible)?; + RawTransactionReader::verify(&slice[offsets[9]..offsets[10]], compatible)?; + Uint32Reader::verify(&slice[offsets[10]..offsets[11]], compatible)?; + TransactionProofReader::verify(&slice[offsets[11]..offsets[12]], compatible)?; Ok(()) } } @@ -2258,6 +2352,8 @@ pub struct TransferUpdateCotaNFTV2EntriesBuilder { pub(crate) claim_infos: ClaimCotaNFTInfoVec, pub(crate) withdrawal_keys: WithdrawalCotaNFTKeyV1Vec, pub(crate) withdrawal_values: WithdrawalCotaNFTValueV1Vec, + pub(crate) leaf_keys: H256Vec, + pub(crate) leaf_values: H256Vec, pub(crate) proof: Bytes, pub(crate) withdrawal_proof: Bytes, pub(crate) action: Bytes, @@ -2266,7 +2362,7 @@ pub struct TransferUpdateCotaNFTV2EntriesBuilder { pub(crate) tx_proof: TransactionProof, } impl TransferUpdateCotaNFTV2EntriesBuilder { - pub const FIELD_COUNT: usize = 10; + pub const FIELD_COUNT: usize = 12; pub fn claim_keys(mut self, v: ClaimCotaNFTKeyVec) -> Self { self.claim_keys = v; @@ -2288,6 +2384,16 @@ impl TransferUpdateCotaNFTV2EntriesBuilder { self } + pub fn leaf_keys(mut self, v: H256Vec) -> Self { + self.leaf_keys = v; + self + } + + pub fn leaf_values(mut self, v: H256Vec) -> Self { + self.leaf_values = v; + self + } + pub fn proof(mut self, v: Bytes) -> Self { self.proof = v; self @@ -2329,6 +2435,8 @@ impl molecule::prelude::Builder for TransferUpdateCotaNFTV2EntriesBuilder { + self.claim_infos.as_slice().len() + self.withdrawal_keys.as_slice().len() + self.withdrawal_values.as_slice().len() + + self.leaf_keys.as_slice().len() + + self.leaf_values.as_slice().len() + self.proof.as_slice().len() + self.withdrawal_proof.as_slice().len() + self.action.as_slice().len() @@ -2349,6 +2457,10 @@ impl molecule::prelude::Builder for TransferUpdateCotaNFTV2EntriesBuilder { offsets.push(total_size); total_size += self.withdrawal_values.as_slice().len(); offsets.push(total_size); + total_size += self.leaf_keys.as_slice().len(); + offsets.push(total_size); + total_size += self.leaf_values.as_slice().len(); + offsets.push(total_size); total_size += self.proof.as_slice().len(); offsets.push(total_size); total_size += self.withdrawal_proof.as_slice().len(); @@ -2368,6 +2480,8 @@ impl molecule::prelude::Builder for TransferUpdateCotaNFTV2EntriesBuilder { writer.write_all(self.claim_infos.as_slice())?; writer.write_all(self.withdrawal_keys.as_slice())?; writer.write_all(self.withdrawal_values.as_slice())?; + writer.write_all(self.leaf_keys.as_slice())?; + writer.write_all(self.leaf_values.as_slice())?; writer.write_all(self.proof.as_slice())?; writer.write_all(self.withdrawal_proof.as_slice())?; writer.write_all(self.action.as_slice())?; From 4ea1afbf8a26314cc7d541039af5f31600249448 Mon Sep 17 00:00:00 2001 From: duanyytop Date: Mon, 16 May 2022 18:19:08 +0800 Subject: [PATCH 03/10] refactor: Update blockchain types --- Cargo.lock | 190 +- Cargo.toml | 1 + src/{raw_tx.mol => blockchain.mol} | 26 +- src/{raw_tx.rs => blockchain.rs} | 5069 +++++++++++++++++++++++----- src/common.mol | 23 +- src/common.rs | 1231 +------ src/define.mol | 1 + src/define.rs | 8 +- src/lib.rs | 4 +- src/mint.mol | 1 + src/mint.rs | 4 +- src/registry.mol | 1 + src/registry.rs | 16 +- src/transfer.mol | 16 +- src/transfer.rs | 131 +- src/transfer_update.mol | 14 +- src/transfer_update.rs | 91 +- src/update.mol | 1 + src/update.rs | 7 +- 19 files changed, 4770 insertions(+), 2065 deletions(-) rename src/{raw_tx.mol => blockchain.mol} (67%) rename src/{raw_tx.rs => blockchain.rs} (63%) diff --git a/Cargo.lock b/Cargo.lock index dd744a0..afa7d2e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -84,6 +84,15 @@ dependencies = [ "crossbeam-channel", ] +[[package]] +name = "ckb-channel" +version = "0.103.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c37f58ccee88155144067490986221d6e65b721f9184ce4f775621d8c453247e" +dependencies = [ + "crossbeam-channel", +] + [[package]] name = "ckb-error" version = "0.101.8" @@ -91,7 +100,19 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "44f068a6fd2bc852172c6bc33465e757953299dfed0aa66a3d52d5a2c7e5272a" dependencies = [ "anyhow", - "ckb-occupied-capacity", + "ckb-occupied-capacity 0.101.8", + "derive_more", + "thiserror", +] + +[[package]] +name = "ckb-error" +version = "0.103.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "548dad083a5846996d38fb2680e4fd20737c23be4c6dbbc897bba63fc69427fd" +dependencies = [ + "anyhow", + "ckb-occupied-capacity 0.103.0", "derive_more", "thiserror", ] @@ -102,8 +123,18 @@ version = "0.101.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1764c5697371b60dba3af51880dda454508fa4a74be5c27f8c32f00fda0e356f" dependencies = [ - "ckb-fixed-hash-core", - "ckb-fixed-hash-macros", + "ckb-fixed-hash-core 0.101.8", + "ckb-fixed-hash-macros 0.101.8", +] + +[[package]] +name = "ckb-fixed-hash" +version = "0.103.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87e16c26d073b6ce9820154d8fb067d95dbfcb91faa693162341e90f26c3363f" +dependencies = [ + "ckb-fixed-hash-core 0.103.0", + "ckb-fixed-hash-macros 0.103.0", ] [[package]] @@ -117,13 +148,36 @@ dependencies = [ "thiserror", ] +[[package]] +name = "ckb-fixed-hash-core" +version = "0.103.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "14c7cda98b0ed0f7c46788e5399b1c69423cad02cf9b8f9f77e3ed7cfbd95dcc" +dependencies = [ + "faster-hex", + "serde", + "thiserror", +] + [[package]] name = "ckb-fixed-hash-macros" version = "0.101.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "47435298b4a697851c58c2d4347cf50d1d9b5893f3534acce4cdc2aae6a8b37e" dependencies = [ - "ckb-fixed-hash-core", + "ckb-fixed-hash-core 0.101.8", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "ckb-fixed-hash-macros" +version = "0.103.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bcc8f5a4d66e01da9f8024aea5a49ee0c93f7a927e932b37c162c7afd38959f4" +dependencies = [ + "ckb-fixed-hash-core 0.103.0", "proc-macro2", "quote", "syn", @@ -139,14 +193,46 @@ dependencies = [ "blake2b-rs", ] +[[package]] +name = "ckb-hash" +version = "0.103.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "380702c09207ac378708f075d72b336adeb17b8887dcfcc1bf9460e10ea1e285" +dependencies = [ + "blake2b-ref 0.2.1", + "blake2b-rs", +] + +[[package]] +name = "ckb-jsonrpc-types" +version = "0.103.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "305760c25e4b4bce5b11b1efe80ee7afe81cb63e39432725b01577fefe719ee1" +dependencies = [ + "ckb-types 0.103.0", + "faster-hex", + "serde", + "serde_json", +] + [[package]] name = "ckb-occupied-capacity" version = "0.101.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "80dfbabc77dc083604adea5587ffa3e8a6ce0afcb92f75928575c6728981edfc" dependencies = [ - "ckb-occupied-capacity-core", - "ckb-occupied-capacity-macros", + "ckb-occupied-capacity-core 0.101.8", + "ckb-occupied-capacity-macros 0.101.8", +] + +[[package]] +name = "ckb-occupied-capacity" +version = "0.103.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c9aa5e0ccfc275af08d2423b8e7cbd98c980d2565950a4e3efc13182f0ae1bf" +dependencies = [ + "ckb-occupied-capacity-core 0.103.0", + "ckb-occupied-capacity-macros 0.103.0", ] [[package]] @@ -158,13 +244,33 @@ dependencies = [ "serde", ] +[[package]] +name = "ckb-occupied-capacity-core" +version = "0.103.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "118085f13bf4d4a160c6df360adcc2142ab40f7cf0aea058d2a5a4ca4dd61172" +dependencies = [ + "serde", +] + [[package]] name = "ckb-occupied-capacity-macros" version = "0.101.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7614dc55f767154813cf60373ff64260e106de6b83461e0470fd51e95ffa671f" dependencies = [ - "ckb-occupied-capacity-core", + "ckb-occupied-capacity-core 0.101.8", + "quote", + "syn", +] + +[[package]] +name = "ckb-occupied-capacity-macros" +version = "0.103.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2ca47bd5b0c381284f3f858cc6fd9087adecc384b1535e869a231061a23a7b59" +dependencies = [ + "ckb-occupied-capacity-core 0.103.0", "quote", "syn", ] @@ -179,6 +285,16 @@ dependencies = [ "serde", ] +[[package]] +name = "ckb-rational" +version = "0.103.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6942caea45b9da94b0321e738f6d58ba9f3d03347936d76924d102d360300bc4" +dependencies = [ + "numext-fixed-uint", + "serde", +] + [[package]] name = "ckb-standalone-types" version = "0.1.2" @@ -210,12 +326,34 @@ dependencies = [ "bit-vec", "bitflags", "bytes", - "ckb-channel", - "ckb-error", - "ckb-fixed-hash", - "ckb-hash", - "ckb-occupied-capacity", - "ckb-rational", + "ckb-channel 0.101.8", + "ckb-error 0.101.8", + "ckb-fixed-hash 0.101.8", + "ckb-hash 0.101.8", + "ckb-occupied-capacity 0.101.8", + "ckb-rational 0.101.8", + "derive_more", + "merkle-cbt", + "molecule", + "numext-fixed-uint", + "once_cell", +] + +[[package]] +name = "ckb-types" +version = "0.103.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2c24d49d19a4b367ba9be3538e404131e5b911ecf8bba93bc08776377d06e67" +dependencies = [ + "bit-vec", + "bitflags", + "bytes", + "ckb-channel 0.103.0", + "ckb-error 0.103.0", + "ckb-fixed-hash 0.103.0", + "ckb-hash 0.103.0", + "ckb-occupied-capacity 0.103.0", + "ckb-rational 0.103.0", "derive_more", "merkle-cbt", "molecule", @@ -229,8 +367,9 @@ version = "0.4.0" dependencies = [ "blake2b-ref 0.3.0", "cfg-if 1.0.0", + "ckb-jsonrpc-types", "ckb-std", - "ckb-types", + "ckb-types 0.101.8", "molecule", "sparse-merkle-tree", ] @@ -308,6 +447,12 @@ dependencies = [ "winapi", ] +[[package]] +name = "itoa" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "112c678d4050afce233f4f2852bb2eb519230b3cf12f33585275537d7e41578d" + [[package]] name = "lazy_static" version = "1.4.0" @@ -463,6 +608,12 @@ dependencies = [ "rand_core", ] +[[package]] +name = "ryu" +version = "1.0.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f3f6f92acf49d1b98f7a81226834412ada05458b7364277387724a237f062695" + [[package]] name = "serde" version = "1.0.130" @@ -483,6 +634,17 @@ dependencies = [ "syn", ] +[[package]] +name = "serde_json" +version = "1.0.81" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b7ce2b32a1aed03c558dc61a5cd328f15aff2dbc17daad8fb8af04d2100e15c" +dependencies = [ + "itoa", + "ryu", + "serde", +] + [[package]] name = "sparse-merkle-tree" version = "0.5.3" diff --git a/Cargo.toml b/Cargo.toml index 7b0eeff..5496cda 100755 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,7 @@ cfg-if = "1.0" blake2b-ref = "0.3" ckb-std = {version = "0.9.0", optional = true} ckb-types = {version = "0.101.1", optional = true} +ckb-jsonrpc-types = ">=0.101.0" molecule = {version = "0.7.2", default_features = false, optional = true} sparse-merkle-tree = {version = "0.5.3", default_features = false} diff --git a/src/raw_tx.mol b/src/blockchain.mol similarity index 67% rename from src/raw_tx.mol rename to src/blockchain.mol index 627f90a..8fbebe4 100644 --- a/src/raw_tx.mol +++ b/src/blockchain.mol @@ -1,13 +1,28 @@ -import common; +/* Basic Types */ +// The `UintN` is used to store a `N` bits unsigned integer +// as a byte array in little endian. +array Uint32 [byte; 4]; +array Uint64 [byte; 8]; +array Uint128 [byte; 16]; +array Byte32 [byte; 32]; +array Uint256 [byte; 32]; array H256 [byte; 32]; -vector BytesVec ; -vector Byte32Vec ; vector Uint32Vec ; vector H256Vec ; +vector Bytes ; +option BytesOpt (Bytes); + +vector BytesVec ; +vector Byte32Vec ; + +/* Types for Chain */ + option ScriptOpt (Script); + +vector TransactionVec ; vector CellDepVec ; vector CellInputVec ; vector CellOutputVec ; @@ -48,6 +63,11 @@ table RawTransaction { outputs_data: BytesVec, } +table Transaction { + raw: RawTransaction, + witnesses: BytesVec, +} + table MerkleProof { indices: Uint32Vec, lemmas: H256Vec, diff --git a/src/raw_tx.rs b/src/blockchain.rs similarity index 63% rename from src/raw_tx.rs rename to src/blockchain.rs index 0197726..65c7452 100644 --- a/src/raw_tx.rs +++ b/src/blockchain.rs @@ -10,11 +10,9 @@ extern crate alloc; pub use alloc::vec::*; // these lines above are manually added -use super::common::*; -use molecule::prelude::*; #[derive(Clone)] -pub struct H256(molecule::bytes::Bytes); -impl ::core::fmt::LowerHex for H256 { +pub struct Uint32(molecule::bytes::Bytes); +impl ::core::fmt::LowerHex for Uint32 { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { use molecule::hex_string; if f.alternate() { @@ -23,31 +21,28 @@ impl ::core::fmt::LowerHex for H256 { write!(f, "{}", hex_string(self.as_slice())) } } -impl ::core::fmt::Debug for H256 { +impl ::core::fmt::Debug for Uint32 { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { write!(f, "{}({:#x})", Self::NAME, self) } } -impl ::core::fmt::Display for H256 { +impl ::core::fmt::Display for Uint32 { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { use molecule::hex_string; let raw_data = hex_string(&self.raw_data()); write!(f, "{}(0x{})", Self::NAME, raw_data) } } -impl ::core::default::Default for H256 { +impl ::core::default::Default for Uint32 { fn default() -> Self { - let v: Vec = vec![ - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, - ]; - H256::new_unchecked(v.into()) + let v: Vec = vec![0, 0, 0, 0]; + Uint32::new_unchecked(v.into()) } } -impl H256 { - pub const ITEM_COUNT: usize = 32; +impl Uint32 { + pub const ITEM_COUNT: usize = 4; pub const ITEM_SIZE: usize = 1; - pub const TOTAL_SIZE: usize = 32; + pub const TOTAL_SIZE: usize = 4; pub fn nth0(&self) -> Byte { Byte::new_unchecked(self.0.slice(0..1)) @@ -65,133 +60,272 @@ impl H256 { Byte::new_unchecked(self.0.slice(3..4)) } - pub fn nth4(&self) -> Byte { - Byte::new_unchecked(self.0.slice(4..5)) + pub fn raw_data(&self) -> molecule::bytes::Bytes { + self.as_bytes() } - pub fn nth5(&self) -> Byte { - Byte::new_unchecked(self.0.slice(5..6)) + pub fn as_reader<'r>(&'r self) -> Uint32Reader<'r> { + Uint32Reader::new_unchecked(self.as_slice()) } +} +impl molecule::prelude::Entity for Uint32 { + type Builder = Uint32Builder; - pub fn nth6(&self) -> Byte { - Byte::new_unchecked(self.0.slice(6..7)) + const NAME: &'static str = "Uint32"; + + fn new_unchecked(data: molecule::bytes::Bytes) -> Self { + Uint32(data) } - pub fn nth7(&self) -> Byte { - Byte::new_unchecked(self.0.slice(7..8)) + fn as_bytes(&self) -> molecule::bytes::Bytes { + self.0.clone() } - pub fn nth8(&self) -> Byte { - Byte::new_unchecked(self.0.slice(8..9)) + fn as_slice(&self) -> &[u8] { + &self.0[..] } - pub fn nth9(&self) -> Byte { - Byte::new_unchecked(self.0.slice(9..10)) + fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { + Uint32Reader::from_slice(slice).map(|reader| reader.to_entity()) } - pub fn nth10(&self) -> Byte { - Byte::new_unchecked(self.0.slice(10..11)) + fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { + Uint32Reader::from_compatible_slice(slice).map(|reader| reader.to_entity()) } - pub fn nth11(&self) -> Byte { - Byte::new_unchecked(self.0.slice(11..12)) + fn new_builder() -> Self::Builder { + ::core::default::Default::default() } - pub fn nth12(&self) -> Byte { - Byte::new_unchecked(self.0.slice(12..13)) + fn as_builder(self) -> Self::Builder { + Self::new_builder().set([self.nth0(), self.nth1(), self.nth2(), self.nth3()]) + } +} +#[derive(Clone, Copy)] +pub struct Uint32Reader<'r>(&'r [u8]); +impl<'r> ::core::fmt::LowerHex for Uint32Reader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl<'r> ::core::fmt::Debug for Uint32Reader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl<'r> ::core::fmt::Display for Uint32Reader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + let raw_data = hex_string(&self.raw_data()); + write!(f, "{}(0x{})", Self::NAME, raw_data) } +} +impl<'r> Uint32Reader<'r> { + pub const ITEM_COUNT: usize = 4; + pub const ITEM_SIZE: usize = 1; + pub const TOTAL_SIZE: usize = 4; - pub fn nth13(&self) -> Byte { - Byte::new_unchecked(self.0.slice(13..14)) + pub fn nth0(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[0..1]) } - pub fn nth14(&self) -> Byte { - Byte::new_unchecked(self.0.slice(14..15)) + pub fn nth1(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[1..2]) } - pub fn nth15(&self) -> Byte { - Byte::new_unchecked(self.0.slice(15..16)) + pub fn nth2(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[2..3]) } - pub fn nth16(&self) -> Byte { - Byte::new_unchecked(self.0.slice(16..17)) + pub fn nth3(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[3..4]) } - pub fn nth17(&self) -> Byte { - Byte::new_unchecked(self.0.slice(17..18)) + pub fn raw_data(&self) -> &'r [u8] { + self.as_slice() } +} +impl<'r> molecule::prelude::Reader<'r> for Uint32Reader<'r> { + type Entity = Uint32; - pub fn nth18(&self) -> Byte { - Byte::new_unchecked(self.0.slice(18..19)) + const NAME: &'static str = "Uint32Reader"; + + fn to_entity(&self) -> Self::Entity { + Self::Entity::new_unchecked(self.as_slice().to_owned().into()) } - pub fn nth19(&self) -> Byte { - Byte::new_unchecked(self.0.slice(19..20)) + fn new_unchecked(slice: &'r [u8]) -> Self { + Uint32Reader(slice) } - pub fn nth20(&self) -> Byte { - Byte::new_unchecked(self.0.slice(20..21)) + fn as_slice(&self) -> &'r [u8] { + self.0 } - pub fn nth21(&self) -> Byte { - Byte::new_unchecked(self.0.slice(21..22)) + fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> { + use molecule::verification_error as ve; + let slice_len = slice.len(); + if slice_len != Self::TOTAL_SIZE { + return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len); + } + Ok(()) + } +} +pub struct Uint32Builder(pub(crate) [Byte; 4]); +impl ::core::fmt::Debug for Uint32Builder { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:?})", Self::NAME, &self.0[..]) + } +} +impl ::core::default::Default for Uint32Builder { + fn default() -> Self { + Uint32Builder([ + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + ]) } +} +impl Uint32Builder { + pub const ITEM_COUNT: usize = 4; + pub const ITEM_SIZE: usize = 1; + pub const TOTAL_SIZE: usize = 4; - pub fn nth22(&self) -> Byte { - Byte::new_unchecked(self.0.slice(22..23)) + pub fn set(mut self, v: [Byte; 4]) -> Self { + self.0 = v; + self } - pub fn nth23(&self) -> Byte { - Byte::new_unchecked(self.0.slice(23..24)) + pub fn nth0(mut self, v: Byte) -> Self { + self.0[0] = v; + self } - pub fn nth24(&self) -> Byte { - Byte::new_unchecked(self.0.slice(24..25)) + pub fn nth1(mut self, v: Byte) -> Self { + self.0[1] = v; + self } - pub fn nth25(&self) -> Byte { - Byte::new_unchecked(self.0.slice(25..26)) + pub fn nth2(mut self, v: Byte) -> Self { + self.0[2] = v; + self } - pub fn nth26(&self) -> Byte { - Byte::new_unchecked(self.0.slice(26..27)) + pub fn nth3(mut self, v: Byte) -> Self { + self.0[3] = v; + self } +} +impl molecule::prelude::Builder for Uint32Builder { + type Entity = Uint32; - pub fn nth27(&self) -> Byte { - Byte::new_unchecked(self.0.slice(27..28)) + const NAME: &'static str = "Uint32Builder"; + + fn expected_length(&self) -> usize { + Self::TOTAL_SIZE } - pub fn nth28(&self) -> Byte { - Byte::new_unchecked(self.0.slice(28..29)) + fn write(&self, writer: &mut W) -> molecule::io::Result<()> { + writer.write_all(self.0[0].as_slice())?; + writer.write_all(self.0[1].as_slice())?; + writer.write_all(self.0[2].as_slice())?; + writer.write_all(self.0[3].as_slice())?; + Ok(()) } - pub fn nth29(&self) -> Byte { - Byte::new_unchecked(self.0.slice(29..30)) + fn build(&self) -> Self::Entity { + let mut inner = Vec::with_capacity(self.expected_length()); + self.write(&mut inner) + .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); + Uint32::new_unchecked(inner.into()) + } +} +#[derive(Clone)] +pub struct Uint64(molecule::bytes::Bytes); +impl ::core::fmt::LowerHex for Uint64 { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) } +} +impl ::core::fmt::Debug for Uint64 { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl ::core::fmt::Display for Uint64 { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + let raw_data = hex_string(&self.raw_data()); + write!(f, "{}(0x{})", Self::NAME, raw_data) + } +} +impl ::core::default::Default for Uint64 { + fn default() -> Self { + let v: Vec = vec![0, 0, 0, 0, 0, 0, 0, 0]; + Uint64::new_unchecked(v.into()) + } +} +impl Uint64 { + pub const ITEM_COUNT: usize = 8; + pub const ITEM_SIZE: usize = 1; + pub const TOTAL_SIZE: usize = 8; - pub fn nth30(&self) -> Byte { - Byte::new_unchecked(self.0.slice(30..31)) + pub fn nth0(&self) -> Byte { + Byte::new_unchecked(self.0.slice(0..1)) } - pub fn nth31(&self) -> Byte { - Byte::new_unchecked(self.0.slice(31..32)) + pub fn nth1(&self) -> Byte { + Byte::new_unchecked(self.0.slice(1..2)) + } + + pub fn nth2(&self) -> Byte { + Byte::new_unchecked(self.0.slice(2..3)) + } + + pub fn nth3(&self) -> Byte { + Byte::new_unchecked(self.0.slice(3..4)) + } + + pub fn nth4(&self) -> Byte { + Byte::new_unchecked(self.0.slice(4..5)) + } + + pub fn nth5(&self) -> Byte { + Byte::new_unchecked(self.0.slice(5..6)) + } + + pub fn nth6(&self) -> Byte { + Byte::new_unchecked(self.0.slice(6..7)) + } + + pub fn nth7(&self) -> Byte { + Byte::new_unchecked(self.0.slice(7..8)) } pub fn raw_data(&self) -> molecule::bytes::Bytes { self.as_bytes() } - pub fn as_reader<'r>(&'r self) -> H256Reader<'r> { - H256Reader::new_unchecked(self.as_slice()) + pub fn as_reader<'r>(&'r self) -> Uint64Reader<'r> { + Uint64Reader::new_unchecked(self.as_slice()) } } -impl molecule::prelude::Entity for H256 { - type Builder = H256Builder; +impl molecule::prelude::Entity for Uint64 { + type Builder = Uint64Builder; - const NAME: &'static str = "H256"; + const NAME: &'static str = "Uint64"; fn new_unchecked(data: molecule::bytes::Bytes) -> Self { - H256(data) + Uint64(data) } fn as_bytes(&self) -> molecule::bytes::Bytes { @@ -203,11 +337,11 @@ impl molecule::prelude::Entity for H256 { } fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { - H256Reader::from_slice(slice).map(|reader| reader.to_entity()) + Uint64Reader::from_slice(slice).map(|reader| reader.to_entity()) } fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { - H256Reader::from_compatible_slice(slice).map(|reader| reader.to_entity()) + Uint64Reader::from_compatible_slice(slice).map(|reader| reader.to_entity()) } fn new_builder() -> Self::Builder { @@ -224,36 +358,12 @@ impl molecule::prelude::Entity for H256 { self.nth5(), self.nth6(), self.nth7(), - self.nth8(), - self.nth9(), - self.nth10(), - self.nth11(), - self.nth12(), - self.nth13(), - self.nth14(), - self.nth15(), - self.nth16(), - self.nth17(), - self.nth18(), - self.nth19(), - self.nth20(), - self.nth21(), - self.nth22(), - self.nth23(), - self.nth24(), - self.nth25(), - self.nth26(), - self.nth27(), - self.nth28(), - self.nth29(), - self.nth30(), - self.nth31(), ]) } } #[derive(Clone, Copy)] -pub struct H256Reader<'r>(&'r [u8]); -impl<'r> ::core::fmt::LowerHex for H256Reader<'r> { +pub struct Uint64Reader<'r>(&'r [u8]); +impl<'r> ::core::fmt::LowerHex for Uint64Reader<'r> { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { use molecule::hex_string; if f.alternate() { @@ -262,22 +372,22 @@ impl<'r> ::core::fmt::LowerHex for H256Reader<'r> { write!(f, "{}", hex_string(self.as_slice())) } } -impl<'r> ::core::fmt::Debug for H256Reader<'r> { +impl<'r> ::core::fmt::Debug for Uint64Reader<'r> { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { write!(f, "{}({:#x})", Self::NAME, self) } } -impl<'r> ::core::fmt::Display for H256Reader<'r> { +impl<'r> ::core::fmt::Display for Uint64Reader<'r> { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { use molecule::hex_string; let raw_data = hex_string(&self.raw_data()); write!(f, "{}(0x{})", Self::NAME, raw_data) } } -impl<'r> H256Reader<'r> { - pub const ITEM_COUNT: usize = 32; +impl<'r> Uint64Reader<'r> { + pub const ITEM_COUNT: usize = 8; pub const ITEM_SIZE: usize = 1; - pub const TOTAL_SIZE: usize = 32; + pub const TOTAL_SIZE: usize = 8; pub fn nth0(&self) -> ByteReader<'r> { ByteReader::new_unchecked(&self.as_slice()[0..1]) @@ -311,117 +421,21 @@ impl<'r> H256Reader<'r> { ByteReader::new_unchecked(&self.as_slice()[7..8]) } - pub fn nth8(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[8..9]) - } - - pub fn nth9(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[9..10]) - } - - pub fn nth10(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[10..11]) - } - - pub fn nth11(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[11..12]) - } - - pub fn nth12(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[12..13]) - } - - pub fn nth13(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[13..14]) - } - - pub fn nth14(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[14..15]) - } - - pub fn nth15(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[15..16]) - } - - pub fn nth16(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[16..17]) - } - - pub fn nth17(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[17..18]) - } - - pub fn nth18(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[18..19]) - } - - pub fn nth19(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[19..20]) - } - - pub fn nth20(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[20..21]) - } - - pub fn nth21(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[21..22]) - } - - pub fn nth22(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[22..23]) - } - - pub fn nth23(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[23..24]) - } - - pub fn nth24(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[24..25]) - } - - pub fn nth25(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[25..26]) - } - - pub fn nth26(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[26..27]) - } - - pub fn nth27(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[27..28]) - } - - pub fn nth28(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[28..29]) - } - - pub fn nth29(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[29..30]) - } - - pub fn nth30(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[30..31]) - } - - pub fn nth31(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[31..32]) - } - pub fn raw_data(&self) -> &'r [u8] { self.as_slice() } } -impl<'r> molecule::prelude::Reader<'r> for H256Reader<'r> { - type Entity = H256; +impl<'r> molecule::prelude::Reader<'r> for Uint64Reader<'r> { + type Entity = Uint64; - const NAME: &'static str = "H256Reader"; + const NAME: &'static str = "Uint64Reader"; fn to_entity(&self) -> Self::Entity { Self::Entity::new_unchecked(self.as_slice().to_owned().into()) } fn new_unchecked(slice: &'r [u8]) -> Self { - H256Reader(slice) + Uint64Reader(slice) } fn as_slice(&self) -> &'r [u8] { @@ -437,39 +451,15 @@ impl<'r> molecule::prelude::Reader<'r> for H256Reader<'r> { Ok(()) } } -pub struct H256Builder(pub(crate) [Byte; 32]); -impl ::core::fmt::Debug for H256Builder { +pub struct Uint64Builder(pub(crate) [Byte; 8]); +impl ::core::fmt::Debug for Uint64Builder { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { write!(f, "{}({:?})", Self::NAME, &self.0[..]) } } -impl ::core::default::Default for H256Builder { +impl ::core::default::Default for Uint64Builder { fn default() -> Self { - H256Builder([ - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), + Uint64Builder([ Byte::default(), Byte::default(), Byte::default(), @@ -481,12 +471,12 @@ impl ::core::default::Default for H256Builder { ]) } } -impl H256Builder { - pub const ITEM_COUNT: usize = 32; +impl Uint64Builder { + pub const ITEM_COUNT: usize = 8; pub const ITEM_SIZE: usize = 1; - pub const TOTAL_SIZE: usize = 32; + pub const TOTAL_SIZE: usize = 8; - pub fn set(mut self, v: [Byte; 32]) -> Self { + pub fn set(mut self, v: [Byte; 8]) -> Self { self.0 = v; self } @@ -530,95 +520,2456 @@ impl H256Builder { self.0[7] = v; self } +} +impl molecule::prelude::Builder for Uint64Builder { + type Entity = Uint64; - pub fn nth8(mut self, v: Byte) -> Self { - self.0[8] = v; - self - } + const NAME: &'static str = "Uint64Builder"; - pub fn nth9(mut self, v: Byte) -> Self { - self.0[9] = v; - self + fn expected_length(&self) -> usize { + Self::TOTAL_SIZE } - pub fn nth10(mut self, v: Byte) -> Self { - self.0[10] = v; - self + fn write(&self, writer: &mut W) -> molecule::io::Result<()> { + writer.write_all(self.0[0].as_slice())?; + writer.write_all(self.0[1].as_slice())?; + writer.write_all(self.0[2].as_slice())?; + writer.write_all(self.0[3].as_slice())?; + writer.write_all(self.0[4].as_slice())?; + writer.write_all(self.0[5].as_slice())?; + writer.write_all(self.0[6].as_slice())?; + writer.write_all(self.0[7].as_slice())?; + Ok(()) } - pub fn nth11(mut self, v: Byte) -> Self { - self.0[11] = v; - self + fn build(&self) -> Self::Entity { + let mut inner = Vec::with_capacity(self.expected_length()); + self.write(&mut inner) + .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); + Uint64::new_unchecked(inner.into()) } - - pub fn nth12(mut self, v: Byte) -> Self { - self.0[12] = v; - self +} +#[derive(Clone)] +pub struct Uint128(molecule::bytes::Bytes); +impl ::core::fmt::LowerHex for Uint128 { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) } - - pub fn nth13(mut self, v: Byte) -> Self { - self.0[13] = v; - self +} +impl ::core::fmt::Debug for Uint128 { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) } - - pub fn nth14(mut self, v: Byte) -> Self { - self.0[14] = v; - self +} +impl ::core::fmt::Display for Uint128 { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + let raw_data = hex_string(&self.raw_data()); + write!(f, "{}(0x{})", Self::NAME, raw_data) } +} +impl ::core::default::Default for Uint128 { + fn default() -> Self { + let v: Vec = vec![0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; + Uint128::new_unchecked(v.into()) + } +} +impl Uint128 { + pub const ITEM_COUNT: usize = 16; + pub const ITEM_SIZE: usize = 1; + pub const TOTAL_SIZE: usize = 16; - pub fn nth15(mut self, v: Byte) -> Self { - self.0[15] = v; - self + pub fn nth0(&self) -> Byte { + Byte::new_unchecked(self.0.slice(0..1)) } - pub fn nth16(mut self, v: Byte) -> Self { - self.0[16] = v; - self + pub fn nth1(&self) -> Byte { + Byte::new_unchecked(self.0.slice(1..2)) } - pub fn nth17(mut self, v: Byte) -> Self { - self.0[17] = v; - self + pub fn nth2(&self) -> Byte { + Byte::new_unchecked(self.0.slice(2..3)) } - pub fn nth18(mut self, v: Byte) -> Self { - self.0[18] = v; - self + pub fn nth3(&self) -> Byte { + Byte::new_unchecked(self.0.slice(3..4)) } - pub fn nth19(mut self, v: Byte) -> Self { - self.0[19] = v; - self + pub fn nth4(&self) -> Byte { + Byte::new_unchecked(self.0.slice(4..5)) } - pub fn nth20(mut self, v: Byte) -> Self { - self.0[20] = v; - self + pub fn nth5(&self) -> Byte { + Byte::new_unchecked(self.0.slice(5..6)) } - pub fn nth21(mut self, v: Byte) -> Self { - self.0[21] = v; - self + pub fn nth6(&self) -> Byte { + Byte::new_unchecked(self.0.slice(6..7)) } - pub fn nth22(mut self, v: Byte) -> Self { - self.0[22] = v; - self + pub fn nth7(&self) -> Byte { + Byte::new_unchecked(self.0.slice(7..8)) } - pub fn nth23(mut self, v: Byte) -> Self { - self.0[23] = v; - self + pub fn nth8(&self) -> Byte { + Byte::new_unchecked(self.0.slice(8..9)) } - pub fn nth24(mut self, v: Byte) -> Self { - self.0[24] = v; - self + pub fn nth9(&self) -> Byte { + Byte::new_unchecked(self.0.slice(9..10)) } - pub fn nth25(mut self, v: Byte) -> Self { - self.0[25] = v; - self + pub fn nth10(&self) -> Byte { + Byte::new_unchecked(self.0.slice(10..11)) + } + + pub fn nth11(&self) -> Byte { + Byte::new_unchecked(self.0.slice(11..12)) + } + + pub fn nth12(&self) -> Byte { + Byte::new_unchecked(self.0.slice(12..13)) + } + + pub fn nth13(&self) -> Byte { + Byte::new_unchecked(self.0.slice(13..14)) + } + + pub fn nth14(&self) -> Byte { + Byte::new_unchecked(self.0.slice(14..15)) + } + + pub fn nth15(&self) -> Byte { + Byte::new_unchecked(self.0.slice(15..16)) + } + + pub fn raw_data(&self) -> molecule::bytes::Bytes { + self.as_bytes() + } + + pub fn as_reader<'r>(&'r self) -> Uint128Reader<'r> { + Uint128Reader::new_unchecked(self.as_slice()) + } +} +impl molecule::prelude::Entity for Uint128 { + type Builder = Uint128Builder; + + const NAME: &'static str = "Uint128"; + + fn new_unchecked(data: molecule::bytes::Bytes) -> Self { + Uint128(data) + } + + fn as_bytes(&self) -> molecule::bytes::Bytes { + self.0.clone() + } + + fn as_slice(&self) -> &[u8] { + &self.0[..] + } + + fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { + Uint128Reader::from_slice(slice).map(|reader| reader.to_entity()) + } + + fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { + Uint128Reader::from_compatible_slice(slice).map(|reader| reader.to_entity()) + } + + fn new_builder() -> Self::Builder { + ::core::default::Default::default() + } + + fn as_builder(self) -> Self::Builder { + Self::new_builder().set([ + self.nth0(), + self.nth1(), + self.nth2(), + self.nth3(), + self.nth4(), + self.nth5(), + self.nth6(), + self.nth7(), + self.nth8(), + self.nth9(), + self.nth10(), + self.nth11(), + self.nth12(), + self.nth13(), + self.nth14(), + self.nth15(), + ]) + } +} +#[derive(Clone, Copy)] +pub struct Uint128Reader<'r>(&'r [u8]); +impl<'r> ::core::fmt::LowerHex for Uint128Reader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl<'r> ::core::fmt::Debug for Uint128Reader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl<'r> ::core::fmt::Display for Uint128Reader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + let raw_data = hex_string(&self.raw_data()); + write!(f, "{}(0x{})", Self::NAME, raw_data) + } +} +impl<'r> Uint128Reader<'r> { + pub const ITEM_COUNT: usize = 16; + pub const ITEM_SIZE: usize = 1; + pub const TOTAL_SIZE: usize = 16; + + pub fn nth0(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[0..1]) + } + + pub fn nth1(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[1..2]) + } + + pub fn nth2(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[2..3]) + } + + pub fn nth3(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[3..4]) + } + + pub fn nth4(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[4..5]) + } + + pub fn nth5(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[5..6]) + } + + pub fn nth6(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[6..7]) + } + + pub fn nth7(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[7..8]) + } + + pub fn nth8(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[8..9]) + } + + pub fn nth9(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[9..10]) + } + + pub fn nth10(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[10..11]) + } + + pub fn nth11(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[11..12]) + } + + pub fn nth12(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[12..13]) + } + + pub fn nth13(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[13..14]) + } + + pub fn nth14(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[14..15]) + } + + pub fn nth15(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[15..16]) + } + + pub fn raw_data(&self) -> &'r [u8] { + self.as_slice() + } +} +impl<'r> molecule::prelude::Reader<'r> for Uint128Reader<'r> { + type Entity = Uint128; + + const NAME: &'static str = "Uint128Reader"; + + fn to_entity(&self) -> Self::Entity { + Self::Entity::new_unchecked(self.as_slice().to_owned().into()) + } + + fn new_unchecked(slice: &'r [u8]) -> Self { + Uint128Reader(slice) + } + + fn as_slice(&self) -> &'r [u8] { + self.0 + } + + fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> { + use molecule::verification_error as ve; + let slice_len = slice.len(); + if slice_len != Self::TOTAL_SIZE { + return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len); + } + Ok(()) + } +} +pub struct Uint128Builder(pub(crate) [Byte; 16]); +impl ::core::fmt::Debug for Uint128Builder { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:?})", Self::NAME, &self.0[..]) + } +} +impl ::core::default::Default for Uint128Builder { + fn default() -> Self { + Uint128Builder([ + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + ]) + } +} +impl Uint128Builder { + pub const ITEM_COUNT: usize = 16; + pub const ITEM_SIZE: usize = 1; + pub const TOTAL_SIZE: usize = 16; + + pub fn set(mut self, v: [Byte; 16]) -> Self { + self.0 = v; + self + } + + pub fn nth0(mut self, v: Byte) -> Self { + self.0[0] = v; + self + } + + pub fn nth1(mut self, v: Byte) -> Self { + self.0[1] = v; + self + } + + pub fn nth2(mut self, v: Byte) -> Self { + self.0[2] = v; + self + } + + pub fn nth3(mut self, v: Byte) -> Self { + self.0[3] = v; + self + } + + pub fn nth4(mut self, v: Byte) -> Self { + self.0[4] = v; + self + } + + pub fn nth5(mut self, v: Byte) -> Self { + self.0[5] = v; + self + } + + pub fn nth6(mut self, v: Byte) -> Self { + self.0[6] = v; + self + } + + pub fn nth7(mut self, v: Byte) -> Self { + self.0[7] = v; + self + } + + pub fn nth8(mut self, v: Byte) -> Self { + self.0[8] = v; + self + } + + pub fn nth9(mut self, v: Byte) -> Self { + self.0[9] = v; + self + } + + pub fn nth10(mut self, v: Byte) -> Self { + self.0[10] = v; + self + } + + pub fn nth11(mut self, v: Byte) -> Self { + self.0[11] = v; + self + } + + pub fn nth12(mut self, v: Byte) -> Self { + self.0[12] = v; + self + } + + pub fn nth13(mut self, v: Byte) -> Self { + self.0[13] = v; + self + } + + pub fn nth14(mut self, v: Byte) -> Self { + self.0[14] = v; + self + } + + pub fn nth15(mut self, v: Byte) -> Self { + self.0[15] = v; + self + } +} +impl molecule::prelude::Builder for Uint128Builder { + type Entity = Uint128; + + const NAME: &'static str = "Uint128Builder"; + + fn expected_length(&self) -> usize { + Self::TOTAL_SIZE + } + + fn write(&self, writer: &mut W) -> molecule::io::Result<()> { + writer.write_all(self.0[0].as_slice())?; + writer.write_all(self.0[1].as_slice())?; + writer.write_all(self.0[2].as_slice())?; + writer.write_all(self.0[3].as_slice())?; + writer.write_all(self.0[4].as_slice())?; + writer.write_all(self.0[5].as_slice())?; + writer.write_all(self.0[6].as_slice())?; + writer.write_all(self.0[7].as_slice())?; + writer.write_all(self.0[8].as_slice())?; + writer.write_all(self.0[9].as_slice())?; + writer.write_all(self.0[10].as_slice())?; + writer.write_all(self.0[11].as_slice())?; + writer.write_all(self.0[12].as_slice())?; + writer.write_all(self.0[13].as_slice())?; + writer.write_all(self.0[14].as_slice())?; + writer.write_all(self.0[15].as_slice())?; + Ok(()) + } + + fn build(&self) -> Self::Entity { + let mut inner = Vec::with_capacity(self.expected_length()); + self.write(&mut inner) + .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); + Uint128::new_unchecked(inner.into()) + } +} +#[derive(Clone)] +pub struct Byte32(molecule::bytes::Bytes); +impl ::core::fmt::LowerHex for Byte32 { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl ::core::fmt::Debug for Byte32 { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl ::core::fmt::Display for Byte32 { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + let raw_data = hex_string(&self.raw_data()); + write!(f, "{}(0x{})", Self::NAME, raw_data) + } +} +impl ::core::default::Default for Byte32 { + fn default() -> Self { + let v: Vec = vec![ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, + ]; + Byte32::new_unchecked(v.into()) + } +} +impl Byte32 { + pub const ITEM_COUNT: usize = 32; + pub const ITEM_SIZE: usize = 1; + pub const TOTAL_SIZE: usize = 32; + + pub fn nth0(&self) -> Byte { + Byte::new_unchecked(self.0.slice(0..1)) + } + + pub fn nth1(&self) -> Byte { + Byte::new_unchecked(self.0.slice(1..2)) + } + + pub fn nth2(&self) -> Byte { + Byte::new_unchecked(self.0.slice(2..3)) + } + + pub fn nth3(&self) -> Byte { + Byte::new_unchecked(self.0.slice(3..4)) + } + + pub fn nth4(&self) -> Byte { + Byte::new_unchecked(self.0.slice(4..5)) + } + + pub fn nth5(&self) -> Byte { + Byte::new_unchecked(self.0.slice(5..6)) + } + + pub fn nth6(&self) -> Byte { + Byte::new_unchecked(self.0.slice(6..7)) + } + + pub fn nth7(&self) -> Byte { + Byte::new_unchecked(self.0.slice(7..8)) + } + + pub fn nth8(&self) -> Byte { + Byte::new_unchecked(self.0.slice(8..9)) + } + + pub fn nth9(&self) -> Byte { + Byte::new_unchecked(self.0.slice(9..10)) + } + + pub fn nth10(&self) -> Byte { + Byte::new_unchecked(self.0.slice(10..11)) + } + + pub fn nth11(&self) -> Byte { + Byte::new_unchecked(self.0.slice(11..12)) + } + + pub fn nth12(&self) -> Byte { + Byte::new_unchecked(self.0.slice(12..13)) + } + + pub fn nth13(&self) -> Byte { + Byte::new_unchecked(self.0.slice(13..14)) + } + + pub fn nth14(&self) -> Byte { + Byte::new_unchecked(self.0.slice(14..15)) + } + + pub fn nth15(&self) -> Byte { + Byte::new_unchecked(self.0.slice(15..16)) + } + + pub fn nth16(&self) -> Byte { + Byte::new_unchecked(self.0.slice(16..17)) + } + + pub fn nth17(&self) -> Byte { + Byte::new_unchecked(self.0.slice(17..18)) + } + + pub fn nth18(&self) -> Byte { + Byte::new_unchecked(self.0.slice(18..19)) + } + + pub fn nth19(&self) -> Byte { + Byte::new_unchecked(self.0.slice(19..20)) + } + + pub fn nth20(&self) -> Byte { + Byte::new_unchecked(self.0.slice(20..21)) + } + + pub fn nth21(&self) -> Byte { + Byte::new_unchecked(self.0.slice(21..22)) + } + + pub fn nth22(&self) -> Byte { + Byte::new_unchecked(self.0.slice(22..23)) + } + + pub fn nth23(&self) -> Byte { + Byte::new_unchecked(self.0.slice(23..24)) + } + + pub fn nth24(&self) -> Byte { + Byte::new_unchecked(self.0.slice(24..25)) + } + + pub fn nth25(&self) -> Byte { + Byte::new_unchecked(self.0.slice(25..26)) + } + + pub fn nth26(&self) -> Byte { + Byte::new_unchecked(self.0.slice(26..27)) + } + + pub fn nth27(&self) -> Byte { + Byte::new_unchecked(self.0.slice(27..28)) + } + + pub fn nth28(&self) -> Byte { + Byte::new_unchecked(self.0.slice(28..29)) + } + + pub fn nth29(&self) -> Byte { + Byte::new_unchecked(self.0.slice(29..30)) + } + + pub fn nth30(&self) -> Byte { + Byte::new_unchecked(self.0.slice(30..31)) + } + + pub fn nth31(&self) -> Byte { + Byte::new_unchecked(self.0.slice(31..32)) + } + + pub fn raw_data(&self) -> molecule::bytes::Bytes { + self.as_bytes() + } + + pub fn as_reader<'r>(&'r self) -> Byte32Reader<'r> { + Byte32Reader::new_unchecked(self.as_slice()) + } +} +impl molecule::prelude::Entity for Byte32 { + type Builder = Byte32Builder; + + const NAME: &'static str = "Byte32"; + + fn new_unchecked(data: molecule::bytes::Bytes) -> Self { + Byte32(data) + } + + fn as_bytes(&self) -> molecule::bytes::Bytes { + self.0.clone() + } + + fn as_slice(&self) -> &[u8] { + &self.0[..] + } + + fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { + Byte32Reader::from_slice(slice).map(|reader| reader.to_entity()) + } + + fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { + Byte32Reader::from_compatible_slice(slice).map(|reader| reader.to_entity()) + } + + fn new_builder() -> Self::Builder { + ::core::default::Default::default() + } + + fn as_builder(self) -> Self::Builder { + Self::new_builder().set([ + self.nth0(), + self.nth1(), + self.nth2(), + self.nth3(), + self.nth4(), + self.nth5(), + self.nth6(), + self.nth7(), + self.nth8(), + self.nth9(), + self.nth10(), + self.nth11(), + self.nth12(), + self.nth13(), + self.nth14(), + self.nth15(), + self.nth16(), + self.nth17(), + self.nth18(), + self.nth19(), + self.nth20(), + self.nth21(), + self.nth22(), + self.nth23(), + self.nth24(), + self.nth25(), + self.nth26(), + self.nth27(), + self.nth28(), + self.nth29(), + self.nth30(), + self.nth31(), + ]) + } +} +#[derive(Clone, Copy)] +pub struct Byte32Reader<'r>(&'r [u8]); +impl<'r> ::core::fmt::LowerHex for Byte32Reader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl<'r> ::core::fmt::Debug for Byte32Reader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl<'r> ::core::fmt::Display for Byte32Reader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + let raw_data = hex_string(&self.raw_data()); + write!(f, "{}(0x{})", Self::NAME, raw_data) + } +} +impl<'r> Byte32Reader<'r> { + pub const ITEM_COUNT: usize = 32; + pub const ITEM_SIZE: usize = 1; + pub const TOTAL_SIZE: usize = 32; + + pub fn nth0(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[0..1]) + } + + pub fn nth1(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[1..2]) + } + + pub fn nth2(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[2..3]) + } + + pub fn nth3(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[3..4]) + } + + pub fn nth4(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[4..5]) + } + + pub fn nth5(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[5..6]) + } + + pub fn nth6(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[6..7]) + } + + pub fn nth7(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[7..8]) + } + + pub fn nth8(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[8..9]) + } + + pub fn nth9(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[9..10]) + } + + pub fn nth10(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[10..11]) + } + + pub fn nth11(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[11..12]) + } + + pub fn nth12(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[12..13]) + } + + pub fn nth13(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[13..14]) + } + + pub fn nth14(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[14..15]) + } + + pub fn nth15(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[15..16]) + } + + pub fn nth16(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[16..17]) + } + + pub fn nth17(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[17..18]) + } + + pub fn nth18(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[18..19]) + } + + pub fn nth19(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[19..20]) + } + + pub fn nth20(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[20..21]) + } + + pub fn nth21(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[21..22]) + } + + pub fn nth22(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[22..23]) + } + + pub fn nth23(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[23..24]) + } + + pub fn nth24(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[24..25]) + } + + pub fn nth25(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[25..26]) + } + + pub fn nth26(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[26..27]) + } + + pub fn nth27(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[27..28]) + } + + pub fn nth28(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[28..29]) + } + + pub fn nth29(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[29..30]) + } + + pub fn nth30(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[30..31]) + } + + pub fn nth31(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[31..32]) + } + + pub fn raw_data(&self) -> &'r [u8] { + self.as_slice() + } +} +impl<'r> molecule::prelude::Reader<'r> for Byte32Reader<'r> { + type Entity = Byte32; + + const NAME: &'static str = "Byte32Reader"; + + fn to_entity(&self) -> Self::Entity { + Self::Entity::new_unchecked(self.as_slice().to_owned().into()) + } + + fn new_unchecked(slice: &'r [u8]) -> Self { + Byte32Reader(slice) + } + + fn as_slice(&self) -> &'r [u8] { + self.0 + } + + fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> { + use molecule::verification_error as ve; + let slice_len = slice.len(); + if slice_len != Self::TOTAL_SIZE { + return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len); + } + Ok(()) + } +} +pub struct Byte32Builder(pub(crate) [Byte; 32]); +impl ::core::fmt::Debug for Byte32Builder { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:?})", Self::NAME, &self.0[..]) + } +} +impl ::core::default::Default for Byte32Builder { + fn default() -> Self { + Byte32Builder([ + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + ]) + } +} +impl Byte32Builder { + pub const ITEM_COUNT: usize = 32; + pub const ITEM_SIZE: usize = 1; + pub const TOTAL_SIZE: usize = 32; + + pub fn set(mut self, v: [Byte; 32]) -> Self { + self.0 = v; + self + } + + pub fn nth0(mut self, v: Byte) -> Self { + self.0[0] = v; + self + } + + pub fn nth1(mut self, v: Byte) -> Self { + self.0[1] = v; + self + } + + pub fn nth2(mut self, v: Byte) -> Self { + self.0[2] = v; + self + } + + pub fn nth3(mut self, v: Byte) -> Self { + self.0[3] = v; + self + } + + pub fn nth4(mut self, v: Byte) -> Self { + self.0[4] = v; + self + } + + pub fn nth5(mut self, v: Byte) -> Self { + self.0[5] = v; + self + } + + pub fn nth6(mut self, v: Byte) -> Self { + self.0[6] = v; + self + } + + pub fn nth7(mut self, v: Byte) -> Self { + self.0[7] = v; + self + } + + pub fn nth8(mut self, v: Byte) -> Self { + self.0[8] = v; + self + } + + pub fn nth9(mut self, v: Byte) -> Self { + self.0[9] = v; + self + } + + pub fn nth10(mut self, v: Byte) -> Self { + self.0[10] = v; + self + } + + pub fn nth11(mut self, v: Byte) -> Self { + self.0[11] = v; + self + } + + pub fn nth12(mut self, v: Byte) -> Self { + self.0[12] = v; + self + } + + pub fn nth13(mut self, v: Byte) -> Self { + self.0[13] = v; + self + } + + pub fn nth14(mut self, v: Byte) -> Self { + self.0[14] = v; + self + } + + pub fn nth15(mut self, v: Byte) -> Self { + self.0[15] = v; + self + } + + pub fn nth16(mut self, v: Byte) -> Self { + self.0[16] = v; + self + } + + pub fn nth17(mut self, v: Byte) -> Self { + self.0[17] = v; + self + } + + pub fn nth18(mut self, v: Byte) -> Self { + self.0[18] = v; + self + } + + pub fn nth19(mut self, v: Byte) -> Self { + self.0[19] = v; + self + } + + pub fn nth20(mut self, v: Byte) -> Self { + self.0[20] = v; + self + } + + pub fn nth21(mut self, v: Byte) -> Self { + self.0[21] = v; + self + } + + pub fn nth22(mut self, v: Byte) -> Self { + self.0[22] = v; + self + } + + pub fn nth23(mut self, v: Byte) -> Self { + self.0[23] = v; + self + } + + pub fn nth24(mut self, v: Byte) -> Self { + self.0[24] = v; + self + } + + pub fn nth25(mut self, v: Byte) -> Self { + self.0[25] = v; + self + } + + pub fn nth26(mut self, v: Byte) -> Self { + self.0[26] = v; + self + } + + pub fn nth27(mut self, v: Byte) -> Self { + self.0[27] = v; + self + } + + pub fn nth28(mut self, v: Byte) -> Self { + self.0[28] = v; + self + } + + pub fn nth29(mut self, v: Byte) -> Self { + self.0[29] = v; + self + } + + pub fn nth30(mut self, v: Byte) -> Self { + self.0[30] = v; + self + } + + pub fn nth31(mut self, v: Byte) -> Self { + self.0[31] = v; + self + } +} +impl molecule::prelude::Builder for Byte32Builder { + type Entity = Byte32; + + const NAME: &'static str = "Byte32Builder"; + + fn expected_length(&self) -> usize { + Self::TOTAL_SIZE + } + + fn write(&self, writer: &mut W) -> molecule::io::Result<()> { + writer.write_all(self.0[0].as_slice())?; + writer.write_all(self.0[1].as_slice())?; + writer.write_all(self.0[2].as_slice())?; + writer.write_all(self.0[3].as_slice())?; + writer.write_all(self.0[4].as_slice())?; + writer.write_all(self.0[5].as_slice())?; + writer.write_all(self.0[6].as_slice())?; + writer.write_all(self.0[7].as_slice())?; + writer.write_all(self.0[8].as_slice())?; + writer.write_all(self.0[9].as_slice())?; + writer.write_all(self.0[10].as_slice())?; + writer.write_all(self.0[11].as_slice())?; + writer.write_all(self.0[12].as_slice())?; + writer.write_all(self.0[13].as_slice())?; + writer.write_all(self.0[14].as_slice())?; + writer.write_all(self.0[15].as_slice())?; + writer.write_all(self.0[16].as_slice())?; + writer.write_all(self.0[17].as_slice())?; + writer.write_all(self.0[18].as_slice())?; + writer.write_all(self.0[19].as_slice())?; + writer.write_all(self.0[20].as_slice())?; + writer.write_all(self.0[21].as_slice())?; + writer.write_all(self.0[22].as_slice())?; + writer.write_all(self.0[23].as_slice())?; + writer.write_all(self.0[24].as_slice())?; + writer.write_all(self.0[25].as_slice())?; + writer.write_all(self.0[26].as_slice())?; + writer.write_all(self.0[27].as_slice())?; + writer.write_all(self.0[28].as_slice())?; + writer.write_all(self.0[29].as_slice())?; + writer.write_all(self.0[30].as_slice())?; + writer.write_all(self.0[31].as_slice())?; + Ok(()) + } + + fn build(&self) -> Self::Entity { + let mut inner = Vec::with_capacity(self.expected_length()); + self.write(&mut inner) + .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); + Byte32::new_unchecked(inner.into()) + } +} +#[derive(Clone)] +pub struct Uint256(molecule::bytes::Bytes); +impl ::core::fmt::LowerHex for Uint256 { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl ::core::fmt::Debug for Uint256 { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl ::core::fmt::Display for Uint256 { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + let raw_data = hex_string(&self.raw_data()); + write!(f, "{}(0x{})", Self::NAME, raw_data) + } +} +impl ::core::default::Default for Uint256 { + fn default() -> Self { + let v: Vec = vec![ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, + ]; + Uint256::new_unchecked(v.into()) + } +} +impl Uint256 { + pub const ITEM_COUNT: usize = 32; + pub const ITEM_SIZE: usize = 1; + pub const TOTAL_SIZE: usize = 32; + + pub fn nth0(&self) -> Byte { + Byte::new_unchecked(self.0.slice(0..1)) + } + + pub fn nth1(&self) -> Byte { + Byte::new_unchecked(self.0.slice(1..2)) + } + + pub fn nth2(&self) -> Byte { + Byte::new_unchecked(self.0.slice(2..3)) + } + + pub fn nth3(&self) -> Byte { + Byte::new_unchecked(self.0.slice(3..4)) + } + + pub fn nth4(&self) -> Byte { + Byte::new_unchecked(self.0.slice(4..5)) + } + + pub fn nth5(&self) -> Byte { + Byte::new_unchecked(self.0.slice(5..6)) + } + + pub fn nth6(&self) -> Byte { + Byte::new_unchecked(self.0.slice(6..7)) + } + + pub fn nth7(&self) -> Byte { + Byte::new_unchecked(self.0.slice(7..8)) + } + + pub fn nth8(&self) -> Byte { + Byte::new_unchecked(self.0.slice(8..9)) + } + + pub fn nth9(&self) -> Byte { + Byte::new_unchecked(self.0.slice(9..10)) + } + + pub fn nth10(&self) -> Byte { + Byte::new_unchecked(self.0.slice(10..11)) + } + + pub fn nth11(&self) -> Byte { + Byte::new_unchecked(self.0.slice(11..12)) + } + + pub fn nth12(&self) -> Byte { + Byte::new_unchecked(self.0.slice(12..13)) + } + + pub fn nth13(&self) -> Byte { + Byte::new_unchecked(self.0.slice(13..14)) + } + + pub fn nth14(&self) -> Byte { + Byte::new_unchecked(self.0.slice(14..15)) + } + + pub fn nth15(&self) -> Byte { + Byte::new_unchecked(self.0.slice(15..16)) + } + + pub fn nth16(&self) -> Byte { + Byte::new_unchecked(self.0.slice(16..17)) + } + + pub fn nth17(&self) -> Byte { + Byte::new_unchecked(self.0.slice(17..18)) + } + + pub fn nth18(&self) -> Byte { + Byte::new_unchecked(self.0.slice(18..19)) + } + + pub fn nth19(&self) -> Byte { + Byte::new_unchecked(self.0.slice(19..20)) + } + + pub fn nth20(&self) -> Byte { + Byte::new_unchecked(self.0.slice(20..21)) + } + + pub fn nth21(&self) -> Byte { + Byte::new_unchecked(self.0.slice(21..22)) + } + + pub fn nth22(&self) -> Byte { + Byte::new_unchecked(self.0.slice(22..23)) + } + + pub fn nth23(&self) -> Byte { + Byte::new_unchecked(self.0.slice(23..24)) + } + + pub fn nth24(&self) -> Byte { + Byte::new_unchecked(self.0.slice(24..25)) + } + + pub fn nth25(&self) -> Byte { + Byte::new_unchecked(self.0.slice(25..26)) + } + + pub fn nth26(&self) -> Byte { + Byte::new_unchecked(self.0.slice(26..27)) + } + + pub fn nth27(&self) -> Byte { + Byte::new_unchecked(self.0.slice(27..28)) + } + + pub fn nth28(&self) -> Byte { + Byte::new_unchecked(self.0.slice(28..29)) + } + + pub fn nth29(&self) -> Byte { + Byte::new_unchecked(self.0.slice(29..30)) + } + + pub fn nth30(&self) -> Byte { + Byte::new_unchecked(self.0.slice(30..31)) + } + + pub fn nth31(&self) -> Byte { + Byte::new_unchecked(self.0.slice(31..32)) + } + + pub fn raw_data(&self) -> molecule::bytes::Bytes { + self.as_bytes() + } + + pub fn as_reader<'r>(&'r self) -> Uint256Reader<'r> { + Uint256Reader::new_unchecked(self.as_slice()) + } +} +impl molecule::prelude::Entity for Uint256 { + type Builder = Uint256Builder; + + const NAME: &'static str = "Uint256"; + + fn new_unchecked(data: molecule::bytes::Bytes) -> Self { + Uint256(data) + } + + fn as_bytes(&self) -> molecule::bytes::Bytes { + self.0.clone() + } + + fn as_slice(&self) -> &[u8] { + &self.0[..] + } + + fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { + Uint256Reader::from_slice(slice).map(|reader| reader.to_entity()) + } + + fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { + Uint256Reader::from_compatible_slice(slice).map(|reader| reader.to_entity()) + } + + fn new_builder() -> Self::Builder { + ::core::default::Default::default() + } + + fn as_builder(self) -> Self::Builder { + Self::new_builder().set([ + self.nth0(), + self.nth1(), + self.nth2(), + self.nth3(), + self.nth4(), + self.nth5(), + self.nth6(), + self.nth7(), + self.nth8(), + self.nth9(), + self.nth10(), + self.nth11(), + self.nth12(), + self.nth13(), + self.nth14(), + self.nth15(), + self.nth16(), + self.nth17(), + self.nth18(), + self.nth19(), + self.nth20(), + self.nth21(), + self.nth22(), + self.nth23(), + self.nth24(), + self.nth25(), + self.nth26(), + self.nth27(), + self.nth28(), + self.nth29(), + self.nth30(), + self.nth31(), + ]) + } +} +#[derive(Clone, Copy)] +pub struct Uint256Reader<'r>(&'r [u8]); +impl<'r> ::core::fmt::LowerHex for Uint256Reader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl<'r> ::core::fmt::Debug for Uint256Reader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl<'r> ::core::fmt::Display for Uint256Reader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + let raw_data = hex_string(&self.raw_data()); + write!(f, "{}(0x{})", Self::NAME, raw_data) + } +} +impl<'r> Uint256Reader<'r> { + pub const ITEM_COUNT: usize = 32; + pub const ITEM_SIZE: usize = 1; + pub const TOTAL_SIZE: usize = 32; + + pub fn nth0(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[0..1]) + } + + pub fn nth1(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[1..2]) + } + + pub fn nth2(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[2..3]) + } + + pub fn nth3(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[3..4]) + } + + pub fn nth4(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[4..5]) + } + + pub fn nth5(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[5..6]) + } + + pub fn nth6(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[6..7]) + } + + pub fn nth7(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[7..8]) + } + + pub fn nth8(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[8..9]) + } + + pub fn nth9(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[9..10]) + } + + pub fn nth10(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[10..11]) + } + + pub fn nth11(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[11..12]) + } + + pub fn nth12(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[12..13]) + } + + pub fn nth13(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[13..14]) + } + + pub fn nth14(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[14..15]) + } + + pub fn nth15(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[15..16]) + } + + pub fn nth16(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[16..17]) + } + + pub fn nth17(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[17..18]) + } + + pub fn nth18(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[18..19]) + } + + pub fn nth19(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[19..20]) + } + + pub fn nth20(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[20..21]) + } + + pub fn nth21(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[21..22]) + } + + pub fn nth22(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[22..23]) + } + + pub fn nth23(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[23..24]) + } + + pub fn nth24(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[24..25]) + } + + pub fn nth25(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[25..26]) + } + + pub fn nth26(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[26..27]) + } + + pub fn nth27(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[27..28]) + } + + pub fn nth28(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[28..29]) + } + + pub fn nth29(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[29..30]) + } + + pub fn nth30(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[30..31]) + } + + pub fn nth31(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[31..32]) + } + + pub fn raw_data(&self) -> &'r [u8] { + self.as_slice() + } +} +impl<'r> molecule::prelude::Reader<'r> for Uint256Reader<'r> { + type Entity = Uint256; + + const NAME: &'static str = "Uint256Reader"; + + fn to_entity(&self) -> Self::Entity { + Self::Entity::new_unchecked(self.as_slice().to_owned().into()) + } + + fn new_unchecked(slice: &'r [u8]) -> Self { + Uint256Reader(slice) + } + + fn as_slice(&self) -> &'r [u8] { + self.0 + } + + fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> { + use molecule::verification_error as ve; + let slice_len = slice.len(); + if slice_len != Self::TOTAL_SIZE { + return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len); + } + Ok(()) + } +} +pub struct Uint256Builder(pub(crate) [Byte; 32]); +impl ::core::fmt::Debug for Uint256Builder { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:?})", Self::NAME, &self.0[..]) + } +} +impl ::core::default::Default for Uint256Builder { + fn default() -> Self { + Uint256Builder([ + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + ]) + } +} +impl Uint256Builder { + pub const ITEM_COUNT: usize = 32; + pub const ITEM_SIZE: usize = 1; + pub const TOTAL_SIZE: usize = 32; + + pub fn set(mut self, v: [Byte; 32]) -> Self { + self.0 = v; + self + } + + pub fn nth0(mut self, v: Byte) -> Self { + self.0[0] = v; + self + } + + pub fn nth1(mut self, v: Byte) -> Self { + self.0[1] = v; + self + } + + pub fn nth2(mut self, v: Byte) -> Self { + self.0[2] = v; + self + } + + pub fn nth3(mut self, v: Byte) -> Self { + self.0[3] = v; + self + } + + pub fn nth4(mut self, v: Byte) -> Self { + self.0[4] = v; + self + } + + pub fn nth5(mut self, v: Byte) -> Self { + self.0[5] = v; + self + } + + pub fn nth6(mut self, v: Byte) -> Self { + self.0[6] = v; + self + } + + pub fn nth7(mut self, v: Byte) -> Self { + self.0[7] = v; + self + } + + pub fn nth8(mut self, v: Byte) -> Self { + self.0[8] = v; + self + } + + pub fn nth9(mut self, v: Byte) -> Self { + self.0[9] = v; + self + } + + pub fn nth10(mut self, v: Byte) -> Self { + self.0[10] = v; + self + } + + pub fn nth11(mut self, v: Byte) -> Self { + self.0[11] = v; + self + } + + pub fn nth12(mut self, v: Byte) -> Self { + self.0[12] = v; + self + } + + pub fn nth13(mut self, v: Byte) -> Self { + self.0[13] = v; + self + } + + pub fn nth14(mut self, v: Byte) -> Self { + self.0[14] = v; + self + } + + pub fn nth15(mut self, v: Byte) -> Self { + self.0[15] = v; + self + } + + pub fn nth16(mut self, v: Byte) -> Self { + self.0[16] = v; + self + } + + pub fn nth17(mut self, v: Byte) -> Self { + self.0[17] = v; + self + } + + pub fn nth18(mut self, v: Byte) -> Self { + self.0[18] = v; + self + } + + pub fn nth19(mut self, v: Byte) -> Self { + self.0[19] = v; + self + } + + pub fn nth20(mut self, v: Byte) -> Self { + self.0[20] = v; + self + } + + pub fn nth21(mut self, v: Byte) -> Self { + self.0[21] = v; + self + } + + pub fn nth22(mut self, v: Byte) -> Self { + self.0[22] = v; + self + } + + pub fn nth23(mut self, v: Byte) -> Self { + self.0[23] = v; + self + } + + pub fn nth24(mut self, v: Byte) -> Self { + self.0[24] = v; + self + } + + pub fn nth25(mut self, v: Byte) -> Self { + self.0[25] = v; + self + } + + pub fn nth26(mut self, v: Byte) -> Self { + self.0[26] = v; + self + } + + pub fn nth27(mut self, v: Byte) -> Self { + self.0[27] = v; + self + } + + pub fn nth28(mut self, v: Byte) -> Self { + self.0[28] = v; + self + } + + pub fn nth29(mut self, v: Byte) -> Self { + self.0[29] = v; + self + } + + pub fn nth30(mut self, v: Byte) -> Self { + self.0[30] = v; + self + } + + pub fn nth31(mut self, v: Byte) -> Self { + self.0[31] = v; + self + } +} +impl molecule::prelude::Builder for Uint256Builder { + type Entity = Uint256; + + const NAME: &'static str = "Uint256Builder"; + + fn expected_length(&self) -> usize { + Self::TOTAL_SIZE + } + + fn write(&self, writer: &mut W) -> molecule::io::Result<()> { + writer.write_all(self.0[0].as_slice())?; + writer.write_all(self.0[1].as_slice())?; + writer.write_all(self.0[2].as_slice())?; + writer.write_all(self.0[3].as_slice())?; + writer.write_all(self.0[4].as_slice())?; + writer.write_all(self.0[5].as_slice())?; + writer.write_all(self.0[6].as_slice())?; + writer.write_all(self.0[7].as_slice())?; + writer.write_all(self.0[8].as_slice())?; + writer.write_all(self.0[9].as_slice())?; + writer.write_all(self.0[10].as_slice())?; + writer.write_all(self.0[11].as_slice())?; + writer.write_all(self.0[12].as_slice())?; + writer.write_all(self.0[13].as_slice())?; + writer.write_all(self.0[14].as_slice())?; + writer.write_all(self.0[15].as_slice())?; + writer.write_all(self.0[16].as_slice())?; + writer.write_all(self.0[17].as_slice())?; + writer.write_all(self.0[18].as_slice())?; + writer.write_all(self.0[19].as_slice())?; + writer.write_all(self.0[20].as_slice())?; + writer.write_all(self.0[21].as_slice())?; + writer.write_all(self.0[22].as_slice())?; + writer.write_all(self.0[23].as_slice())?; + writer.write_all(self.0[24].as_slice())?; + writer.write_all(self.0[25].as_slice())?; + writer.write_all(self.0[26].as_slice())?; + writer.write_all(self.0[27].as_slice())?; + writer.write_all(self.0[28].as_slice())?; + writer.write_all(self.0[29].as_slice())?; + writer.write_all(self.0[30].as_slice())?; + writer.write_all(self.0[31].as_slice())?; + Ok(()) + } + + fn build(&self) -> Self::Entity { + let mut inner = Vec::with_capacity(self.expected_length()); + self.write(&mut inner) + .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); + Uint256::new_unchecked(inner.into()) + } +} +#[derive(Clone)] +pub struct H256(molecule::bytes::Bytes); +impl ::core::fmt::LowerHex for H256 { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl ::core::fmt::Debug for H256 { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl ::core::fmt::Display for H256 { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + let raw_data = hex_string(&self.raw_data()); + write!(f, "{}(0x{})", Self::NAME, raw_data) + } +} +impl ::core::default::Default for H256 { + fn default() -> Self { + let v: Vec = vec![ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, + ]; + H256::new_unchecked(v.into()) + } +} +impl H256 { + pub const ITEM_COUNT: usize = 32; + pub const ITEM_SIZE: usize = 1; + pub const TOTAL_SIZE: usize = 32; + + pub fn nth0(&self) -> Byte { + Byte::new_unchecked(self.0.slice(0..1)) + } + + pub fn nth1(&self) -> Byte { + Byte::new_unchecked(self.0.slice(1..2)) + } + + pub fn nth2(&self) -> Byte { + Byte::new_unchecked(self.0.slice(2..3)) + } + + pub fn nth3(&self) -> Byte { + Byte::new_unchecked(self.0.slice(3..4)) + } + + pub fn nth4(&self) -> Byte { + Byte::new_unchecked(self.0.slice(4..5)) + } + + pub fn nth5(&self) -> Byte { + Byte::new_unchecked(self.0.slice(5..6)) + } + + pub fn nth6(&self) -> Byte { + Byte::new_unchecked(self.0.slice(6..7)) + } + + pub fn nth7(&self) -> Byte { + Byte::new_unchecked(self.0.slice(7..8)) + } + + pub fn nth8(&self) -> Byte { + Byte::new_unchecked(self.0.slice(8..9)) + } + + pub fn nth9(&self) -> Byte { + Byte::new_unchecked(self.0.slice(9..10)) + } + + pub fn nth10(&self) -> Byte { + Byte::new_unchecked(self.0.slice(10..11)) + } + + pub fn nth11(&self) -> Byte { + Byte::new_unchecked(self.0.slice(11..12)) + } + + pub fn nth12(&self) -> Byte { + Byte::new_unchecked(self.0.slice(12..13)) + } + + pub fn nth13(&self) -> Byte { + Byte::new_unchecked(self.0.slice(13..14)) + } + + pub fn nth14(&self) -> Byte { + Byte::new_unchecked(self.0.slice(14..15)) + } + + pub fn nth15(&self) -> Byte { + Byte::new_unchecked(self.0.slice(15..16)) + } + + pub fn nth16(&self) -> Byte { + Byte::new_unchecked(self.0.slice(16..17)) + } + + pub fn nth17(&self) -> Byte { + Byte::new_unchecked(self.0.slice(17..18)) + } + + pub fn nth18(&self) -> Byte { + Byte::new_unchecked(self.0.slice(18..19)) + } + + pub fn nth19(&self) -> Byte { + Byte::new_unchecked(self.0.slice(19..20)) + } + + pub fn nth20(&self) -> Byte { + Byte::new_unchecked(self.0.slice(20..21)) + } + + pub fn nth21(&self) -> Byte { + Byte::new_unchecked(self.0.slice(21..22)) + } + + pub fn nth22(&self) -> Byte { + Byte::new_unchecked(self.0.slice(22..23)) + } + + pub fn nth23(&self) -> Byte { + Byte::new_unchecked(self.0.slice(23..24)) + } + + pub fn nth24(&self) -> Byte { + Byte::new_unchecked(self.0.slice(24..25)) + } + + pub fn nth25(&self) -> Byte { + Byte::new_unchecked(self.0.slice(25..26)) + } + + pub fn nth26(&self) -> Byte { + Byte::new_unchecked(self.0.slice(26..27)) + } + + pub fn nth27(&self) -> Byte { + Byte::new_unchecked(self.0.slice(27..28)) + } + + pub fn nth28(&self) -> Byte { + Byte::new_unchecked(self.0.slice(28..29)) + } + + pub fn nth29(&self) -> Byte { + Byte::new_unchecked(self.0.slice(29..30)) + } + + pub fn nth30(&self) -> Byte { + Byte::new_unchecked(self.0.slice(30..31)) + } + + pub fn nth31(&self) -> Byte { + Byte::new_unchecked(self.0.slice(31..32)) + } + + pub fn raw_data(&self) -> molecule::bytes::Bytes { + self.as_bytes() + } + + pub fn as_reader<'r>(&'r self) -> H256Reader<'r> { + H256Reader::new_unchecked(self.as_slice()) + } +} +impl molecule::prelude::Entity for H256 { + type Builder = H256Builder; + + const NAME: &'static str = "H256"; + + fn new_unchecked(data: molecule::bytes::Bytes) -> Self { + H256(data) + } + + fn as_bytes(&self) -> molecule::bytes::Bytes { + self.0.clone() + } + + fn as_slice(&self) -> &[u8] { + &self.0[..] + } + + fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { + H256Reader::from_slice(slice).map(|reader| reader.to_entity()) + } + + fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { + H256Reader::from_compatible_slice(slice).map(|reader| reader.to_entity()) + } + + fn new_builder() -> Self::Builder { + ::core::default::Default::default() + } + + fn as_builder(self) -> Self::Builder { + Self::new_builder().set([ + self.nth0(), + self.nth1(), + self.nth2(), + self.nth3(), + self.nth4(), + self.nth5(), + self.nth6(), + self.nth7(), + self.nth8(), + self.nth9(), + self.nth10(), + self.nth11(), + self.nth12(), + self.nth13(), + self.nth14(), + self.nth15(), + self.nth16(), + self.nth17(), + self.nth18(), + self.nth19(), + self.nth20(), + self.nth21(), + self.nth22(), + self.nth23(), + self.nth24(), + self.nth25(), + self.nth26(), + self.nth27(), + self.nth28(), + self.nth29(), + self.nth30(), + self.nth31(), + ]) + } +} +#[derive(Clone, Copy)] +pub struct H256Reader<'r>(&'r [u8]); +impl<'r> ::core::fmt::LowerHex for H256Reader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl<'r> ::core::fmt::Debug for H256Reader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl<'r> ::core::fmt::Display for H256Reader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + let raw_data = hex_string(&self.raw_data()); + write!(f, "{}(0x{})", Self::NAME, raw_data) + } +} +impl<'r> H256Reader<'r> { + pub const ITEM_COUNT: usize = 32; + pub const ITEM_SIZE: usize = 1; + pub const TOTAL_SIZE: usize = 32; + + pub fn nth0(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[0..1]) + } + + pub fn nth1(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[1..2]) + } + + pub fn nth2(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[2..3]) + } + + pub fn nth3(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[3..4]) + } + + pub fn nth4(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[4..5]) + } + + pub fn nth5(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[5..6]) + } + + pub fn nth6(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[6..7]) + } + + pub fn nth7(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[7..8]) + } + + pub fn nth8(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[8..9]) + } + + pub fn nth9(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[9..10]) + } + + pub fn nth10(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[10..11]) + } + + pub fn nth11(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[11..12]) + } + + pub fn nth12(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[12..13]) + } + + pub fn nth13(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[13..14]) + } + + pub fn nth14(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[14..15]) + } + + pub fn nth15(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[15..16]) + } + + pub fn nth16(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[16..17]) + } + + pub fn nth17(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[17..18]) + } + + pub fn nth18(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[18..19]) + } + + pub fn nth19(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[19..20]) + } + + pub fn nth20(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[20..21]) + } + + pub fn nth21(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[21..22]) + } + + pub fn nth22(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[22..23]) + } + + pub fn nth23(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[23..24]) + } + + pub fn nth24(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[24..25]) + } + + pub fn nth25(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[25..26]) + } + + pub fn nth26(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[26..27]) + } + + pub fn nth27(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[27..28]) + } + + pub fn nth28(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[28..29]) + } + + pub fn nth29(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[29..30]) + } + + pub fn nth30(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[30..31]) + } + + pub fn nth31(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[31..32]) + } + + pub fn raw_data(&self) -> &'r [u8] { + self.as_slice() + } +} +impl<'r> molecule::prelude::Reader<'r> for H256Reader<'r> { + type Entity = H256; + + const NAME: &'static str = "H256Reader"; + + fn to_entity(&self) -> Self::Entity { + Self::Entity::new_unchecked(self.as_slice().to_owned().into()) + } + + fn new_unchecked(slice: &'r [u8]) -> Self { + H256Reader(slice) + } + + fn as_slice(&self) -> &'r [u8] { + self.0 + } + + fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> { + use molecule::verification_error as ve; + let slice_len = slice.len(); + if slice_len != Self::TOTAL_SIZE { + return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len); + } + Ok(()) + } +} +pub struct H256Builder(pub(crate) [Byte; 32]); +impl ::core::fmt::Debug for H256Builder { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:?})", Self::NAME, &self.0[..]) + } +} +impl ::core::default::Default for H256Builder { + fn default() -> Self { + H256Builder([ + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + ]) + } +} +impl H256Builder { + pub const ITEM_COUNT: usize = 32; + pub const ITEM_SIZE: usize = 1; + pub const TOTAL_SIZE: usize = 32; + + pub fn set(mut self, v: [Byte; 32]) -> Self { + self.0 = v; + self + } + + pub fn nth0(mut self, v: Byte) -> Self { + self.0[0] = v; + self + } + + pub fn nth1(mut self, v: Byte) -> Self { + self.0[1] = v; + self + } + + pub fn nth2(mut self, v: Byte) -> Self { + self.0[2] = v; + self + } + + pub fn nth3(mut self, v: Byte) -> Self { + self.0[3] = v; + self + } + + pub fn nth4(mut self, v: Byte) -> Self { + self.0[4] = v; + self + } + + pub fn nth5(mut self, v: Byte) -> Self { + self.0[5] = v; + self + } + + pub fn nth6(mut self, v: Byte) -> Self { + self.0[6] = v; + self + } + + pub fn nth7(mut self, v: Byte) -> Self { + self.0[7] = v; + self + } + + pub fn nth8(mut self, v: Byte) -> Self { + self.0[8] = v; + self + } + + pub fn nth9(mut self, v: Byte) -> Self { + self.0[9] = v; + self + } + + pub fn nth10(mut self, v: Byte) -> Self { + self.0[10] = v; + self + } + + pub fn nth11(mut self, v: Byte) -> Self { + self.0[11] = v; + self + } + + pub fn nth12(mut self, v: Byte) -> Self { + self.0[12] = v; + self + } + + pub fn nth13(mut self, v: Byte) -> Self { + self.0[13] = v; + self + } + + pub fn nth14(mut self, v: Byte) -> Self { + self.0[14] = v; + self + } + + pub fn nth15(mut self, v: Byte) -> Self { + self.0[15] = v; + self + } + + pub fn nth16(mut self, v: Byte) -> Self { + self.0[16] = v; + self + } + + pub fn nth17(mut self, v: Byte) -> Self { + self.0[17] = v; + self + } + + pub fn nth18(mut self, v: Byte) -> Self { + self.0[18] = v; + self + } + + pub fn nth19(mut self, v: Byte) -> Self { + self.0[19] = v; + self + } + + pub fn nth20(mut self, v: Byte) -> Self { + self.0[20] = v; + self + } + + pub fn nth21(mut self, v: Byte) -> Self { + self.0[21] = v; + self + } + + pub fn nth22(mut self, v: Byte) -> Self { + self.0[22] = v; + self + } + + pub fn nth23(mut self, v: Byte) -> Self { + self.0[23] = v; + self + } + + pub fn nth24(mut self, v: Byte) -> Self { + self.0[24] = v; + self + } + + pub fn nth25(mut self, v: Byte) -> Self { + self.0[25] = v; + self } pub fn nth26(mut self, v: Byte) -> Self { @@ -626,86 +2977,394 @@ impl H256Builder { self } - pub fn nth27(mut self, v: Byte) -> Self { - self.0[27] = v; - self + pub fn nth27(mut self, v: Byte) -> Self { + self.0[27] = v; + self + } + + pub fn nth28(mut self, v: Byte) -> Self { + self.0[28] = v; + self + } + + pub fn nth29(mut self, v: Byte) -> Self { + self.0[29] = v; + self + } + + pub fn nth30(mut self, v: Byte) -> Self { + self.0[30] = v; + self + } + + pub fn nth31(mut self, v: Byte) -> Self { + self.0[31] = v; + self + } +} +impl molecule::prelude::Builder for H256Builder { + type Entity = H256; + + const NAME: &'static str = "H256Builder"; + + fn expected_length(&self) -> usize { + Self::TOTAL_SIZE + } + + fn write(&self, writer: &mut W) -> molecule::io::Result<()> { + writer.write_all(self.0[0].as_slice())?; + writer.write_all(self.0[1].as_slice())?; + writer.write_all(self.0[2].as_slice())?; + writer.write_all(self.0[3].as_slice())?; + writer.write_all(self.0[4].as_slice())?; + writer.write_all(self.0[5].as_slice())?; + writer.write_all(self.0[6].as_slice())?; + writer.write_all(self.0[7].as_slice())?; + writer.write_all(self.0[8].as_slice())?; + writer.write_all(self.0[9].as_slice())?; + writer.write_all(self.0[10].as_slice())?; + writer.write_all(self.0[11].as_slice())?; + writer.write_all(self.0[12].as_slice())?; + writer.write_all(self.0[13].as_slice())?; + writer.write_all(self.0[14].as_slice())?; + writer.write_all(self.0[15].as_slice())?; + writer.write_all(self.0[16].as_slice())?; + writer.write_all(self.0[17].as_slice())?; + writer.write_all(self.0[18].as_slice())?; + writer.write_all(self.0[19].as_slice())?; + writer.write_all(self.0[20].as_slice())?; + writer.write_all(self.0[21].as_slice())?; + writer.write_all(self.0[22].as_slice())?; + writer.write_all(self.0[23].as_slice())?; + writer.write_all(self.0[24].as_slice())?; + writer.write_all(self.0[25].as_slice())?; + writer.write_all(self.0[26].as_slice())?; + writer.write_all(self.0[27].as_slice())?; + writer.write_all(self.0[28].as_slice())?; + writer.write_all(self.0[29].as_slice())?; + writer.write_all(self.0[30].as_slice())?; + writer.write_all(self.0[31].as_slice())?; + Ok(()) + } + + fn build(&self) -> Self::Entity { + let mut inner = Vec::with_capacity(self.expected_length()); + self.write(&mut inner) + .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); + H256::new_unchecked(inner.into()) + } +} +#[derive(Clone)] +pub struct Uint32Vec(molecule::bytes::Bytes); +impl ::core::fmt::LowerHex for Uint32Vec { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl ::core::fmt::Debug for Uint32Vec { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl ::core::fmt::Display for Uint32Vec { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{} [", Self::NAME)?; + for i in 0..self.len() { + if i == 0 { + write!(f, "{}", self.get_unchecked(i))?; + } else { + write!(f, ", {}", self.get_unchecked(i))?; + } + } + write!(f, "]") + } +} +impl ::core::default::Default for Uint32Vec { + fn default() -> Self { + let v: Vec = vec![0, 0, 0, 0]; + Uint32Vec::new_unchecked(v.into()) + } +} +impl Uint32Vec { + pub const ITEM_SIZE: usize = 4; + + pub fn total_size(&self) -> usize { + molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count() + } + + pub fn item_count(&self) -> usize { + molecule::unpack_number(self.as_slice()) as usize + } + + pub fn len(&self) -> usize { + self.item_count() + } + + pub fn is_empty(&self) -> bool { + self.len() == 0 } - pub fn nth28(mut self, v: Byte) -> Self { - self.0[28] = v; - self + pub fn get(&self, idx: usize) -> Option { + if idx >= self.len() { + None + } else { + Some(self.get_unchecked(idx)) + } } - pub fn nth29(mut self, v: Byte) -> Self { - self.0[29] = v; + pub fn get_unchecked(&self, idx: usize) -> Uint32 { + let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx; + let end = start + Self::ITEM_SIZE; + Uint32::new_unchecked(self.0.slice(start..end)) + } + + pub fn as_reader<'r>(&'r self) -> Uint32VecReader<'r> { + Uint32VecReader::new_unchecked(self.as_slice()) + } +} +impl molecule::prelude::Entity for Uint32Vec { + type Builder = Uint32VecBuilder; + + const NAME: &'static str = "Uint32Vec"; + + fn new_unchecked(data: molecule::bytes::Bytes) -> Self { + Uint32Vec(data) + } + + fn as_bytes(&self) -> molecule::bytes::Bytes { + self.0.clone() + } + + fn as_slice(&self) -> &[u8] { + &self.0[..] + } + + fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { + Uint32VecReader::from_slice(slice).map(|reader| reader.to_entity()) + } + + fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { + Uint32VecReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) + } + + fn new_builder() -> Self::Builder { + ::core::default::Default::default() + } + + fn as_builder(self) -> Self::Builder { + Self::new_builder().extend(self.into_iter()) + } +} +#[derive(Clone, Copy)] +pub struct Uint32VecReader<'r>(&'r [u8]); +impl<'r> ::core::fmt::LowerHex for Uint32VecReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl<'r> ::core::fmt::Debug for Uint32VecReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl<'r> ::core::fmt::Display for Uint32VecReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{} [", Self::NAME)?; + for i in 0..self.len() { + if i == 0 { + write!(f, "{}", self.get_unchecked(i))?; + } else { + write!(f, ", {}", self.get_unchecked(i))?; + } + } + write!(f, "]") + } +} +impl<'r> Uint32VecReader<'r> { + pub const ITEM_SIZE: usize = 4; + + pub fn total_size(&self) -> usize { + molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count() + } + + pub fn item_count(&self) -> usize { + molecule::unpack_number(self.as_slice()) as usize + } + + pub fn len(&self) -> usize { + self.item_count() + } + + pub fn is_empty(&self) -> bool { + self.len() == 0 + } + + pub fn get(&self, idx: usize) -> Option> { + if idx >= self.len() { + None + } else { + Some(self.get_unchecked(idx)) + } + } + + pub fn get_unchecked(&self, idx: usize) -> Uint32Reader<'r> { + let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx; + let end = start + Self::ITEM_SIZE; + Uint32Reader::new_unchecked(&self.as_slice()[start..end]) + } +} +impl<'r> molecule::prelude::Reader<'r> for Uint32VecReader<'r> { + type Entity = Uint32Vec; + + const NAME: &'static str = "Uint32VecReader"; + + fn to_entity(&self) -> Self::Entity { + Self::Entity::new_unchecked(self.as_slice().to_owned().into()) + } + + fn new_unchecked(slice: &'r [u8]) -> Self { + Uint32VecReader(slice) + } + + fn as_slice(&self) -> &'r [u8] { + self.0 + } + + fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> { + use molecule::verification_error as ve; + let slice_len = slice.len(); + if slice_len < molecule::NUMBER_SIZE { + return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len); + } + let item_count = molecule::unpack_number(slice) as usize; + if item_count == 0 { + if slice_len != molecule::NUMBER_SIZE { + return ve!(Self, TotalSizeNotMatch, molecule::NUMBER_SIZE, slice_len); + } + return Ok(()); + } + let total_size = molecule::NUMBER_SIZE + Self::ITEM_SIZE * item_count; + if slice_len != total_size { + return ve!(Self, TotalSizeNotMatch, total_size, slice_len); + } + Ok(()) + } +} +#[derive(Debug, Default)] +pub struct Uint32VecBuilder(pub(crate) Vec); +impl Uint32VecBuilder { + pub const ITEM_SIZE: usize = 4; + + pub fn set(mut self, v: Vec) -> Self { + self.0 = v; self } - pub fn nth30(mut self, v: Byte) -> Self { - self.0[30] = v; + pub fn push(mut self, v: Uint32) -> Self { + self.0.push(v); self } - pub fn nth31(mut self, v: Byte) -> Self { - self.0[31] = v; + pub fn extend>(mut self, iter: T) -> Self { + for elem in iter { + self.0.push(elem); + } self } + + pub fn replace(&mut self, index: usize, v: Uint32) -> Option { + self.0 + .get_mut(index) + .map(|item| ::core::mem::replace(item, v)) + } } -impl molecule::prelude::Builder for H256Builder { - type Entity = H256; +impl molecule::prelude::Builder for Uint32VecBuilder { + type Entity = Uint32Vec; - const NAME: &'static str = "H256Builder"; + const NAME: &'static str = "Uint32VecBuilder"; fn expected_length(&self) -> usize { - Self::TOTAL_SIZE + molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.0.len() } fn write(&self, writer: &mut W) -> molecule::io::Result<()> { - writer.write_all(self.0[0].as_slice())?; - writer.write_all(self.0[1].as_slice())?; - writer.write_all(self.0[2].as_slice())?; - writer.write_all(self.0[3].as_slice())?; - writer.write_all(self.0[4].as_slice())?; - writer.write_all(self.0[5].as_slice())?; - writer.write_all(self.0[6].as_slice())?; - writer.write_all(self.0[7].as_slice())?; - writer.write_all(self.0[8].as_slice())?; - writer.write_all(self.0[9].as_slice())?; - writer.write_all(self.0[10].as_slice())?; - writer.write_all(self.0[11].as_slice())?; - writer.write_all(self.0[12].as_slice())?; - writer.write_all(self.0[13].as_slice())?; - writer.write_all(self.0[14].as_slice())?; - writer.write_all(self.0[15].as_slice())?; - writer.write_all(self.0[16].as_slice())?; - writer.write_all(self.0[17].as_slice())?; - writer.write_all(self.0[18].as_slice())?; - writer.write_all(self.0[19].as_slice())?; - writer.write_all(self.0[20].as_slice())?; - writer.write_all(self.0[21].as_slice())?; - writer.write_all(self.0[22].as_slice())?; - writer.write_all(self.0[23].as_slice())?; - writer.write_all(self.0[24].as_slice())?; - writer.write_all(self.0[25].as_slice())?; - writer.write_all(self.0[26].as_slice())?; - writer.write_all(self.0[27].as_slice())?; - writer.write_all(self.0[28].as_slice())?; - writer.write_all(self.0[29].as_slice())?; - writer.write_all(self.0[30].as_slice())?; - writer.write_all(self.0[31].as_slice())?; + writer.write_all(&molecule::pack_number(self.0.len() as molecule::Number))?; + for inner in &self.0[..] { + writer.write_all(inner.as_slice())?; + } Ok(()) } - fn build(&self) -> Self::Entity { - let mut inner = Vec::with_capacity(self.expected_length()); - self.write(&mut inner) - .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); - H256::new_unchecked(inner.into()) + fn build(&self) -> Self::Entity { + let mut inner = Vec::with_capacity(self.expected_length()); + self.write(&mut inner) + .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); + Uint32Vec::new_unchecked(inner.into()) + } +} +pub struct Uint32VecIterator(Uint32Vec, usize, usize); +impl ::core::iter::Iterator for Uint32VecIterator { + type Item = Uint32; + + fn next(&mut self) -> Option { + if self.1 >= self.2 { + None + } else { + let ret = self.0.get_unchecked(self.1); + self.1 += 1; + Some(ret) + } + } +} +impl ::core::iter::ExactSizeIterator for Uint32VecIterator { + fn len(&self) -> usize { + self.2 - self.1 + } +} +impl ::core::iter::IntoIterator for Uint32Vec { + type IntoIter = Uint32VecIterator; + type Item = Uint32; + + fn into_iter(self) -> Self::IntoIter { + let len = self.len(); + Uint32VecIterator(self, 0, len) + } +} +impl<'r> Uint32VecReader<'r> { + pub fn iter<'t>(&'t self) -> Uint32VecReaderIterator<'t, 'r> { + Uint32VecReaderIterator(&self, 0, self.len()) + } +} +pub struct Uint32VecReaderIterator<'t, 'r>(&'t Uint32VecReader<'r>, usize, usize); +impl<'t: 'r, 'r> ::core::iter::Iterator for Uint32VecReaderIterator<'t, 'r> { + type Item = Uint32Reader<'t>; + + fn next(&mut self) -> Option { + if self.1 >= self.2 { + None + } else { + let ret = self.0.get_unchecked(self.1); + self.1 += 1; + Some(ret) + } + } +} +impl<'t: 'r, 'r> ::core::iter::ExactSizeIterator for Uint32VecReaderIterator<'t, 'r> { + fn len(&self) -> usize { + self.2 - self.1 } } #[derive(Clone)] -pub struct BytesVec(molecule::bytes::Bytes); -impl ::core::fmt::LowerHex for BytesVec { +pub struct H256Vec(molecule::bytes::Bytes); +impl ::core::fmt::LowerHex for H256Vec { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { use molecule::hex_string; if f.alternate() { @@ -714,12 +3373,12 @@ impl ::core::fmt::LowerHex for BytesVec { write!(f, "{}", hex_string(self.as_slice())) } } -impl ::core::fmt::Debug for BytesVec { +impl ::core::fmt::Debug for H256Vec { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { write!(f, "{}({:#x})", Self::NAME, self) } } -impl ::core::fmt::Display for BytesVec { +impl ::core::fmt::Display for H256Vec { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { write!(f, "{} [", Self::NAME)?; for i in 0..self.len() { @@ -732,23 +3391,21 @@ impl ::core::fmt::Display for BytesVec { write!(f, "]") } } -impl ::core::default::Default for BytesVec { +impl ::core::default::Default for H256Vec { fn default() -> Self { - let v: Vec = vec![4, 0, 0, 0]; - BytesVec::new_unchecked(v.into()) + let v: Vec = vec![0, 0, 0, 0]; + H256Vec::new_unchecked(v.into()) } } -impl BytesVec { +impl H256Vec { + pub const ITEM_SIZE: usize = 32; + pub fn total_size(&self) -> usize { - molecule::unpack_number(self.as_slice()) as usize + molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count() } pub fn item_count(&self) -> usize { - if self.total_size() == molecule::NUMBER_SIZE { - 0 - } else { - (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1 - } + molecule::unpack_number(self.as_slice()) as usize } pub fn len(&self) -> usize { @@ -759,7 +3416,7 @@ impl BytesVec { self.len() == 0 } - pub fn get(&self, idx: usize) -> Option { + pub fn get(&self, idx: usize) -> Option { if idx >= self.len() { None } else { @@ -767,30 +3424,23 @@ impl BytesVec { } } - pub fn get_unchecked(&self, idx: usize) -> Bytes { - let slice = self.as_slice(); - let start_idx = molecule::NUMBER_SIZE * (1 + idx); - let start = molecule::unpack_number(&slice[start_idx..]) as usize; - if idx == self.len() - 1 { - Bytes::new_unchecked(self.0.slice(start..)) - } else { - let end_idx = start_idx + molecule::NUMBER_SIZE; - let end = molecule::unpack_number(&slice[end_idx..]) as usize; - Bytes::new_unchecked(self.0.slice(start..end)) - } + pub fn get_unchecked(&self, idx: usize) -> H256 { + let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx; + let end = start + Self::ITEM_SIZE; + H256::new_unchecked(self.0.slice(start..end)) } - pub fn as_reader<'r>(&'r self) -> BytesVecReader<'r> { - BytesVecReader::new_unchecked(self.as_slice()) + pub fn as_reader<'r>(&'r self) -> H256VecReader<'r> { + H256VecReader::new_unchecked(self.as_slice()) } } -impl molecule::prelude::Entity for BytesVec { - type Builder = BytesVecBuilder; +impl molecule::prelude::Entity for H256Vec { + type Builder = H256VecBuilder; - const NAME: &'static str = "BytesVec"; + const NAME: &'static str = "H256Vec"; fn new_unchecked(data: molecule::bytes::Bytes) -> Self { - BytesVec(data) + H256Vec(data) } fn as_bytes(&self) -> molecule::bytes::Bytes { @@ -802,11 +3452,11 @@ impl molecule::prelude::Entity for BytesVec { } fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { - BytesVecReader::from_slice(slice).map(|reader| reader.to_entity()) + H256VecReader::from_slice(slice).map(|reader| reader.to_entity()) } fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { - BytesVecReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) + H256VecReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) } fn new_builder() -> Self::Builder { @@ -818,8 +3468,8 @@ impl molecule::prelude::Entity for BytesVec { } } #[derive(Clone, Copy)] -pub struct BytesVecReader<'r>(&'r [u8]); -impl<'r> ::core::fmt::LowerHex for BytesVecReader<'r> { +pub struct H256VecReader<'r>(&'r [u8]); +impl<'r> ::core::fmt::LowerHex for H256VecReader<'r> { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { use molecule::hex_string; if f.alternate() { @@ -828,12 +3478,12 @@ impl<'r> ::core::fmt::LowerHex for BytesVecReader<'r> { write!(f, "{}", hex_string(self.as_slice())) } } -impl<'r> ::core::fmt::Debug for BytesVecReader<'r> { +impl<'r> ::core::fmt::Debug for H256VecReader<'r> { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { write!(f, "{}({:#x})", Self::NAME, self) } } -impl<'r> ::core::fmt::Display for BytesVecReader<'r> { +impl<'r> ::core::fmt::Display for H256VecReader<'r> { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { write!(f, "{} [", Self::NAME)?; for i in 0..self.len() { @@ -846,17 +3496,15 @@ impl<'r> ::core::fmt::Display for BytesVecReader<'r> { write!(f, "]") } } -impl<'r> BytesVecReader<'r> { +impl<'r> H256VecReader<'r> { + pub const ITEM_SIZE: usize = 32; + pub fn total_size(&self) -> usize { - molecule::unpack_number(self.as_slice()) as usize + molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count() } pub fn item_count(&self) -> usize { - if self.total_size() == molecule::NUMBER_SIZE { - 0 - } else { - (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1 - } + molecule::unpack_number(self.as_slice()) as usize } pub fn len(&self) -> usize { @@ -867,7 +3515,7 @@ impl<'r> BytesVecReader<'r> { self.len() == 0 } - pub fn get(&self, idx: usize) -> Option> { + pub fn get(&self, idx: usize) -> Option> { if idx >= self.len() { None } else { @@ -875,144 +3523,90 @@ impl<'r> BytesVecReader<'r> { } } - pub fn get_unchecked(&self, idx: usize) -> BytesReader<'r> { - let slice = self.as_slice(); - let start_idx = molecule::NUMBER_SIZE * (1 + idx); - let start = molecule::unpack_number(&slice[start_idx..]) as usize; - if idx == self.len() - 1 { - BytesReader::new_unchecked(&self.as_slice()[start..]) - } else { - let end_idx = start_idx + molecule::NUMBER_SIZE; - let end = molecule::unpack_number(&slice[end_idx..]) as usize; - BytesReader::new_unchecked(&self.as_slice()[start..end]) - } + pub fn get_unchecked(&self, idx: usize) -> H256Reader<'r> { + let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx; + let end = start + Self::ITEM_SIZE; + H256Reader::new_unchecked(&self.as_slice()[start..end]) } } -impl<'r> molecule::prelude::Reader<'r> for BytesVecReader<'r> { - type Entity = BytesVec; +impl<'r> molecule::prelude::Reader<'r> for H256VecReader<'r> { + type Entity = H256Vec; - const NAME: &'static str = "BytesVecReader"; + const NAME: &'static str = "H256VecReader"; fn to_entity(&self) -> Self::Entity { Self::Entity::new_unchecked(self.as_slice().to_owned().into()) } fn new_unchecked(slice: &'r [u8]) -> Self { - BytesVecReader(slice) + H256VecReader(slice) } fn as_slice(&self) -> &'r [u8] { self.0 } - fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> { + fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> { use molecule::verification_error as ve; let slice_len = slice.len(); if slice_len < molecule::NUMBER_SIZE { return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len); } - let total_size = molecule::unpack_number(slice) as usize; - if slice_len != total_size { - return ve!(Self, TotalSizeNotMatch, total_size, slice_len); - } - if slice_len == molecule::NUMBER_SIZE { + let item_count = molecule::unpack_number(slice) as usize; + if item_count == 0 { + if slice_len != molecule::NUMBER_SIZE { + return ve!(Self, TotalSizeNotMatch, molecule::NUMBER_SIZE, slice_len); + } return Ok(()); } - if slice_len < molecule::NUMBER_SIZE * 2 { - return ve!( - Self, - TotalSizeNotMatch, - molecule::NUMBER_SIZE * 2, - slice_len - ); - } - let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize; - if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 { - return ve!(Self, OffsetsNotMatch); - } - if slice_len < offset_first { - return ve!(Self, HeaderIsBroken, offset_first, slice_len); - } - let mut offsets: Vec = slice[molecule::NUMBER_SIZE..offset_first] - .chunks_exact(molecule::NUMBER_SIZE) - .map(|x| molecule::unpack_number(x) as usize) - .collect(); - offsets.push(total_size); - if offsets.windows(2).any(|i| i[0] > i[1]) { - return ve!(Self, OffsetsNotMatch); - } - for pair in offsets.windows(2) { - let start = pair[0]; - let end = pair[1]; - BytesReader::verify(&slice[start..end], compatible)?; + let total_size = molecule::NUMBER_SIZE + Self::ITEM_SIZE * item_count; + if slice_len != total_size { + return ve!(Self, TotalSizeNotMatch, total_size, slice_len); } Ok(()) } } #[derive(Debug, Default)] -pub struct BytesVecBuilder(pub(crate) Vec); -impl BytesVecBuilder { - pub fn set(mut self, v: Vec) -> Self { +pub struct H256VecBuilder(pub(crate) Vec); +impl H256VecBuilder { + pub const ITEM_SIZE: usize = 32; + + pub fn set(mut self, v: Vec) -> Self { self.0 = v; self } - pub fn push(mut self, v: Bytes) -> Self { + pub fn push(mut self, v: H256) -> Self { self.0.push(v); self } - pub fn extend>(mut self, iter: T) -> Self { + pub fn extend>(mut self, iter: T) -> Self { for elem in iter { self.0.push(elem); } self } - pub fn replace(&mut self, index: usize, v: Bytes) -> Option { + pub fn replace(&mut self, index: usize, v: H256) -> Option { self.0 .get_mut(index) .map(|item| ::core::mem::replace(item, v)) } } -impl molecule::prelude::Builder for BytesVecBuilder { - type Entity = BytesVec; +impl molecule::prelude::Builder for H256VecBuilder { + type Entity = H256Vec; - const NAME: &'static str = "BytesVecBuilder"; + const NAME: &'static str = "H256VecBuilder"; fn expected_length(&self) -> usize { - molecule::NUMBER_SIZE * (self.0.len() + 1) - + self - .0 - .iter() - .map(|inner| inner.as_slice().len()) - .sum::() + molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.0.len() } fn write(&self, writer: &mut W) -> molecule::io::Result<()> { - let item_count = self.0.len(); - if item_count == 0 { - writer.write_all(&molecule::pack_number( - molecule::NUMBER_SIZE as molecule::Number, - ))?; - } else { - let (total_size, offsets) = self.0.iter().fold( - ( - molecule::NUMBER_SIZE * (item_count + 1), - Vec::with_capacity(item_count), - ), - |(start, mut offsets), inner| { - offsets.push(start); - (start + inner.as_slice().len(), offsets) - }, - ); - writer.write_all(&molecule::pack_number(total_size as molecule::Number))?; - for offset in offsets.into_iter() { - writer.write_all(&molecule::pack_number(offset as molecule::Number))?; - } - for inner in self.0.iter() { - writer.write_all(inner.as_slice())?; - } + writer.write_all(&molecule::pack_number(self.0.len() as molecule::Number))?; + for inner in &self.0[..] { + writer.write_all(inner.as_slice())?; } Ok(()) } @@ -1021,12 +3615,12 @@ impl molecule::prelude::Builder for BytesVecBuilder { let mut inner = Vec::with_capacity(self.expected_length()); self.write(&mut inner) .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); - BytesVec::new_unchecked(inner.into()) + H256Vec::new_unchecked(inner.into()) } } -pub struct BytesVecIterator(BytesVec, usize, usize); -impl ::core::iter::Iterator for BytesVecIterator { - type Item = Bytes; +pub struct H256VecIterator(H256Vec, usize, usize); +impl ::core::iter::Iterator for H256VecIterator { + type Item = H256; fn next(&mut self) -> Option { if self.1 >= self.2 { @@ -1038,28 +3632,28 @@ impl ::core::iter::Iterator for BytesVecIterator { } } } -impl ::core::iter::ExactSizeIterator for BytesVecIterator { +impl ::core::iter::ExactSizeIterator for H256VecIterator { fn len(&self) -> usize { self.2 - self.1 } } -impl ::core::iter::IntoIterator for BytesVec { - type IntoIter = BytesVecIterator; - type Item = Bytes; +impl ::core::iter::IntoIterator for H256Vec { + type IntoIter = H256VecIterator; + type Item = H256; fn into_iter(self) -> Self::IntoIter { let len = self.len(); - BytesVecIterator(self, 0, len) + H256VecIterator(self, 0, len) } } -impl<'r> BytesVecReader<'r> { - pub fn iter<'t>(&'t self) -> BytesVecReaderIterator<'t, 'r> { - BytesVecReaderIterator(&self, 0, self.len()) +impl<'r> H256VecReader<'r> { + pub fn iter<'t>(&'t self) -> H256VecReaderIterator<'t, 'r> { + H256VecReaderIterator(&self, 0, self.len()) } } -pub struct BytesVecReaderIterator<'t, 'r>(&'t BytesVecReader<'r>, usize, usize); -impl<'t: 'r, 'r> ::core::iter::Iterator for BytesVecReaderIterator<'t, 'r> { - type Item = BytesReader<'t>; +pub struct H256VecReaderIterator<'t, 'r>(&'t H256VecReader<'r>, usize, usize); +impl<'t: 'r, 'r> ::core::iter::Iterator for H256VecReaderIterator<'t, 'r> { + type Item = H256Reader<'t>; fn next(&mut self) -> Option { if self.1 >= self.2 { @@ -1071,14 +3665,14 @@ impl<'t: 'r, 'r> ::core::iter::Iterator for BytesVecReaderIterator<'t, 'r> { } } } -impl<'t: 'r, 'r> ::core::iter::ExactSizeIterator for BytesVecReaderIterator<'t, 'r> { +impl<'t: 'r, 'r> ::core::iter::ExactSizeIterator for H256VecReaderIterator<'t, 'r> { fn len(&self) -> usize { self.2 - self.1 } } #[derive(Clone)] -pub struct Byte32Vec(molecule::bytes::Bytes); -impl ::core::fmt::LowerHex for Byte32Vec { +pub struct Bytes(molecule::bytes::Bytes); +impl ::core::fmt::LowerHex for Bytes { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { use molecule::hex_string; if f.alternate() { @@ -1087,32 +3681,26 @@ impl ::core::fmt::LowerHex for Byte32Vec { write!(f, "{}", hex_string(self.as_slice())) } } -impl ::core::fmt::Debug for Byte32Vec { +impl ::core::fmt::Debug for Bytes { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { write!(f, "{}({:#x})", Self::NAME, self) } } -impl ::core::fmt::Display for Byte32Vec { +impl ::core::fmt::Display for Bytes { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{} [", Self::NAME)?; - for i in 0..self.len() { - if i == 0 { - write!(f, "{}", self.get_unchecked(i))?; - } else { - write!(f, ", {}", self.get_unchecked(i))?; - } - } - write!(f, "]") + use molecule::hex_string; + let raw_data = hex_string(&self.raw_data()); + write!(f, "{}(0x{})", Self::NAME, raw_data) } } -impl ::core::default::Default for Byte32Vec { +impl ::core::default::Default for Bytes { fn default() -> Self { let v: Vec = vec![0, 0, 0, 0]; - Byte32Vec::new_unchecked(v.into()) + Bytes::new_unchecked(v.into()) } } -impl Byte32Vec { - pub const ITEM_SIZE: usize = 32; +impl Bytes { + pub const ITEM_SIZE: usize = 1; pub fn total_size(&self) -> usize { molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count() @@ -1130,7 +3718,7 @@ impl Byte32Vec { self.len() == 0 } - pub fn get(&self, idx: usize) -> Option { + pub fn get(&self, idx: usize) -> Option { if idx >= self.len() { None } else { @@ -1138,23 +3726,27 @@ impl Byte32Vec { } } - pub fn get_unchecked(&self, idx: usize) -> Byte32 { + pub fn get_unchecked(&self, idx: usize) -> Byte { let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx; let end = start + Self::ITEM_SIZE; - Byte32::new_unchecked(self.0.slice(start..end)) + Byte::new_unchecked(self.0.slice(start..end)) } - pub fn as_reader<'r>(&'r self) -> Byte32VecReader<'r> { - Byte32VecReader::new_unchecked(self.as_slice()) + pub fn raw_data(&self) -> molecule::bytes::Bytes { + self.0.slice(molecule::NUMBER_SIZE..) + } + + pub fn as_reader<'r>(&'r self) -> BytesReader<'r> { + BytesReader::new_unchecked(self.as_slice()) } } -impl molecule::prelude::Entity for Byte32Vec { - type Builder = Byte32VecBuilder; +impl molecule::prelude::Entity for Bytes { + type Builder = BytesBuilder; - const NAME: &'static str = "Byte32Vec"; + const NAME: &'static str = "Bytes"; fn new_unchecked(data: molecule::bytes::Bytes) -> Self { - Byte32Vec(data) + Bytes(data) } fn as_bytes(&self) -> molecule::bytes::Bytes { @@ -1166,11 +3758,11 @@ impl molecule::prelude::Entity for Byte32Vec { } fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { - Byte32VecReader::from_slice(slice).map(|reader| reader.to_entity()) + BytesReader::from_slice(slice).map(|reader| reader.to_entity()) } fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { - Byte32VecReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) + BytesReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) } fn new_builder() -> Self::Builder { @@ -1182,8 +3774,8 @@ impl molecule::prelude::Entity for Byte32Vec { } } #[derive(Clone, Copy)] -pub struct Byte32VecReader<'r>(&'r [u8]); -impl<'r> ::core::fmt::LowerHex for Byte32VecReader<'r> { +pub struct BytesReader<'r>(&'r [u8]); +impl<'r> ::core::fmt::LowerHex for BytesReader<'r> { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { use molecule::hex_string; if f.alternate() { @@ -1192,26 +3784,20 @@ impl<'r> ::core::fmt::LowerHex for Byte32VecReader<'r> { write!(f, "{}", hex_string(self.as_slice())) } } -impl<'r> ::core::fmt::Debug for Byte32VecReader<'r> { +impl<'r> ::core::fmt::Debug for BytesReader<'r> { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { write!(f, "{}({:#x})", Self::NAME, self) } } -impl<'r> ::core::fmt::Display for Byte32VecReader<'r> { +impl<'r> ::core::fmt::Display for BytesReader<'r> { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{} [", Self::NAME)?; - for i in 0..self.len() { - if i == 0 { - write!(f, "{}", self.get_unchecked(i))?; - } else { - write!(f, ", {}", self.get_unchecked(i))?; - } - } - write!(f, "]") + use molecule::hex_string; + let raw_data = hex_string(&self.raw_data()); + write!(f, "{}(0x{})", Self::NAME, raw_data) } } -impl<'r> Byte32VecReader<'r> { - pub const ITEM_SIZE: usize = 32; +impl<'r> BytesReader<'r> { + pub const ITEM_SIZE: usize = 1; pub fn total_size(&self) -> usize { molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count() @@ -1229,7 +3815,7 @@ impl<'r> Byte32VecReader<'r> { self.len() == 0 } - pub fn get(&self, idx: usize) -> Option> { + pub fn get(&self, idx: usize) -> Option> { if idx >= self.len() { None } else { @@ -1237,23 +3823,27 @@ impl<'r> Byte32VecReader<'r> { } } - pub fn get_unchecked(&self, idx: usize) -> Byte32Reader<'r> { + pub fn get_unchecked(&self, idx: usize) -> ByteReader<'r> { let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx; let end = start + Self::ITEM_SIZE; - Byte32Reader::new_unchecked(&self.as_slice()[start..end]) + ByteReader::new_unchecked(&self.as_slice()[start..end]) + } + + pub fn raw_data(&self) -> &'r [u8] { + &self.as_slice()[molecule::NUMBER_SIZE..] } } -impl<'r> molecule::prelude::Reader<'r> for Byte32VecReader<'r> { - type Entity = Byte32Vec; +impl<'r> molecule::prelude::Reader<'r> for BytesReader<'r> { + type Entity = Bytes; - const NAME: &'static str = "Byte32VecReader"; + const NAME: &'static str = "BytesReader"; fn to_entity(&self) -> Self::Entity { Self::Entity::new_unchecked(self.as_slice().to_owned().into()) } fn new_unchecked(slice: &'r [u8]) -> Self { - Byte32VecReader(slice) + BytesReader(slice) } fn as_slice(&self) -> &'r [u8] { @@ -1281,37 +3871,37 @@ impl<'r> molecule::prelude::Reader<'r> for Byte32VecReader<'r> { } } #[derive(Debug, Default)] -pub struct Byte32VecBuilder(pub(crate) Vec); -impl Byte32VecBuilder { - pub const ITEM_SIZE: usize = 32; +pub struct BytesBuilder(pub(crate) Vec); +impl BytesBuilder { + pub const ITEM_SIZE: usize = 1; - pub fn set(mut self, v: Vec) -> Self { + pub fn set(mut self, v: Vec) -> Self { self.0 = v; self } - pub fn push(mut self, v: Byte32) -> Self { + pub fn push(mut self, v: Byte) -> Self { self.0.push(v); self } - pub fn extend>(mut self, iter: T) -> Self { + pub fn extend>(mut self, iter: T) -> Self { for elem in iter { self.0.push(elem); } self } - pub fn replace(&mut self, index: usize, v: Byte32) -> Option { + pub fn replace(&mut self, index: usize, v: Byte) -> Option { self.0 .get_mut(index) .map(|item| ::core::mem::replace(item, v)) } } -impl molecule::prelude::Builder for Byte32VecBuilder { - type Entity = Byte32Vec; +impl molecule::prelude::Builder for BytesBuilder { + type Entity = Bytes; - const NAME: &'static str = "Byte32VecBuilder"; + const NAME: &'static str = "BytesBuilder"; fn expected_length(&self) -> usize { molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.0.len() @@ -1329,12 +3919,12 @@ impl molecule::prelude::Builder for Byte32VecBuilder { let mut inner = Vec::with_capacity(self.expected_length()); self.write(&mut inner) .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); - Byte32Vec::new_unchecked(inner.into()) + Bytes::new_unchecked(inner.into()) } } -pub struct Byte32VecIterator(Byte32Vec, usize, usize); -impl ::core::iter::Iterator for Byte32VecIterator { - type Item = Byte32; +pub struct BytesIterator(Bytes, usize, usize); +impl ::core::iter::Iterator for BytesIterator { + type Item = Byte; fn next(&mut self) -> Option { if self.1 >= self.2 { @@ -1346,47 +3936,208 @@ impl ::core::iter::Iterator for Byte32VecIterator { } } } -impl ::core::iter::ExactSizeIterator for Byte32VecIterator { +impl ::core::iter::ExactSizeIterator for BytesIterator { fn len(&self) -> usize { self.2 - self.1 } } -impl ::core::iter::IntoIterator for Byte32Vec { - type IntoIter = Byte32VecIterator; - type Item = Byte32; +impl ::core::iter::IntoIterator for Bytes { + type IntoIter = BytesIterator; + type Item = Byte; fn into_iter(self) -> Self::IntoIter { let len = self.len(); - Byte32VecIterator(self, 0, len) + BytesIterator(self, 0, len) } } -impl<'r> Byte32VecReader<'r> { - pub fn iter<'t>(&'t self) -> Byte32VecReaderIterator<'t, 'r> { - Byte32VecReaderIterator(&self, 0, self.len()) +#[derive(Clone)] +pub struct BytesOpt(molecule::bytes::Bytes); +impl ::core::fmt::LowerHex for BytesOpt { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) } } -pub struct Byte32VecReaderIterator<'t, 'r>(&'t Byte32VecReader<'r>, usize, usize); -impl<'t: 'r, 'r> ::core::iter::Iterator for Byte32VecReaderIterator<'t, 'r> { - type Item = Byte32Reader<'t>; +impl ::core::fmt::Debug for BytesOpt { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl ::core::fmt::Display for BytesOpt { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + if let Some(v) = self.to_opt() { + write!(f, "{}(Some({}))", Self::NAME, v) + } else { + write!(f, "{}(None)", Self::NAME) + } + } +} +impl ::core::default::Default for BytesOpt { + fn default() -> Self { + let v: Vec = vec![]; + BytesOpt::new_unchecked(v.into()) + } +} +impl BytesOpt { + pub fn is_none(&self) -> bool { + self.0.is_empty() + } - fn next(&mut self) -> Option { - if self.1 >= self.2 { + pub fn is_some(&self) -> bool { + !self.0.is_empty() + } + + pub fn to_opt(&self) -> Option { + if self.is_none() { None } else { - let ret = self.0.get_unchecked(self.1); - self.1 += 1; - Some(ret) + Some(Bytes::new_unchecked(self.0.clone())) } } + + pub fn as_reader<'r>(&'r self) -> BytesOptReader<'r> { + BytesOptReader::new_unchecked(self.as_slice()) + } } -impl<'t: 'r, 'r> ::core::iter::ExactSizeIterator for Byte32VecReaderIterator<'t, 'r> { - fn len(&self) -> usize { - self.2 - self.1 +impl molecule::prelude::Entity for BytesOpt { + type Builder = BytesOptBuilder; + + const NAME: &'static str = "BytesOpt"; + + fn new_unchecked(data: molecule::bytes::Bytes) -> Self { + BytesOpt(data) + } + + fn as_bytes(&self) -> molecule::bytes::Bytes { + self.0.clone() + } + + fn as_slice(&self) -> &[u8] { + &self.0[..] + } + + fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { + BytesOptReader::from_slice(slice).map(|reader| reader.to_entity()) + } + + fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { + BytesOptReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) + } + + fn new_builder() -> Self::Builder { + ::core::default::Default::default() + } + + fn as_builder(self) -> Self::Builder { + Self::new_builder().set(self.to_opt()) + } +} +#[derive(Clone, Copy)] +pub struct BytesOptReader<'r>(&'r [u8]); +impl<'r> ::core::fmt::LowerHex for BytesOptReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl<'r> ::core::fmt::Debug for BytesOptReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl<'r> ::core::fmt::Display for BytesOptReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + if let Some(v) = self.to_opt() { + write!(f, "{}(Some({}))", Self::NAME, v) + } else { + write!(f, "{}(None)", Self::NAME) + } + } +} +impl<'r> BytesOptReader<'r> { + pub fn is_none(&self) -> bool { + self.0.is_empty() + } + + pub fn is_some(&self) -> bool { + !self.0.is_empty() + } + + pub fn to_opt(&self) -> Option> { + if self.is_none() { + None + } else { + Some(BytesReader::new_unchecked(self.as_slice())) + } + } +} +impl<'r> molecule::prelude::Reader<'r> for BytesOptReader<'r> { + type Entity = BytesOpt; + + const NAME: &'static str = "BytesOptReader"; + + fn to_entity(&self) -> Self::Entity { + Self::Entity::new_unchecked(self.as_slice().to_owned().into()) + } + + fn new_unchecked(slice: &'r [u8]) -> Self { + BytesOptReader(slice) + } + + fn as_slice(&self) -> &'r [u8] { + self.0 + } + + fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> { + if !slice.is_empty() { + BytesReader::verify(&slice[..], compatible)?; + } + Ok(()) + } +} +#[derive(Debug, Default)] +pub struct BytesOptBuilder(pub(crate) Option); +impl BytesOptBuilder { + pub fn set(mut self, v: Option) -> Self { + self.0 = v; + self + } +} +impl molecule::prelude::Builder for BytesOptBuilder { + type Entity = BytesOpt; + + const NAME: &'static str = "BytesOptBuilder"; + + fn expected_length(&self) -> usize { + self.0 + .as_ref() + .map(|ref inner| inner.as_slice().len()) + .unwrap_or(0) + } + + fn write(&self, writer: &mut W) -> molecule::io::Result<()> { + self.0 + .as_ref() + .map(|ref inner| writer.write_all(inner.as_slice())) + .unwrap_or(Ok(())) + } + + fn build(&self) -> Self::Entity { + let mut inner = Vec::with_capacity(self.expected_length()); + self.write(&mut inner) + .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); + BytesOpt::new_unchecked(inner.into()) } } #[derive(Clone)] -pub struct Uint32Vec(molecule::bytes::Bytes); -impl ::core::fmt::LowerHex for Uint32Vec { +pub struct BytesVec(molecule::bytes::Bytes); +impl ::core::fmt::LowerHex for BytesVec { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { use molecule::hex_string; if f.alternate() { @@ -1395,12 +4146,12 @@ impl ::core::fmt::LowerHex for Uint32Vec { write!(f, "{}", hex_string(self.as_slice())) } } -impl ::core::fmt::Debug for Uint32Vec { +impl ::core::fmt::Debug for BytesVec { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { write!(f, "{}({:#x})", Self::NAME, self) } } -impl ::core::fmt::Display for Uint32Vec { +impl ::core::fmt::Display for BytesVec { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { write!(f, "{} [", Self::NAME)?; for i in 0..self.len() { @@ -1413,21 +4164,23 @@ impl ::core::fmt::Display for Uint32Vec { write!(f, "]") } } -impl ::core::default::Default for Uint32Vec { +impl ::core::default::Default for BytesVec { fn default() -> Self { - let v: Vec = vec![0, 0, 0, 0]; - Uint32Vec::new_unchecked(v.into()) + let v: Vec = vec![4, 0, 0, 0]; + BytesVec::new_unchecked(v.into()) } } -impl Uint32Vec { - pub const ITEM_SIZE: usize = 4; - +impl BytesVec { pub fn total_size(&self) -> usize { - molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count() + molecule::unpack_number(self.as_slice()) as usize } pub fn item_count(&self) -> usize { - molecule::unpack_number(self.as_slice()) as usize + if self.total_size() == molecule::NUMBER_SIZE { + 0 + } else { + (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1 + } } pub fn len(&self) -> usize { @@ -1438,7 +4191,7 @@ impl Uint32Vec { self.len() == 0 } - pub fn get(&self, idx: usize) -> Option { + pub fn get(&self, idx: usize) -> Option { if idx >= self.len() { None } else { @@ -1446,23 +4199,30 @@ impl Uint32Vec { } } - pub fn get_unchecked(&self, idx: usize) -> Uint32 { - let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx; - let end = start + Self::ITEM_SIZE; - Uint32::new_unchecked(self.0.slice(start..end)) + pub fn get_unchecked(&self, idx: usize) -> Bytes { + let slice = self.as_slice(); + let start_idx = molecule::NUMBER_SIZE * (1 + idx); + let start = molecule::unpack_number(&slice[start_idx..]) as usize; + if idx == self.len() - 1 { + Bytes::new_unchecked(self.0.slice(start..)) + } else { + let end_idx = start_idx + molecule::NUMBER_SIZE; + let end = molecule::unpack_number(&slice[end_idx..]) as usize; + Bytes::new_unchecked(self.0.slice(start..end)) + } } - pub fn as_reader<'r>(&'r self) -> Uint32VecReader<'r> { - Uint32VecReader::new_unchecked(self.as_slice()) + pub fn as_reader<'r>(&'r self) -> BytesVecReader<'r> { + BytesVecReader::new_unchecked(self.as_slice()) } } -impl molecule::prelude::Entity for Uint32Vec { - type Builder = Uint32VecBuilder; +impl molecule::prelude::Entity for BytesVec { + type Builder = BytesVecBuilder; - const NAME: &'static str = "Uint32Vec"; + const NAME: &'static str = "BytesVec"; fn new_unchecked(data: molecule::bytes::Bytes) -> Self { - Uint32Vec(data) + BytesVec(data) } fn as_bytes(&self) -> molecule::bytes::Bytes { @@ -1474,11 +4234,11 @@ impl molecule::prelude::Entity for Uint32Vec { } fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { - Uint32VecReader::from_slice(slice).map(|reader| reader.to_entity()) + BytesVecReader::from_slice(slice).map(|reader| reader.to_entity()) } fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { - Uint32VecReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) + BytesVecReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) } fn new_builder() -> Self::Builder { @@ -1490,8 +4250,8 @@ impl molecule::prelude::Entity for Uint32Vec { } } #[derive(Clone, Copy)] -pub struct Uint32VecReader<'r>(&'r [u8]); -impl<'r> ::core::fmt::LowerHex for Uint32VecReader<'r> { +pub struct BytesVecReader<'r>(&'r [u8]); +impl<'r> ::core::fmt::LowerHex for BytesVecReader<'r> { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { use molecule::hex_string; if f.alternate() { @@ -1500,12 +4260,12 @@ impl<'r> ::core::fmt::LowerHex for Uint32VecReader<'r> { write!(f, "{}", hex_string(self.as_slice())) } } -impl<'r> ::core::fmt::Debug for Uint32VecReader<'r> { +impl<'r> ::core::fmt::Debug for BytesVecReader<'r> { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { write!(f, "{}({:#x})", Self::NAME, self) } } -impl<'r> ::core::fmt::Display for Uint32VecReader<'r> { +impl<'r> ::core::fmt::Display for BytesVecReader<'r> { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { write!(f, "{} [", Self::NAME)?; for i in 0..self.len() { @@ -1518,15 +4278,17 @@ impl<'r> ::core::fmt::Display for Uint32VecReader<'r> { write!(f, "]") } } -impl<'r> Uint32VecReader<'r> { - pub const ITEM_SIZE: usize = 4; - +impl<'r> BytesVecReader<'r> { pub fn total_size(&self) -> usize { - molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count() + molecule::unpack_number(self.as_slice()) as usize } pub fn item_count(&self) -> usize { - molecule::unpack_number(self.as_slice()) as usize + if self.total_size() == molecule::NUMBER_SIZE { + 0 + } else { + (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1 + } } pub fn len(&self) -> usize { @@ -1537,7 +4299,7 @@ impl<'r> Uint32VecReader<'r> { self.len() == 0 } - pub fn get(&self, idx: usize) -> Option> { + pub fn get(&self, idx: usize) -> Option> { if idx >= self.len() { None } else { @@ -1545,90 +4307,144 @@ impl<'r> Uint32VecReader<'r> { } } - pub fn get_unchecked(&self, idx: usize) -> Uint32Reader<'r> { - let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx; - let end = start + Self::ITEM_SIZE; - Uint32Reader::new_unchecked(&self.as_slice()[start..end]) + pub fn get_unchecked(&self, idx: usize) -> BytesReader<'r> { + let slice = self.as_slice(); + let start_idx = molecule::NUMBER_SIZE * (1 + idx); + let start = molecule::unpack_number(&slice[start_idx..]) as usize; + if idx == self.len() - 1 { + BytesReader::new_unchecked(&self.as_slice()[start..]) + } else { + let end_idx = start_idx + molecule::NUMBER_SIZE; + let end = molecule::unpack_number(&slice[end_idx..]) as usize; + BytesReader::new_unchecked(&self.as_slice()[start..end]) + } } } -impl<'r> molecule::prelude::Reader<'r> for Uint32VecReader<'r> { - type Entity = Uint32Vec; +impl<'r> molecule::prelude::Reader<'r> for BytesVecReader<'r> { + type Entity = BytesVec; - const NAME: &'static str = "Uint32VecReader"; + const NAME: &'static str = "BytesVecReader"; fn to_entity(&self) -> Self::Entity { Self::Entity::new_unchecked(self.as_slice().to_owned().into()) } fn new_unchecked(slice: &'r [u8]) -> Self { - Uint32VecReader(slice) + BytesVecReader(slice) } fn as_slice(&self) -> &'r [u8] { self.0 } - fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> { + fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> { use molecule::verification_error as ve; let slice_len = slice.len(); if slice_len < molecule::NUMBER_SIZE { return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len); } - let item_count = molecule::unpack_number(slice) as usize; - if item_count == 0 { - if slice_len != molecule::NUMBER_SIZE { - return ve!(Self, TotalSizeNotMatch, molecule::NUMBER_SIZE, slice_len); - } - return Ok(()); - } - let total_size = molecule::NUMBER_SIZE + Self::ITEM_SIZE * item_count; + let total_size = molecule::unpack_number(slice) as usize; if slice_len != total_size { return ve!(Self, TotalSizeNotMatch, total_size, slice_len); } + if slice_len == molecule::NUMBER_SIZE { + return Ok(()); + } + if slice_len < molecule::NUMBER_SIZE * 2 { + return ve!( + Self, + TotalSizeNotMatch, + molecule::NUMBER_SIZE * 2, + slice_len + ); + } + let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize; + if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 { + return ve!(Self, OffsetsNotMatch); + } + if slice_len < offset_first { + return ve!(Self, HeaderIsBroken, offset_first, slice_len); + } + let mut offsets: Vec = slice[molecule::NUMBER_SIZE..offset_first] + .chunks_exact(molecule::NUMBER_SIZE) + .map(|x| molecule::unpack_number(x) as usize) + .collect(); + offsets.push(total_size); + if offsets.windows(2).any(|i| i[0] > i[1]) { + return ve!(Self, OffsetsNotMatch); + } + for pair in offsets.windows(2) { + let start = pair[0]; + let end = pair[1]; + BytesReader::verify(&slice[start..end], compatible)?; + } Ok(()) } } #[derive(Debug, Default)] -pub struct Uint32VecBuilder(pub(crate) Vec); -impl Uint32VecBuilder { - pub const ITEM_SIZE: usize = 4; - - pub fn set(mut self, v: Vec) -> Self { +pub struct BytesVecBuilder(pub(crate) Vec); +impl BytesVecBuilder { + pub fn set(mut self, v: Vec) -> Self { self.0 = v; self } - pub fn push(mut self, v: Uint32) -> Self { + pub fn push(mut self, v: Bytes) -> Self { self.0.push(v); self } - pub fn extend>(mut self, iter: T) -> Self { + pub fn extend>(mut self, iter: T) -> Self { for elem in iter { self.0.push(elem); } self } - pub fn replace(&mut self, index: usize, v: Uint32) -> Option { + pub fn replace(&mut self, index: usize, v: Bytes) -> Option { self.0 .get_mut(index) .map(|item| ::core::mem::replace(item, v)) } } -impl molecule::prelude::Builder for Uint32VecBuilder { - type Entity = Uint32Vec; +impl molecule::prelude::Builder for BytesVecBuilder { + type Entity = BytesVec; - const NAME: &'static str = "Uint32VecBuilder"; + const NAME: &'static str = "BytesVecBuilder"; fn expected_length(&self) -> usize { - molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.0.len() + molecule::NUMBER_SIZE * (self.0.len() + 1) + + self + .0 + .iter() + .map(|inner| inner.as_slice().len()) + .sum::() } fn write(&self, writer: &mut W) -> molecule::io::Result<()> { - writer.write_all(&molecule::pack_number(self.0.len() as molecule::Number))?; - for inner in &self.0[..] { - writer.write_all(inner.as_slice())?; + let item_count = self.0.len(); + if item_count == 0 { + writer.write_all(&molecule::pack_number( + molecule::NUMBER_SIZE as molecule::Number, + ))?; + } else { + let (total_size, offsets) = self.0.iter().fold( + ( + molecule::NUMBER_SIZE * (item_count + 1), + Vec::with_capacity(item_count), + ), + |(start, mut offsets), inner| { + offsets.push(start); + (start + inner.as_slice().len(), offsets) + }, + ); + writer.write_all(&molecule::pack_number(total_size as molecule::Number))?; + for offset in offsets.into_iter() { + writer.write_all(&molecule::pack_number(offset as molecule::Number))?; + } + for inner in self.0.iter() { + writer.write_all(inner.as_slice())?; + } } Ok(()) } @@ -1637,12 +4453,12 @@ impl molecule::prelude::Builder for Uint32VecBuilder { let mut inner = Vec::with_capacity(self.expected_length()); self.write(&mut inner) .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); - Uint32Vec::new_unchecked(inner.into()) + BytesVec::new_unchecked(inner.into()) } } -pub struct Uint32VecIterator(Uint32Vec, usize, usize); -impl ::core::iter::Iterator for Uint32VecIterator { - type Item = Uint32; +pub struct BytesVecIterator(BytesVec, usize, usize); +impl ::core::iter::Iterator for BytesVecIterator { + type Item = Bytes; fn next(&mut self) -> Option { if self.1 >= self.2 { @@ -1654,28 +4470,28 @@ impl ::core::iter::Iterator for Uint32VecIterator { } } } -impl ::core::iter::ExactSizeIterator for Uint32VecIterator { +impl ::core::iter::ExactSizeIterator for BytesVecIterator { fn len(&self) -> usize { self.2 - self.1 } } -impl ::core::iter::IntoIterator for Uint32Vec { - type IntoIter = Uint32VecIterator; - type Item = Uint32; +impl ::core::iter::IntoIterator for BytesVec { + type IntoIter = BytesVecIterator; + type Item = Bytes; fn into_iter(self) -> Self::IntoIter { let len = self.len(); - Uint32VecIterator(self, 0, len) + BytesVecIterator(self, 0, len) } } -impl<'r> Uint32VecReader<'r> { - pub fn iter<'t>(&'t self) -> Uint32VecReaderIterator<'t, 'r> { - Uint32VecReaderIterator(&self, 0, self.len()) +impl<'r> BytesVecReader<'r> { + pub fn iter<'t>(&'t self) -> BytesVecReaderIterator<'t, 'r> { + BytesVecReaderIterator(&self, 0, self.len()) } } -pub struct Uint32VecReaderIterator<'t, 'r>(&'t Uint32VecReader<'r>, usize, usize); -impl<'t: 'r, 'r> ::core::iter::Iterator for Uint32VecReaderIterator<'t, 'r> { - type Item = Uint32Reader<'t>; +pub struct BytesVecReaderIterator<'t, 'r>(&'t BytesVecReader<'r>, usize, usize); +impl<'t: 'r, 'r> ::core::iter::Iterator for BytesVecReaderIterator<'t, 'r> { + type Item = BytesReader<'t>; fn next(&mut self) -> Option { if self.1 >= self.2 { @@ -1687,14 +4503,14 @@ impl<'t: 'r, 'r> ::core::iter::Iterator for Uint32VecReaderIterator<'t, 'r> { } } } -impl<'t: 'r, 'r> ::core::iter::ExactSizeIterator for Uint32VecReaderIterator<'t, 'r> { +impl<'t: 'r, 'r> ::core::iter::ExactSizeIterator for BytesVecReaderIterator<'t, 'r> { fn len(&self) -> usize { self.2 - self.1 } } #[derive(Clone)] -pub struct H256Vec(molecule::bytes::Bytes); -impl ::core::fmt::LowerHex for H256Vec { +pub struct Byte32Vec(molecule::bytes::Bytes); +impl ::core::fmt::LowerHex for Byte32Vec { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { use molecule::hex_string; if f.alternate() { @@ -1703,12 +4519,12 @@ impl ::core::fmt::LowerHex for H256Vec { write!(f, "{}", hex_string(self.as_slice())) } } -impl ::core::fmt::Debug for H256Vec { +impl ::core::fmt::Debug for Byte32Vec { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { write!(f, "{}({:#x})", Self::NAME, self) } } -impl ::core::fmt::Display for H256Vec { +impl ::core::fmt::Display for Byte32Vec { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { write!(f, "{} [", Self::NAME)?; for i in 0..self.len() { @@ -1721,13 +4537,13 @@ impl ::core::fmt::Display for H256Vec { write!(f, "]") } } -impl ::core::default::Default for H256Vec { +impl ::core::default::Default for Byte32Vec { fn default() -> Self { let v: Vec = vec![0, 0, 0, 0]; - H256Vec::new_unchecked(v.into()) + Byte32Vec::new_unchecked(v.into()) } } -impl H256Vec { +impl Byte32Vec { pub const ITEM_SIZE: usize = 32; pub fn total_size(&self) -> usize { @@ -1746,7 +4562,7 @@ impl H256Vec { self.len() == 0 } - pub fn get(&self, idx: usize) -> Option { + pub fn get(&self, idx: usize) -> Option { if idx >= self.len() { None } else { @@ -1754,23 +4570,23 @@ impl H256Vec { } } - pub fn get_unchecked(&self, idx: usize) -> H256 { + pub fn get_unchecked(&self, idx: usize) -> Byte32 { let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx; let end = start + Self::ITEM_SIZE; - H256::new_unchecked(self.0.slice(start..end)) + Byte32::new_unchecked(self.0.slice(start..end)) } - pub fn as_reader<'r>(&'r self) -> H256VecReader<'r> { - H256VecReader::new_unchecked(self.as_slice()) + pub fn as_reader<'r>(&'r self) -> Byte32VecReader<'r> { + Byte32VecReader::new_unchecked(self.as_slice()) } } -impl molecule::prelude::Entity for H256Vec { - type Builder = H256VecBuilder; +impl molecule::prelude::Entity for Byte32Vec { + type Builder = Byte32VecBuilder; - const NAME: &'static str = "H256Vec"; + const NAME: &'static str = "Byte32Vec"; fn new_unchecked(data: molecule::bytes::Bytes) -> Self { - H256Vec(data) + Byte32Vec(data) } fn as_bytes(&self) -> molecule::bytes::Bytes { @@ -1782,11 +4598,11 @@ impl molecule::prelude::Entity for H256Vec { } fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { - H256VecReader::from_slice(slice).map(|reader| reader.to_entity()) + Byte32VecReader::from_slice(slice).map(|reader| reader.to_entity()) } fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { - H256VecReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) + Byte32VecReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) } fn new_builder() -> Self::Builder { @@ -1798,8 +4614,8 @@ impl molecule::prelude::Entity for H256Vec { } } #[derive(Clone, Copy)] -pub struct H256VecReader<'r>(&'r [u8]); -impl<'r> ::core::fmt::LowerHex for H256VecReader<'r> { +pub struct Byte32VecReader<'r>(&'r [u8]); +impl<'r> ::core::fmt::LowerHex for Byte32VecReader<'r> { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { use molecule::hex_string; if f.alternate() { @@ -1808,12 +4624,12 @@ impl<'r> ::core::fmt::LowerHex for H256VecReader<'r> { write!(f, "{}", hex_string(self.as_slice())) } } -impl<'r> ::core::fmt::Debug for H256VecReader<'r> { +impl<'r> ::core::fmt::Debug for Byte32VecReader<'r> { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { write!(f, "{}({:#x})", Self::NAME, self) } } -impl<'r> ::core::fmt::Display for H256VecReader<'r> { +impl<'r> ::core::fmt::Display for Byte32VecReader<'r> { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { write!(f, "{} [", Self::NAME)?; for i in 0..self.len() { @@ -1826,7 +4642,7 @@ impl<'r> ::core::fmt::Display for H256VecReader<'r> { write!(f, "]") } } -impl<'r> H256VecReader<'r> { +impl<'r> Byte32VecReader<'r> { pub const ITEM_SIZE: usize = 32; pub fn total_size(&self) -> usize { @@ -1845,7 +4661,7 @@ impl<'r> H256VecReader<'r> { self.len() == 0 } - pub fn get(&self, idx: usize) -> Option> { + pub fn get(&self, idx: usize) -> Option> { if idx >= self.len() { None } else { @@ -1853,23 +4669,23 @@ impl<'r> H256VecReader<'r> { } } - pub fn get_unchecked(&self, idx: usize) -> H256Reader<'r> { + pub fn get_unchecked(&self, idx: usize) -> Byte32Reader<'r> { let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx; let end = start + Self::ITEM_SIZE; - H256Reader::new_unchecked(&self.as_slice()[start..end]) + Byte32Reader::new_unchecked(&self.as_slice()[start..end]) } } -impl<'r> molecule::prelude::Reader<'r> for H256VecReader<'r> { - type Entity = H256Vec; +impl<'r> molecule::prelude::Reader<'r> for Byte32VecReader<'r> { + type Entity = Byte32Vec; - const NAME: &'static str = "H256VecReader"; + const NAME: &'static str = "Byte32VecReader"; fn to_entity(&self) -> Self::Entity { Self::Entity::new_unchecked(self.as_slice().to_owned().into()) } fn new_unchecked(slice: &'r [u8]) -> Self { - H256VecReader(slice) + Byte32VecReader(slice) } fn as_slice(&self) -> &'r [u8] { @@ -1897,37 +4713,37 @@ impl<'r> molecule::prelude::Reader<'r> for H256VecReader<'r> { } } #[derive(Debug, Default)] -pub struct H256VecBuilder(pub(crate) Vec); -impl H256VecBuilder { +pub struct Byte32VecBuilder(pub(crate) Vec); +impl Byte32VecBuilder { pub const ITEM_SIZE: usize = 32; - pub fn set(mut self, v: Vec) -> Self { + pub fn set(mut self, v: Vec) -> Self { self.0 = v; self } - pub fn push(mut self, v: H256) -> Self { + pub fn push(mut self, v: Byte32) -> Self { self.0.push(v); self } - pub fn extend>(mut self, iter: T) -> Self { + pub fn extend>(mut self, iter: T) -> Self { for elem in iter { self.0.push(elem); } self } - pub fn replace(&mut self, index: usize, v: H256) -> Option { + pub fn replace(&mut self, index: usize, v: Byte32) -> Option { self.0 .get_mut(index) .map(|item| ::core::mem::replace(item, v)) } } -impl molecule::prelude::Builder for H256VecBuilder { - type Entity = H256Vec; +impl molecule::prelude::Builder for Byte32VecBuilder { + type Entity = Byte32Vec; - const NAME: &'static str = "H256VecBuilder"; + const NAME: &'static str = "Byte32VecBuilder"; fn expected_length(&self) -> usize { molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.0.len() @@ -1945,12 +4761,12 @@ impl molecule::prelude::Builder for H256VecBuilder { let mut inner = Vec::with_capacity(self.expected_length()); self.write(&mut inner) .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); - H256Vec::new_unchecked(inner.into()) + Byte32Vec::new_unchecked(inner.into()) } } -pub struct H256VecIterator(H256Vec, usize, usize); -impl ::core::iter::Iterator for H256VecIterator { - type Item = H256; +pub struct Byte32VecIterator(Byte32Vec, usize, usize); +impl ::core::iter::Iterator for Byte32VecIterator { + type Item = Byte32; fn next(&mut self) -> Option { if self.1 >= self.2 { @@ -1962,28 +4778,28 @@ impl ::core::iter::Iterator for H256VecIterator { } } } -impl ::core::iter::ExactSizeIterator for H256VecIterator { +impl ::core::iter::ExactSizeIterator for Byte32VecIterator { fn len(&self) -> usize { self.2 - self.1 } } -impl ::core::iter::IntoIterator for H256Vec { - type IntoIter = H256VecIterator; - type Item = H256; +impl ::core::iter::IntoIterator for Byte32Vec { + type IntoIter = Byte32VecIterator; + type Item = Byte32; fn into_iter(self) -> Self::IntoIter { let len = self.len(); - H256VecIterator(self, 0, len) + Byte32VecIterator(self, 0, len) } } -impl<'r> H256VecReader<'r> { - pub fn iter<'t>(&'t self) -> H256VecReaderIterator<'t, 'r> { - H256VecReaderIterator(&self, 0, self.len()) +impl<'r> Byte32VecReader<'r> { + pub fn iter<'t>(&'t self) -> Byte32VecReaderIterator<'t, 'r> { + Byte32VecReaderIterator(&self, 0, self.len()) } } -pub struct H256VecReaderIterator<'t, 'r>(&'t H256VecReader<'r>, usize, usize); -impl<'t: 'r, 'r> ::core::iter::Iterator for H256VecReaderIterator<'t, 'r> { - type Item = H256Reader<'t>; +pub struct Byte32VecReaderIterator<'t, 'r>(&'t Byte32VecReader<'r>, usize, usize); +impl<'t: 'r, 'r> ::core::iter::Iterator for Byte32VecReaderIterator<'t, 'r> { + type Item = Byte32Reader<'t>; fn next(&mut self) -> Option { if self.1 >= self.2 { @@ -1995,14 +4811,199 @@ impl<'t: 'r, 'r> ::core::iter::Iterator for H256VecReaderIterator<'t, 'r> { } } } -impl<'t: 'r, 'r> ::core::iter::ExactSizeIterator for H256VecReaderIterator<'t, 'r> { +impl<'t: 'r, 'r> ::core::iter::ExactSizeIterator for Byte32VecReaderIterator<'t, 'r> { fn len(&self) -> usize { self.2 - self.1 } } #[derive(Clone)] -pub struct ScriptOpt(molecule::bytes::Bytes); -impl ::core::fmt::LowerHex for ScriptOpt { +pub struct ScriptOpt(molecule::bytes::Bytes); +impl ::core::fmt::LowerHex for ScriptOpt { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl ::core::fmt::Debug for ScriptOpt { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl ::core::fmt::Display for ScriptOpt { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + if let Some(v) = self.to_opt() { + write!(f, "{}(Some({}))", Self::NAME, v) + } else { + write!(f, "{}(None)", Self::NAME) + } + } +} +impl ::core::default::Default for ScriptOpt { + fn default() -> Self { + let v: Vec = vec![]; + ScriptOpt::new_unchecked(v.into()) + } +} +impl ScriptOpt { + pub fn is_none(&self) -> bool { + self.0.is_empty() + } + + pub fn is_some(&self) -> bool { + !self.0.is_empty() + } + + pub fn to_opt(&self) -> Option