2018-10-10 13:51:43 -07:00
|
|
|
//! The `pubsub` module implements a threaded subscription service on client RPC request
|
|
|
|
|
2019-05-06 07:31:50 -07:00
|
|
|
use crate::rpc_subscriptions::{Confirmations, RpcSubscriptions};
|
2018-10-10 13:51:43 -07:00
|
|
|
use bs58;
|
2019-02-04 16:41:03 -08:00
|
|
|
use jsonrpc_core::{Error, ErrorCode, Result};
|
|
|
|
use jsonrpc_derive::rpc;
|
2019-02-17 08:38:36 -08:00
|
|
|
use jsonrpc_pubsub::typed::Subscriber;
|
2019-02-17 10:01:21 -08:00
|
|
|
use jsonrpc_pubsub::{Session, SubscriptionId};
|
2018-10-25 11:13:08 -07:00
|
|
|
use solana_sdk::account::Account;
|
|
|
|
use solana_sdk::pubkey::Pubkey;
|
2019-01-21 09:59:09 -08:00
|
|
|
use solana_sdk::signature::Signature;
|
2019-04-05 19:56:17 -07:00
|
|
|
use solana_sdk::transaction;
|
2018-10-10 13:51:43 -07:00
|
|
|
use std::mem;
|
2019-02-17 12:51:12 -08:00
|
|
|
use std::sync::{atomic, Arc};
|
2018-10-10 13:51:43 -07:00
|
|
|
|
2019-04-05 11:20:22 -07:00
|
|
|
#[rpc(server)]
|
2019-02-04 16:41:03 -08:00
|
|
|
pub trait RpcSolPubSub {
|
|
|
|
type Metadata;
|
|
|
|
|
2019-03-14 09:48:27 -07:00
|
|
|
// Get notification every time account data is changed
|
2019-02-04 16:41:03 -08:00
|
|
|
// Accepts pubkey parameter as base-58 encoded string
|
|
|
|
#[pubsub(
|
|
|
|
subscription = "accountNotification",
|
|
|
|
subscribe,
|
|
|
|
name = "accountSubscribe"
|
|
|
|
)]
|
2019-05-06 07:31:50 -07:00
|
|
|
fn account_subscribe(
|
|
|
|
&self,
|
|
|
|
_: Self::Metadata,
|
|
|
|
_: Subscriber<Account>,
|
|
|
|
_: String,
|
|
|
|
_: Option<Confirmations>,
|
|
|
|
);
|
2019-02-04 16:41:03 -08:00
|
|
|
|
|
|
|
// Unsubscribe from account notification subscription.
|
|
|
|
#[pubsub(
|
|
|
|
subscription = "accountNotification",
|
|
|
|
unsubscribe,
|
|
|
|
name = "accountUnsubscribe"
|
|
|
|
)]
|
|
|
|
fn account_unsubscribe(&self, _: Option<Self::Metadata>, _: SubscriptionId) -> Result<bool>;
|
|
|
|
|
2019-03-14 09:48:27 -07:00
|
|
|
// Get notification every time account data owned by a particular program is changed
|
2019-03-06 14:31:58 -08:00
|
|
|
// Accepts pubkey parameter as base-58 encoded string
|
|
|
|
#[pubsub(
|
|
|
|
subscription = "programNotification",
|
|
|
|
subscribe,
|
|
|
|
name = "programSubscribe"
|
|
|
|
)]
|
2019-05-06 07:31:50 -07:00
|
|
|
fn program_subscribe(
|
|
|
|
&self,
|
|
|
|
_: Self::Metadata,
|
|
|
|
_: Subscriber<(String, Account)>,
|
|
|
|
_: String,
|
|
|
|
_: Option<Confirmations>,
|
|
|
|
);
|
2019-03-06 14:31:58 -08:00
|
|
|
|
|
|
|
// Unsubscribe from account notification subscription.
|
|
|
|
#[pubsub(
|
|
|
|
subscription = "programNotification",
|
|
|
|
unsubscribe,
|
|
|
|
name = "programUnsubscribe"
|
|
|
|
)]
|
|
|
|
fn program_unsubscribe(&self, _: Option<Self::Metadata>, _: SubscriptionId) -> Result<bool>;
|
|
|
|
|
2019-02-04 16:41:03 -08:00
|
|
|
// Get notification when signature is verified
|
|
|
|
// Accepts signature parameter as base-58 encoded string
|
|
|
|
#[pubsub(
|
|
|
|
subscription = "signatureNotification",
|
|
|
|
subscribe,
|
|
|
|
name = "signatureSubscribe"
|
|
|
|
)]
|
2019-04-05 19:56:17 -07:00
|
|
|
fn signature_subscribe(
|
|
|
|
&self,
|
|
|
|
_: Self::Metadata,
|
2019-05-06 07:31:50 -07:00
|
|
|
_: Subscriber<transaction::Result<()>>,
|
2019-04-05 19:56:17 -07:00
|
|
|
_: String,
|
2019-05-06 07:31:50 -07:00
|
|
|
_: Option<Confirmations>,
|
2019-04-05 19:56:17 -07:00
|
|
|
);
|
2019-02-04 16:41:03 -08:00
|
|
|
|
|
|
|
// Unsubscribe from signature notification subscription.
|
|
|
|
#[pubsub(
|
|
|
|
subscription = "signatureNotification",
|
|
|
|
unsubscribe,
|
|
|
|
name = "signatureUnsubscribe"
|
|
|
|
)]
|
|
|
|
fn signature_unsubscribe(&self, _: Option<Self::Metadata>, _: SubscriptionId) -> Result<bool>;
|
2018-10-10 13:51:43 -07:00
|
|
|
}
|
|
|
|
|
2019-02-18 16:25:17 -08:00
|
|
|
#[derive(Default)]
|
2019-02-17 09:09:46 -08:00
|
|
|
pub struct RpcSolPubSubImpl {
|
2018-10-12 11:04:14 -07:00
|
|
|
uid: Arc<atomic::AtomicUsize>,
|
2019-02-17 12:51:12 -08:00
|
|
|
subscriptions: Arc<RpcSubscriptions>,
|
2018-10-10 13:51:43 -07:00
|
|
|
}
|
2018-10-12 11:04:14 -07:00
|
|
|
|
|
|
|
impl RpcSolPubSubImpl {
|
2019-02-18 16:25:17 -08:00
|
|
|
pub fn new(subscriptions: Arc<RpcSubscriptions>) -> Self {
|
2019-02-17 12:51:12 -08:00
|
|
|
let uid = Arc::new(atomic::AtomicUsize::default());
|
2019-02-18 16:25:17 -08:00
|
|
|
Self { uid, subscriptions }
|
2019-02-17 12:51:12 -08:00
|
|
|
}
|
2019-02-17 12:24:36 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl RpcSolPubSub for RpcSolPubSubImpl {
|
|
|
|
type Metadata = Arc<Session>;
|
2018-10-10 13:51:43 -07:00
|
|
|
|
2019-02-17 12:24:36 -08:00
|
|
|
fn account_subscribe(
|
2019-02-17 09:09:46 -08:00
|
|
|
&self,
|
2019-02-17 12:24:36 -08:00
|
|
|
_meta: Self::Metadata,
|
2019-02-17 09:09:46 -08:00
|
|
|
subscriber: Subscriber<Account>,
|
|
|
|
pubkey_str: String,
|
2019-05-06 07:31:50 -07:00
|
|
|
confirmations: Option<Confirmations>,
|
2019-02-17 09:09:46 -08:00
|
|
|
) {
|
2018-10-10 13:51:43 -07:00
|
|
|
let pubkey_vec = bs58::decode(pubkey_str).into_vec().unwrap();
|
|
|
|
if pubkey_vec.len() != mem::size_of::<Pubkey>() {
|
|
|
|
subscriber
|
|
|
|
.reject(Error {
|
|
|
|
code: ErrorCode::InvalidParams,
|
|
|
|
message: "Invalid Request: Invalid pubkey provided".into(),
|
|
|
|
data: None,
|
2018-12-07 19:01:28 -08:00
|
|
|
})
|
|
|
|
.unwrap();
|
2018-10-10 13:51:43 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
let pubkey = Pubkey::new(&pubkey_vec);
|
|
|
|
|
|
|
|
let id = self.uid.fetch_add(1, atomic::Ordering::SeqCst);
|
|
|
|
let sub_id = SubscriptionId::Number(id as u64);
|
2018-10-30 11:47:14 -07:00
|
|
|
info!("account_subscribe: account={:?} id={:?}", pubkey, sub_id);
|
2018-10-10 13:51:43 -07:00
|
|
|
let sink = subscriber.assign_id(sub_id.clone()).unwrap();
|
|
|
|
|
2019-02-17 12:51:12 -08:00
|
|
|
self.subscriptions
|
2019-05-06 07:31:50 -07:00
|
|
|
.add_account_subscription(&pubkey, confirmations, &sub_id, &sink)
|
2018-10-10 13:51:43 -07:00
|
|
|
}
|
|
|
|
|
2019-02-17 12:24:36 -08:00
|
|
|
fn account_unsubscribe(
|
|
|
|
&self,
|
|
|
|
_meta: Option<Self::Metadata>,
|
|
|
|
id: SubscriptionId,
|
|
|
|
) -> Result<bool> {
|
|
|
|
info!("account_unsubscribe: id={:?}", id);
|
2019-02-17 12:51:12 -08:00
|
|
|
if self.subscriptions.remove_account_subscription(&id) {
|
2019-02-17 12:24:36 -08:00
|
|
|
Ok(true)
|
|
|
|
} else {
|
|
|
|
Err(Error {
|
|
|
|
code: ErrorCode::InvalidParams,
|
|
|
|
message: "Invalid Request: Subscription id does not exist".into(),
|
|
|
|
data: None,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-06 14:31:58 -08:00
|
|
|
fn program_subscribe(
|
|
|
|
&self,
|
|
|
|
_meta: Self::Metadata,
|
|
|
|
subscriber: Subscriber<(String, Account)>,
|
|
|
|
pubkey_str: String,
|
2019-05-06 07:31:50 -07:00
|
|
|
confirmations: Option<Confirmations>,
|
2019-03-06 14:31:58 -08:00
|
|
|
) {
|
|
|
|
let pubkey_vec = bs58::decode(pubkey_str).into_vec().unwrap();
|
|
|
|
if pubkey_vec.len() != mem::size_of::<Pubkey>() {
|
|
|
|
subscriber
|
|
|
|
.reject(Error {
|
|
|
|
code: ErrorCode::InvalidParams,
|
|
|
|
message: "Invalid Request: Invalid pubkey provided".into(),
|
|
|
|
data: None,
|
|
|
|
})
|
|
|
|
.unwrap();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let pubkey = Pubkey::new(&pubkey_vec);
|
|
|
|
|
|
|
|
let id = self.uid.fetch_add(1, atomic::Ordering::SeqCst);
|
|
|
|
let sub_id = SubscriptionId::Number(id as u64);
|
2019-03-08 15:27:54 -08:00
|
|
|
info!("program_subscribe: account={:?} id={:?}", pubkey, sub_id);
|
2019-03-06 14:31:58 -08:00
|
|
|
let sink = subscriber.assign_id(sub_id.clone()).unwrap();
|
|
|
|
|
|
|
|
self.subscriptions
|
2019-05-06 07:31:50 -07:00
|
|
|
.add_program_subscription(&pubkey, confirmations, &sub_id, &sink)
|
2019-03-06 14:31:58 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn program_unsubscribe(
|
|
|
|
&self,
|
|
|
|
_meta: Option<Self::Metadata>,
|
|
|
|
id: SubscriptionId,
|
|
|
|
) -> Result<bool> {
|
2019-03-08 15:27:54 -08:00
|
|
|
info!("program_unsubscribe: id={:?}", id);
|
2019-03-06 14:31:58 -08:00
|
|
|
if self.subscriptions.remove_program_subscription(&id) {
|
|
|
|
Ok(true)
|
|
|
|
} else {
|
|
|
|
Err(Error {
|
|
|
|
code: ErrorCode::InvalidParams,
|
|
|
|
message: "Invalid Request: Subscription id does not exist".into(),
|
|
|
|
data: None,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-17 12:24:36 -08:00
|
|
|
fn signature_subscribe(
|
2018-10-10 13:51:43 -07:00
|
|
|
&self,
|
2019-02-17 12:24:36 -08:00
|
|
|
_meta: Self::Metadata,
|
2019-05-06 07:31:50 -07:00
|
|
|
subscriber: Subscriber<transaction::Result<()>>,
|
2018-10-10 13:51:43 -07:00
|
|
|
signature_str: String,
|
2019-05-06 07:31:50 -07:00
|
|
|
confirmations: Option<Confirmations>,
|
2018-10-10 13:51:43 -07:00
|
|
|
) {
|
2018-10-12 11:04:14 -07:00
|
|
|
info!("signature_subscribe");
|
2018-10-10 13:51:43 -07:00
|
|
|
let signature_vec = bs58::decode(signature_str).into_vec().unwrap();
|
|
|
|
if signature_vec.len() != mem::size_of::<Signature>() {
|
|
|
|
subscriber
|
|
|
|
.reject(Error {
|
|
|
|
code: ErrorCode::InvalidParams,
|
|
|
|
message: "Invalid Request: Invalid signature provided".into(),
|
|
|
|
data: None,
|
2018-12-07 19:01:28 -08:00
|
|
|
})
|
|
|
|
.unwrap();
|
2018-10-10 13:51:43 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
let signature = Signature::new(&signature_vec);
|
2019-05-06 07:31:50 -07:00
|
|
|
|
2018-10-10 13:51:43 -07:00
|
|
|
let id = self.uid.fetch_add(1, atomic::Ordering::SeqCst);
|
|
|
|
let sub_id = SubscriptionId::Number(id as u64);
|
2019-05-06 07:31:50 -07:00
|
|
|
info!(
|
|
|
|
"signature_subscribe: signature={:?} id={:?}",
|
|
|
|
signature, sub_id
|
|
|
|
);
|
2018-10-10 13:51:43 -07:00
|
|
|
let sink = subscriber.assign_id(sub_id.clone()).unwrap();
|
|
|
|
|
2019-02-18 16:25:17 -08:00
|
|
|
self.subscriptions
|
2019-05-06 07:31:50 -07:00
|
|
|
.add_signature_subscription(&signature, confirmations, &sub_id, &sink);
|
2018-10-10 13:51:43 -07:00
|
|
|
}
|
|
|
|
|
2019-02-04 16:41:03 -08:00
|
|
|
fn signature_unsubscribe(
|
|
|
|
&self,
|
|
|
|
_meta: Option<Self::Metadata>,
|
|
|
|
id: SubscriptionId,
|
|
|
|
) -> Result<bool> {
|
2018-10-12 11:04:14 -07:00
|
|
|
info!("signature_unsubscribe");
|
2019-02-17 12:51:12 -08:00
|
|
|
if self.subscriptions.remove_signature_subscription(&id) {
|
2018-10-10 13:51:43 -07:00
|
|
|
Ok(true)
|
|
|
|
} else {
|
|
|
|
Err(Error {
|
|
|
|
code: ErrorCode::InvalidParams,
|
|
|
|
message: "Invalid Request: Subscription id does not exist".into(),
|
|
|
|
data: None,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2019-05-06 07:31:50 -07:00
|
|
|
use crate::bank_forks::BankForks;
|
2019-05-07 11:16:22 -07:00
|
|
|
use crate::genesis_utils::create_genesis_block;
|
2019-02-04 16:41:03 -08:00
|
|
|
use jsonrpc_core::futures::sync::mpsc;
|
|
|
|
use jsonrpc_core::Response;
|
2019-02-17 10:17:58 -08:00
|
|
|
use jsonrpc_pubsub::{PubSubHandler, Session};
|
2019-03-02 13:23:22 -08:00
|
|
|
use solana_budget_api;
|
2019-04-03 08:45:57 -07:00
|
|
|
use solana_budget_api::budget_instruction;
|
2019-04-05 09:42:54 -07:00
|
|
|
use solana_runtime::bank::Bank;
|
2019-03-01 16:39:23 -08:00
|
|
|
use solana_sdk::pubkey::Pubkey;
|
2018-12-03 10:26:28 -08:00
|
|
|
use solana_sdk::signature::{Keypair, KeypairUtil};
|
2019-05-06 07:31:50 -07:00
|
|
|
use solana_sdk::system_program;
|
2019-04-03 08:45:57 -07:00
|
|
|
use solana_sdk::system_transaction;
|
2019-04-05 09:42:54 -07:00
|
|
|
use solana_sdk::transaction::{self, Transaction};
|
2019-05-06 07:31:50 -07:00
|
|
|
use std::sync::RwLock;
|
2019-02-17 09:09:46 -08:00
|
|
|
use std::thread::sleep;
|
2019-02-17 10:17:58 -08:00
|
|
|
use std::time::Duration;
|
2018-10-10 13:51:43 -07:00
|
|
|
use tokio::prelude::{Async, Stream};
|
|
|
|
|
2019-02-18 14:21:23 -08:00
|
|
|
fn process_transaction_and_notify(
|
2019-05-06 07:31:50 -07:00
|
|
|
bank_forks: &Arc<RwLock<BankForks>>,
|
2019-02-17 15:33:25 -08:00
|
|
|
tx: &Transaction,
|
|
|
|
subscriptions: &RpcSubscriptions,
|
2019-05-06 07:31:50 -07:00
|
|
|
) -> transaction::Result<()> {
|
|
|
|
bank_forks
|
|
|
|
.write()
|
|
|
|
.unwrap()
|
|
|
|
.get(0)
|
|
|
|
.unwrap()
|
|
|
|
.process_transaction(tx)?;
|
|
|
|
subscriptions.notify_subscribers(0, &bank_forks);
|
|
|
|
Ok(())
|
2019-02-17 15:33:25 -08:00
|
|
|
}
|
|
|
|
|
2019-02-17 12:24:36 -08:00
|
|
|
fn create_session() -> Arc<Session> {
|
|
|
|
Arc::new(Session::new(mpsc::channel(1).0))
|
|
|
|
}
|
|
|
|
|
2018-10-10 13:51:43 -07:00
|
|
|
#[test]
|
|
|
|
fn test_signature_subscribe() {
|
2019-05-07 11:16:22 -07:00
|
|
|
let (genesis_block, alice) = create_genesis_block(10_000);
|
2018-10-10 13:51:43 -07:00
|
|
|
let bob = Keypair::new();
|
|
|
|
let bob_pubkey = bob.pubkey();
|
2019-01-24 12:04:04 -08:00
|
|
|
let bank = Bank::new(&genesis_block);
|
2019-05-06 07:31:50 -07:00
|
|
|
let blockhash = bank.last_blockhash();
|
|
|
|
let bank_forks = Arc::new(RwLock::new(BankForks::new(0, bank)));
|
2018-10-10 13:51:43 -07:00
|
|
|
|
2019-02-18 16:25:17 -08:00
|
|
|
let rpc = RpcSolPubSubImpl::default();
|
2018-10-10 13:51:43 -07:00
|
|
|
|
2019-02-17 12:51:12 -08:00
|
|
|
// Test signature subscriptions
|
2019-04-03 08:45:57 -07:00
|
|
|
let tx = system_transaction::transfer(&alice, &bob_pubkey, 20, blockhash, 0);
|
2018-10-10 13:51:43 -07:00
|
|
|
|
2019-02-17 12:24:36 -08:00
|
|
|
let session = create_session();
|
2019-01-28 12:52:05 -08:00
|
|
|
let (subscriber, _id_receiver, mut receiver) =
|
|
|
|
Subscriber::new_test("signatureNotification");
|
2019-05-06 07:31:50 -07:00
|
|
|
rpc.signature_subscribe(session, subscriber, tx.signatures[0].to_string(), None);
|
2018-10-10 13:51:43 -07:00
|
|
|
|
2019-05-06 07:31:50 -07:00
|
|
|
process_transaction_and_notify(&bank_forks, &tx, &rpc.subscriptions).unwrap();
|
2018-10-10 13:51:43 -07:00
|
|
|
sleep(Duration::from_millis(200));
|
|
|
|
|
|
|
|
// Test signature confirmation notification
|
|
|
|
let string = receiver.poll();
|
|
|
|
if let Async::Ready(Some(response)) = string.unwrap() {
|
2019-04-05 19:56:17 -07:00
|
|
|
let expected_res: Option<transaction::Result<()>> = Some(Ok(()));
|
|
|
|
let expected_res_str =
|
|
|
|
serde_json::to_string(&serde_json::to_value(expected_res).unwrap()).unwrap();
|
|
|
|
let expected = format!(r#"{{"jsonrpc":"2.0","method":"signatureNotification","params":{{"result":{},"subscription":0}}}}"#, expected_res_str);
|
2018-10-10 13:51:43 -07:00
|
|
|
assert_eq!(expected, response);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_signature_unsubscribe() {
|
2019-05-07 11:16:22 -07:00
|
|
|
let (genesis_block, alice) = create_genesis_block(10_000);
|
2019-03-30 20:37:33 -07:00
|
|
|
let bob_pubkey = Pubkey::new_rand();
|
2019-01-24 12:04:04 -08:00
|
|
|
let bank = Bank::new(&genesis_block);
|
2018-10-10 13:51:43 -07:00
|
|
|
let arc_bank = Arc::new(bank);
|
2019-03-02 10:25:16 -08:00
|
|
|
let blockhash = arc_bank.last_blockhash();
|
2018-10-10 13:51:43 -07:00
|
|
|
|
2019-02-17 12:24:36 -08:00
|
|
|
let session = create_session();
|
2018-10-10 13:51:43 -07:00
|
|
|
|
|
|
|
let mut io = PubSubHandler::default();
|
2019-02-18 16:25:17 -08:00
|
|
|
let rpc = RpcSolPubSubImpl::default();
|
2018-10-10 13:51:43 -07:00
|
|
|
io.extend_with(rpc.to_delegate());
|
|
|
|
|
2019-04-03 08:45:57 -07:00
|
|
|
let tx = system_transaction::transfer(&alice, &bob_pubkey, 20, blockhash, 0);
|
2018-10-10 13:51:43 -07:00
|
|
|
let req = format!(
|
|
|
|
r#"{{"jsonrpc":"2.0","id":1,"method":"signatureSubscribe","params":["{}"]}}"#,
|
2018-10-26 14:43:34 -07:00
|
|
|
tx.signatures[0].to_string()
|
2018-10-10 13:51:43 -07:00
|
|
|
);
|
2018-10-12 11:04:14 -07:00
|
|
|
let _res = io.handle_request_sync(&req, session.clone());
|
2018-10-10 13:51:43 -07:00
|
|
|
|
|
|
|
let req =
|
|
|
|
format!(r#"{{"jsonrpc":"2.0","id":1,"method":"signatureUnsubscribe","params":[0]}}"#);
|
2018-10-12 11:04:14 -07:00
|
|
|
let res = io.handle_request_sync(&req, session.clone());
|
2018-10-10 13:51:43 -07:00
|
|
|
|
|
|
|
let expected = format!(r#"{{"jsonrpc":"2.0","result":true,"id":1}}"#);
|
2019-02-17 15:33:25 -08:00
|
|
|
let expected: Response = serde_json::from_str(&expected).unwrap();
|
2018-10-10 13:51:43 -07:00
|
|
|
|
2019-02-17 15:33:25 -08:00
|
|
|
let result: Response = serde_json::from_str(&res.unwrap()).unwrap();
|
2018-10-10 13:51:43 -07:00
|
|
|
assert_eq!(expected, result);
|
|
|
|
|
|
|
|
// Test bad parameter
|
|
|
|
let req =
|
|
|
|
format!(r#"{{"jsonrpc":"2.0","id":1,"method":"signatureUnsubscribe","params":[1]}}"#);
|
2018-10-12 11:04:14 -07:00
|
|
|
let res = io.handle_request_sync(&req, session.clone());
|
2018-10-10 13:51:43 -07:00
|
|
|
let expected = format!(r#"{{"jsonrpc":"2.0","error":{{"code":-32602,"message":"Invalid Request: Subscription id does not exist"}},"id":1}}"#);
|
2019-02-17 15:33:25 -08:00
|
|
|
let expected: Response = serde_json::from_str(&expected).unwrap();
|
2018-10-10 13:51:43 -07:00
|
|
|
|
2019-02-17 15:33:25 -08:00
|
|
|
let result: Response = serde_json::from_str(&res.unwrap()).unwrap();
|
2018-10-10 13:51:43 -07:00
|
|
|
assert_eq!(expected, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_account_subscribe() {
|
2019-05-07 11:16:22 -07:00
|
|
|
let (mut genesis_block, alice) = create_genesis_block(10_000);
|
2019-03-12 11:44:41 -07:00
|
|
|
|
|
|
|
// This test depends on the budget program
|
|
|
|
genesis_block
|
2019-04-02 07:57:00 -07:00
|
|
|
.native_instruction_processors
|
2019-03-12 11:44:41 -07:00
|
|
|
.push(("solana_budget_program".to_string(), solana_budget_api::id()));
|
|
|
|
|
2019-03-30 20:37:33 -07:00
|
|
|
let bob_pubkey = Pubkey::new_rand();
|
2018-10-10 13:51:43 -07:00
|
|
|
let witness = Keypair::new();
|
|
|
|
let contract_funds = Keypair::new();
|
|
|
|
let contract_state = Keypair::new();
|
2019-03-02 13:23:22 -08:00
|
|
|
let budget_program_id = solana_budget_api::id();
|
2018-10-16 09:43:49 -07:00
|
|
|
let executable = false; // TODO
|
2019-01-24 12:04:04 -08:00
|
|
|
let bank = Bank::new(&genesis_block);
|
2019-05-06 07:31:50 -07:00
|
|
|
let blockhash = bank.last_blockhash();
|
|
|
|
let bank_forks = Arc::new(RwLock::new(BankForks::new(0, bank)));
|
2018-10-10 13:51:43 -07:00
|
|
|
|
2019-02-18 16:25:17 -08:00
|
|
|
let rpc = RpcSolPubSubImpl::default();
|
2019-02-17 12:24:36 -08:00
|
|
|
let session = create_session();
|
2019-01-28 12:52:05 -08:00
|
|
|
let (subscriber, _id_receiver, mut receiver) = Subscriber::new_test("accountNotification");
|
2019-05-06 07:31:50 -07:00
|
|
|
rpc.account_subscribe(
|
|
|
|
session,
|
|
|
|
subscriber,
|
|
|
|
contract_state.pubkey().to_string(),
|
|
|
|
None,
|
|
|
|
);
|
2018-10-10 13:51:43 -07:00
|
|
|
|
2019-04-03 08:45:57 -07:00
|
|
|
let tx = system_transaction::create_user_account(
|
|
|
|
&alice,
|
|
|
|
&contract_funds.pubkey(),
|
|
|
|
51,
|
|
|
|
blockhash,
|
|
|
|
0,
|
|
|
|
);
|
2019-05-06 07:31:50 -07:00
|
|
|
process_transaction_and_notify(&bank_forks, &tx, &rpc.subscriptions).unwrap();
|
2018-10-10 13:51:43 -07:00
|
|
|
|
2019-04-03 08:45:57 -07:00
|
|
|
let ixs = budget_instruction::when_signed(
|
2019-03-23 05:14:30 -07:00
|
|
|
&contract_funds.pubkey(),
|
2019-03-09 19:28:43 -08:00
|
|
|
&bob_pubkey,
|
|
|
|
&contract_state.pubkey(),
|
|
|
|
&witness.pubkey(),
|
2018-10-10 13:51:43 -07:00
|
|
|
None,
|
2019-03-07 13:19:43 -08:00
|
|
|
51,
|
2018-10-10 13:51:43 -07:00
|
|
|
);
|
2019-03-29 16:29:20 -07:00
|
|
|
let tx = Transaction::new_signed_instructions(&[&contract_funds], ixs, blockhash);
|
2019-05-06 07:31:50 -07:00
|
|
|
process_transaction_and_notify(&bank_forks, &tx, &rpc.subscriptions).unwrap();
|
2018-10-10 13:51:43 -07:00
|
|
|
sleep(Duration::from_millis(200));
|
|
|
|
|
2019-03-07 13:19:43 -08:00
|
|
|
// Test signature confirmation notification #1
|
2018-10-10 13:51:43 -07:00
|
|
|
let string = receiver.poll();
|
2019-05-06 07:31:50 -07:00
|
|
|
let expected_data = bank_forks
|
|
|
|
.read()
|
|
|
|
.unwrap()
|
|
|
|
.get(0)
|
|
|
|
.unwrap()
|
|
|
|
.get_account(&contract_state.pubkey())
|
|
|
|
.unwrap()
|
|
|
|
.data;
|
2018-10-10 13:51:43 -07:00
|
|
|
let expected = json!({
|
|
|
|
"jsonrpc": "2.0",
|
|
|
|
"method": "accountNotification",
|
|
|
|
"params": {
|
|
|
|
"result": {
|
2018-11-12 09:29:17 -08:00
|
|
|
"owner": budget_program_id,
|
2019-03-05 16:28:14 -08:00
|
|
|
"lamports": 51,
|
2019-03-14 09:48:27 -07:00
|
|
|
"data": expected_data,
|
2018-10-16 09:43:49 -07:00
|
|
|
"executable": executable,
|
2018-10-10 13:51:43 -07:00
|
|
|
},
|
|
|
|
"subscription": 0,
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if let Async::Ready(Some(response)) = string.unwrap() {
|
|
|
|
assert_eq!(serde_json::to_string(&expected).unwrap(), response);
|
|
|
|
}
|
|
|
|
|
2019-04-03 08:45:57 -07:00
|
|
|
let tx =
|
|
|
|
system_transaction::create_user_account(&alice, &witness.pubkey(), 1, blockhash, 0);
|
2019-05-06 07:31:50 -07:00
|
|
|
process_transaction_and_notify(&bank_forks, &tx, &rpc.subscriptions).unwrap();
|
2018-10-10 13:51:43 -07:00
|
|
|
sleep(Duration::from_millis(200));
|
2019-04-03 08:45:57 -07:00
|
|
|
let ix = budget_instruction::apply_signature(
|
2019-03-23 05:14:30 -07:00
|
|
|
&witness.pubkey(),
|
2019-03-09 19:28:43 -08:00
|
|
|
&contract_state.pubkey(),
|
|
|
|
&bob_pubkey,
|
2018-10-10 13:51:43 -07:00
|
|
|
);
|
2019-03-29 16:29:20 -07:00
|
|
|
let tx = Transaction::new_signed_instructions(&[&witness], vec![ix], blockhash);
|
2019-05-06 07:31:50 -07:00
|
|
|
process_transaction_and_notify(&bank_forks, &tx, &rpc.subscriptions).unwrap();
|
2018-10-10 13:51:43 -07:00
|
|
|
sleep(Duration::from_millis(200));
|
|
|
|
|
2019-05-06 07:31:50 -07:00
|
|
|
assert_eq!(
|
|
|
|
bank_forks
|
|
|
|
.read()
|
|
|
|
.unwrap()
|
|
|
|
.get(0)
|
|
|
|
.unwrap()
|
|
|
|
.get_account(&contract_state.pubkey()),
|
|
|
|
None
|
|
|
|
);
|
2018-10-10 13:51:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_account_unsubscribe() {
|
2019-03-30 20:37:33 -07:00
|
|
|
let bob_pubkey = Pubkey::new_rand();
|
2019-02-17 12:24:36 -08:00
|
|
|
let session = create_session();
|
2018-10-10 13:51:43 -07:00
|
|
|
|
|
|
|
let mut io = PubSubHandler::default();
|
2019-02-18 16:25:17 -08:00
|
|
|
let rpc = RpcSolPubSubImpl::default();
|
2018-10-12 11:04:14 -07:00
|
|
|
|
2018-10-10 13:51:43 -07:00
|
|
|
io.extend_with(rpc.to_delegate());
|
|
|
|
|
|
|
|
let req = format!(
|
|
|
|
r#"{{"jsonrpc":"2.0","id":1,"method":"accountSubscribe","params":["{}"]}}"#,
|
|
|
|
bob_pubkey.to_string()
|
|
|
|
);
|
2018-10-12 11:04:14 -07:00
|
|
|
let _res = io.handle_request_sync(&req, session.clone());
|
2018-10-10 13:51:43 -07:00
|
|
|
|
|
|
|
let req =
|
|
|
|
format!(r#"{{"jsonrpc":"2.0","id":1,"method":"accountUnsubscribe","params":[0]}}"#);
|
2018-10-12 11:04:14 -07:00
|
|
|
let res = io.handle_request_sync(&req, session.clone());
|
2018-10-10 13:51:43 -07:00
|
|
|
|
|
|
|
let expected = format!(r#"{{"jsonrpc":"2.0","result":true,"id":1}}"#);
|
2019-02-17 15:33:25 -08:00
|
|
|
let expected: Response = serde_json::from_str(&expected).unwrap();
|
2018-10-10 13:51:43 -07:00
|
|
|
|
2019-02-17 15:33:25 -08:00
|
|
|
let result: Response = serde_json::from_str(&res.unwrap()).unwrap();
|
2018-10-10 13:51:43 -07:00
|
|
|
assert_eq!(expected, result);
|
|
|
|
|
|
|
|
// Test bad parameter
|
|
|
|
let req =
|
|
|
|
format!(r#"{{"jsonrpc":"2.0","id":1,"method":"accountUnsubscribe","params":[1]}}"#);
|
2018-10-12 11:04:14 -07:00
|
|
|
let res = io.handle_request_sync(&req, session.clone());
|
2018-10-10 13:51:43 -07:00
|
|
|
let expected = format!(r#"{{"jsonrpc":"2.0","error":{{"code":-32602,"message":"Invalid Request: Subscription id does not exist"}},"id":1}}"#);
|
2019-02-17 15:33:25 -08:00
|
|
|
let expected: Response = serde_json::from_str(&expected).unwrap();
|
2018-10-10 13:51:43 -07:00
|
|
|
|
2019-02-17 15:33:25 -08:00
|
|
|
let result: Response = serde_json::from_str(&res.unwrap()).unwrap();
|
2018-10-10 13:51:43 -07:00
|
|
|
assert_eq!(expected, result);
|
|
|
|
}
|
2019-05-06 07:31:50 -07:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_panic]
|
|
|
|
fn test_account_confirmations_not_fulfilled() {
|
2019-05-07 11:16:22 -07:00
|
|
|
let (genesis_block, alice) = create_genesis_block(10_000);
|
2019-05-06 07:31:50 -07:00
|
|
|
let bank = Bank::new(&genesis_block);
|
|
|
|
let blockhash = bank.last_blockhash();
|
|
|
|
let bank_forks = Arc::new(RwLock::new(BankForks::new(0, bank)));
|
|
|
|
let bob = Keypair::new();
|
|
|
|
|
|
|
|
let rpc = RpcSolPubSubImpl::default();
|
|
|
|
let session = create_session();
|
|
|
|
let (subscriber, _id_receiver, mut receiver) = Subscriber::new_test("accountNotification");
|
|
|
|
rpc.account_subscribe(session, subscriber, bob.pubkey().to_string(), Some(2));
|
|
|
|
|
|
|
|
let tx = system_transaction::transfer(&alice, &bob.pubkey(), 100, blockhash, 0);
|
|
|
|
bank_forks
|
|
|
|
.write()
|
|
|
|
.unwrap()
|
|
|
|
.get(0)
|
|
|
|
.unwrap()
|
|
|
|
.process_transaction(&tx)
|
|
|
|
.unwrap();
|
|
|
|
rpc.subscriptions.notify_subscribers(0, &bank_forks);
|
|
|
|
let _panic = receiver.poll();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_account_confirmations() {
|
2019-05-07 11:16:22 -07:00
|
|
|
let (genesis_block, alice) = create_genesis_block(10_000);
|
2019-05-06 07:31:50 -07:00
|
|
|
let bank = Bank::new(&genesis_block);
|
|
|
|
let blockhash = bank.last_blockhash();
|
|
|
|
let bank_forks = Arc::new(RwLock::new(BankForks::new(0, bank)));
|
|
|
|
let bob = Keypair::new();
|
|
|
|
|
|
|
|
let rpc = RpcSolPubSubImpl::default();
|
|
|
|
let session = create_session();
|
|
|
|
let (subscriber, _id_receiver, mut receiver) = Subscriber::new_test("accountNotification");
|
|
|
|
rpc.account_subscribe(session, subscriber, bob.pubkey().to_string(), Some(2));
|
|
|
|
|
|
|
|
let tx = system_transaction::transfer(&alice, &bob.pubkey(), 100, blockhash, 0);
|
|
|
|
bank_forks
|
|
|
|
.write()
|
|
|
|
.unwrap()
|
|
|
|
.get(0)
|
|
|
|
.unwrap()
|
|
|
|
.process_transaction(&tx)
|
|
|
|
.unwrap();
|
|
|
|
rpc.subscriptions.notify_subscribers(0, &bank_forks);
|
|
|
|
|
|
|
|
let bank0 = bank_forks.read().unwrap()[0].clone();
|
|
|
|
let bank1 = Bank::new_from_parent(&bank0, &Pubkey::default(), 1);
|
|
|
|
bank_forks.write().unwrap().insert(bank1);
|
|
|
|
rpc.subscriptions.notify_subscribers(1, &bank_forks);
|
|
|
|
let bank1 = bank_forks.read().unwrap()[1].clone();
|
|
|
|
let bank2 = Bank::new_from_parent(&bank1, &Pubkey::default(), 2);
|
|
|
|
bank_forks.write().unwrap().insert(bank2);
|
|
|
|
rpc.subscriptions.notify_subscribers(2, &bank_forks);
|
|
|
|
let string = receiver.poll();
|
|
|
|
let expected = json!({
|
|
|
|
"jsonrpc": "2.0",
|
|
|
|
"method": "accountNotification",
|
|
|
|
"params": {
|
|
|
|
"result": {
|
|
|
|
"owner": system_program::id(),
|
|
|
|
"lamports": 100,
|
|
|
|
"data": [],
|
|
|
|
"executable": false,
|
|
|
|
},
|
|
|
|
"subscription": 0,
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if let Async::Ready(Some(response)) = string.unwrap() {
|
|
|
|
assert_eq!(serde_json::to_string(&expected).unwrap(), response);
|
|
|
|
}
|
|
|
|
}
|
2018-10-10 13:51:43 -07:00
|
|
|
}
|