Skip to content

Commit

Permalink
fix Ci, more bittests
Browse files Browse the repository at this point in the history
  • Loading branch information
fl0rek committed Nov 21, 2024
1 parent 388dd50 commit 963a391
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 11 deletions.
2 changes: 1 addition & 1 deletion grpc/tests/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ pub async fn new_test_client() -> Result<GrpcClient<TestAuthInterceptor>> {
}

pub fn load_account(path: &str) -> TestAccount {
let account_file = format!("{path}.addr"); // TODO: consolidate
let account_file = format!("{path}.addr");
let key_file = format!("{path}.plaintext-key");

let account = fs::read_to_string(account_file).expect("file with account name to exists");
Expand Down
14 changes: 7 additions & 7 deletions rpc/src/state.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use celestia_types::state::{
AccAddress, Address, Balance, QueryDelegationResponse, QueryRedelegationsResponse,
QueryUnbondingDelegationResponse, TxResponse, Uint, ValAddress,
QueryUnbondingDelegationResponse, RawTxResponse, Uint, ValAddress,
};
use celestia_types::{blob::RawBlob, TxConfig};
use jsonrpsee::proc_macros::rpc;
Expand Down Expand Up @@ -31,7 +31,7 @@ pub trait State {
dest: &ValAddress,
amount: Uint,
config: TxConfig,
) -> Result<TxResponse, Error>;
) -> Result<RawTxResponse, Error>;

/// CancelUnbondingDelegation cancels a user's pending undelegation from a validator.
#[method(name = "state.CancelUnbondingDelegation")]
Expand All @@ -41,7 +41,7 @@ pub trait State {
amount: Uint,
height: Uint,
config: TxConfig,
) -> Result<TxResponse, Error>;
) -> Result<RawTxResponse, Error>;

/// Delegate sends a user's liquid tokens to a validator for delegation.
#[method(name = "state.Delegate")]
Expand All @@ -50,7 +50,7 @@ pub trait State {
addr: &ValAddress,
amount: Uint,
config: TxConfig,
) -> Result<TxResponse, Error>;
) -> Result<RawTxResponse, Error>;

/// IsStopped checks if the Module's context has been stopped.
#[method(name = "state.IsStopped")]
Expand Down Expand Up @@ -84,7 +84,7 @@ pub trait State {
&self,
blobs: &[RawBlob],
config: TxConfig,
) -> Result<TxResponse, Error>;
) -> Result<RawTxResponse, Error>;

/// Transfer sends the given amount of coins from default wallet of the node to the given account address.
#[method(name = "state.Transfer")]
Expand All @@ -93,7 +93,7 @@ pub trait State {
to: &AccAddress,
amount: Uint,
config: TxConfig,
) -> Result<TxResponse, Error>;
) -> Result<RawTxResponse, Error>;

/// Undelegate undelegates a user's delegated tokens, unbonding them from the current validator.
#[method(name = "Undelegate")]
Expand All @@ -102,5 +102,5 @@ pub trait State {
addr: &ValAddress,
amount: Uint,
config: TxConfig,
) -> Result<TxResponse, Error>;
) -> Result<RawTxResponse, Error>;
}
2 changes: 1 addition & 1 deletion rpc/tests/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ async fn submit_pay_for_blob() {
.unwrap();

let received_blob = client
.blob_get(tx_response.height.into(), namespace, blob.commitment)
.blob_get(tx_response.height as u64, namespace, blob.commitment)
.await
.unwrap();

Expand Down
64 changes: 63 additions & 1 deletion types/src/bit_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,17 @@ impl TryFrom<CompactBitArray> for BitVector {
type Error = Error;

fn try_from(value: CompactBitArray) -> Result<Self, Self::Error> {
let num_bits = (value.elems.len() - 1) * 8 + value.extra_bits_stored as usize;
let num_bytes = value.elems.len();
if num_bytes == 0 && value.extra_bits_stored != 0 {
return Err(Error::MalformedCompactBitArray);
}

let last_byte_bits = match value.extra_bits_stored {
0 => 8,
n @ 1..=7 => n,
_ => return Err(Error::MalformedCompactBitArray),
} as usize;
let num_bits = num_bytes.saturating_sub(1) * 8 + last_byte_bits;
let mut bit_vec =
BitVec::<_, Msb0>::try_from_vec(value.elems).map_err(|_| Error::BitarrayTooLarge)?;

Expand Down Expand Up @@ -39,6 +49,7 @@ impl From<BitVector> for CompactBitArray {
#[cfg(test)]
mod tests {
use super::*;
use bitvec::prelude::*;

#[test]
fn test_empty() {
Expand All @@ -51,4 +62,55 @@ mod tests {
let result = CompactBitArray::from(vec);
assert_eq!(source, result);
}

#[test]
fn byte_aligned() {
let source = CompactBitArray {
extra_bits_stored: 0,
elems: vec![0b00000001, 0b00000010],
};
let expected_bits = bits![0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0];

let vec = BitVector::try_from(source.clone()).unwrap();
assert_eq!(vec.0.len(), 16);
assert_eq!(vec.0, expected_bits);

let result = CompactBitArray::from(vec);
assert_eq!(source, result);
}

// test relies on the fact that we're setting uninitialised part of bitfield to 0,
// which we do anyway for safety
#[test]
fn with_extra_bytes() {
let source = CompactBitArray {
extra_bits_stored: 1,
elems: vec![0b00000000, 0b10000000],
};
let expected_bits = bits![0, 0, 0, 0, 0, 0, 0, 0, 1];

let vec = BitVector::try_from(source.clone()).unwrap();
assert_eq!(vec.0.len(), 9);
assert_eq!(vec.0, expected_bits);

let result = CompactBitArray::from(vec);
assert_eq!(source, result);
}

#[test]
fn malformed() {
let source = CompactBitArray {
extra_bits_stored: 8,
elems: vec![0b00000000],
};
let error = BitVector::try_from(source.clone()).unwrap_err();
assert!(matches!(error, Error::MalformedCompactBitArray));

let source = CompactBitArray {
extra_bits_stored: 1,
elems: vec![],
};
let error = BitVector::try_from(source.clone()).unwrap_err();
assert!(matches!(error, Error::MalformedCompactBitArray));
}
}
4 changes: 4 additions & 0 deletions types/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ pub enum Error {
#[error("Bit array to large")]
BitarrayTooLarge,

/// Malformed CompactBitArray
#[error("CompactBitArray malformed")]
MalformedCompactBitArray,

/// Wrong proof type.
#[error("Wrong proof type")]
WrongProofType,
Expand Down
5 changes: 4 additions & 1 deletion types/src/state/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub use celestia_proto::cosmos::tx::v1beta1::TxBody as RawTxBody;

pub type Signature = Vec<u8>;

// [`BOND_DENOM`] defines the native staking denomination
/// [`BOND_DENOM`] defines the native staking denomination
pub const BOND_DENOM: &str = "utia";

/// [`Tx`] is the standard type used for broadcasting transactions.
Expand Down Expand Up @@ -218,6 +218,9 @@ pub struct Coin {
}

impl Fee {
/// Create [`Fee`] struct with provided number of utia and gas limit,
/// without setting custom [`Fee::payer`] or [`Fee::granter`] fields,
/// which means first tx signer is responsible for paying.
pub fn new(utia_fee: u64, gas_limit: u64) -> Self {
Fee {
amount: vec![Coin {
Expand Down

0 comments on commit 963a391

Please sign in to comment.