Program mutable lamports (#4964)

This commit is contained in:
Jack May 2019-07-08 15:52:25 -08:00 committed by GitHub
parent 281deae102
commit 841e5e326c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 58 additions and 3 deletions

View File

@ -73,6 +73,7 @@ fn main() {
"dep_crate",
"iter",
"many_args",
"external_spend",
"noop",
"panic",
"tick_height",

View File

@ -0,0 +1,3 @@
/target/
Cargo.lock

View File

@ -0,0 +1,22 @@
# Note: This crate must be built using build.sh
[package]
name = "solana-bpf-rust-external-spend"
version = "0.17.0"
description = "Solana BPF external spend program written in Rust"
authors = ["Solana Maintainers <maintainers@solana.com>"]
repository = "https://github.com/solana-labs/solana"
license = "Apache-2.0"
homepage = "https://solana.com/"
edition = "2018"
[dependencies]
solana-sdk-bpf-utils = { path = "../../../../sdk/bpf/rust/rust-utils", version = "0.17.0" }
[workspace]
members = []
[lib]
crate-type = ["cdylib"]
name = "solana_bpf_rust_external_spend"

View File

@ -0,0 +1,6 @@
[dependencies.compiler_builtins]
path = "../../../../sdk/bpf/dependencies/rust-bpf-sysroot/src/compiler-builtins"
features = ["c", "mem"]
[target.bpfel-unknown-unknown.dependencies]
alloc = { path = "../../../../sdk/bpf/dependencies/rust-bpf-sysroot/src/liballoc" }

View File

@ -0,0 +1,21 @@
//! @brief Example Rust-based BPF program that moves a lamport from one account to another
#![no_std]
#![allow(unreachable_code)]
#![allow(unused_attributes)]
extern crate solana_sdk_bpf_utils;
use solana_sdk_bpf_utils::entrypoint::*;
use solana_sdk_bpf_utils::{entrypoint, info};
entrypoint!(process_instruction);
fn process_instruction(ka: &mut [SolKeyedAccount], _info: &SolClusterInfo, _data: &[u8]) -> bool {
// account 0 is the mint and not owned by this program, any debit of its lamports
// should result in a failed program execution. Test to ensure that this debit
// is seen by the runtime and fails as expected
*ka[0].lamports -= 1;
info!("Success");
true
}

View File

@ -94,6 +94,7 @@ mod bpf {
("solana_bpf_rust_dep_crate", true),
("solana_bpf_rust_iter", true),
// ("solana_bpf_rust_many_args", true), // Issue #3099
("solana_bpf_rust_external_spend", false),
("solana_bpf_rust_noop", true),
("solana_bpf_rust_panic", false),
];
@ -110,6 +111,7 @@ mod bpf {
..
} = create_genesis_block(50);
let bank = Bank::new(&genesis_block);
// register some ticks, used by solana_bpf_rust_tick_height
for i in 0..10 {
bank.register_tick(&hash::hash(format!("hashing {}", i).as_bytes()));

View File

@ -15,7 +15,7 @@ pub struct SolKeyedAccount<'a> {
/// Public key of the account
pub is_signer: bool,
/// Number of lamports owned by this account
pub lamports: u64,
pub lamports: &'a mut u64,
/// On-chain data within this account
pub data: &'a mut [u8],
/// Program that owns this account
@ -79,7 +79,7 @@ pub unsafe fn deserialize<'a>(
offset += size_of::<SolPubkey>();
#[allow(clippy::cast_ptr_alignment)]
let lamports = *(input.add(offset) as *const u64);
let lamports = &mut *(input.add(offset) as *mut u64);
offset += size_of::<u64>();
#[allow(clippy::cast_ptr_alignment)]

View File

@ -82,7 +82,7 @@ pub fn sol_log_params(ka: &[SolKeyedAccount], data: &[u8]) {
sol_log("- Key");
sol_log_key(&k.key);
sol_log("- Lamports");
sol_log_64(0, 0, 0, 0, k.lamports);
sol_log_64(0, 0, 0, 0, *k.lamports);
sol_log("- AccountData");
sol_log_slice(k.data);
sol_log("- Owner");