fix: set epoch in tests

This commit is contained in:
Brice Dobry
2022-12-16 22:37:44 -05:00
parent a11b7e1275
commit 4fda7beeab
10 changed files with 26 additions and 18 deletions

View File

@@ -82,10 +82,11 @@ pub enum NativeHandle {
SingleArg(&'static dyn Fn(Value) -> Result<Value>),
DoubleArg(&'static dyn Fn(Value, Value) -> Result<Value>),
MoreArg(&'static dyn Fn(Vec<Value>) -> Result<Value>),
MoreArgEnv(&'static dyn Fn(Vec<Value>, &mut Environment) -> Result<Value>),
}
impl NativeHandle {
pub fn apply(&self, mut args: Vec<Value>) -> Result<Value> {
pub fn apply(&self, mut args: Vec<Value>, env: &mut Environment) -> Result<Value> {
match self {
Self::SingleArg(function) => {
check_argument_count(1, &args)?;
@@ -98,6 +99,7 @@ impl NativeHandle {
function(first, second)
}
Self::MoreArg(function) => function(args),
Self::MoreArgEnv(function) => function(args, env),
}
}
}
@@ -178,7 +180,7 @@ impl DefinedFunction {
if *env.contract_context.get_clarity_version() < ClarityVersion::Clarity2 {
match (type_sig, value) {
(
TypeSignature::CallableType(CallableSubtype::Trait(trait_identifier)),
TypeSignature::TraitReferenceType(trait_identifier),
Value::Principal(PrincipalData::Contract(callee_contract_id)),
) => {
// Argument is a trait reference, probably leading to a dynamic contract call

View File

@@ -543,14 +543,14 @@ impl EventBatch {
impl<'a, 'hooks> OwnedEnvironment<'a, 'hooks> {
#[cfg(any(test, feature = "testing"))]
pub fn new(database: ClarityDatabase<'a>) -> OwnedEnvironment<'a, '_> {
pub fn new(database: ClarityDatabase<'a>, epoch: StacksEpochId) -> OwnedEnvironment<'a, '_> {
OwnedEnvironment {
context: GlobalContext::new(
false,
CHAIN_ID_TESTNET,
database,
LimitedCostTracker::new_free(),
StacksEpochId::Epoch2_05,
epoch,
),
call_stack: CallStack::new(),
}

View File

@@ -3035,7 +3035,7 @@ mod test {
let conn = store.as_docs_clarity_db();
let docs_test_id = QualifiedContractIdentifier::local("docs-test").unwrap();
let docs_principal_id = PrincipalData::Contract(docs_test_id);
let mut env = OwnedEnvironment::new(conn);
let mut env = OwnedEnvironment::new(conn, StacksEpochId::latest());
let balance = STXBalance::initial(1000);
env.execute_in_env::<_, _, ()>(
QualifiedContractIdentifier::local("tokens").unwrap().into(),

View File

@@ -278,7 +278,7 @@ pub fn lookup_reserved_functions(name: &str, version: &ClarityVersion) -> Option
),
Equals => NativeFunction205(
"native_eq",
NativeHandle::MoreArg(&native_eq),
NativeHandle::MoreArgEnv(&native_eq),
ClarityCostFunction::Eq,
&cost_input_sized_vararg,
),
@@ -566,7 +566,7 @@ pub fn lookup_reserved_functions(name: &str, version: &ClarityVersion) -> Option
}
}
fn native_eq(args: Vec<Value>) -> Result<Value> {
fn native_eq(args: Vec<Value>, env: &mut Environment) -> Result<Value> {
// TODO: this currently uses the derived equality checks of Value,
// however, that's probably not how we want to implement equality
// checks on the ::ListTypes

View File

@@ -289,7 +289,7 @@ pub fn apply(
CallableType::NativeFunction(_, function, cost_function) => {
runtime_cost(*cost_function, env, evaluated_args.len())
.map_err(Error::from)
.and_then(|_| function.apply(evaluated_args))
.and_then(|_| function.apply(evaluated_args, env))
}
CallableType::NativeFunction205(_, function, cost_function, cost_input_handle) => {
let cost_input = if env.epoch() >= &StacksEpochId::Epoch2_05 {
@@ -299,7 +299,7 @@ pub fn apply(
};
runtime_cost(*cost_function, env, cost_input)
.map_err(Error::from)
.and_then(|_| function.apply(evaluated_args))
.and_then(|_| function.apply(evaluated_args, env))
}
CallableType::UserFunction(function) => function.apply(&evaluated_args, env),
_ => panic!("Should be unreachable."),

View File

@@ -29,6 +29,7 @@ use crate::vm::types::{
};
use crate::vm::version::ClarityVersion;
use crate::vm::ContractContext;
use stacks_common::types::StacksEpochId;
use stacks_common::util::hash::hex_bytes;
const FIRST_CLASS_TOKENS: &str = "(define-fungible-token stackaroos)
@@ -1382,6 +1383,6 @@ fn test_all() {
test_native_stx_ops,
];
for test in to_test.iter() {
with_memory_environment(test, true);
with_memory_environment(test, StacksEpochId::latest(), true);
}
}

View File

@@ -21,6 +21,7 @@ use crate::types::chainstate::StacksBlockId;
use rstest::rstest;
#[cfg(any(test, feature = "testing"))]
use rstest_reuse::{self, *};
use stacks_common::types::StacksEpochId;
use crate::vm::ast;
use crate::vm::ast::errors::ParseErrors;
@@ -140,7 +141,7 @@ fn test_get_block_info_eval() {
for i in 0..contracts.len() {
let mut marf = MemoryBackingStore::new();
let mut owned_env = OwnedEnvironment::new(marf.as_clarity_db());
let mut owned_env = OwnedEnvironment::new(marf.as_clarity_db(), StacksEpochId::latest());
let contract_identifier = QualifiedContractIdentifier::local("test-contract").unwrap();
owned_env
.initialize_contract(
@@ -1016,7 +1017,7 @@ fn test_at_unknown_block() {
}
}
with_memory_environment(test, true);
with_memory_environment(test, StacksEpochId::latest(), true);
}
#[test]
@@ -1036,7 +1037,7 @@ fn test_as_max_len() {
.unwrap();
}
with_memory_environment(test, true);
with_memory_environment(test, StacksEpochId::latest(), true);
}
#[test]
@@ -1115,6 +1116,7 @@ fn test_cc_stack_depth() {
RuntimeErrorType::MaxStackDepthReached.into()
);
},
StacksEpochId::latest(),
false,
);
}
@@ -1157,6 +1159,7 @@ fn test_cc_trait_stack_depth() {
RuntimeErrorType::MaxStackDepthReached.into()
);
},
StacksEpochId::latest(),
false,
);
}
@@ -1174,6 +1177,6 @@ fn test_all() {
];
for test in to_test.iter() {
eprintln!("..");
with_memory_environment(test, false);
with_memory_environment(test, StacksEpochId::latest(), false);
}
}

View File

@@ -55,13 +55,13 @@ mod sequences;
mod simple_apply_eval;
mod traits;
pub fn with_memory_environment<F>(f: F, top_level: bool)
pub fn with_memory_environment<F>(f: F, epoch: StacksEpochId, top_level: bool)
where
F: FnOnce(&mut OwnedEnvironment) -> (),
{
let mut marf_kv = MemoryBackingStore::new();
let mut owned_env = OwnedEnvironment::new(marf_kv.as_clarity_db());
let mut owned_env = OwnedEnvironment::new(marf_kv.as_clarity_db(), epoch);
// start an initial transaction.
if !top_level {
owned_env.begin();

View File

@@ -102,7 +102,7 @@ fn test_simple_let(#[case] version: ClarityVersion, #[case] epoch: StacksEpochId
if let Ok(parsed_program) = parse(&contract_id, &program, version, epoch) {
let context = LocalContext::new();
let mut marf = MemoryBackingStore::new();
let mut env = OwnedEnvironment::new(marf.as_clarity_db());
let mut env = OwnedEnvironment::new(marf.as_clarity_db(), StacksEpochId::latest());
assert_eq!(
Ok(Value::Int(7)),

View File

@@ -14,6 +14,8 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
use stacks_common::types::StacksEpochId;
use crate::vm::analysis::errors::CheckError;
use crate::vm::ast::ASTRules;
use crate::vm::contexts::{Environment, GlobalContext, OwnedEnvironment};
@@ -66,7 +68,7 @@ fn test_all() {
test_let3_trait,
];
for test in to_test.iter() {
with_memory_environment(test, false);
with_memory_environment(test, StacksEpochId::latest(), false);
}
}