Skip to content

Commit

Permalink
Test that the proof the server sends is valid in an integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
Avi-D-coder committed Jul 30, 2024
1 parent a18cd34 commit 843b214
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
31 changes: 31 additions & 0 deletions demo-contract-tests/tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,37 @@ mod tests {
fixture.transfer_from_contract_purse_by_uref_to_user_fails(fixture.admin, amount)
}

/// Delete this test when proof-from-server.json goes out of date.
#[test]
fn submit_batch_to_contract_proof_from_server() {
let receipt = include_bytes!("testdata/proof-from-server.json");

// precheck proofs before contract tests that are hard to debug
let proof_outputs =
verify_execution(&serde_json_wasm::from_slice(receipt).unwrap()).unwrap();
assert_eq!(proof_outputs.pre_batch_trie_root, None);

let mut fixture = TestContext::new(None);

// must match the key in the receipt simple_batches_0
let user_1_secret_key =
SecretKey::from_pem(include_str!("../../testdata/users/user-1/secret_key.pem"))
.unwrap();

let user_1_public_key = fixture.create_funded_account_for_secret_key(user_1_secret_key);
let user_1_account_hash = user_1_public_key.to_account_hash();

let user_1_pre_deposit_bal = fixture.get_user_balance(user_1_account_hash);
fixture.deposit_succeeds(user_1_public_key.clone(), U512::from(500u64));
fixture.deposit_succeeds(user_1_public_key, U512::from(500u64));
let user_1_post_deposit_bal = fixture.get_user_balance(user_1_account_hash);

// submit proof to contract
fixture.submit_proof_to_contract_expect_success(fixture.admin, receipt.to_vec());

assert!(user_1_post_deposit_bal <= user_1_pre_deposit_bal - U512::from(5u64));
}

#[test]
fn submit_batch_to_contract_simple() {
let receipt0 = include_bytes!("testdata/test_prove_simple_batches_0.json");
Expand Down
1 change: 1 addition & 0 deletions demo-contract-tests/tests/testdata/proof-from-server.json

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions kairos-server/src/state/submit_batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ pub async fn submit_proof_to_contract(
) {
let proof_serialized = Bytes::from(serde_json::to_vec(receipt).expect("could not serialize"));

let proof_json_file = std::fs::File::create("proof.json").expect("could not create proof.json");
serde_json::to_writer(proof_json_file, receipt).expect("could not write proof.json");

tracing::info!("Submitting proof to contract: {:?}", contract_hash);
let submit_batch = ExecutableDeployItem::StoredContractByHash {
hash: contract_hash,
Expand Down

0 comments on commit 843b214

Please sign in to comment.