Skip to content

Commit

Permalink
Update seth-rpc to use new message types
Browse files Browse the repository at this point in the history
Signed-off-by: Adam Ludvik <[email protected]>
  • Loading branch information
Adam Ludvik committed Nov 10, 2017
1 parent 13beea2 commit f242c27
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
27 changes: 22 additions & 5 deletions families/seth/rpc/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ use sawtooth_sdk::messages::client_block::{
ClientBlockListResponse,
ClientBlockGetByIdRequest,
ClientBlockGetByNumRequest,
ClientBlockGetByTransactionIdRequest,
ClientBlockGetResponse,
ClientBlockGetResponse_Status,
};
Expand Down Expand Up @@ -75,6 +76,7 @@ pub enum BlockKey {
Earliest,
Number(u64),
Signature(String),
Transaction(String),
}

pub enum BlockKeyParseError {
Expand Down Expand Up @@ -391,11 +393,7 @@ impl<S: MessageSender> ValidatorClient<S> {
Message_MessageType::CLIENT_TRANSACTION_GET_REQUEST, &request)?;

let block = {
if response.block_id == "" {
None
} else {
self.get_block(BlockKey::Signature(response.block_id.clone())).ok()
}
self.get_block(BlockKey::Transaction(txn_id.clone())).ok()
};

match response.status {
Expand Down Expand Up @@ -455,6 +453,12 @@ impl<S: MessageSender> ValidatorClient<S> {
request.set_block_id(String::from("0000000000000000"));
response = self.send_request(message_type, &request)?;
},
BlockKey::Transaction(transaction_id) => {
let mut request = ClientBlockGetByTransactionIdRequest::new();
let message_type: Message_MessageType = Message_MessageType::CLIENT_BLOCK_GET_BY_TRANSACTION_ID_REQUEST;
request.set_transaction_id(transaction_id);
response = self.send_request(message_type, &request)?;
},
};

match response.status {
Expand Down Expand Up @@ -497,6 +501,14 @@ impl<S: MessageSender> ValidatorClient<S> {
return Err(String::from(format!("{:?}", error)));
},
},
BlockKey::Transaction(transaction_id) => match self.transaction_to_block_id(transaction_id) {
Ok(block_id) => {
request.set_head_id(block_id);
},
Err(error) => {
return Err(String::from(format!("{:?}", error)));
},
},
}

let response: ClientStateGetResponse =
Expand Down Expand Up @@ -612,4 +624,9 @@ impl<S: MessageSender> ValidatorClient<S> {
self.get_block(BlockKey::Number(block_num)).map(|block|
String::from(block.header_signature))
}

fn transaction_to_block_id(&mut self, transaction_id: String) -> Result<String, Error> {
self.get_block(BlockKey::Transaction(transaction_id)).map(|block|
String::from(block.header_signature))
}
}
16 changes: 12 additions & 4 deletions families/seth/rpc/tests/test_seth_rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
from sawtooth_sdk.protobuf.client_block_pb2 import ClientBlockListResponse
from sawtooth_sdk.protobuf.client_block_pb2 import ClientBlockGetByIdRequest
from sawtooth_sdk.protobuf.client_block_pb2 import ClientBlockGetByNumRequest
from sawtooth_sdk.protobuf.client_block_pb2 import \
ClientBlockGetByTransactionIdRequest
from sawtooth_sdk.protobuf.client_block_pb2 import ClientBlockGetResponse
from sawtooth_sdk.protobuf.client_state_pb2 import ClientStateGetRequest
from sawtooth_sdk.protobuf.client_state_pb2 import ClientStateGetResponse
Expand Down Expand Up @@ -498,7 +500,7 @@ def test_get_transaction_by_hash(self):

self._send_transaction_response(msg, block.batches[1].transactions[1])

msg, request = self._receive_block_request_id()
msg, request = self._receive_block_request_transaction()

self._send_block_back(msg, block)

Expand Down Expand Up @@ -735,7 +737,7 @@ def test_get_transaction_receipt(self):

msg, request = self._receive_transaction_request()
self._send_transaction_response(msg)
msg, request = self._receive_block_request_id()
msg, request = self._receive_block_request_transaction()
block = Block(
header=BlockHeader(block_num=self.block_num).SerializeToString(),
header_signature=self.block_id,
Expand Down Expand Up @@ -1228,8 +1230,7 @@ def _send_transaction_response(self, msg, transaction=None):
Message.CLIENT_TRANSACTION_GET_RESPONSE,
ClientTransactionGetResponse(
status=ClientBlockGetResponse.OK,
transaction=transaction,
block_id=self.block_id),
transaction=transaction),
msg)


Expand All @@ -1247,6 +1248,13 @@ def _receive_receipt_request(self):
request.ParseFromString(msg.content)
return msg, request

def _receive_block_request_transaction(self):
msg = self.validator.receive()
self.assertEqual(msg.message_type,
Message.CLIENT_BLOCK_GET_BY_TRANSACTION_ID_REQUEST)
request = ClientBlockGetByTransactionIdRequest()
request.ParseFromString(msg.content)
return msg, request

def _receive_block_request_id(self):
msg = self.validator.receive()
Expand Down

0 comments on commit f242c27

Please sign in to comment.