Skip to content
This repository was archived by the owner on Jan 30, 2025. It is now read-only.

Commit a3900fb

Browse files
committed
write every new json, seal and journal to new directory, skip proving if seal already exists
1 parent bccbf8e commit a3900fb

File tree

10 files changed

+94
-42
lines changed

10 files changed

+94
-42
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ tests/Elf.sol
1313
cli/addresses/local
1414

1515
# Data from binance
16+
data/
1617
stripped_prices.json
1718
response.json
1819

apps/src/bin/publisher.rs

+52-24
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ use risc0_ethereum_contracts::encode_seal;
3030
use risc0_zkvm::{default_prover, ExecutorEnv, ProverOpts, VerifierContext};
3131
use url::Url;
3232
use std::fs;
33+
use std::path::Path;
3334

3435
// `IDataFeedFeeder` interface automatically generated via the alloy `sol!` macro.
3536
alloy::sol!(
@@ -56,10 +57,6 @@ struct Args {
5657
/// Application's contract address on Ethereum
5758
#[clap(long)]
5859
contract: Address,
59-
60-
/// Path to json
61-
#[clap(long)]
62-
json_path: String,
6360
}
6461

6562
fn main() -> Result<()> {
@@ -74,31 +71,62 @@ fn main() -> Result<()> {
7471
.wallet(wallet)
7572
.on_http(args.rpc_url);
7673

77-
// path to json file where information about prices is stored
78-
let json_path = args.json_path;
79-
let json_string = fs::read_to_string(json_path).expect("Unable to read file");
8074

81-
let guest_input = GuestInputType {
82-
json_string: String::from(json_string),
83-
currency_pairs: vec![String::from("ETHBTC"), String::from("BTCUSDT"), String::from("ETHUSDT"), String::from("ETHUSDC")],
84-
};
75+
let data_storage_path = Path::new("data/");
76+
77+
let mut max_number = 0;
78+
79+
if data_storage_path.is_dir() {
80+
for entry in fs::read_dir(data_storage_path)? {
81+
let entry = entry?;
82+
if let Some(folder_name) = entry.file_name().to_str() {
83+
if let Ok(number) = folder_name.parse::<u32>() {
84+
if number > max_number {
85+
max_number = number;
86+
}
87+
}
88+
}
89+
}
90+
}
91+
92+
let max_folder_path = data_storage_path.join(format!("{}", max_number));
93+
let seal_path = max_folder_path.join("seal.bin");
94+
let journal_path = max_folder_path.join("journal.bin");
95+
let json_path = max_folder_path.join("prover_input.json");
96+
97+
let already_proven = seal_path.exists() && journal_path.exists();
98+
99+
if !already_proven {
100+
let json_string = fs::read_to_string(json_path).expect("Unable to read file");
101+
102+
let guest_input = GuestInputType {
103+
json_string: String::from(json_string),
104+
currency_pairs: vec![String::from("ETHBTC"), String::from("BTCUSDT"), String::from("ETHUSDT"), String::from("ETHUSDC")],
105+
};
85106

86-
let env = ExecutorEnv::builder().write(&guest_input).unwrap().build()?;
107+
let env = ExecutorEnv::builder().write(&guest_input).unwrap().build()?;
87108

88-
let receipt = default_prover()
89-
.prove_with_ctx(
90-
env,
91-
&VerifierContext::default(),
92-
JSON_PARSER_ELF,
93-
&ProverOpts::groth16(),
94-
)?
95-
.receipt;
109+
let receipt = default_prover()
110+
.prove_with_ctx(
111+
env,
112+
&VerifierContext::default(),
113+
JSON_PARSER_ELF,
114+
&ProverOpts::groth16(),
115+
)?
116+
.receipt;
96117

97-
// Encode the seal with the selector.
98-
let seal = encode_seal(&receipt)?;
118+
// Encode the seal with the selector.
119+
let seal = encode_seal(&receipt)?;
120+
121+
// Extract the journal from the receipt.
122+
let journal = receipt.journal.bytes.clone();
123+
124+
fs::write(seal_path.clone(), &seal)?;
125+
fs::write(journal_path.clone(), &journal)?;
126+
}
99127

100-
// Extract the journal from the receipt.
101-
let journal = receipt.journal.bytes.clone();
128+
let seal = fs::read(seal_path.clone())?;
129+
let journal = fs::read(journal_path.clone())?;
102130

103131
// Decode Journal: Upon receiving the proof, the application decodes the journal to extract
104132
// the verified numbers. This ensures that the numbers being submitted to the blockchain match

cli/feed_feeder.py

+33-18
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,40 @@
33
from utils.network import *
44

55

6-
def prepare_json (_binance_flag, _test_set_nr, binance_json_file):
7-
if ((not _binance_flag) and (_test_set_nr is None)) == True:
8-
print("Use ether the --binance or --test-set-nr=1 or --test-set-nr=2 flag")
9-
sys.exit(1)
6+
def prepare_json (_binance_flag, test_data_1, test_data_2):
7+
8+
latest_data = -1
9+
10+
os.makedirs("data/", exist_ok=True)
11+
12+
existing_data = os.listdir("data/")
13+
for d in existing_data:
14+
if (d.isdigit() == True):
15+
latest_data = max(latest_data, int(d))
1016

11-
test_input_file = "test_inputs/0" + str(_test_set_nr) + ".json"
17+
new_data_dir = "data/" + str(latest_data + 1) + "/"
18+
os.makedirs(new_data_dir)
1219

13-
if _binance_flag:
20+
files = ["prover_input.json", "seal.bin", "journal.bin"]
21+
22+
if test_data_1 == True:
23+
for f in files:
24+
run_subprocess(["cp", "test_data_1/0/" + f, new_data_dir + f], "copy" + " test_data_1/0/" + f + " to" + new_data_dir + f)
25+
26+
elif test_data_2 == True:
27+
for f in files:
28+
run_subprocess(["cp", "test_data_2/0/" + f, new_data_dir + f], "copy" + " test_data_2/0/" + f + " to " + new_data_dir + f)
29+
30+
elif _binance_flag == True:
1431
run_subprocess(["python3", "data-provider/script.py"], "request from binance")
32+
run_subprocess(["cp", "stripped_prices.json", new_data_dir + "prover_input.json"], "copy binance data to " + new_data_dir)
1533

16-
if _test_set_nr is not None:
17-
if not os.path.isfile(test_input_file):
18-
print(test_input_file, "file does not exist")
19-
sys.exit(1)
20-
run_subprocess(["cp", test_input_file, binance_json_file], "copy test input to prover input dir")
34+
else:
35+
print("expected either --test-data-1 or --test-data-2 or --binance flag")
36+
sys.exit(1)
2137

2238

23-
def feed_data(net, binance_json_file):
39+
def feed_data(net):
2440
command = [
2541
"cargo",
2642
"run",
@@ -29,8 +45,7 @@ def feed_data(net, binance_json_file):
2945
"--",
3046
chain_id(net),
3147
rpc_url(net),
32-
"--contract=" + get_feeder_address(net.value),
33-
"--json-path=" + binance_json_file
48+
"--contract=" + get_feeder_address(net.value)
3449
]
3550

3651
run_subprocess(command, "DataFeeder feeding")
@@ -41,12 +56,12 @@ def feed_data(net, binance_json_file):
4156

4257
data_source_group = parser.add_mutually_exclusive_group()
4358
data_source_group.add_argument('--binance', action='store_true', help='Request data from binance and feed')
44-
data_source_group.add_argument('-nr', '--test-set-nr', action='store', type=int, help='Take dataset from test_inputs/, possible values: 1,2')
59+
data_source_group.add_argument('--test-data-1', action='store_true', help='Take dataset from test_data_1/')
60+
data_source_group.add_argument('--test-data-2', action='store_true', help='Take dataset from test_data_2')
4561

4662
args = parser.parse_args()
4763

4864

49-
binance_json_file = "stripped_prices.json"
50-
prepare_json(args.binance, args.test_set_nr, binance_json_file)
51-
feed_data(args.network, binance_json_file)
65+
prepare_json(args.binance, args.test_data_1, args.test_data_2)
66+
feed_data(args.network)
5267

cli/utils/network.py

+5
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ def parse_pairname(value):
2020
class network_enum(enum.Enum):
2121
LOCAL = "local"
2222
SEPOLIA = "sepolia"
23+
NEON_DEVNET = "neon_devnet"
2324
ETH_MAINNET = "eth_mainnet"
2425

2526
def parse_network(value):
@@ -62,6 +63,8 @@ def chain_id(net):
6263
return "--chain-id=31337"
6364
case network_enum.SEPOLIA:
6465
return "--chain-id=11155111"
66+
case network_enum.NEON_DEVNET:
67+
return "--chain-id=245022926"
6568
case network_enum.ETH_MAINNET:
6669
return "--chain-id=1"
6770

@@ -74,5 +77,7 @@ def rpc_url(net):
7477
return "--rpc-url=http://localhost:8545"
7578
case network_enum.SEPOLIA:
7679
return "--rpc-url=https://eth-sepolia.g.alchemy.com/v2/" + alchemy_api_key
80+
case network_enum.NEON_DEVNET:
81+
return "--rpc-url=https://devnet.neonevm.org"
7782
case network_enum.ETH_MAINNET:
7883
return "--rpc-url=https://eth-mainnet.g.alchemy.com/v2/" + alchemy_api_key

test_data_1/0/journal.bin

800 Bytes
Binary file not shown.
File renamed without changes.

test_data_1/0/seal.bin

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
P�iu��E�H� D�-u@:�i��@�(1u��]�*ٸ���~�� ��i2�j.c�B���3��f���&4\�
2+
P%�{G�+�3�b�H��01j3��� Ǚާ7
3+
���E�vW,�_�6�k��We��%E'�?0 Ľ��*�����ڝ6�72r�[�?����/��C�˥b�fncN�i�+-��Sl ���#͞����� mB�Ї54 � �;7�{��'�v>h4c�ᯗm�E���Z2\]�-

test_data_2/0/journal.bin

800 Bytes
Binary file not shown.
File renamed without changes.

test_data_2/0/seal.bin

260 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)