Skip to content
Snippets Groups Projects
gtest_genesis.rs 18.9 KiB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528
// Copyright 2021 Axiom-Team
//
// This file is part of Duniter-v2S.
//
// Duniter-v2S is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, version 3 of the License.
//
// Duniter-v2S is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with Duniter-v2S. If not, see <https://www.gnu.org/licenses/>.

use common_runtime::constants::*;
use common_runtime::entities::IdtyData;
use common_runtime::*;
use gtest_runtime::{
    opaque::SessionKeys, parameters, AccountConfig, AccountId, AuthorityMembersConfig, BabeConfig,
    CertConfig, GenesisConfig, IdentityConfig, MembershipConfig, SessionConfig, SmithCertConfig,
    SmithMembershipConfig, SudoConfig, SystemConfig, TechnicalCommitteeConfig,
    UniversalDividendConfig,
};
use serde::Deserialize;
use sp_core::{blake2_256, Decode, Encode, H256};
use std::collections::{BTreeMap, HashMap};

type MembershipData = sp_membership::MembershipData<u32>;

// get values of parameters
static EXISTENTIAL_DEPOSIT: u64 = parameters::ExistentialDeposit::get();
static SMITH_MEMBERSHIP_EXPIRE_ON: u32 = parameters::SmithMembershipPeriod::get();
static SMITH_CERTS_EXPIRE_ON: u32 = parameters::SmithValidityPeriod::get();
static MIN_CERT: u32 = parameters::WotMinCertForMembership::get();
static SMITH_MIN_CERT: u32 = parameters::SmithWotMinCertForMembership::get();

// define structure of json
#[derive(Clone, Deserialize)]
struct GenesisJson {
    identities: HashMap<String, Identity>,
    smiths: HashMap<String, Smith>,
    first_ud: u64,
    first_ud_reeval: u32,
    initial_monetary_mass: u64,
    wallets: HashMap<AccountId, u64>, // u128
    sudo_key: Option<AccountId>,
    technical_committee: Vec<String>,
}

/// identities
#[derive(Clone, Deserialize)]
struct Identity {
    /// indentity index matching the order of appearance in the Ǧ1v1 blockchain
    index: u32,
    /// ss58 address in gtest network
    owner_key: AccountId,
    /// optional ss58 address in the Ğ1v1
    old_owner_key: Option<AccountId>,
    /// block at which the membership is set to expire (0 for expired members)
    membership_expire_on: u32,
    /// block at which the next cert can be emitted
    next_cert_issuable_on: u32,
    /// balance of the account of this identity
    balance: u64, // u128
    /// certs received with their expiration block
    certs_received: HashMap<String, u32>,
}

/// smith members
#[derive(Clone, Deserialize)]
struct Smith {
    /// optional pre-set session keys (at least for the smith bootstraping the blockchain)
    session_keys: Option<String>,
    /// smith certification received
    certs_received: Vec<String>,
}

// Timestamp to block number
// fn to_bn(genesis_timestamp: u64, timestamp: u64) -> u32 {
//     let duration_in_secs = timestamp.saturating_sub(genesis_timestamp);
//     (duration_in_secs / 6) as u32
// }

// copied from duniter primitives
fn validate_idty_name(idty_name: &str) -> bool {
    idty_name.len() >= 3
        && idty_name.len() <= 42 // length smaller than 42
        // all characters are alphanumeric or - or _
        && idty_name
            .chars()
            .all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '_')
}

/// ============================================================================================ ///
/// build genesis from json file
pub fn build_genesis(
    // path of genesis config
    genesis_config_path: &str,
    // wasm binary
    wasm_binary: &[u8],
    // useful to enforce Alice authority when developing
    maybe_force_authority: Option<Vec<u8>>,
) -> Result<GenesisConfig, String> {
    // preparatory steps

    // define genesis timestamp
    let genesis_timestamp: u64 =
        if let Ok(genesis_timestamp) = std::env::var("DUNITER_GENESIS_TIMESTAMP") {
            genesis_timestamp
                .parse()
                .map_err(|_| "DUNITER_GENESIS_TIMESTAMP must be a number".to_owned())?
        } else {
            use std::time::SystemTime;
            SystemTime::now()
                .duration_since(SystemTime::UNIX_EPOCH)
                .expect("SystemTime before UNIX EPOCH!")
                .as_secs()
        };
    log::info!("genesis timestamp: {}", genesis_timestamp);

    // open json genesis file
    let file = std::fs::File::open(&genesis_config_path).map_err(|e| {
        format!(
            "Error opening gen conf file `{}`: {}",
            genesis_config_path, e
        )
    })?;

    // memory map the file to avoid loading it in memory
    let bytes = unsafe {
        memmap2::Mmap::map(&file).map_err(|e| {
            format!(
                "Error mmaping gen conf file `{}`: {}",
                genesis_config_path, e
            )
        })?
    };

    // parse the json file
    let genesis_data: GenesisJson = serde_json::from_slice(&bytes)
        .map_err(|e| format!("Error parsing gen conf file: {}", e))?;

    // declare variables for building genesis
    // -------------------------------------
    // track if fatal error occured, but let processing continue
    let mut fatal = false;
    // monetary mass for double check
    let mut monetary_mass = 0u64; // u128
                                  // wallet index to generate random id
    let mut wallet_index: u32 = 0;
    // counter for online authorities at genesis
    let mut counter_online_authorities = 0;
    // track identity index
    let mut identity_index = HashMap::new();
    // counter for certifications
    let mut counter_cert = 0u32;
    // counter for smith certifications
    let mut counter_smith_cert = 0u32;
    // track inactive identities
    let mut inactive_identities = HashMap::<u32, &str>::new();

    // declare variables to fill in genesis
    // -------------------------------------
    // account inserted in genesis
    let mut accounts = BTreeMap::new();
    // members of technical committee
    let mut technical_committee_members = Vec::new();
    // memberships
    let mut memberships = BTreeMap::new();
    // identities
    let mut identities = Vec::new();
    // certifications
    let mut certs_by_receiver = BTreeMap::new();
    // initial authorities
    let mut initial_authorities = BTreeMap::new();
    // session keys
    let mut session_keys_map = BTreeMap::new();
    // smith memberships
    let mut smith_memberships = BTreeMap::new();
    // smith certifications
    let mut smith_certs_by_receiver = BTreeMap::new();

    // SIMPLE WALLETS //
    for (pubkey, balance) in &genesis_data.wallets {
        // check existential deposit
        if balance < &EXISTENTIAL_DEPOSIT {
            log::warn!("wallet {pubkey} has {balance} cǦT which is below {EXISTENTIAL_DEPOSIT}");
            fatal = true;
        }

        // double check the monetary mass
        monetary_mass += balance;

        wallet_index += 1;
        // json prevents duplicate wallets
        accounts.insert(
            pubkey.clone(),
            GenesisAccountData {
                random_id: H256(blake2_256(&(wallet_index, &pubkey).encode())),
                balance: *balance,
                is_identity: false,
            },
        );
    }

    // IDENTITIES //
    for (name, identity) in &genesis_data.identities {
        // identity name
        if !validate_idty_name(&name) {
            return Err(format!("Identity name '{}' is invalid", &name));
        }

        // check existential deposit
        if identity.balance < EXISTENTIAL_DEPOSIT {
            if identity.membership_expire_on != 0 {
                log::warn!(
                    "expired identity {name} has {} cǦT which is below {EXISTENTIAL_DEPOSIT}",
                    identity.balance
                );
                fatal = true;
            } else {
                // member identities can still be below existential deposit thanks to sufficient
                log::info!(
                    "identity {name} has {} cǦT which is below {EXISTENTIAL_DEPOSIT}",
                    identity.balance
                );
            }
        }

        // Money
        // check that wallet with same owner_key does not exist
        if accounts.get(&identity.owner_key).is_some() {
            log::warn!(
                "{name} owner_key {} already exists as a simple wallet",
                identity.owner_key
            );
            fatal = true;
        }
        // insert as an account
        accounts.insert(
            identity.owner_key.clone(),
            GenesisAccountData {
                random_id: H256(blake2_256(&(identity.index, &identity.owner_key).encode())),
                balance: identity.balance,
                is_identity: true,
            },
        );

        // double check the monetary mass
        monetary_mass += identity.balance;

        // insert identity
        // check that index does not already exist
        if let Some(other_name) = identity_index.get(&identity.index) {
            log::warn!(
                "{other_name} already has identity index {} of {name}",
                identity.index
            );
            fatal = true;
        }
        identity_index.insert(identity.index, name);

        // only add the identity if not expired
        if identity.membership_expire_on != 0 {
            identities.push(GenesisIdty {
                index: identity.index,
                name: common_runtime::IdtyName::from(name.as_str()),
                value: common_runtime::IdtyValue {
                    data: IdtyData::new(),
                    next_creatable_identity_on: identity.next_cert_issuable_on,
                    old_owner_key: match identity.old_owner_key.clone() {
                        Some(address) => Some((address, 0)), // FIXME old owner key expiration
                        None => None,
                    },
                    // old_owner_key: None,
                    owner_key: identity.owner_key.clone(),
                    // TODO remove the removable_on field of identity
                    removable_on: 0,
                    status: IdtyStatus::Validated,
                },
            });
        } else {
            inactive_identities.insert(identity.index, name);
        };

        // insert the membershup data (only if not expired)
        if identity.membership_expire_on != 0 {
            memberships.insert(
                identity.index,
                MembershipData {
                    expire_on: identity.membership_expire_on,
                },
            );
        }
    }

    // Technical Comittee //
    // NOTE : when changing owner key, the technical committee is not changed
    for name in &genesis_data.technical_committee {
        if let Some(identity) = &genesis_data.identities.get(name) {
            technical_committee_members.push(identity.owner_key.clone());
        } else {
            log::error!("Identity '{}' does not exist", name);
            fatal = true;
        }
    }

    // CERTIFICATIONS //
    for (_, identity) in &genesis_data.identities {
        let mut certs = BTreeMap::new();
        for (issuer, expire_on) in &identity.certs_received {
            if let Some(issuer) = &genesis_data.identities.get(issuer) {
                certs.insert(issuer.index, Some(expire_on.clone()));
                counter_cert += 1;
            } else {
                log::error!("Identity '{}' does not exist", issuer);
                fatal = true;
            };
        }
        certs_by_receiver.insert(identity.index, certs);
    }

    // SMITHS SUB-WOT //
    for (name, smith_data) in &genesis_data.smiths {
        // check that smith exists
        if let Some(identity) = &genesis_data.identities.get(&name.clone()) {
            // Initial authorities and session keys
            let session_keys_bytes = if let Some(declared_session_keys) = &smith_data.session_keys {
                counter_online_authorities += 1;
                // insert authority as online
                initial_authorities.insert(identity.index, (identity.owner_key.clone(), true));
                // decode session keys or force to given value
                match maybe_force_authority {
                    Some(ref bytes) => bytes.clone(),
                    None => hex::decode(&declared_session_keys[2..])
                        .map_err(|_| format!("invalid session keys for idty {}", &name))?,
                }
            } else {
                // still authority but offline
                initial_authorities.insert(identity.index, (identity.owner_key.clone(), false));
                // fake session keys
                let mut fake_bytes = Vec::with_capacity(128);
                for _ in 0..4 {
                    fake_bytes.extend_from_slice(identity.owner_key.as_ref())
                }
                fake_bytes
            };

            // insert session keys to map
            session_keys_map.insert(
                identity.owner_key.clone(),
                SessionKeys::decode(&mut &session_keys_bytes[..]).unwrap(),
            );

            // smith certifications
            let mut certs = BTreeMap::new();
            for issuer in &smith_data.certs_received {
                let issuer_index = &genesis_data
                    .identities
                    .get(issuer)
                    .ok_or(format!("Identity '{}' does not exist", issuer))?
                    .index;
                certs.insert(*issuer_index, Some(SMITH_CERTS_EXPIRE_ON));
                counter_smith_cert += 1;
            }
            smith_certs_by_receiver.insert(identity.index, certs);

            // smith memberships
            smith_memberships.insert(
                identity.index,
                MembershipData {
                    expire_on: SMITH_MEMBERSHIP_EXPIRE_ON,
                },
            );
        } else {
            log::error!("Smith '{}' does not correspond to exising identity", &name);
            fatal = true;
        }
    }

    // Verify certifications coherence (can be ignored for old users)
    for (idty_index, receiver_certs) in &certs_by_receiver {
        if receiver_certs.len() < MIN_CERT as usize {
            let name = identity_index.get(idty_index).unwrap();
            let identity = genesis_data.identities.get(name.clone()).unwrap();
            if identity.membership_expire_on != 0 {
                log::warn!(
                    "[{}] has received only {}/{} certifications",
                    name,
                    receiver_certs.len(),
                    MIN_CERT
                );
                fatal = true;
            }
        }
    }

    // Verify smith certifications coherence
    for (idty_index, certs) in &smith_certs_by_receiver {
        if certs.len() < SMITH_MIN_CERT as usize {
            log::warn!(
                "[{}] has received only {}/{} smith certifications",
                identity_index.get(idty_index).unwrap(),
                certs.len(),
                SMITH_MIN_CERT
            );
            fatal = true;
        }
    }

    // check number of online authorities
    if counter_online_authorities != 1 {
        log::error!("one and only one smith must be online, not {counter_online_authorities}");
    }

    // check monetary mass
    if monetary_mass != genesis_data.initial_monetary_mass {
        log::warn!(
            "actuel monetary_mass ({monetary_mass}) and initial_monetary_mass ({}) do not match",
            genesis_data.initial_monetary_mass
        );
        fatal = true;
    }

    // give genesis info
    log::info!(
        "prepared genesis with:
        - {} accounts ({} identities, {} simple wallets)
        - {} total identities ({} active, {} inactive)
        - {} smiths
        - {} initial online authorities
        - {} certifications
        - {} smith certifications
        - {} members in technical committee",
        accounts.len(),
        identity_index.len(),
        &genesis_data.wallets.len(),
        identity_index.len(),
        identities.len(),
        inactive_identities.len(),
        smith_memberships.len(),
        counter_online_authorities,
        counter_cert,
        counter_smith_cert,
        technical_committee_members.len(),
    );

    // some more checks
    assert_eq!(identities.len(), memberships.len());
    assert_eq!(smith_memberships.len(), initial_authorities.len());
    assert_eq!(smith_memberships.len(), session_keys_map.len());
    assert_eq!(
        identity_index.len(),
        identities.len() + inactive_identities.len()
    );
    assert_eq!(
        accounts.len(),
        identity_index.len() + &genesis_data.wallets.len()
    );
    // no inactive tech comm
    for tech_com_member in &genesis_data.technical_committee {
        assert!(!inactive_identities.values().any(|&v| v == tech_com_member));
    }
    // no inactive smith
    for (smith, _) in &genesis_data.smiths {
        assert!(!inactive_identities.values().any(|&v| v == smith));
    }

    // check the logs to see all the fatal error preventing from starting gtest currency
    if fatal {
        log::error!("some previously logged error prevent from building a sane genesis");
        panic!();
    }

    // return genesis config
    Ok(gtest_runtime::GenesisConfig {
        system: SystemConfig {
            // Add Wasm runtime to storage.
            code: wasm_binary.to_vec(),
        },
        account: AccountConfig { accounts },
        authority_discovery: Default::default(),
        authority_members: AuthorityMembersConfig {
            initial_authorities,
        },
        balances: Default::default(),
        babe: BabeConfig {
            authorities: Vec::with_capacity(0),
            epoch_config: Some(BABE_GENESIS_EPOCH_CONFIG),
        },
        grandpa: Default::default(),
        im_online: Default::default(),
        session: SessionConfig {
            keys: session_keys_map
                .into_iter()
                .map(|(account_id, session_keys)| (account_id.clone(), account_id, session_keys))
                .collect::<Vec<_>>(),
        },
        sudo: SudoConfig {
            key: genesis_data.sudo_key,
        },
        technical_committee: TechnicalCommitteeConfig {
            members: technical_committee_members,
            ..Default::default()
        },
        identity: IdentityConfig { identities },
        cert: CertConfig {
            apply_cert_period_at_genesis: false,
            certs_by_receiver,
        },
        membership: MembershipConfig { memberships },
        smith_cert: SmithCertConfig {
            apply_cert_period_at_genesis: true,
            certs_by_receiver: smith_certs_by_receiver,
        },
        smith_membership: SmithMembershipConfig {
            memberships: smith_memberships,
        },
        universal_dividend: UniversalDividendConfig {
            first_reeval: genesis_data.first_ud_reeval,
            first_ud: genesis_data.first_ud,
            initial_monetary_mass: genesis_data.initial_monetary_mass,
        },
        treasury: Default::default(),
    })
}