Skip to content

Commit

Permalink
updated the code according to code review notes
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander Sukhachev <[email protected]>
  • Loading branch information
alexsdsr committed Feb 16, 2024
1 parent 53953a6 commit 7903a99
Show file tree
Hide file tree
Showing 27 changed files with 427 additions and 537 deletions.
44 changes: 0 additions & 44 deletions config.json

This file was deleted.

26 changes: 0 additions & 26 deletions examples/migration/besu-config.json

This file was deleted.

103 changes: 49 additions & 54 deletions examples/migration/src/ledger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,78 +208,73 @@ struct BesuConfig {
#[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
struct BesuContracts {
did_registry: BesuContractConfig,
did_validator: BesuContractConfig,
cred_def_registry: BesuContractConfig,
schema_registry: BesuContractConfig,
credential_definition_registry: BesuContractConfig,
role_control: BesuContractConfig,
validator_control: BesuContractConfig,
account_control: BesuContractConfig,
upgrade_control: BesuContractConfig,
legacy_mapping_registry: BesuContractConfig,
ethereum_did_registry: BesuContractConfig,
}

#[derive(Serialize, Deserialize)]
struct BesuContractConfig {
#[serde(rename_all = "camelCase")]
pub struct BesuContractConfig {
address: String,
path: String,
spec_path: String,
}

fn build_contract_path(contract_path: &str) -> String {
let mut cur_dir = env::current_dir().unwrap();
cur_dir.push("../../"); // project root directory
cur_dir.push(contract_path);
fs::canonicalize(&cur_dir)
.unwrap()
.to_str()
.unwrap()
.to_string()
}

impl BesuLedger {

pub const CHAIN_ID: u64 = 1337;
pub const NODE_ADDRESS: &'static str = "http://127.0.0.1:8545";

fn build_contract_path(contract_path: &str) -> String {
let mut cur_dir = env::current_dir().unwrap();
cur_dir.push("../../"); // project root directory
cur_dir.push(contract_path);
fs::canonicalize(&cur_dir)
.unwrap()
.to_str()
.unwrap()
.to_string()
}

fn contracts() -> Vec<ContractConfig> {
let mut conf_json_path = env::current_dir().unwrap();
conf_json_path.push("../../config.json");

let json_content = fs::read_to_string(conf_json_path).expect("config file reading failed");
let conf: Value = serde_json::from_str(&json_content).expect("config file parsing failed");

let contracts_conf = conf
.get("contracts").expect("'contracts' must be in the config file")
.as_object().expect("'contracts' is expected to be an object");


let result: Vec<_> = contracts_conf
.iter()
.filter(|(contract_name,_)| {
let cn = contract_name.as_str();
let contracts = [
"did_registry", "cred_def_registry", "schema_registry",
"role_control", "legacy_mapping_registry"
];
contracts.contains(cn)
})
.map(|(_,contract_conf)| {
let addr = contract_conf
.get("address").expect("'address' not found in the contract config")
.as_str().expect("'address' must be a string");

let spec = contract_conf
.get("spec_path").expect("'spec_path' not found in the contract config")
.as_str().expect("'spec_path' must be a string");

ContractConfig {
address: String::from(addr),
spec_path: Some(build_contract_path(spec)),
spec: None,
}
}).collect();

return result;
fn contracts(contracts: &BesuContracts) -> Vec<ContractConfig> {
vec![
ContractConfig {
address: contracts.ethereum_did_registry.address.to_string(),
spec_path: Some(build_contract_path(contracts.ethereum_did_registry.spec_path.as_str())),
spec: None,
},
ContractConfig {
address: contracts.schema_registry.address.to_string(),
spec_path: Some(build_contract_path(contracts.schema_registry.spec_path.as_str())),
spec: None,
},
ContractConfig {
address: contracts.cred_def_registry.address.to_string(),
spec_path: Some(build_contract_path(contracts.cred_def_registry.spec_path.as_str())),
spec: None,
},
ContractConfig {
address: contracts.role_control.address.to_string(),
spec_path: Some(build_contract_path(contracts.role_control.spec_path.as_str())),
spec: None,
},
ContractConfig {
address: contracts.legacy_mapping_registry.address.to_string(),
spec_path: Some(build_contract_path(contracts.legacy_mapping_registry.spec_path.as_str())),
spec: None,
},
]
}

pub async fn new() -> BesuLedger {
let file = fs::File::open("besu-config.json").expect("Unable to open besu config file");
let file = fs::File::open("../../network/config.json").expect("Unable to open besu config file");
let config: BesuConfig =
serde_json::from_reader(file).expect("Unable to parse besu config file");

Expand Down
42 changes: 42 additions & 0 deletions network/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"chainId": 1337,
"nodeAddress": "http://127.0.0.1:8545",
"contracts": {
"didValidator": {
"specPath": "smart_contracts/artifacts/contracts/did/DidValidator.sol/DidValidator.json",
"address": "0x0000000000000000000000000000000000002222"
},
"credDefRegistry": {
"specPath": "smart_contracts/artifacts/contracts/cl/CredentialDefinitionRegistry.sol/CredentialDefinitionRegistry.json",
"address": "0x0000000000000000000000000000000000004444"
},
"schemaRegistry": {
"specPath": "smart_contracts/artifacts/contracts/cl/SchemaRegistry.sol/SchemaRegistry.json",
"address": "0x0000000000000000000000000000000000005555"
},
"roleControl": {
"specPath": "smart_contracts/artifacts/contracts/auth/RoleControl.sol/RoleControl.json",
"address": "0x0000000000000000000000000000000000006666"
},
"validatorControl": {
"specPath": "smart_contracts/artifacts/contracts/network/ValidatorControl.sol/ValidatorControl.json",
"address": "0x0000000000000000000000000000000000007777"
},
"accountControl": {
"specPath": "smart_contracts/artifacts/contracts/auth/AccountControl.sol/AccountControl.json",
"address": "0x0000000000000000000000000000000000008888"
},
"upgradeControl": {
"specPath": "smart_contracts/artifacts/contracts/upgrade/UpgradeControl.sol/UpgradeControl.json",
"address": "0x0000000000000000000000000000000000009999"
},
"legacyMappingRegistry": {
"specPath": "smart_contracts/artifacts/contracts/migration/LegacyMappingRegistry.sol/LegacyMappingRegistry.json",
"address": "0x0000000000000000000000000000000000019999"
},
"ethereumDidRegistry": {
"specPath": "smart_contracts/artifacts/contracts/did/EthereumExtDidRegistry.sol/EthereumExtDidRegistry.json",
"address": "0x0000000000000000000000000000000000003333"
}
}
}
33 changes: 9 additions & 24 deletions smart_contracts/demos/utils/actor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { readFileSync } from 'fs'
import { resolve } from 'path'
import { readBesuConfig } from '../../utils'
import {
RoleControl,
SchemaRegistry,
Expand All @@ -24,18 +23,19 @@ export class Actor {
}

public async init() {
const addresses = this.readContractsAddresses()
const besuConfig = readBesuConfig()
const contracts = besuConfig.contracts

this.roleControl = await new RoleControl(this.account).getInstance(addresses['role_control'])
this.validatorControl = await new ValidatorControl(this.account).getInstance(addresses['validator_control'])
this.roleControl = await new RoleControl(this.account).getInstance(contracts.roleControl.address)
this.validatorControl = await new ValidatorControl(this.account).getInstance(contracts.validatorControl.address)
this.ethereumDIDRegistry = await new EthereumExtDidRegistry(this.account).getInstance(
addresses['ethereum_did_registry'],
contracts.ethereumDidRegistry.address,
)
this.schemaRegistry = await new SchemaRegistry(this.account).getInstance(addresses['schema_registry'])
this.schemaRegistry = await new SchemaRegistry(this.account).getInstance(contracts.schemaRegistry.address)
this.credentialDefinitionRegistry = await new CredentialDefinitionRegistry(this.account).getInstance(
addresses['cred_def_registry'],
contracts.credDefRegistry.address,
)
this.upgradeControl = await new UpgradeControl(this.account).getInstance(addresses['upgrade_control'])
this.upgradeControl = await new UpgradeControl(this.account).getInstance(contracts.upgradeControl.address)
return this
}

Expand All @@ -54,19 +54,4 @@ export class Actor {
public get didDocument() {
return this.account.didDocument
}

private readContractsAddresses(): { [key: string]: string } {
const configPath = resolve('..', 'config.json')

const data = readFileSync(configPath, 'utf8')
const contracts = JSON.parse(data).contracts

const result = {}

for (const contractName of Object.keys(contracts)) {
result[contractName] = contracts[contractName].address
}

return result
}
}
Loading

0 comments on commit 7903a99

Please sign in to comment.