Skip to content

Commit

Permalink
Add base64 (binary64) encoding for getConfirmedTransaction/getConfirm…
Browse files Browse the repository at this point in the history
…edBlock
  • Loading branch information
mvines committed Aug 18, 2020
1 parent 3534145 commit b5f3ced
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 28 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1068,7 +1068,7 @@ fn process_confirm(
if let Some(transaction_status) = status {
if config.verbose {
match rpc_client
.get_confirmed_transaction(signature, UiTransactionEncoding::Binary)
.get_confirmed_transaction(signature, UiTransactionEncoding::Binary64)
{
Ok(confirmed_transaction) => {
println!(
Expand Down
30 changes: 15 additions & 15 deletions docs/src/apps/jsonrpc-api.md

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions ledger-tool/src/bigtable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ async fn upload(
for (i, slot) in blocks_to_upload.iter().enumerate() {
let _ = match blockstore.get_confirmed_block(
*slot,
Some(solana_transaction_status::UiTransactionEncoding::Binary),
Some(solana_transaction_status::UiTransactionEncoding::Binary64),
) {
Ok(confirmed_block) => sender.send((*slot, Some(confirmed_block))),
Err(err) => {
Expand Down Expand Up @@ -231,7 +231,7 @@ async fn block(slot: Slot) -> Result<(), Box<dyn std::error::Error>> {
.map_err(|err| format!("Failed to connect to storage: {:?}", err))?;

let block = bigtable
.get_confirmed_block(slot, UiTransactionEncoding::Binary)
.get_confirmed_block(slot, UiTransactionEncoding::Binary64)
.await?;

println!("Slot: {}", slot);
Expand Down Expand Up @@ -276,7 +276,7 @@ async fn confirm(signature: &Signature, verbose: bool) -> Result<(), Box<dyn std

if verbose {
match bigtable
.get_confirmed_transaction(signature, UiTransactionEncoding::Binary)
.get_confirmed_transaction(signature, UiTransactionEncoding::Binary64)
.await
{
Ok(Some(confirmed_transaction)) => {
Expand Down
4 changes: 2 additions & 2 deletions ledger/src/blockstore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1959,7 +1959,7 @@ impl Blockstore {
None => return Ok(vec![]),
Some((slot, _)) => {
let confirmed_block = self
.get_confirmed_block(slot, Some(UiTransactionEncoding::Binary))
.get_confirmed_block(slot, Some(UiTransactionEncoding::Binary64))
.map_err(|err| {
BlockstoreError::IO(IOError::new(
ErrorKind::Other,
Expand Down Expand Up @@ -2013,7 +2013,7 @@ impl Blockstore {
None => (0, HashSet::new()),
Some((slot, _)) => {
let confirmed_block = self
.get_confirmed_block(slot, Some(UiTransactionEncoding::Binary))
.get_confirmed_block(slot, Some(UiTransactionEncoding::Binary64))
.map_err(|err| {
BlockstoreError::IO(IOError::new(
ErrorKind::Other,
Expand Down
4 changes: 3 additions & 1 deletion stake-monitor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,8 @@ fn process_confirmed_block(
("err", "Transaction signature verification failed", String)
);
}
} else {
error!("Transaction decode failed");
}
}
}
Expand All @@ -289,7 +291,7 @@ fn load_blocks(
let mut blocks = vec![];
for slot in slots.into_iter() {
let block =
rpc_client.get_confirmed_block_with_encoding(slot, UiTransactionEncoding::Binary)?;
rpc_client.get_confirmed_block_with_encoding(slot, UiTransactionEncoding::Binary64)?;
blocks.push((slot, block));
}
Ok(blocks)
Expand Down
1 change: 1 addition & 0 deletions transaction-status/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ license = "Apache-2.0"
edition = "2018"

[dependencies]
base64 = "0.12.3"
bincode = "1.3.1"
bs58 = "0.3.1"
Inflector = "0.11.4"
Expand Down
25 changes: 20 additions & 5 deletions transaction-status/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,25 +217,31 @@ pub struct TransactionWithStatusMeta {
#[derive(Serialize, Deserialize, Clone, Copy, Debug, PartialEq)]
#[serde(rename_all = "camelCase")]
pub enum UiTransactionEncoding {
Binary,
Binary, // base-58 encoded string. SLOW! Avoid this encoding
Binary64, // base-64 encoded string
Json,
JsonParsed,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", untagged)]
pub enum EncodedTransaction {
Binary(String),
LegacyBinary(String),
Binary(String, UiTransactionEncoding),
Json(UiTransaction),
}

impl EncodedTransaction {
pub fn encode(transaction: Transaction, encoding: UiTransactionEncoding) -> Self {
match encoding {
UiTransactionEncoding::Binary => EncodedTransaction::Binary(
UiTransactionEncoding::Binary => EncodedTransaction::LegacyBinary(
bs58::encode(bincode::serialize(&transaction).unwrap()).into_string(),
),
_ => {
UiTransactionEncoding::Binary64 => EncodedTransaction::Binary(
base64::encode(bincode::serialize(&transaction).unwrap()),
encoding,
),
UiTransactionEncoding::Json | UiTransactionEncoding::JsonParsed => {
let message = if encoding == UiTransactionEncoding::Json {
UiMessage::Raw(UiRawMessage {
header: transaction.message.header,
Expand Down Expand Up @@ -298,10 +304,19 @@ impl EncodedTransaction {
pub fn decode(&self) -> Option<Transaction> {
match self {
EncodedTransaction::Json(_) => None,
EncodedTransaction::Binary(blob) => bs58::decode(blob)
EncodedTransaction::LegacyBinary(blob) => bs58::decode(blob)
.into_vec()
.ok()
.and_then(|bytes| bincode::deserialize(&bytes).ok()),
EncodedTransaction::Binary(blob, encoding) => {
if *encoding == UiTransactionEncoding::Binary64 {
base64::decode(blob)
.ok()
.and_then(|bytes| bincode::deserialize(&bytes).ok())
} else {
None
}
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion watchtower/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ fn load_blocks(
let mut blocks = vec![];
for slot in slots.into_iter() {
let block =
rpc_client.get_confirmed_block_with_encoding(slot, UiTransactionEncoding::Binary)?;
rpc_client.get_confirmed_block_with_encoding(slot, UiTransactionEncoding::Binary64)?;
blocks.push((slot, block));
}
Ok(blocks)
Expand Down

0 comments on commit b5f3ced

Please sign in to comment.