Skip to content
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

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 50 additions & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,15 @@ type QueryRoot {
"""
epoch(id: Int!): Epoch
"""
List all validators
"""
validators( first: Int = 10,
"""
Cursor for pagination
"""
after: String
): ValidatorConnection!
"""
Get the epoch by id/ed25519
"""
validator(id: Int, ed25519: String): Validator
Expand Down Expand Up @@ -547,6 +556,47 @@ type Validator {
"""
after: String
): HeaderConnection!
"""
Count the total blocks number
"""
totalBlocks: Int!
"""
Count the total tickets number
"""
totalTickets: Int!
"""
Count the total epochs number
"""
totalEpochs: Int!
}

type ValidatorConnection {
"""
Information to aid in pagination.
"""
pageInfo: PageInfo!
"""
A list of edges.
"""
edges: [ValidatorEdge!]!
"""
A list of nodes.
"""
nodes: [Validator!]!
}

"""
An edge in a connection.
"""
type ValidatorEdge {
"""
The item at the end of the edge
"""
node: Validator!
"""
A cursor for use in pagination
"""
cursor: String!
}

type WorkResult {
Expand Down
80 changes: 79 additions & 1 deletion src/bin/mock-data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,83 @@ async fn main() {
}
println!("Generated mock services");

// mock work report
// mock epoch for next generate
for epoch in 10000..10020i32 {
sqlx::query!(
"INSERT INTO epochs (id, block,entropy,tickets_entropy) VALUES ($1,$2,$3,$4)",
epoch,
epoch,
"0xthisismockedepoch0000000000000000000000000000000000000000000info",
"0xthisismockedepoch00000000000000000000000000000000000000000ticket",
)
.execute(&pool)
.await
.unwrap();
}

// mock core history (vindex (8, 9), with epochs (10000 - 10020))
for vindex in 8..10i32 {
for epoch in 10000..10020i32 {
let v1 = Sha256::digest(epoch.to_be_bytes())[0];
let v2 = (v1 as f64 / 255.0) * (1.25 - 0.75) + 0.75; // (0.75; ~ 1.25)
let extrinsic_count = (epoch as f64 * v2) as i32;
sqlx::query!(
"INSERT INTO epochs_cores (epoch_id,vindex,gas_used,imports,extrinsic_count,extrinsic_size,exports,bundle_size,da_load,popularity) VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10)",
epoch,
vindex,
epoch as i64 * 10, // gas_used
epoch / 100, // imports
extrinsic_count,
extrinsic_count * 100, // extrinsic size
extrinsic_count / 100, // exports
(epoch as f32 * 1.1) as i32, // bundle_size
epoch as i64 * 1000, // da_load
epoch as i64 / 100 // popularity
).execute(&pool).await.unwrap();
}
}

// mock validator history (validators(11-20), with epochs (10000 - 10020))
for vindex in 11..21i32 {
let r = Sha256::digest(vindex.to_be_bytes());
let ed25519 = format!("0x{}", hex::encode(r));
let bandersnatch = format!(
"0x{}",
hex::encode(Sha256::digest((vindex * 10).to_be_bytes()))
);

let validator: i32 = sqlx::query_scalar!(
"INSERT INTO validators (ed25519,bandersnatch,name,details,software,ip,website,scores) VALUES ($1,$2,$3,$4,$5,$6,$7,$8) RETURNING id",
ed25519,
bandersnatch,
format!("Spacejam-{}", vindex),
"This is spacejam official node",
format!("spacejam v{}", r[0] % 3),
format!("{}.{}.{}.{}", r[0], r[1], r[2], r[3]), // ip
"https://spacejam.app",
vindex, // scores
)
.fetch_one(&pool)
.await.unwrap();

for epoch in 10000..10020i32 {
let vindex = vindex - 10;
let v1 = Sha256::digest(vindex.to_be_bytes())[0];
let blocks = ((v1 as f32 / 255.0) * 100f32) as i32; // (1 ~ 100)

sqlx::query!(
"INSERT INTO epochs_validators (epoch_id,validator_id,vindex,blocks,tickets,preimages,guarantees,assurances) VALUES ($1,$2,$3,$4,$5,$6,$7,$8)",
epoch,
validator,
vindex,
blocks, // blocks
blocks * 10, // tickets
blocks / 10, // preimages
blocks * 100, // guarantees
blocks / 20 // assurances
)
.execute(&pool)
.await.unwrap();
}
}
}
10 changes: 10 additions & 0 deletions src/models/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@ impl Header {
Ok(data)
}

/// count the validator's anchoring blocks
pub async fn count_by_author(pool: &PgPool, author: i32) -> Result<i64> {
let count = query_scalar!("SELECT COUNT(slot) FROM headers WHERE author_id=$1", author)
.fetch_one(pool)
.await?
.unwrap_or(0);

Ok(count)
}

/// NOTE: this will not being used, consider remove it.
pub async fn get(pool: &PgPool, slot: i32) -> Result<Self> {
let data = query_as!(Self, "SELECT * FROM headers WHERE slot=$1", slot)
Expand Down
Loading