Skip to content

Commit

Permalink
Problem (Fix crypto-com#949): deposit amount fee calculation is not c…
Browse files Browse the repository at this point in the history
…orrect

Solution:
- Fix DummySigner for deposit transaction
- Fix mock_enc_dec payload padding
  • Loading branch information
yihuang committed Feb 21, 2020
1 parent 8183883 commit c7592e3
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 7 deletions.
17 changes: 14 additions & 3 deletions chain-abci/src/enclave_bridge/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@ pub fn handle_enc_dec(_req: &RequestQuery, resp: &mut ResponseQuery, _storage: &
resp.code = 1;
}

fn pad_payload(payload: &[u8]) -> Vec<u8> {
let unit = 16_usize;
let payload_len = payload.len();
// PKCS7 padding -- if the len is multiple of the block length, it'll still be padded
let padding_len = unit - payload_len % unit + 5;
let mut result = Vec::with_capacity(payload.len() + padding_len);
result.extend_from_slice(payload);
result.extend_from_slice(&vec![0; padding_len]);
result
}

/// temporary mock
#[cfg(feature = "mock-enc-dec")]
pub fn handle_enc_dec(_req: &RequestQuery, resp: &mut ResponseQuery, storage: &Storage) {
Expand All @@ -53,7 +64,7 @@ pub fn handle_enc_dec(_req: &RequestQuery, resp: &mut ResponseQuery, storage: &S
key_from: 0,
txid: tx.id(),
init_vector: [0u8; 12],
txpayload: plain.encode(),
txpayload: pad_payload(&plain.encode()),
},
}),
};
Expand All @@ -68,7 +79,7 @@ pub fn handle_enc_dec(_req: &RequestQuery, resp: &mut ResponseQuery, storage: &S
key_from: 0,
txid: maintx.id(),
init_vector: [0u8; 12],
txpayload: plain.encode(),
txpayload: pad_payload(&plain.encode()),
},
}),
};
Expand All @@ -84,7 +95,7 @@ pub fn handle_enc_dec(_req: &RequestQuery, resp: &mut ResponseQuery, storage: &S
key_from: 0,
txid: tx.id(),
init_vector: [0u8; 12],
txpayload: plain.encode(),
txpayload: pad_payload(&plain.encode()),
},
}),
};
Expand Down
6 changes: 3 additions & 3 deletions client-core/src/signer/dummy_signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl DummySigner {
let unit = 16_usize;
let plain_payload_len = plain_txaux.encode().len();
// PKCS7 padding -- if the len is multiple of the block length, it'll still be padded
vec![0; plain_payload_len + unit - plain_payload_len % unit]
vec![0; plain_payload_len + unit - plain_payload_len % unit + 5]
}

/// Creates a mock merkletree
Expand Down Expand Up @@ -81,9 +81,9 @@ impl DummySigner {
TxAux::EnclaveTx(tx_enclave_aux)
}

/// Mock the txaux for withdraw transactions
/// Mock the txaux for deposit transactions
pub fn mock_txaux_for_deposit(&self, input_len: usize) -> Result<TxAux> {
let total_pubkeys_len = 2;
let total_pubkeys_len = 1;
let witness = self.schnorr_sign_inputs_len(total_pubkeys_len, input_len)?;
let plain_payload = PlainTxAux::DepositStakeTx(witness);
let padded_payload = self.pad_payload(plain_payload);
Expand Down
16 changes: 16 additions & 0 deletions integration-tests/pytests/test_small.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import pytest
from chainrpc import RPC
from .common import wait_for_tx

rpc = RPC()


@pytest.mark.withfee
def test_deposit_amount(addresses):
'execute a deposit amount to self, check staking state change'
bonded1 = int(rpc.staking.state(addresses.unbonded_staking)['bonded'])
txid = rpc.staking.deposit_amount(addresses.unbonded_staking, 100000000)
wait_for_tx(rpc, txid)
rpc.wallet.sync()
bonded2 = int(rpc.staking.state(addresses.unbonded_staking)['bonded'])
assert bonded1 + 100000000 == bonded2
2 changes: 1 addition & 1 deletion integration-tests/zero_fee_cluster.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"node_seed": "95356c3c5791cdd1601087c6448aa2a3b1f6f01ecce89b601194e0d6f27fdd7b",
"bonded_coin": 500000000000000000,
"unbonded_coin": 500000000000000000,
"base_port": 16650
"base_port": 26650
}
],
"chain_config_patch": [
Expand Down

0 comments on commit c7592e3

Please sign in to comment.