Skip to content

Commit

Permalink
Finalized validator db sync.
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre-André Long committed Jul 7, 2024
1 parent 5813862 commit b07a268
Show file tree
Hide file tree
Showing 26 changed files with 1,884 additions and 109 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ For a program to custody private data, it must import **`data_custody_protocol.a

#### Multiple Custody steps

In case **Custody** step was called more than once for a single `request_id`:
In case **Custody** step was called more than once for a single `custody_hash`:

- Between step 3 and step 4, validator bots must call `dcp_core_protocol.aleo/join_shares_as_validator` as many time as there are additional **Custody** step.

Expand Down
4 changes: 4 additions & 0 deletions development/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ cd ./programs/dcp_reconstruct_secret_offchain
leo build
cd ../..

cd ./programs/dcp_hash_custody_offchain
leo build
cd ../../../..

cd examples/nft_marketplace/programs/arc721_example
leo build
cd ../../../..
Expand Down
2 changes: 1 addition & 1 deletion development/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ nft_view_record_plaintext=$(
--ciphertext $nft_view_record_ciphertext
);

withdraw



withdraw_nft(
Expand Down
5 changes: 5 additions & 0 deletions programs/dcp_hash_custody_offchain/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.env
*.avm
*.prover
*.verifier
outputs/
15 changes: 15 additions & 0 deletions programs/dcp_hash_custody_offchain/build/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# dcp_hash_custody_offchain.aleo

## Build Guide

To compile this Aleo program, run:

```bash
snarkvm build
```

To execute this Aleo program, run:

```bash
snarkvm run hello
```
12 changes: 12 additions & 0 deletions programs/dcp_hash_custody_offchain/build/main.aleo
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
program dcp_hash_custody_offchain.aleo;

struct Custody:
origin as address;
custody_key as field;
threshold as u8;


function hash_custody:
input r0 as Custody.private;
hash.bhp256 r0 into r1 as field;
output r1 as field.private;
6 changes: 6 additions & 0 deletions programs/dcp_hash_custody_offchain/build/program.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"program": "dcp_hash_custody_offchain.aleo",
"version": "0.0.0",
"description": "",
"license": "MIT"
}
7 changes: 7 additions & 0 deletions programs/dcp_hash_custody_offchain/program.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"program": "dcp_hash_custody_offchain.aleo",
"version": "0.1.0",
"description": "",
"license": "MIT",
"dependencies": null
}
50 changes: 50 additions & 0 deletions programs/dcp_hash_custody_offchain/src/main.leo
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@


program dcp_hash_custody_offchain.aleo {
struct Custody {
origin: address,
custody_key: field,
threshold: u8,
}

transition hash_custody(custody: Custody) -> field {
return BHP256::hash_to_field(custody);
}
}


/*
import { Account, ProgramManager } from '@aleohq/sdk';
const programManager = new ProgramManager();
const account = new Account();
programManager.setAccount(account);
const secret_santa_v001_program = `INSERT SOURCE FOR import secret_santa_v001.aleo HERE`;
function add_gift_tag_bulk_inputs(pairs){
return (
"["
+ pairs.map(
([addr1, addr2]) => `[${addr1}, ${addr2}]`
).join(", ")
+ "]"
);
}
async function add_gift_tags(pairs) {
const program = generate_add_gift_tag_function(pairs.length);
const inputs = add_gift_tag_bulk_inputs(pairs);
const executionResponse = await programManager.executeOffline(
program,
"add_gift_tag_bulk",
inputs,
false,
{
"secret_santa_v001.aleo": secret_santa_v001_program
}
);
const result = executionResponse.getOutputs();
return result;
}
*/
6 changes: 6 additions & 0 deletions validators/run-rpc/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Run RPC

Either run this RPC or call Leo Wallet's RPC.

- Necessary to run this RPC to test/dev on Devnet.
- This RPC necessists running Haruka's explorer and exposing the corresponding Postegres database
20 changes: 20 additions & 0 deletions validators/run-rpc/db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import pg from 'pg';

const { Pool, Client } = pg;

import * as dotenv from 'dotenv';
dotenv.config();


export const pg_client = new Client({
user: process.env.POSTGRES_USER,
password: process.env.POSTGRES_PASSWORD,
host: process.env.POSTGRES_HOST,
port: process.env.POSTGRES_PORT,
database: process.env.POSTGRES_DB,
search_path: process.env.POSTGRES_SCHEMA
})

await pg_client.connect();

await pg_client.query(`SET search_path TO '${process.env.POSTGRES_SCHEMA}';`);
193 changes: 193 additions & 0 deletions validators/run-rpc/methods.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@

import { pg_client } from "./db.js";


export const chainStatus = async () => {
return {
"online": true,
"statusTitle": "Everything is working as expected",
"statusMessage": "There may be some temporary issues with the blockchain, but everything should be working as expected.",
"time": Date.now()
}
}

export const aleoTransactionsForProgram = async (params) => {
const {
programId,
functionName,
page,
maxTransactions,
} = params;
const sql_query = `
WITH transition_inputs AS (
SELECT
ts.id AS transition_id,
jsonb_agg(i) AS inputs
FROM transition ts
JOIN LATERAL get_transition_inputs(ts.id) i ON true
GROUP BY ts.id
),
transition_outputs AS (
SELECT
ts.id AS transition_id,
jsonb_agg(o) AS outputs
FROM transition ts
JOIN LATERAL get_transition_outputs(ts.id) o ON true
GROUP BY ts.id
),
transitions AS (
SELECT
ts.transition_id as id,
ts.transaction_execute_id,
ts.function_name as function,
ts.program_id as program,
tsi.inputs,
tso.outputs
FROM transaction_execute te
JOIN transition ts ON te.id = ts.transaction_execute_id
LEFT JOIN transition_inputs tsi ON ts.id = tsi.transition_id
LEFT JOIN transition_outputs tso ON ts.id = tso.transition_id
)
SELECT
b.height,
b.timestamp as finalizedAt,
ct.type,
t.id as index,
jsonb_build_object(
'type', ct.type,
'id', t.original_transaction_id,
'execution', jsonb_build_object(
'transitions', jsonb_agg(ots),
'global_state_root', te.global_state_root,
'proof', te.proof
)
) as transaction
FROM transaction t
JOIN transaction_execute te on te.transaction_id = t.id
JOIN confirmed_transaction ct on t.confirmed_transaction_id = ct.id
JOIN transition ts on te.id = ts.transaction_execute_id
JOIN transitions ots on te.id = ots.transaction_execute_id
JOIN block b on ct.block_id = b.id
WHERE ts.program_id = '${programId}'
AND ts.function_name = '${functionName}'
group by t.id, b.height, b.timestamp, te.global_state_root, te.proof, ct.type
ORDER BY b.height
LIMIT ${maxTransactions} OFFSET ${page}*${maxTransactions};
`;
const query_res = await pg_client.query(sql_query);
return query_res.rows ? query_res.rows.map(reformat_aleo_transaction) : [];
}

const reformat_aleo_transaction = (raw_db_tx) => {
const [status, type] = extractFirstTwoWords(raw_db_tx.type);
raw_db_tx.status = status;
raw_db_tx.type = type;
raw_db_tx.transaction.type = type;
const transitions = raw_db_tx?.transaction?.execution?.transitions;
if (raw_db_tx?.transaction?.execution == null || transitions == null) {
return raw_db_tx;
}
raw_db_tx.transaction.execution.transitions
= raw_db_tx.transaction.execution.transitions.map(reformat_aleo_transition);
return raw_db_tx;
}


const reformat_aleo_transition = (raw_db_ts) => {
raw_db_ts.inputs = raw_db_ts.inputs || [];
raw_db_ts.outputs = raw_db_ts.outputs || [];

const inputs = raw_db_ts.inputs.map(reformat_aleo_input);
const outputs = raw_db_ts.outputs.map(reformat_aleo_output);

return {
...raw_db_ts,
inputs,
outputs
}
}


const reformat_aleo_input = (raw_db_input) => {
if (raw_db_input.type == 'Private') {
return {
type: 'private',
id: raw_db_input.ciphertext_hash,
value: raw_db_input.ciphertext
};
}
if (raw_db_input.type == 'Public') {
return {
type: 'public',
id: raw_db_input.plaintext_hash,
value: raw_db_input.plaintext,
};
}
if (raw_db_input.type == 'Record') {
return {
type: 'record',
id: raw_db_input.serial_number,
value: '',
tag: raw_db_input.tag
};
}
if (raw_db_input.type == 'ExternalRecord') {
return {
type: 'external_record',
id: raw_db_input.commitment,
value: ''
};
}
}

const reformat_aleo_output = (raw_db_output) => {
if (raw_db_output.type == 'Private') {
return {
type: 'private',
id: raw_db_output.ciphertext_hash,
value: raw_db_output.ciphertext
};
}
if (raw_db_output.type == 'Public') {
return {
type: 'public',
id: raw_db_output.plaintext_hash,
value: raw_db_output.plaintext
};
}
if (raw_db_output.type == 'Record') {
return {
type: 'record',
id: raw_db_output.commitment,
checksum: raw_db_output.checksum,
value: raw_db_output.record_ciphertext
};
}
if (raw_db_output.type == 'ExternalRecord') {
return {
type: 'external_record',
id: raw_db_output.external_record_commitment,
value: ''
};
}
if (raw_db_output.type == 'Future') {
return {
type: 'future',
id: raw_db_output.future_hash,
value: null
};
}
}


function extractFirstTwoWords(text) {
const regex = /^([A-Z]?[a-z]+)([A-Z][a-z]*|\s+[A-Za-z]+)/;
const match = text.match(regex);

if (match) {
// Combine the first and second captured groups, trim any extra spaces.
return [match[1].toLowerCase(), match[2].trim().toLowerCase()];
}
return ['', ''];
}
Loading

0 comments on commit b07a268

Please sign in to comment.