Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Xearty authored and PetarKirov committed Mar 1, 2024
1 parent 5b59de6 commit 97d1fa6
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 53 deletions.
2 changes: 1 addition & 1 deletion beacon-light-client/plonky2/circuits/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ num = { version = "0.4.0", default-features = false }
log = "0.4.14"
redis-work-queue = "0.1.3"
futures-lite = "1"
redis = { version = "0.23", features = ["json", "aio", "tokio-comp"] }
redis = { version = "0.23", features = ["aio", "tokio-comp"] }
typenum = "1.16.0"

2 changes: 0 additions & 2 deletions beacon-light-client/plonky2/circuits_executables/Cargo.lock

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

Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ edition = "2021"
circuits = { path = "../circuits" }
num = { version = "0.4.0", features = ["serde"] }
plonky2 = { git = "https://github.com/metacraft-labs/plonky2" }
redis = { version = "0.23", features = ["json", "aio", "tokio-comp"] }
redis = { version = "0.23", features = ["aio", "tokio-comp"] }
serde = "1.0.164"
serde_json = "1.0.96"
anyhow = "1.0.71"
Expand Down
54 changes: 30 additions & 24 deletions beacon-light-client/plonky2/circuits_executables/src/crud/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use plonky2::{
circuit_data::CircuitData, config::PoseidonGoldilocksConfig, proof::ProofWithPublicInputs,
},
};
use redis::{aio::Connection, AsyncCommands, JsonAsyncCommands};
use redis::{aio::Connection, AsyncCommands};
use serde::{de::DeserializeOwned, Deserialize, Deserializer, Serialize, Serializer};

use super::proof_storage::proof_storage::ProofStorage;
Expand Down Expand Up @@ -180,12 +180,11 @@ pub async fn fetch_validator_balance_input(
}

pub async fn fetch_final_layer_input(con: &mut Connection) -> Result<FinalCircuitInput> {
let result: String = con
.json_get(VALIDATOR_COMMITMENT_CONSTANTS.final_proof_input_key, "$")
let json: String = con
.get(VALIDATOR_COMMITMENT_CONSTANTS.final_proof_input_key)
.await?;
let result_vec = &serde_json::from_str::<Vec<FinalCircuitInput>>(&result)?;
ensure!(!result_vec.is_empty(), "Could not fetch json object");
Ok(result_vec[0].clone())
let result = serde_json::from_str::<FinalCircuitInput>(&json)?;
Ok(result)
}

pub async fn save_balance_proof(
Expand Down Expand Up @@ -214,16 +213,16 @@ pub async fn save_balance_proof(
.set_proof(proof_index, &proof.to_bytes())
.await?;

con.json_set(
format!(
save_json_object(
con,
&format!(
"{}:{}:{}",
VALIDATOR_COMMITMENT_CONSTANTS
.balance_verification_proof_key
.to_owned(),
level,
index
),
"$",
&balance_proof,
)
.await?;
Expand All @@ -243,11 +242,9 @@ pub async fn save_final_proof(
proof: proof.to_bytes(),
};

con.json_set(
VALIDATOR_COMMITMENT_CONSTANTS
.final_layer_proof_key
.to_owned(),
"$",
save_json_object(
con,
&VALIDATOR_COMMITMENT_CONSTANTS.final_layer_proof_key,
&final_proof,
)
.await?;
Expand Down Expand Up @@ -370,15 +367,15 @@ pub async fn save_zero_validator_proof(
.set_proof(proof_index, &proof.to_bytes())
.await?;

con.json_set(
format!(
save_json_object(
con,
&format!(
"{}:zeroes:{}",
VALIDATOR_COMMITMENT_CONSTANTS
.validator_proof_key
.to_owned(),
depth,
),
"$",
&validator_proof,
)
.await?;
Expand Down Expand Up @@ -410,16 +407,16 @@ pub async fn save_validator_proof(
.set_proof(proof_index, &proof.to_bytes())
.await?;

con.json_set(
format!(
save_json_object(
con,
&format!(
"{}:{}:{}",
VALIDATOR_COMMITMENT_CONSTANTS
.validator_proof_key
.to_owned(),
gindex,
epoch
),
"$",
&validator_proof,
)
.await?;
Expand Down Expand Up @@ -454,10 +451,19 @@ pub async fn fetch_redis_json_object<T: DeserializeOwned + Clone>(
con: &mut Connection,
key: String,
) -> Result<T> {
let result: String = con.json_get(key, "$").await?;
let result_vec = &serde_json::from_str::<Vec<T>>(&result)?;
ensure!(!result_vec.is_empty(), "Could not fetch json object");
Ok(result_vec[0].clone())
let json: String = con.get(key).await?;
let result = serde_json::from_str::<T>(&json)?;
Ok(result)
}

pub async fn save_json_object<T: Serialize>(
con: &mut Connection,
key: &str,
object: &T,
) -> Result<()> {
let json = serde_json::to_string(object)?;
con.set(key, json).await?;
Ok(())
}

pub async fn fetch_proof<T: NeedsChange + KeyProvider + DeserializeOwned + Clone>(
Expand Down
3 changes: 3 additions & 0 deletions beacon-light-client/plonky2/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"strictPropertyInitialization": false
}
}
51 changes: 26 additions & 25 deletions relay/implementations/redis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
} from '../types/types';
import { RedisClientType, createClient } from 'redis';
import CONSTANTS from '../../beacon-light-client/plonky2/constants/validator_commitment_constants.json';
import { Redis as RedisClient} from 'ioredis';
import { Redis as RedisClient } from 'ioredis';
import chalk from 'chalk';

export class Redis implements IRedis {
Expand Down Expand Up @@ -88,12 +88,21 @@ export class Redis implements IRedis {
const latestEpoch = await this.getLatestEpoch(`${CONSTANTS.validatorProofKey}:${gindex}`, BigInt(epoch));
if (latestEpoch === null) {
const depth = Math.floor(Math.log2(Number(gindex) + 1));
const result = await this.client.json_get(`${CONSTANTS.validatorProofKey}:zeroes:${depth}`, hashKey) as any;
return result;
const result = await this.client.get(`${CONSTANTS.validatorProofKey}:zeroes:${depth}`);
if (result === null) {
return null;
}

return JSON.parse(result)[hashKey];

}

const key = `${CONSTANTS.validatorProofKey}:${gindex}:${latestEpoch}`;
return this.client.json_get(key, hashKey) as any;
const result = await this.client.get(key);
if (result === null) {
return null;
}
return JSON.parse(result)[hashKey];
}

async notifyAboutNewProof(): Promise<void> {
Expand Down Expand Up @@ -131,11 +140,11 @@ export class Redis implements IRedis {
let allValidators: Validator[] = new Array(keys.length);

for (const [keyBatchIndex, batchKeys] of splitIntoBatches(keys, batchSize).entries()) {
const res = await this.client.json_mget(batchKeys, '$');
const res = await this.client.mget(batchKeys);
if (res === null) {
continue;
}
const batchValidators = (res as any[]).filter((v) => v !== null).flat();
const batchValidators = (res.filter((v) => v !== null) as string[]).map((json: any) => JSON.parse(json));

for (const [index, redisValidator] of batchValidators.entries()) {
try {
Expand Down Expand Up @@ -199,10 +208,7 @@ export class Redis implements IRedis {
async isZeroBalanceEmpty() {
await this.waitForConnection();

const result = await this.client.json_get(
`${CONSTANTS.validatorBalanceInputKey}:${CONSTANTS.validatorRegistryLimit}`,
);

const result = await this.client.get(`${CONSTANTS.validatorBalanceInputKey}:${CONSTANTS.validatorRegistryLimit}`);
return result == null;
}

Expand All @@ -213,12 +219,11 @@ export class Redis implements IRedis {
await this.addToEpochLookup(`${CONSTANTS.validatorKey}:${validator.index}`, epoch);
return [
`${CONSTANTS.validatorKey}:${validator.index}:${epoch}`,
'$',
JSON.stringify(validator.data),
];
}));

await this.client.sendCommand(new RedisReJSON.Command('JSON.MSET', args));
await this.client.mset(args);
}

async saveValidatorBalancesInput(
Expand All @@ -229,12 +234,11 @@ export class Redis implements IRedis {
const args = inputsWithIndices.map(ii => {
return [
`${CONSTANTS.validatorBalanceInputKey}:${ii.index}`,
'$',
JSON.stringify(ii.input),
];
});

await this.client.sendCommand(new RedisReJSON.Command('JSON.MSET', args));
await this.client.mset(args);
}

async saveFinalProofInput(input: {
Expand All @@ -248,10 +252,9 @@ export class Redis implements IRedis {
}) {
await this.waitForConnection();

await this.client.json_set(
await this.client.set(
CONSTANTS.finalProofInputKey,
"$",
input as any
JSON.stringify(input),
);
}

Expand All @@ -266,7 +269,7 @@ export class Redis implements IRedis {
},
): Promise<void> {
await this.waitForConnection();
await this.client.json_set(`${CONSTANTS.validatorProofKey}:${gindex}:${epoch}`, "$", proof as any);
await this.client.set(`${CONSTANTS.validatorProofKey}:${gindex}:${epoch}`, JSON.stringify(proof));
}

async saveZeroValidatorProof(
Expand All @@ -279,7 +282,7 @@ export class Redis implements IRedis {
},
): Promise<void> {
await this.waitForConnection();
await this.client.json_set(`${CONSTANTS.validatorProofKey}:zeroes:${depth}`, "$", proof as any);
await this.client.set(`${CONSTANTS.validatorProofKey}:zeroes:${depth}`, JSON.stringify(proof));
}

async saveBalanceProof(
Expand All @@ -297,10 +300,9 @@ export class Redis implements IRedis {
): Promise<void> {
await this.waitForConnection();

await this.client.json_set(
await this.client.set(
`${CONSTANTS.balanceVerificationProofKey}:${level}:${index}`,
'$',
proof as any,
JSON.stringify(proof),
);
}

Expand Down Expand Up @@ -363,10 +365,9 @@ export class Redis implements IRedis {
): Promise<void> {
await this.waitForConnection();

await this.client.json_set(
await this.client.set(
`proof:${prevSlot}:${nextSlot}`,
'$',
proof as any,
JSON.stringify(proof),
);
}

Expand Down

0 comments on commit 97d1fa6

Please sign in to comment.