Skip to content

Commit

Permalink
Test that native VPs correctly run out of gas
Browse files Browse the repository at this point in the history
  • Loading branch information
sug0 committed Mar 25, 2024
1 parent 57cb950 commit eb302a8
Showing 1 changed file with 81 additions and 0 deletions.
81 changes: 81 additions & 0 deletions crates/namada/src/ledger/protocol/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1222,4 +1222,85 @@ mod tests {
}
}
}

#[test]
fn test_native_vp_out_of_gas() {
let (mut state, _validators) = test_utils::setup_default_storage();

// some random token address
let token_address = Address::Established([0xff; 20].into());

let src_address = Address::Established([0xab; 20].into());
let dst_address = Address::Established([0xba; 20].into());

// supply an address with 1000 of said token
namada_token::credit_tokens(
&mut state,
&token_address,
&src_address,
1000.into(),
)
.unwrap();

// commit storage changes. this will act as the
// initial state of the chain
state.commit_tx();
state.commit_block().unwrap();

// "execute" a dummy tx, by manually performing its state changes
let (dummy_tx, changed_keys, verifiers) = {
let mut tx = Tx::from_type(TxType::Raw);
tx.set_code(namada_tx::Code::new(vec![], None));
tx.set_data(namada_tx::Data::new(vec![]));

// transfer half of the supply of src to dst
namada_token::transfer(
&mut state,
&token_address,
&src_address,
&dst_address,
500.into(),
)
.unwrap();

let changed_keys = {
let mut set = BTreeSet::new();
set.insert(namada_token::storage_key::balance_key(
&token_address,
&src_address,
));
set.insert(namada_token::storage_key::balance_key(
&token_address,
&dst_address,
));
set
};

let verifiers = {
let mut set = BTreeSet::new();
set.insert(Address::Internal(InternalAddress::Multitoken));
set
};

(tx, changed_keys, verifiers)
};

// temp vp cache
let (mut vp_cache, _) =
wasm::compilation_cache::common::testing::cache();

// gas meter with no gas left
let gas_meter = TxGasMeter::new(0);

let result = execute_vps(
verifiers,
changed_keys,
&dummy_tx,
&TxIndex::default(),
&state,
&gas_meter,
&mut vp_cache,
);
assert!(matches!(result.unwrap_err(), Error::GasError(_)));
}
}

0 comments on commit eb302a8

Please sign in to comment.