Skip to content

Commit 47114e3

Browse files
popzxcOleksandr Anyshchenko
authored andcommitted
Remove validator_id method from AfterCommitContext (#1508)
1 parent 63d6d32 commit 47114e3

File tree

5 files changed

+32
-22
lines changed

5 files changed

+32
-22
lines changed

exonum/src/helpers/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ pub mod multisig;
2121
pub mod user_agent;
2222
#[macro_use]
2323
pub mod metrics;
24+
pub mod validator;
2425

2526
use env_logger::Builder;
2627
use log::SetLoggerError;

exonum/src/helpers/validator.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2019 The Exonum Team
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
use exonum_crypto::PublicKey;
16+
use exonum_merkledb::Snapshot;
17+
18+
use crate::{blockchain::Schema as CoreSchema, helpers::ValidatorId};
19+
20+
/// Attempts to find a `ValidatorId` by the provided service public key.
21+
pub fn validator_id(snapshot: &dyn Snapshot, service_public_key: PublicKey) -> Option<ValidatorId> {
22+
CoreSchema::new(snapshot)
23+
.consensus_config()
24+
.find_validator(|validator_keys| service_public_key == validator_keys.service_key)
25+
}

exonum/src/runtime/rust/service.rs

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use std::fmt::{self, Debug};
1919
use crate::{
2020
blockchain::Schema as CoreSchema,
2121
crypto::{Hash, PublicKey, SecretKey},
22-
helpers::{Height, ValidatorId},
22+
helpers::Height,
2323
messages::Verified,
2424
node::ApiSender,
2525
runtime::{
@@ -191,16 +191,6 @@ impl<'a, 'b> TransactionContext<'a, 'b> {
191191
&self.inner.caller
192192
}
193193

194-
/// Returns the validator ID if the transaction author is a validator.
195-
pub fn validator_id(&self) -> Option<ValidatorId> {
196-
// TODO Perhaps we should optimize this method [ECR-3222]
197-
self.caller().author().and_then(|author| {
198-
CoreSchema::new(self.fork())
199-
.consensus_config()
200-
.find_validator(|validator_keys| author == validator_keys.service_key)
201-
})
202-
}
203-
204194
/// Enqueue dispatcher action.
205195
pub fn dispatch_action(&self, action: dispatcher::Action) {
206196
self.inner
@@ -331,14 +321,6 @@ impl<'a> AfterCommitContext<'a> {
331321
}
332322
}
333323

334-
/// Returns the validator ID if the current node is a validator.
335-
pub fn validator_id(&self) -> Option<ValidatorId> {
336-
// TODO Perhaps we should optimize this method [ECR-3222]
337-
CoreSchema::new(self.snapshot)
338-
.consensus_config()
339-
.find_validator(|validator_keys| self.service_keypair.0 == validator_keys.service_key)
340-
}
341-
342324
/// Returns a current blockchain height. This height is "height of the latest committed block".
343325
pub fn height(&self) -> Height {
344326
// TODO Perhaps we should optimize this method [ECR-3222]

services/supervisor/src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub use self::{
3131
use exonum::{
3232
blockchain::{self, InstanceCollection},
3333
crypto::Hash,
34-
helpers::byzantine_quorum,
34+
helpers::{byzantine_quorum, validator::validator_id},
3535
runtime::{
3636
api::ServiceApiBuilder,
3737
rust::{AfterCommitContext, BeforeCommitContext, Service, Transaction},
@@ -129,7 +129,8 @@ impl Service for Supervisor {
129129
let tx_sender = context.transaction_broadcaster();
130130
let keypair = context.service_keypair.clone();
131131
let instance_id = context.instance.id;
132-
let is_validator = context.validator_id().is_some();
132+
let is_validator =
133+
validator_id(context.snapshot, context.service_keypair.0).is_some();
133134
move || {
134135
if is_validator {
135136
trace!(

services/time/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ pub mod transactions;
4444

4545
use exonum::{
4646
crypto::Hash,
47+
helpers::validator::validator_id,
4748
runtime::{
4849
api::ServiceApiBuilder,
4950
rust::{AfterCommitContext, Service},
@@ -84,7 +85,7 @@ impl Service for TimeService {
8485
/// Creates transaction after commit of the block.
8586
fn after_commit(&self, context: AfterCommitContext) {
8687
// The transaction must be created by the validator.
87-
if context.validator_id().is_some() {
88+
if validator_id(context.snapshot, context.service_keypair.0).is_some() {
8889
context.broadcast_transaction(TxTime::new(self.time.current_time()));
8990
}
9091
}

0 commit comments

Comments
 (0)