feat: mark the accounts a chain names, and make genesis unmissable
Some accounts are special and nothing about the address said so. The treasury looked like any other empty account, the minting sentinel looked like a wallet, and the twenty-one accounts endowed at genesis looked like they earned it. No curated list. The chain names them itself, in three places that are three different claims and are not flattened into one badge: a runtime constant (compiled in, immutable for that spec_version), a storage value (assigned, and changeable), and a balance in block zero (history, and not a role — an account can be endowed and have no job). Labels derive from the chain's own name, so a pallet added next year is labelled without an edit. The chip is the claim; the citation beneath it is the evidence. Two rules hold it together, both learned the hard way and both now in CLAUDE.md. An account is told from a hash by registry path, never by shape — after `normalise` both are `0x` and sixty-four hex characters, so `decode_typed` now returns the account set the decoder collected while it still knew. And a storage entry names an account only if its value *is* one: `System::Events` is full of accounts and names none, and the first cut labelled half the chain's active addresses `Events`. Computing all of this walks every plain storage entry and enumerates block zero — dozens of round trips, fine once and pathological on every account page view — so it is cached for five minutes. Constants change with the runtime, state assignments almost never, genesis never. Block zero gets the endowments, because they have no extrinsic and no event and are invisible to anyone who has not read the chainspec, and a Genesis link sits in the nav to give somebody who would never think to look an unmissable way to. Mainnet started with 5,670,000 QTC across 21 accounts — one holds 5,669,940 and the other twenty got 3 each — shown beside what each holds today, so what a founding account did with its stake is one row. When genesis state cannot be read the page says so rather than showing an empty table: missing data and a chain that endowed nobody are different claims. Closes #2 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Jp6a8EDar9ueEhAxzep4V5
This commit is contained in:
@@ -194,6 +194,10 @@ pub struct ConstantDescription {
|
||||
/// which would be a runtime describing itself wrongly, worth showing as a
|
||||
/// gap rather than hiding.
|
||||
pub value: Option<serde_json::Value>,
|
||||
/// Any account ids inside that value, identified by registry path rather
|
||||
/// than by looking like one. This is how `MiningRewards::MintingAccount` is
|
||||
/// known to name an account while a same-shaped hash constant would not.
|
||||
pub accounts: Vec<String>,
|
||||
/// Documentation from the runtime source.
|
||||
pub docs: Vec<String>,
|
||||
}
|
||||
@@ -812,18 +816,7 @@ impl Runtime {
|
||||
map: &StorageMap,
|
||||
raw: &[u8],
|
||||
) -> Result<serde_json::Value, RuntimeError> {
|
||||
let mut cursor = raw;
|
||||
let value =
|
||||
scale_value::scale::decode_as_type(&mut cursor, map.value_ty, &self.metadata.types)
|
||||
.map_err(|e| RuntimeError::Decode(e.to_string()))?;
|
||||
if !cursor.is_empty() {
|
||||
return Err(RuntimeError::Decode(format!(
|
||||
"{} trailing bytes in storage value",
|
||||
cursor.len()
|
||||
)));
|
||||
}
|
||||
let mut accounts = BTreeSet::new();
|
||||
Ok(self.normalise(&value, &mut accounts))
|
||||
Ok(self.decode_typed(map.value_ty, raw)?.0)
|
||||
}
|
||||
|
||||
/// Decode a storage value against the type its entry declares.
|
||||
@@ -836,10 +829,25 @@ impl Runtime {
|
||||
target: &StorageTarget,
|
||||
raw: &[u8],
|
||||
) -> Result<serde_json::Value, RuntimeError> {
|
||||
Ok(self.decode_typed(target.value_ty, raw)?.0)
|
||||
}
|
||||
|
||||
/// Decode a value, and say which of it was an account.
|
||||
///
|
||||
/// The accounts cannot be recovered afterwards. `normalise` renders an
|
||||
/// `AccountId32` and an `H256` identically — both are `0x` and sixty-four
|
||||
/// hex characters — so telling them apart from the JSON is guesswork that
|
||||
/// wrongly claims `System::ParentHash` and `ZkTree::Root` are accounts. The
|
||||
/// registry knows, `normalise` collects them while it still does, and this
|
||||
/// is how that answer gets out.
|
||||
pub fn decode_typed(
|
||||
&self,
|
||||
ty: u32,
|
||||
raw: &[u8],
|
||||
) -> Result<(serde_json::Value, Vec<String>), RuntimeError> {
|
||||
let mut cursor = raw;
|
||||
let value =
|
||||
scale_value::scale::decode_as_type(&mut cursor, target.value_ty, &self.metadata.types)
|
||||
.map_err(|e| RuntimeError::Decode(e.to_string()))?;
|
||||
let value = scale_value::scale::decode_as_type(&mut cursor, ty, &self.metadata.types)
|
||||
.map_err(|e| RuntimeError::Decode(e.to_string()))?;
|
||||
if !cursor.is_empty() {
|
||||
return Err(RuntimeError::Decode(format!(
|
||||
"{} trailing bytes in storage value",
|
||||
@@ -847,7 +855,13 @@ impl Runtime {
|
||||
)));
|
||||
}
|
||||
let mut accounts = BTreeSet::new();
|
||||
Ok(self.normalise(&value, &mut accounts))
|
||||
let rendered = self.normalise(&value, &mut accounts);
|
||||
Ok((rendered, accounts.into_iter().collect()))
|
||||
}
|
||||
|
||||
/// A storage entry's value type, for decoding a raw read.
|
||||
pub fn storage_value_ty(&self, target: &StorageTarget) -> u32 {
|
||||
target.value_ty
|
||||
}
|
||||
|
||||
/// Every plain storage entry, which is everything readable without a key.
|
||||
@@ -898,17 +912,21 @@ impl Runtime {
|
||||
constants: pallet
|
||||
.constants
|
||||
.iter()
|
||||
.map(|c| ConstantDescription {
|
||||
name: c.name.clone(),
|
||||
type_name: self.type_name(c.ty.id),
|
||||
// The value as the runtime compiled it in, decoded
|
||||
// against its own type. This is the half a reader
|
||||
// actually came for: `ExistentialDeposit` is a number
|
||||
// somebody chose, and reading it out of the chain beats
|
||||
// reading it out of a source tree that may not be the
|
||||
// one this runtime was built from.
|
||||
value: self.decode_constant(c.ty.id, &c.value),
|
||||
docs: c.docs.clone(),
|
||||
.map(|c| {
|
||||
let decoded = self.decode_constant(c.ty.id, &c.value);
|
||||
ConstantDescription {
|
||||
name: c.name.clone(),
|
||||
type_name: self.type_name(c.ty.id),
|
||||
// The value as the runtime compiled it in, decoded
|
||||
// against its own type. This is the half a reader
|
||||
// actually came for: `ExistentialDeposit` is a number
|
||||
// somebody chose, and reading it out of the chain beats
|
||||
// reading it out of a source tree that may not be the
|
||||
// one this runtime was built from.
|
||||
value: decoded.as_ref().map(|(v, _)| v.clone()),
|
||||
accounts: decoded.map(|(_, a)| a).unwrap_or_default(),
|
||||
docs: c.docs.clone(),
|
||||
}
|
||||
})
|
||||
.collect(),
|
||||
storage: pallet
|
||||
@@ -1017,15 +1035,8 @@ impl Runtime {
|
||||
}
|
||||
|
||||
/// A constant's compiled-in value, rendered the same way event fields are.
|
||||
fn decode_constant(&self, ty: u32, bytes: &[u8]) -> Option<serde_json::Value> {
|
||||
let mut cursor = bytes;
|
||||
let value =
|
||||
scale_value::scale::decode_as_type(&mut cursor, ty, &self.metadata.types).ok()?;
|
||||
if !cursor.is_empty() {
|
||||
return None;
|
||||
}
|
||||
let mut accounts = BTreeSet::new();
|
||||
Some(self.normalise(&value, &mut accounts))
|
||||
fn decode_constant(&self, ty: u32, bytes: &[u8]) -> Option<(serde_json::Value, Vec<String>)> {
|
||||
self.decode_typed(ty, bytes).ok()
|
||||
}
|
||||
|
||||
/// A readable name for a type id.
|
||||
@@ -1757,6 +1768,38 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
/// The distinction the whole "special accounts" idea rests on. After
|
||||
/// `normalise`, an `AccountId32` and an `H256` are both `0x` and
|
||||
/// sixty-four hex characters — telling them apart by shape wrongly claims
|
||||
/// `System::ParentHash` names an account. The registry knows; this is how
|
||||
/// the answer gets out.
|
||||
#[test]
|
||||
fn an_account_is_distinguished_from_a_hash_by_its_type() {
|
||||
let rt = Runtime::from_metadata(&metadata()).expect("parses");
|
||||
let d = rt.describe();
|
||||
|
||||
let minting = d
|
||||
.pallets
|
||||
.iter()
|
||||
.find(|p| p.name == "MiningRewards")
|
||||
.and_then(|p| p.constants.iter().find(|c| c.name == "MintingAccount"))
|
||||
.expect("declared");
|
||||
assert_eq!(minting.accounts.len(), 1, "{:?}", minting.accounts);
|
||||
assert_eq!(
|
||||
minting.accounts[0].as_str(),
|
||||
minting.value.as_ref().and_then(|v| v.as_str()).unwrap()
|
||||
);
|
||||
|
||||
// A constant that is a number, not an account.
|
||||
let target = d
|
||||
.pallets
|
||||
.iter()
|
||||
.find(|p| p.name == "QPoW")
|
||||
.and_then(|p| p.constants.iter().find(|c| c.name == "TargetBlockTime"))
|
||||
.expect("declared");
|
||||
assert!(target.accounts.is_empty(), "{:?}", target.accounts);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_plain_entry_is_not_a_map() {
|
||||
let rt = Runtime::from_metadata(&metadata()).expect("parses");
|
||||
|
||||
Reference in New Issue
Block a user