Skip to content

Commit

Permalink
tests seem to pass with this
Browse files Browse the repository at this point in the history
  • Loading branch information
Ekleog-NEAR committed Nov 18, 2023
1 parent 82cc8ae commit a0589b6
Show file tree
Hide file tree
Showing 14 changed files with 51 additions and 20 deletions.
14 changes: 7 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ elastic-array = "0.11"
enum-map = "2.1.0"
enumset = "1.0"
expect-test = "1.3.0"
finite-wasm = "0.5.0"
finite-wasm = { version = "0.5.0", features = ["instrument"] }
flate2 = "1.0.22"
fs2 = "0.4"
futures = "0.3.5"
Expand Down Expand Up @@ -189,7 +189,7 @@ near-database-tool= { path = "tools/database" }
near-async = { path = "core/async" }
near-cache = { path = "utils/near-cache" }
near-chain = { path = "chain/chain" }
near-chain-configs = { path = "core/chain-configs" }
near-chain-configs = { path = "core/chain-configs", features = ["metrics"] }
near-chain-primitives = { path = "chain/chain-primitives" }
near-chunks = { path = "chain/chunks" }
near-chunks-primitives = { path = "chain/chunks-primitives" }
Expand Down Expand Up @@ -228,7 +228,7 @@ near-stable-hasher = { path = "utils/near-stable-hasher" }
near-state-parts = { path = "tools/state-parts" }
near-state-parts-dump-check = { path = "tools/state-parts-dump-check" }
near-state-viewer = { path = "tools/state-viewer", package = "state-viewer" }
near-store = { path = "core/store" }
near-store = { path = "core/store", features = ["test_features"] }
near-telemetry = { path = "chain/telemetry" }
near-test-contracts = { path = "runtime/near-test-contracts" }
near-undo-block = { path = "tools/undo-block" }
Expand All @@ -238,7 +238,7 @@ near-vm-compiler-singlepass = { path = "runtime/near-vm/compiler-singlepass" }
near-vm-compiler-test-derive = { path = "runtime/near-vm/compiler-test-derive" }
near-vm-engine = { path = "runtime/near-vm/engine" }
near-vm-engine-universal = { path = "runtime/near-vm/engine-universal" }
near-vm-runner = { path = "runtime/near-vm-runner" }
near-vm-runner = { path = "runtime/near-vm-runner", features = ["clap"] }
near-vm-test-generator = { path = "runtime/near-vm/test-generator" }
near-vm-types = { path = "runtime/near-vm/types" }
near-vm-vm = { path = "runtime/near-vm/vm" }
Expand Down Expand Up @@ -270,7 +270,7 @@ protobuf = "3.0.1"
protobuf-codegen = "3.0.1"
pwasm-utils_12 = { package = "pwasm-utils", version = "0.12" }
quote = "1.0"
rand = "0.8.5"
rand = { version = "0.8.5", features = ["small_rng"] }
rand_chacha = "0.3.1"
rand_core = "0.5"
rand_hc = "0.3.1"
Expand All @@ -289,12 +289,12 @@ runtime-tester = { path = "test-utils/runtime-tester" }
rusqlite = { version = "0.29.0", features = ["bundled", "chrono", "functions"] }
rustc-demangle = "0.1"
rust-s3 = { version = "0.32.3", features = ["blocking"] }
rustix = "0.37"
rustix = { version = "0.37", features = ["param", "mm"] }
secp256k1 = { version = "0.27.0", features = ["recovery", "rand-std"] }
semver = "1.0.4"
serde = { version = "1.0.136", features = ["alloc", "derive", "rc"] }
serde_ignored = "0.1"
serde_json = "1.0.68"
serde_json = { version = "1.0.68", features = ["preserve_order"] }
serde_repr = "0.1.8"
serde_with = { version = "3.0", features = ["base64"] }
serde_yaml = "0.9"
Expand Down
2 changes: 1 addition & 1 deletion core/store/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ thiserror.workspace = true
tokio.workspace = true
tracing.workspace = true

near-chain-configs = { workspace = true, features = ["metrics"] }
near-chain-configs = { workspace = true }
near-crypto.workspace = true
near-fmt.workspace = true
near-o11y.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion nearcore/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ xz2.workspace = true

near-async.workspace = true
near-chain.workspace = true
near-chain-configs = { workspace = true, features = ["metrics"] }
near-chain-configs = { workspace = true }
near-chunks.workspace = true
near-client.workspace = true
near-client-primitives.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion runtime/near-test-contracts/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ once_cell.workspace = true
wat.workspace = true
wasm-encoder.workspace = true
wasm-smith.workspace = true
rand = { workspace = true, features = ["small_rng"] }
rand = { workspace = true }
arbitrary.workspace = true

[features]
Expand Down
4 changes: 2 additions & 2 deletions runtime/near-vm-runner/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ borsh.workspace = true
clap = { workspace = true, optional = true }
ed25519-dalek.workspace = true
enum-map.workspace = true
finite-wasm = { workspace = true, features = ["instrument"] }
finite-wasm = { workspace = true }
loupe.workspace = true
memoffset.workspace = true
num-rational.workspace = true
Expand Down Expand Up @@ -80,7 +80,7 @@ near-test-contracts.workspace = true
# circumstances it is necessary to refer to the `[lib]` output rather than using `crate::*` stuff.
near-vm-runner.workspace = true
rand.workspace = true
serde_json = { workspace = true, features = ["preserve_order"] }
serde_json = { workspace = true }
wasm-smith.workspace = true
wasmprinter.workspace = true
wat.workspace = true
Expand Down
4 changes: 2 additions & 2 deletions runtime/near-vm-runner/src/logic/gas_counter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ use std::collections::HashMap;

#[inline]
pub fn with_ext_cost_counter(f: impl FnOnce(&mut HashMap<ExtCosts, u64>)) {
#[cfg(feature = "costs_counting")]
#[cfg(any(test, feature = "costs_counting"))]
{
thread_local! {
static EXT_COSTS_COUNTER: std::cell::RefCell<HashMap<ExtCosts, u64>> =
Default::default();
}
EXT_COSTS_COUNTER.with(|rc| f(&mut *rc.borrow_mut()));
}
#[cfg(not(feature = "costs_counting"))]
#[cfg(not(any(test, feature = "costs_counting")))]
let _ = f;
}

Expand Down
26 changes: 26 additions & 0 deletions runtime/near-vm-runner/src/logic/logic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1146,43 +1146,69 @@ impl<'a> VMLogic<'a> {
public_key_len: u64,
public_key_ptr: u64,
) -> Result<u64> {
eprintln!("entering ed25519_verify with {signature_len}, {signature_ptr}, {message_len}, {message_ptr}, {public_key_len}, {public_key_ptr}");
use ed25519_dalek::Verifier;

eprintln!("paying base");
self.gas_counter.pay_base(ed25519_verify_base)?;
crate::with_ext_cost_counter(|c| eprintln!("current costs: {c:?}"));

let signature: ed25519_dalek::Signature = {
eprintln!("getting mem");
let vec = get_memory_or_register!(self, signature_ptr, signature_len)?;
crate::with_ext_cost_counter(|c| eprintln!("current costs: {c:?}"));
if vec.len() != ed25519_dalek::SIGNATURE_LENGTH {
eprintln!("length mismatch");
return Err(VMLogicError::HostError(HostError::Ed25519VerifyInvalidInput {
msg: "invalid signature length".to_string(),
}));
}
// Sanity-check that was performed by ed25519-dalek in from_bytes before version 2,
// but was removed with version 2. It is not actually any good a check, but we need
// it to avoid costs changing.
if vec[ed25519_dalek::SIGNATURE_LENGTH - 1] & 0b1110_0000 != 0 {
return Ok(false as u64);
}
eprintln!("recovering length");
match <&[u8; 64]>::try_from(&vec[..]) {
Ok(b) => ed25519_dalek::Signature::from_bytes(b),
Err(_) => return Ok(false as u64),
}
};
eprintln!("got signature {signature:?}");

eprintln!("getting message");
let message = get_memory_or_register!(self, message_ptr, message_len)?;
crate::with_ext_cost_counter(|c| eprintln!("current costs: {c:?}"));
eprintln!("got message {message:?}, paying gas per");
self.gas_counter.pay_per(ed25519_verify_byte, message.len() as u64)?;
crate::with_ext_cost_counter(|c| eprintln!("current costs: {c:?}"));

let public_key: ed25519_dalek::VerifyingKey = {
eprintln!("getting public key");
let vec = get_memory_or_register!(self, public_key_ptr, public_key_len)?;
crate::with_ext_cost_counter(|c| eprintln!("current costs: {c:?}"));
eprintln!("checking len");
if vec.len() != ed25519_dalek::PUBLIC_KEY_LENGTH {
eprintln!("wrong len");
return Err(VMLogicError::HostError(HostError::Ed25519VerifyInvalidInput {
msg: "invalid public key length".to_string(),
}));
}
eprintln!("casting");
let b = match <&[u8; 32]>::try_from(&vec[..]) {
Ok(b) => b,
Err(_) => return Ok(false as u64),
};
eprintln!("getting verif key");
match ed25519_dalek::VerifyingKey::from_bytes(b) {
Ok(public_key) => public_key,
Err(_) => return Ok(false as u64),
}
};
crate::with_ext_cost_counter(|c| eprintln!("current costs: {c:?}"));

eprintln!("verifying");
match public_key.verify(&message, &signature) {
Err(_) => Ok(false as u64),
Ok(()) => Ok(true as u64),
Expand Down
5 changes: 5 additions & 0 deletions runtime/near-vm-runner/src/logic/tests/ed25519_verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ fn check_ed25519_verify(
logic.internal_mem_write(public_key).ptr
};

eprintln!("before ed25519_verify with {signature_len}, {signature_ptr}, {message_len}, {message_ptr}, {public_key_len}, {public_key_ptr}");
let result = logic.ed25519_verify(
signature_len,
signature_ptr,
Expand All @@ -85,10 +86,14 @@ fn check_ed25519_verify(
public_key_len,
public_key_ptr,
);
eprintln!("ed25519_verify result: {result:?}");

let want = want.map_err(VMLogicError::HostError);
eprintln!("want: {want:?}");
assert_eq!(want, result);
eprintln!("result was equal to want");
assert_costs(want_costs);
eprintln!("cost assertions passed");
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion runtime/near-vm/compiler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ workspace = true
finite-wasm.workspace = true
near-vm-vm.workspace = true
near-vm-types.workspace = true
wasmparser = { version = "0.99.0", default-features = false }
wasmparser = { version = "0.99.0" }
target-lexicon.workspace = true
enumset.workspace = true
hashbrown = { workspace = true, optional = true }
Expand Down
2 changes: 1 addition & 1 deletion runtime/near-vm/engine/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ thiserror.workspace = true
cfg-if.workspace = true
tracing.workspace = true
crossbeam-queue.workspace = true
rustix = { workspace = true, features = ["param", "mm"] }
rustix = { workspace = true }

[badges]
maintenance = { status = "actively-developed" }
2 changes: 1 addition & 1 deletion runtime/runtime-params-estimator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ near-o11y.workspace = true
near-primitives.workspace = true
near-store.workspace = true
near-test-contracts.workspace = true
near-vm-runner = { workspace = true, features = ["clap"] }
near-vm-runner = { workspace = true }
nearcore.workspace = true
node-runtime.workspace = true

Expand Down
2 changes: 1 addition & 1 deletion runtime/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,6 @@ serde_json.workspace = true
tempfile.workspace = true

near-chain-configs.workspace = true
near-store = { workspace = true, features = ["test_features"] }
near-store = { workspace = true }
near-test-contracts.workspace = true
testlib.workspace = true
2 changes: 1 addition & 1 deletion tools/rpctypegen/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ serde.workspace = true
syn.workspace = true

[dev-dependencies]
serde_json = { workspace = true, features = ["preserve_order"] }
serde_json = { workspace = true }

[features]
test = []
Expand Down
2 changes: 1 addition & 1 deletion tools/rpctypegen/macro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ proc-macro = true

[dependencies]
serde.workspace = true
serde_json = { workspace = true, optional = true, features = ["preserve_order"] }
serde_json = { workspace = true, optional = true }
syn.workspace = true
fs2.workspace = true

Expand Down

0 comments on commit a0589b6

Please sign in to comment.