Witness account data in Budget (#4650)

* Add support for contracts based on account data to Budget

* Add program_id to account constraints

* No longer require a signature for the account data witness

* Rename bank::store to store_account

* fmt

* Add a doc

* clippy
This commit is contained in:
Greg Fitzgerald
2019-06-13 18:20:28 -07:00
committed by GitHub
parent b1ac8f933b
commit c6b4a3a706
4 changed files with 210 additions and 13 deletions

View File

@@ -5,6 +5,7 @@
use chrono::prelude::*;
use serde_derive::{Deserialize, Serialize};
use solana_sdk::hash::Hash;
use solana_sdk::pubkey::Pubkey;
use std::mem;
@@ -16,6 +17,9 @@ pub enum Witness {
/// A signature from Pubkey.
Signature,
/// Account snapshot.
AccountData(Hash, Pubkey),
}
/// Some amount of lamports that should be sent to the `to` `Pubkey`.
@@ -28,6 +32,23 @@ pub struct Payment {
pub to: Pubkey,
}
/// The account constraints a Condition would wait on.
/// Note: ideally this would be function that accepts an Account and returns
/// a bool, but we don't have a way to pass functions over the wire. To simulate
/// higher order programming, create your own program that includes an instruction
/// that sets account data to a boolean. Pass that account key and program_id here.
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
pub struct AccountConstraints {
/// The account holder.
pub key: Pubkey,
/// The program id that must own the account at `key`.
pub program_id: Pubkey,
/// The hash of the data in the account at `key`.
pub data_hash: Hash,
}
/// A data type representing a `Witness` that the payment plan is waiting on.
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
pub enum Condition {
@@ -36,6 +57,9 @@ pub enum Condition {
/// Wait for a `Signature` `Witness` from `Pubkey`.
Signature(Pubkey),
/// Wait for the account with the given constraints.
AccountData(AccountConstraints),
}
impl Condition {
@@ -46,6 +70,14 @@ impl Condition {
(Condition::Timestamp(dt, pubkey), Witness::Timestamp(last_time)) => {
pubkey == from && dt <= last_time
}
(
Condition::AccountData(constraints),
Witness::AccountData(actual_hash, program_id),
) => {
constraints.program_id == *program_id
&& constraints.key == *from
&& constraints.data_hash == *actual_hash
}
_ => false,
}
}
@@ -83,6 +115,24 @@ impl BudgetExpr {
)
}
/// Create a budget that pays `lamports` to `to` after witnessing account data in `account_pubkey` with the given hash.
pub fn new_payment_when_account_data(
account_pubkey: &Pubkey,
account_program_id: &Pubkey,
account_hash: Hash,
lamports: u64,
to: &Pubkey,
) -> Self {
BudgetExpr::After(
Condition::AccountData(AccountConstraints {
key: *account_pubkey,
program_id: *account_program_id,
data_hash: account_hash,
}),
Box::new(Self::new_payment(lamports, to)),
)
}
/// Create a budget that pays `lamports` to `to` after being witnessed by `witness` unless
/// canceled with a signature from `from`.
pub fn new_cancelable_authorized_payment(