2018-10-10 13:51:43 -07:00
|
|
|
//! The `pubsub` module implements a threaded subscription service on client RPC request
|
|
|
|
|
2019-02-17 08:38:36 -08:00
|
|
|
use crate::bank::Bank;
|
2019-02-17 10:17:58 -08:00
|
|
|
use crate::rpc_status::RpcSignatureStatus;
|
2019-02-17 08:38:36 -08:00
|
|
|
use crate::rpc_subscriptions::RpcSubscriptions;
|
2018-10-10 13:51:43 -07:00
|
|
|
use bs58;
|
2019-02-04 16:41:03 -08:00
|
|
|
use jsonrpc_core::futures::Future;
|
|
|
|
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;
|
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-02-04 16:41:03 -08:00
|
|
|
#[rpc]
|
|
|
|
pub trait RpcSolPubSub {
|
|
|
|
type Metadata;
|
|
|
|
|
|
|
|
// Get notification every time account userdata is changed
|
|
|
|
// Accepts pubkey parameter as base-58 encoded string
|
|
|
|
#[pubsub(
|
|
|
|
subscription = "accountNotification",
|
|
|
|
subscribe,
|
|
|
|
name = "accountSubscribe"
|
|
|
|
)]
|
|
|
|
fn account_subscribe(&self, _: Self::Metadata, _: Subscriber<Account>, _: String);
|
|
|
|
|
|
|
|
// Unsubscribe from account notification subscription.
|
|
|
|
#[pubsub(
|
|
|
|
subscription = "accountNotification",
|
|
|
|
unsubscribe,
|
|
|
|
name = "accountUnsubscribe"
|
|
|
|
)]
|
|
|
|
fn account_unsubscribe(&self, _: Option<Self::Metadata>, _: SubscriptionId) -> Result<bool>;
|
|
|
|
|
|
|
|
// Get notification when signature is verified
|
|
|
|
// Accepts signature parameter as base-58 encoded string
|
|
|
|
#[pubsub(
|
|
|
|
subscription = "signatureNotification",
|
|
|
|
subscribe,
|
|
|
|
name = "signatureSubscribe"
|
|
|
|
)]
|
|
|
|
fn signature_subscribe(&self, _: Self::Metadata, _: Subscriber<RpcSignatureStatus>, _: String);
|
|
|
|
|
|
|
|
// 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-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
|
|
|
bank: Arc<Bank>,
|
|
|
|
subscriptions: Arc<RpcSubscriptions>,
|
2018-10-10 13:51:43 -07:00
|
|
|
}
|
2018-10-12 11:04:14 -07:00
|
|
|
|
|
|
|
impl RpcSolPubSubImpl {
|
2019-02-17 12:51:12 -08:00
|
|
|
pub fn new_with_subscriptions(bank: Arc<Bank>, subscriptions: Arc<RpcSubscriptions>) -> Self {
|
|
|
|
let uid = Arc::new(atomic::AtomicUsize::default());
|
|
|
|
Self {
|
|
|
|
uid,
|
2018-10-12 11:04:14 -07:00
|
|
|
bank,
|
2019-02-17 12:51:12 -08:00
|
|
|
subscriptions,
|
2018-10-12 11:04:14 -07:00
|
|
|
}
|
|
|
|
}
|
2019-02-17 12:51:12 -08:00
|
|
|
|
|
|
|
pub fn new(bank: Arc<Bank>) -> Self {
|
|
|
|
Self::new_with_subscriptions(bank, Arc::new(RpcSubscriptions::default()))
|
|
|
|
}
|
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,
|
|
|
|
) {
|
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-01-21 09:59:09 -08:00
|
|
|
.add_account_subscription(&pubkey, &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,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signature_subscribe(
|
2018-10-10 13:51:43 -07:00
|
|
|
&self,
|
2019-02-17 12:24:36 -08:00
|
|
|
_meta: Self::Metadata,
|
2019-02-04 16:41:03 -08:00
|
|
|
subscriber: Subscriber<RpcSignatureStatus>,
|
2018-10-10 13:51:43 -07:00
|
|
|
signature_str: String,
|
|
|
|
) {
|
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);
|
|
|
|
let id = self.uid.fetch_add(1, atomic::Ordering::SeqCst);
|
|
|
|
let sub_id = SubscriptionId::Number(id as u64);
|
|
|
|
let sink = subscriber.assign_id(sub_id.clone()).unwrap();
|
|
|
|
|
2019-02-17 12:51:12 -08:00
|
|
|
let status = self.bank.get_signature_status(&signature);
|
2018-12-17 07:55:56 -08:00
|
|
|
if status.is_none() {
|
2019-02-17 12:51:12 -08:00
|
|
|
self.subscriptions
|
2019-01-21 09:59:09 -08:00
|
|
|
.add_signature_subscription(&signature, &sub_id, &sink);
|
2018-12-17 07:55:56 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
match status.unwrap() {
|
2019-01-31 06:53:52 -08:00
|
|
|
Ok(_) => {
|
2018-10-10 13:51:43 -07:00
|
|
|
sink.notify(Ok(RpcSignatureStatus::Confirmed))
|
|
|
|
.wait()
|
|
|
|
.unwrap();
|
|
|
|
}
|
2019-01-21 09:59:09 -08:00
|
|
|
_ => self
|
2019-02-17 12:51:12 -08:00
|
|
|
.subscriptions
|
2019-01-21 09:59:09 -08:00
|
|
|
.add_signature_subscription(&signature, &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-02-17 15:33:25 -08:00
|
|
|
use crate::bank;
|
2019-01-24 12:04:04 -08:00
|
|
|
use crate::genesis_block::GenesisBlock;
|
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};
|
2018-12-04 14:38:19 -08:00
|
|
|
use solana_sdk::budget_program;
|
|
|
|
use solana_sdk::budget_transaction::BudgetTransaction;
|
2018-12-03 10:26:28 -08:00
|
|
|
use solana_sdk::signature::{Keypair, KeypairUtil};
|
2018-12-04 20:19:48 -08:00
|
|
|
use solana_sdk::system_transaction::SystemTransaction;
|
2019-02-17 15:33:25 -08:00
|
|
|
use solana_sdk::transaction::Transaction;
|
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-17 15:33:25 -08:00
|
|
|
pub fn process_transaction_and_notify(
|
|
|
|
bank: &Bank,
|
|
|
|
tx: &Transaction,
|
|
|
|
subscriptions: &RpcSubscriptions,
|
|
|
|
) -> bank::Result<()> {
|
|
|
|
bank.process_transaction(tx)?;
|
|
|
|
|
|
|
|
for pubkey in &tx.account_keys {
|
|
|
|
if let Some(account) = &bank.get_account(pubkey) {
|
|
|
|
subscriptions.check_account(pubkey, account);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let signature = &tx.signatures[0];
|
|
|
|
let status = bank.get_signature_status(signature).unwrap();
|
|
|
|
subscriptions.check_signature(signature, &status);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
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-01-24 12:04:04 -08:00
|
|
|
let (genesis_block, alice) = GenesisBlock::new(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);
|
2018-10-10 13:51:43 -07:00
|
|
|
let arc_bank = Arc::new(bank);
|
|
|
|
let last_id = arc_bank.last_id();
|
|
|
|
|
2019-02-17 12:51:12 -08:00
|
|
|
let rpc = RpcSolPubSubImpl::new(arc_bank.clone());
|
2018-10-10 13:51:43 -07:00
|
|
|
|
2019-02-17 12:51:12 -08:00
|
|
|
// Test signature subscriptions
|
2019-02-01 07:36:35 -08:00
|
|
|
let tx = SystemTransaction::new_move(&alice, bob_pubkey, 20, last_id, 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-02-17 12:24:36 -08:00
|
|
|
rpc.signature_subscribe(session, subscriber, tx.signatures[0].to_string());
|
2018-10-10 13:51:43 -07:00
|
|
|
|
2019-02-17 15:33:25 -08:00
|
|
|
process_transaction_and_notify(&arc_bank, &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() {
|
|
|
|
let expected = format!(r#"{{"jsonrpc":"2.0","method":"signatureNotification","params":{{"result":"Confirmed","subscription":0}}}}"#);
|
|
|
|
assert_eq!(expected, response);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_signature_unsubscribe() {
|
2019-01-24 12:04:04 -08:00
|
|
|
let (genesis_block, alice) = GenesisBlock::new(10_000);
|
2018-10-10 13:51:43 -07:00
|
|
|
let bob_pubkey = Keypair::new().pubkey();
|
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);
|
|
|
|
let last_id = arc_bank.last_id();
|
|
|
|
|
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-17 12:51:12 -08:00
|
|
|
let rpc = RpcSolPubSubImpl::new(arc_bank.clone());
|
2018-10-10 13:51:43 -07:00
|
|
|
io.extend_with(rpc.to_delegate());
|
|
|
|
|
2019-02-01 07:36:35 -08:00
|
|
|
let tx = SystemTransaction::new_move(&alice, bob_pubkey, 20, last_id, 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-01-24 12:04:04 -08:00
|
|
|
let (genesis_block, alice) = GenesisBlock::new(10_000);
|
2018-10-10 13:51:43 -07:00
|
|
|
let bob_pubkey = Keypair::new().pubkey();
|
|
|
|
let witness = Keypair::new();
|
|
|
|
let contract_funds = Keypair::new();
|
|
|
|
let contract_state = Keypair::new();
|
2018-11-23 12:45:34 -08:00
|
|
|
let budget_program_id = budget_program::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);
|
2018-10-10 13:51:43 -07:00
|
|
|
let arc_bank = Arc::new(bank);
|
|
|
|
let last_id = arc_bank.last_id();
|
|
|
|
|
2019-02-17 12:51:12 -08:00
|
|
|
let rpc = RpcSolPubSubImpl::new(arc_bank.clone());
|
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-02-17 12:24:36 -08:00
|
|
|
rpc.account_subscribe(session, subscriber, contract_state.pubkey().to_string());
|
2018-10-10 13:51:43 -07:00
|
|
|
|
2019-02-01 07:36:35 -08:00
|
|
|
let tx = SystemTransaction::new_program_account(
|
2019-01-24 12:04:04 -08:00
|
|
|
&alice,
|
2018-10-10 13:51:43 -07:00
|
|
|
contract_funds.pubkey(),
|
|
|
|
last_id,
|
|
|
|
50,
|
|
|
|
0,
|
|
|
|
budget_program_id,
|
|
|
|
0,
|
|
|
|
);
|
2019-02-17 15:33:25 -08:00
|
|
|
process_transaction_and_notify(&arc_bank, &tx, &rpc.subscriptions).unwrap();
|
2018-10-10 13:51:43 -07:00
|
|
|
|
2019-02-01 07:36:35 -08:00
|
|
|
let tx = SystemTransaction::new_program_account(
|
2019-01-24 12:04:04 -08:00
|
|
|
&alice,
|
2018-10-10 13:51:43 -07:00
|
|
|
contract_state.pubkey(),
|
|
|
|
last_id,
|
|
|
|
1,
|
|
|
|
196,
|
|
|
|
budget_program_id,
|
|
|
|
0,
|
|
|
|
);
|
2018-10-12 11:04:14 -07:00
|
|
|
|
2019-02-17 15:33:25 -08:00
|
|
|
process_transaction_and_notify(&arc_bank, &tx, &rpc.subscriptions).unwrap();
|
2018-10-10 13:51:43 -07:00
|
|
|
|
|
|
|
// Test signature confirmation notification #1
|
|
|
|
let string = receiver.poll();
|
2018-10-12 11:04:14 -07:00
|
|
|
|
2018-10-10 13:51:43 -07:00
|
|
|
let expected_userdata = arc_bank
|
|
|
|
.get_account(&contract_state.pubkey())
|
|
|
|
.unwrap()
|
|
|
|
.userdata;
|
2018-10-12 11:04:14 -07:00
|
|
|
|
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,
|
2018-10-10 13:51:43 -07:00
|
|
|
"tokens": 1,
|
2018-10-16 09:43:49 -07:00
|
|
|
"userdata": expected_userdata,
|
|
|
|
"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-02-01 07:36:35 -08:00
|
|
|
let tx = BudgetTransaction::new_when_signed(
|
2018-10-10 13:51:43 -07:00
|
|
|
&contract_funds,
|
|
|
|
bob_pubkey,
|
|
|
|
contract_state.pubkey(),
|
|
|
|
witness.pubkey(),
|
|
|
|
None,
|
|
|
|
50,
|
|
|
|
last_id,
|
|
|
|
);
|
2019-02-17 15:33:25 -08:00
|
|
|
process_transaction_and_notify(&arc_bank, &tx, &rpc.subscriptions).unwrap();
|
2018-10-10 13:51:43 -07:00
|
|
|
sleep(Duration::from_millis(200));
|
|
|
|
|
|
|
|
// Test signature confirmation notification #2
|
|
|
|
let string = receiver.poll();
|
|
|
|
let expected_userdata = arc_bank
|
|
|
|
.get_account(&contract_state.pubkey())
|
|
|
|
.unwrap()
|
|
|
|
.userdata;
|
|
|
|
let expected = json!({
|
|
|
|
"jsonrpc": "2.0",
|
|
|
|
"method": "accountNotification",
|
|
|
|
"params": {
|
|
|
|
"result": {
|
2018-11-12 09:29:17 -08:00
|
|
|
"owner": budget_program_id,
|
2018-10-10 13:51:43 -07:00
|
|
|
"tokens": 51,
|
2018-10-16 09:43:49 -07:00
|
|
|
"userdata": expected_userdata,
|
|
|
|
"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-02-01 07:36:35 -08:00
|
|
|
let tx = SystemTransaction::new_account(&alice, witness.pubkey(), 1, last_id, 0);
|
2019-02-17 15:33:25 -08:00
|
|
|
process_transaction_and_notify(&arc_bank, &tx, &rpc.subscriptions).unwrap();
|
2018-10-10 13:51:43 -07:00
|
|
|
sleep(Duration::from_millis(200));
|
2019-02-01 07:36:35 -08:00
|
|
|
let tx = BudgetTransaction::new_signature(
|
2018-10-10 13:51:43 -07:00
|
|
|
&witness,
|
|
|
|
contract_state.pubkey(),
|
|
|
|
bob_pubkey,
|
|
|
|
last_id,
|
|
|
|
);
|
2019-02-17 15:33:25 -08:00
|
|
|
process_transaction_and_notify(&arc_bank, &tx, &rpc.subscriptions).unwrap();
|
2018-10-10 13:51:43 -07:00
|
|
|
sleep(Duration::from_millis(200));
|
|
|
|
|
|
|
|
let expected_userdata = arc_bank
|
|
|
|
.get_account(&contract_state.pubkey())
|
|
|
|
.unwrap()
|
|
|
|
.userdata;
|
|
|
|
let expected = json!({
|
|
|
|
"jsonrpc": "2.0",
|
|
|
|
"method": "accountNotification",
|
|
|
|
"params": {
|
|
|
|
"result": {
|
2018-11-12 09:29:17 -08:00
|
|
|
"owner": budget_program_id,
|
2018-10-10 13:51:43 -07:00
|
|
|
"tokens": 1,
|
2018-10-16 09:43:49 -07:00
|
|
|
"userdata": expected_userdata,
|
|
|
|
"executable": executable,
|
2018-10-10 13:51:43 -07:00
|
|
|
},
|
|
|
|
"subscription": 0,
|
|
|
|
}
|
|
|
|
});
|
|
|
|
let string = receiver.poll();
|
|
|
|
if let Async::Ready(Some(response)) = string.unwrap() {
|
|
|
|
assert_eq!(serde_json::to_string(&expected).unwrap(), response);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_account_unsubscribe() {
|
2019-01-24 12:04:04 -08:00
|
|
|
let (genesis_block, _) = GenesisBlock::new(10_000);
|
2018-10-10 13:51:43 -07:00
|
|
|
let bob_pubkey = Keypair::new().pubkey();
|
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-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-17 12:51:12 -08:00
|
|
|
let rpc = RpcSolPubSubImpl::new(arc_bank.clone());
|
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);
|
|
|
|
}
|
|
|
|
}
|