-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pubkey: Add a simple program comparing two pubkeys
#### Problem It can be expensive to compare two pubkeys in an on-chain program, but it hasn't been quantified. #### Summary of changes Add a Rust and Zig program to do that
- Loading branch information
Showing
12 changed files
with
166 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
[package] | ||
name = "solana-program-rosetta-pubkey" | ||
version = "1.0.0" | ||
edition = "2021" | ||
|
||
[features] | ||
test-sbf = [] | ||
|
||
[dependencies] | ||
solana-program-entrypoint = "2.1.0" | ||
solana-pubkey = "2.1.0" | ||
|
||
[dev-dependencies] | ||
solana-instruction = "2.1.0" | ||
solana-program-test = "2.1.0" | ||
solana-sdk = "2.1.0" | ||
|
||
[lib] | ||
crate-type = ["cdylib", "lib"] | ||
|
||
[lints] | ||
workspace = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
//! A program demonstrating a comparison of pubkeys | ||
#![deny(missing_docs)] | ||
#![allow(clippy::arithmetic_side_effects)] | ||
|
||
use solana_pubkey::Pubkey; | ||
|
||
/// Entrypoint for the program | ||
#[no_mangle] | ||
pub extern "C" fn entrypoint(input: *mut u8) -> u64 { | ||
unsafe { | ||
let key: &Pubkey = &*(input.add(16) as *const Pubkey); | ||
let owner: &Pubkey = &*(input.add(16 + 32) as *const Pubkey); | ||
|
||
if *key == *owner { | ||
0 | ||
} else { | ||
1 | ||
} | ||
} | ||
} | ||
#[cfg(target_os = "solana")] | ||
#[no_mangle] | ||
fn custom_panic(_info: &core::panic::PanicInfo<'_>) {} | ||
solana_program_entrypoint::custom_heap_default!(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
use { | ||
solana_instruction::{AccountMeta, Instruction}, | ||
solana_program_test::*, | ||
solana_pubkey::Pubkey, | ||
solana_sdk::{account::Account, signature::Signer, transaction::Transaction}, | ||
}; | ||
|
||
const PROGRAM_ID: Pubkey = Pubkey::from_str_const("PubkeyComp111111111111111111111111111111111"); | ||
const TEST_KEY: Pubkey = Pubkey::from_str_const("PubkeyComp111111111111111111111111111111112"); | ||
|
||
#[tokio::test] | ||
async fn correct_key() { | ||
let mut program_test = ProgramTest::new( | ||
option_env!("PROGRAM_NAME").unwrap_or("solana_program_rosetta_pubkey"), | ||
PROGRAM_ID, | ||
None, | ||
); | ||
program_test.add_account( | ||
TEST_KEY, | ||
Account { | ||
lamports: 100_000, | ||
data: vec![0], | ||
owner: TEST_KEY, | ||
..Account::default() | ||
}, | ||
); | ||
let (banks_client, payer, recent_blockhash) = program_test.start().await; | ||
|
||
let mut transaction = Transaction::new_with_payer( | ||
&[Instruction::new_with_bincode( | ||
PROGRAM_ID, | ||
&(), | ||
vec![AccountMeta::new_readonly(TEST_KEY, false)], | ||
)], | ||
Some(&payer.pubkey()), | ||
); | ||
transaction.sign(&[&payer], recent_blockhash); | ||
banks_client.process_transaction(transaction).await.unwrap(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
const std = @import("std"); | ||
const solana = @import("solana-program-sdk"); | ||
|
||
pub fn build(b: *std.Build) !void { | ||
const target = b.resolveTargetQuery(solana.sbf_target); | ||
const optimize = .ReleaseFast; | ||
const program = b.addSharedLibrary(.{ | ||
.name = "solana_program_rosetta_pubkey", | ||
.root_source_file = b.path("main.zig"), | ||
.target = target, | ||
.optimize = optimize, | ||
}); | ||
_ = solana.buildProgram(b, program, target, optimize); | ||
b.installArtifact(program); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
.{ | ||
.name = "solana-program-rosetta-pubkey-zig", | ||
// This is a [Semantic Version](https://semver.org/). | ||
// In a future version of Zig it will be used for package deduplication. | ||
.version = "0.13.0", | ||
|
||
// This field is optional. | ||
// This is currently advisory only; Zig does not yet do anything | ||
// with this value. | ||
.minimum_zig_version = "0.13.0", | ||
|
||
// This field is optional. | ||
// Each dependency must either provide a `url` and `hash`, or a `path`. | ||
// `zig build --fetch` can be used to fetch all dependencies of a package, recursively. | ||
// Once all dependencies are fetched, `zig build` no longer requires | ||
// internet connectivity. | ||
.dependencies = .{ | ||
.@"solana-program-sdk" = .{ | ||
.url = "https://github.com/joncinque/solana-program-sdk-zig/archive/refs/tags/v0.14.0.tar.gz", | ||
.hash = "1220bdfa4ea1ab6330959ce4bc40feb5b39a7f98923a266a94b69e27fd20c3526786", | ||
}, | ||
}, | ||
.paths = .{ | ||
"build.zig", | ||
"build.zig.zon", | ||
"main.zig", | ||
"../../LICENSE", | ||
"../../README.md", | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
const std = @import("std"); | ||
const PublicKey = @import("solana-program-sdk").PublicKey; | ||
|
||
export fn entrypoint(input: [*]u8) u64 { | ||
const id: *align(1) PublicKey = @ptrCast(input + 16); | ||
const owner_id: *align(1) PublicKey = @ptrCast(input + 16 + 32); | ||
if (id.equals(owner_id.*)) { | ||
return 0; | ||
} else { | ||
return 1; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.