Skip to content

Commit c9c540f

Browse files
committed
flatbuffers: build new message definitions with flatbuffers
1 parent 8d8d11c commit c9c540f

File tree

10 files changed

+445
-10
lines changed

10 files changed

+445
-10
lines changed

Cargo.lock

Lines changed: 36 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ links = "protosol"
1313

1414
[build-dependencies]
1515
prost-build = "0.11.9"
16+
flatc-rust = "0.2.0"
1617

1718
[dependencies]
1819
prost = "0.11.9"
20+
flatbuffers = "25.9.23"

build.rs

Lines changed: 54 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
use std::{env, fs, path::{Path, PathBuf}};
1+
extern crate flatc_rust;
2+
use std::{
3+
env, fs,
4+
path::{Path, PathBuf},
5+
};
26

37
/// Normalize path only on Windows, else just return unchanged.
48
fn maybe_normalize_windows_path(path: &Path) -> PathBuf {
@@ -18,34 +22,74 @@ fn maybe_normalize_windows_path(path: &Path) -> PathBuf {
1822
}
1923
}
2024

21-
fn main() -> Result<(), Box<dyn std::error::Error>> {
22-
let proto_dir = PathBuf::from("proto");
25+
fn monitor_and_get_files(
26+
dir: &PathBuf,
27+
env_var: &str,
28+
extension: &str,
29+
) -> Result<(Vec<PathBuf>, PathBuf), Box<dyn std::error::Error>> {
2330
let abs = env::current_dir()
2431
.expect("cwd")
25-
.join(&proto_dir)
32+
.join(dir)
2633
.canonicalize()
2734
.map(|p| maybe_normalize_windows_path(&p))
28-
.expect("canonicalize proto dir");
35+
.expect("canonicalize dir");
2936

3037
println!("cargo:rerun-if-changed={}", abs.display());
31-
println!("cargo:PROTO_DIR={}", abs.display());
38+
println!("cargo:{}={}", env_var, abs.display());
3239

33-
let mut proto_files = vec![];
40+
let mut files = vec![];
3441
for entry in fs::read_dir(&abs)? {
3542
let path = entry?.path();
36-
if path.extension().and_then(|e| e.to_str()) == Some("proto") {
43+
if path.extension().and_then(|e| e.to_str()) == Some(extension) {
3744
println!("cargo:rerun-if-changed={}", path.display());
38-
proto_files.push(path);
45+
files.push(path);
3946
}
4047
}
4148

49+
Ok((files, abs))
50+
}
51+
52+
fn compile_protos() -> Result<(), Box<dyn std::error::Error>> {
53+
let proto_dir = PathBuf::from("proto");
54+
let (proto_files, abs) = monitor_and_get_files(&proto_dir, "PROTO_DIR", "proto")?;
55+
4256
let out_dir = PathBuf::from(env::var("OUT_DIR")?);
4357
let mut config = prost_build::Config::new();
4458
config.out_dir(&out_dir);
4559
config.compile_protos(
46-
&proto_files.iter().map(|p| p.display().to_string()).collect::<Vec<_>>(),
60+
&proto_files
61+
.iter()
62+
.map(|p| p.display().to_string())
63+
.collect::<Vec<_>>(),
4764
&[abs],
4865
)?;
4966

5067
Ok(())
5168
}
69+
70+
fn compile_flatbuffers() -> Result<(), Box<dyn std::error::Error>> {
71+
let flatbuffers_dir = PathBuf::from("flatbuffers");
72+
let (flatbuffers_files, abs) =
73+
monitor_and_get_files(&flatbuffers_dir, "FLATBUFFERS_DIR", "fbs")?;
74+
75+
let out_dir = PathBuf::from(env::var("OUT_DIR")?);
76+
flatc_rust::run(flatc_rust::Args {
77+
lang: "rust",
78+
inputs: flatbuffers_files
79+
.iter()
80+
.map(|p| p.as_path())
81+
.collect::<Vec<_>>()
82+
.as_slice(),
83+
out_dir: out_dir.as_path(),
84+
includes: &[abs.as_path()],
85+
..Default::default()
86+
})?;
87+
88+
Ok(())
89+
}
90+
91+
fn main() -> Result<(), Box<dyn std::error::Error>> {
92+
compile_protos()?;
93+
compile_flatbuffers()?;
94+
Ok(())
95+
}

flatbuffers/block.fbs

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// Generated from block.proto
2+
3+
include "metadata.fbs";
4+
include "context.fbs";
5+
include "txn.fbs";
6+
7+
namespace org.solana.sealevel.v2;
8+
9+
table CostTracker {
10+
block_cost:ulong;
11+
vote_cost:ulong;
12+
}
13+
14+
table Inflation {
15+
initial:double;
16+
terminal:double;
17+
taper:double;
18+
foundation:double;
19+
foundation_term:double;
20+
}
21+
22+
table FeeRateGovernor {
23+
target_lamports_per_signature:ulong;
24+
target_signatures_per_slot:ulong;
25+
min_lamports_per_signature:ulong;
26+
max_lamports_per_signature:ulong;
27+
burn_percent:ubyte;
28+
}
29+
30+
table BankFields {
31+
block_height:ulong;
32+
poh:Hash (required);
33+
parent_bank_hash:Hash (required);
34+
parent_lthash:LtHash (required);
35+
current_slot:ulong;
36+
parent_slot:ulong;
37+
prev_lps:ulong;
38+
fee_rate_governor:FeeRateGovernor (required);
39+
parent_signature_count:ulong;
40+
hashes_per_tick:ulong;
41+
ticks_per_slot:ulong;
42+
slots_per_year:double;
43+
inflation:Inflation (required);
44+
genesis_creation_time:ulong;
45+
prev_epoch_capitalization:ulong;
46+
vote_accounts_t_1:[VoteAccount] (required);
47+
vote_accounts_t_2:[VoteAccount] (required);
48+
}
49+
50+
struct LeaderScheduleHash {
51+
hash:[ubyte:16];
52+
}
53+
54+
table LeaderScheduleEffects {
55+
leaders_epoch:ulong;
56+
leaders_slot0:ulong;
57+
leaders_slot_cnt:ulong;
58+
leader_pub_cnt:ulong;
59+
leaders_sched_cnt:ulong;
60+
leader_schedule_hash:LeaderScheduleHash (required);
61+
}
62+
63+
table BlockContext {
64+
transactions:[TransactionMessage] (required);
65+
account_states:[Account] (required);
66+
blockhash_queue:[Hash] (required);
67+
bank_fields:BankFields (required);
68+
features:FeatureSet;
69+
}
70+
71+
table BlockEffects {
72+
has_err:bool;
73+
slot_capitalization:ulong;
74+
bank_hash:Hash;
75+
cost_tracker:CostTracker;
76+
leader_schedule:LeaderScheduleEffects;
77+
}
78+
79+
table BlockFixture {
80+
metadata:FixtureMetadata (required);
81+
input:BlockContext (required);
82+
output:BlockEffects (required);
83+
}
84+

flatbuffers/context.fbs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Generated from context.proto
2+
3+
namespace org.solana.sealevel.v2;
4+
5+
struct Pubkey {
6+
address:[ubyte:32];
7+
}
8+
9+
struct Signature {
10+
signature:[ubyte:64];
11+
}
12+
13+
struct Hash {
14+
hash:[ubyte:32];
15+
}
16+
17+
struct LtHash {
18+
hash:[ushort:1024];
19+
}
20+
21+
table FeatureSet {
22+
features:[ulong];
23+
}
24+
25+
table Account {
26+
address:Pubkey (required);
27+
lamports:ulong;
28+
data:[ubyte] (required);
29+
executable:bool;
30+
owner:Pubkey (required);
31+
}
32+
33+
table VoteAccount {
34+
vote_account:Account (required);
35+
stake:ulong;
36+
}

flatbuffers/elf.fbs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Generated from elf.proto
2+
3+
include "metadata.fbs";
4+
include "context.fbs";
5+
6+
namespace org.solana.sealevel.v2;
7+
8+
table ELFBinary {
9+
data:[ubyte] (required);
10+
}
11+
12+
table ELFLoaderCtx {
13+
elf:ELFBinary (required);
14+
features:FeatureSet;
15+
deploy_checks:bool;
16+
}
17+
18+
table ELFLoaderEffects {
19+
rodata:[ubyte];
20+
rodata_sz:ulong;
21+
text_cnt:ulong;
22+
text_off:ulong;
23+
entry_pc:ulong;
24+
calldests:[ulong];
25+
err_code:ubyte;
26+
}
27+
28+
table ELFLoaderFixture {
29+
metadata:FixtureMetadata (required);
30+
input:ELFLoaderCtx (required);
31+
output:ELFLoaderEffects (required);
32+
}
33+

flatbuffers/instr.fbs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Generated from invoke.proto
2+
3+
include "metadata.fbs";
4+
include "context.fbs";
5+
6+
namespace org.solana.sealevel.v2;
7+
8+
table InstrAccount {
9+
index:ubyte;
10+
is_writable:bool;
11+
is_signer:bool;
12+
}
13+
14+
table InstrContext {
15+
program_id:Pubkey (required);
16+
account_states:[Account] (required);
17+
instr_accounts:[InstrAccount] (required);
18+
instr_data:[ubyte] (required);
19+
cu_avail:ulong;
20+
features:FeatureSet;
21+
}
22+
23+
table InstrEffects {
24+
err_code:ubyte;
25+
custom_err_code:uint;
26+
modified_accounts:[Account];
27+
cu_remaining:ulong;
28+
return_data:[ubyte];
29+
}
30+
31+
table InstrFixture {
32+
metadata:FixtureMetadata (required);
33+
input:InstrContext (required);
34+
output:InstrEffects (required);
35+
}
36+

flatbuffers/metadata.fbs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Generated from metadata.proto
2+
3+
namespace org.solana.sealevel.v2;
4+
5+
table FixtureMetadata {
6+
fn_entrypoint:string (required);
7+
}
8+

0 commit comments

Comments
 (0)