Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix backends aot_code loading #722

Merged
merged 2 commits into from
Jun 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 86 additions & 7 deletions crates/generator/src/backend_manage.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,73 @@
use anyhow::{bail, Context, Result};
use gw_common::H256;
use gw_common::{blake2b::new_blake2b, H256};
use gw_config::{BackendConfig, BackendSwitchConfig, BackendType};
use gw_types::bytes::Bytes;
use std::{collections::HashMap, fs};

#[cfg(has_asm)]
use crate::types::vm::AotCode;

#[derive(Default, Clone)]
pub struct BackendCheckSum {
pub validator: H256,
pub generator: H256,
}

impl std::fmt::Debug for BackendCheckSum {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("BackendCheckSum")
.field("validator", &hex::encode(self.validator.as_slice()))
.field("generator", &hex::encode(self.generator.as_slice()))
.finish()
}
}

#[derive(Clone)]
pub struct Backend {
pub validator: Bytes,
pub generator: Bytes,
pub validator_script_type_hash: H256,
pub backend_type: BackendType,
pub checksum: BackendCheckSum,
}

impl Backend {
pub fn new(
backend_type: BackendType,
validator_script_type_hash: H256,
validator: Bytes,
generator: Bytes,
) -> Self {
let checksum = {
let validator = {
let mut hasher = new_blake2b();
hasher.update(&validator);
let mut buf = [0u8; 32];
hasher.finalize(&mut buf);
buf.into()
};
let generator = {
let mut hasher = new_blake2b();
hasher.update(&generator);
let mut buf = [0u8; 32];
hasher.finalize(&mut buf);
buf.into()
};

BackendCheckSum {
validator,
generator,
}
};

Self {
validator,
generator,
validator_script_type_hash,
backend_type,
checksum,
}
}
}

#[derive(Default)]
Expand Down Expand Up @@ -52,6 +107,8 @@ impl BackendManage {
.cloned()
.unwrap_or_default();

let switch_height = config.switch_height;

// register backends
for config in config.backends {
let BackendConfig {
Expand All @@ -74,16 +131,24 @@ impl BackendManage {
let hash: [u8; 32] = validator_script_type_hash.into();
hash.into()
};
let backend = Backend {
let backend = Backend::new(
backend_type,
validator_script_type_hash,
validator,
generator,
validator_script_type_hash,
backend_type,
};
);
#[cfg(has_asm)]
if compile {
self.compile_backend(&backend);
}

log::debug!(
"registry backend {:?}({:?}) at height {}",
backend.backend_type,
backend.checksum,
switch_height
);

backends.insert(backend.validator_script_type_hash, backend);
}

Expand All @@ -94,12 +159,12 @@ impl BackendManage {
#[cfg(has_asm)]
fn compile_backend(&mut self, backend: &Backend) {
self.aot_codes.0.insert(
backend.validator_script_type_hash,
backend.checksum.generator,
self.aot_compile(&backend.generator, 0)
.expect("Ahead-of-time compile"),
);
self.aot_codes.1.insert(
backend.validator_script_type_hash,
backend.checksum.generator,
self.aot_compile(&backend.generator, 1)
.expect("Ahead-of-time compile"),
);
Expand All @@ -118,6 +183,15 @@ impl BackendManage {
pub fn get_backend(&self, block_number: u64, code_hash: &H256) -> Option<&Backend> {
self.get_backends_at_height(block_number)
.and_then(|(_number, backends)| backends.get(code_hash))
.map(|backend| {
log::debug!(
"get backend {:?}({:?}) at height {}",
backend.backend_type,
backend.checksum,
block_number
);
backend
})
}

#[cfg(has_asm)]
Expand All @@ -140,6 +214,11 @@ impl BackendManage {
/// get aot_code according to special VM version
#[cfg(has_asm)]
pub(crate) fn get_aot_code(&self, code_hash: &H256, vm_version: u32) -> Option<&AotCode> {
log::debug!(
"get_aot_code hash: {} version: {}",
hex::encode(code_hash.as_slice()),
vm_version
);
match vm_version {
0 => self.aot_codes.0.get(code_hash),
1 => self.aot_codes.1.get(code_hash),
Expand Down
2 changes: 1 addition & 1 deletion crates/generator/src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ impl Generator {
#[cfg(has_asm)]
let aot_code_opt = self
.backend_manage
.get_aot_code(&backend.validator_script_type_hash, global_vm_version);
.get_aot_code(&backend.checksum.generator, global_vm_version);
#[cfg(has_asm)]
if aot_code_opt.is_none() {
log::warn!("[machine_run] Not AOT mode!");
Expand Down
24 changes: 6 additions & 18 deletions crates/rpc-server/src/registry.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use anyhow::{anyhow, Result};
use async_trait::async_trait;
use ckb_types::prelude::{Builder, Entity};
use gw_common::{blake2b::new_blake2b, state::State, H256};
use gw_common::{state::State, H256};
use gw_config::{
ChainConfig, ConsensusConfig, FeeConfig, MemPoolConfig, NodeMode, RPCMethods, RPCRateLimit,
RPCServerConfig,
Expand Down Expand Up @@ -1365,23 +1365,11 @@ fn get_backend_info(generator: Arc<Generator>) -> Vec<BackendInfo> {
.expect("backends")
.1
.values()
.map(|b| {
let mut validator_code_hash = [0u8; 32];
let mut hasher = new_blake2b();
hasher.update(&b.validator);
hasher.finalize(&mut validator_code_hash);
let mut generator_code_hash = [0u8; 32];
let mut hasher = new_blake2b();
hasher.update(&b.generator);
hasher.finalize(&mut generator_code_hash);
BackendInfo {
validator_code_hash: validator_code_hash.into(),
generator_code_hash: generator_code_hash.into(),
validator_script_type_hash: ckb_fixed_hash::H256(
b.validator_script_type_hash.into(),
),
backend_type: to_rpc_backend_type(&b.backend_type),
}
.map(|b| BackendInfo {
validator_code_hash: ckb_fixed_hash::H256(b.checksum.validator.into()),
generator_code_hash: ckb_fixed_hash::H256(b.checksum.generator.into()),
validator_script_type_hash: ckb_fixed_hash::H256(b.validator_script_type_hash.into()),
backend_type: to_rpc_backend_type(&b.backend_type),
})
.collect()
}
Expand Down