Skip to content

Commit

Permalink
Pre-Release 2.0.3 snapshot (#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mingela authored Jun 26, 2019
1 parent ab7fad6 commit 11d96d1
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.google.gson.JsonPrimitive;
import io.reactivex.Scheduler;
import io.reactivex.schedulers.Schedulers;
import iroha.protocol.Endpoint;
import iroha.protocol.Endpoint.TxStatus;
import iroha.protocol.TransactionOuterClass;
Expand All @@ -26,6 +28,8 @@
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors;
import java.util.function.Function;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
Expand All @@ -50,7 +54,8 @@ public class AccountManager implements UserQuorumProvider, RegistrationProvider
private static final JsonParser parser = new JsonParser();
private static final int INITIAL_KEYS_AMOUNT = 1;

private final Set<String> registeredAccounts = new HashSet<>();
private final Scheduler scheduler = Schedulers.from(Executors.newCachedThreadPool());
private final Set<String> registeredAccounts = ConcurrentHashMap.newKeySet();

private final String brvsAccountId;
private final KeyPair brvsAccountKeyPair;
Expand Down Expand Up @@ -221,11 +226,12 @@ private String getDomain(String accountId) {
* {@inheritDoc}
*/
@Override
public synchronized void register(String accountId) {
public void register(String accountId) {
scheduler.scheduleDirect(new RegistrationRunnable(accountId));
}

private void doRegister(String accountId) {
logger.info("Going to register " + accountId);
if (registeredAccounts.contains(accountId)) {
throw new IllegalArgumentException("User " + accountId + " is already registered.");
}
if (!hasValidFormat(accountId)) {
throw new IllegalArgumentException(
"Invalid account format [" + accountId + "]. Use 'username@domain'.");
Expand Down Expand Up @@ -331,7 +337,7 @@ private List<String> getAccountSignatories(String targetAccountId) {
* {@inheritDoc}
*/
@Override
public synchronized Iterable<String> getRegisteredAccounts() {
public Iterable<String> getRegisteredAccounts() {
return registeredAccounts;
}

Expand Down Expand Up @@ -411,4 +417,21 @@ private Endpoint.TxStatus sendWithLastStatusWaiting(
ValidationUtils.subscriptionStrategy
).blockingLast().getTxStatus();
}

/**
* Intermediary runnable-wrapper for brvs registration
*/
private class RegistrationRunnable implements Runnable {

private final String accountId;

RegistrationRunnable(String accountId) {
this.accountId = accountId;
}

@Override
public void run() {
doRegister(accountId);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -111,21 +111,23 @@ private void sendTransactions(List<Transaction> transactions, boolean check) {
if (transactions.size() > 1) {
irohaAPI.transactionListSync(transactions);
if (check) {
transactions.forEach(this::checkIrohaStatus);
transactions.forEach(transaction ->
scheduler.scheduleDirect(new IrohaStatusRunnable(transaction))
);
}
} else {
final Transaction transaction = transactions.get(0);
irohaAPI.transactionSync(transaction);
if (check) {
checkIrohaStatus(transaction);
scheduler.scheduleDirect(new IrohaStatusRunnable(transaction));
}
}
}

private void checkIrohaStatus(Transaction transaction) {
final ToriiResponse statusResponse = ValidationUtils.subscriptionStrategy
.subscribe(irohaAPI, Utils.hash(transaction))
.subscribeOn(scheduler).blockingLast();
.blockingLast();
if (!statusResponse.getTxStatus().equals(TxStatus.COMMITTED)) {
logger.warn("Transaction " + ValidationUtils.hexHash(transaction) + " failed in Iroha: "
+ statusResponse.getTxStatus());
Expand Down Expand Up @@ -202,4 +204,21 @@ public void rejectAndSend(TransactionBatch transactionBatch, String reason) {
sendRejectedUserTransaction(transactionBatch);
}
}

/**
* Intermediary runnable-wrapper for Iroha status checking
*/
private class IrohaStatusRunnable implements Runnable {

private final Transaction transaction;

IrohaStatusRunnable(Transaction transaction) {
this.transaction = transaction;
}

@Override
public void run() {
checkIrohaStatus(transaction);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
import static org.junit.jupiter.api.Assertions.assertNull;

import com.d3.commons.config.RMQConfig;
import io.reactivex.Maybe;
import iroha.protocol.BlockOuterClass;
import iroha.protocol.Endpoint.ToriiResponse;
import iroha.protocol.Endpoint.TxStatus;
import iroha.protocol.Primitive.GrantablePermission;
import iroha.protocol.Primitive.RolePermission;
Expand All @@ -20,8 +18,8 @@
import iroha.protocol.TransactionOuterClass;
import iroha.validation.config.ValidationServiceContext;
import iroha.validation.listener.BrvsIrohaChainListener;
import iroha.validation.rules.impl.core.SampleRule;
import iroha.validation.rules.impl.assets.TransferTxVolumeRule;
import iroha.validation.rules.impl.core.SampleRule;
import iroha.validation.service.ValidationService;
import iroha.validation.service.impl.ValidationServiceImpl;
import iroha.validation.transactions.provider.impl.AccountManager;
Expand All @@ -46,6 +44,7 @@
import jp.co.soramitsu.iroha.java.QueryBuilder;
import jp.co.soramitsu.iroha.java.Transaction;
import jp.co.soramitsu.iroha.java.Utils;
import jp.co.soramitsu.iroha.java.subscription.WaitForTerminalStatus;
import jp.co.soramitsu.iroha.testcontainers.IrohaContainer;
import jp.co.soramitsu.iroha.testcontainers.PeerConfig;
import jp.co.soramitsu.iroha.testcontainers.detail.GenesisBlockBuilder;
Expand Down Expand Up @@ -75,15 +74,19 @@ class IrohaIntegrationTest {
private static final String validatorId = String.format("%s@%s", validatorName, domainName);
private static final String asset = "bux";
private static final String assetId = String.format("%s#%s", asset, domainName);
private static final int TRANSACTION_VALIDATION_TIMEOUT = 10000;
private static final int TRANSACTION_REACTION_TIMEOUT = 5000;
private static final int INITIALIZATION_TIME = 5000;
private CacheProvider cacheProvider;
private TransactionVerdictStorage transactionVerdictStorage;
private AccountManager accountManager;
private static final GenericContainer rmq = new GenericContainer<>("rabbitmq:3-management")
.withExposedPorts(5672);
private static final GenericContainer mongo = new GenericContainer<>("mongo:4.0.6")
.withExposedPorts(27017);
private static final WaitForTerminalStatus terminalStrategy = new WaitForTerminalStatus(
Arrays.asList(
TxStatus.COMMITTED,
TxStatus.REJECTED
));

private IrohaContainer iroha;
private IrohaAPI irohaAPI;
Expand Down Expand Up @@ -243,7 +246,7 @@ void setUp() throws InterruptedException {
mongoHost = mongo.getContainerIpAddress();
mongoPort = mongo.getMappedPort(27017);

Thread.sleep(TRANSACTION_VALIDATION_TIMEOUT);
Thread.sleep(INITIALIZATION_TIME);

irohaAPI.transactionSync(Transaction.builder(senderId)
.grantPermission(validatorId, GrantablePermission.can_add_my_signatory)
Expand All @@ -266,7 +269,7 @@ void setUp() throws InterruptedException {

// construct BRVS using some account for block streaming and validator keypair
validationService = getService(irohaAPI);
Thread.sleep(TRANSACTION_REACTION_TIMEOUT);
Thread.sleep(INITIALIZATION_TIME);
// subscribe to new transactions
validationService.verifyTransactions();
}
Expand All @@ -288,7 +291,7 @@ void tearDown() {
* BRVS and committed in Iroha so account "abcd@notary" exists in Iroha
*/
@Test
void createAccountTransactionOnTransferLimitValidatorTest() throws InterruptedException {
void createAccountTransactionOnTransferLimitValidatorTest() {
// send create account transaction to check rules
String newAccountName = "abcd";
TransactionOuterClass.Transaction transaction = Transaction.builder(receiverId)
Expand All @@ -302,11 +305,12 @@ void createAccountTransactionOnTransferLimitValidatorTest() throws InterruptedEx
cacheProvider.unlockPendingAccount(receiverId);
irohaAPI.transactionSync(transaction);

Thread.sleep(TRANSACTION_REACTION_TIMEOUT);
// Check account is not blocked
assertNull(cacheProvider.getAccountBlockedBy(ValidationUtils.hexHash(transaction)));

Thread.sleep(TRANSACTION_VALIDATION_TIMEOUT);
irohaAPI.transaction(transaction, terminalStrategy).blockingSubscribe(status -> {
if (status.getTxStatus().equals(TxStatus.ENOUGH_SIGNATURES_COLLECTED)) {
// Check account is blocked
assertNull(cacheProvider.getAccountBlockedBy(ValidationUtils.hexHash(transaction)));
}
});

assertEquals(Verdict.VALIDATED, transactionVerdictStorage
.getTransactionVerdict(ValidationUtils.hexHash(transaction)).getStatus());
Expand All @@ -332,7 +336,7 @@ void createAccountTransactionOnTransferLimitValidatorTest() throws InterruptedEx
* BRVS and committed in Iroha so destination account balance is increased by 100 "bux#notary"
*/
@Test
void validTransferAssetOnTransferLimitValidatorTest() throws InterruptedException {
void validTransferAssetOnTransferLimitValidatorTest() {
final String initialBalance = irohaAPI.query(new QueryBuilder(receiverId, Instant.now(), 1)
.getAccountAssets(receiverId)
.buildSigned(receiverKeypair))
Expand All @@ -345,13 +349,13 @@ void validTransferAssetOnTransferLimitValidatorTest() throws InterruptedExceptio
.setQuorum(2)
.sign(senderKeypair).build();
cacheProvider.unlockPendingAccount(senderId);
irohaAPI.transactionSync(transaction);

Thread.sleep(TRANSACTION_REACTION_TIMEOUT);
// Check account is blocked
assertEquals(senderId, cacheProvider.getAccountBlockedBy(ValidationUtils.hexHash(transaction)));

Thread.sleep(TRANSACTION_VALIDATION_TIMEOUT);
irohaAPI.transaction(transaction, terminalStrategy).blockingSubscribe(status -> {
if (status.getTxStatus().equals(TxStatus.ENOUGH_SIGNATURES_COLLECTED)) {
// Check account is blocked
assertEquals(senderId,
cacheProvider.getAccountBlockedBy(ValidationUtils.hexHash(transaction)));
}
});

assertEquals(Verdict.VALIDATED, transactionVerdictStorage
.getTransactionVerdict(ValidationUtils.hexHash(transaction)).getStatus());
Expand Down Expand Up @@ -390,18 +394,18 @@ void invalidTransferAssetOnTransferLimitValidatorTest() throws InterruptedExcept
.setQuorum(2)
.sign(senderKeypair).build();
cacheProvider.unlockPendingAccount(senderId);
final Maybe<ToriiResponse> lastElementStatus = irohaAPI.transaction(transaction).lastElement();

Thread.sleep(TRANSACTION_REACTION_TIMEOUT);
// Check account is not blocked
assertNull(cacheProvider.getAccountBlockedBy(ValidationUtils.hexHash(transaction)));

Thread.sleep(TRANSACTION_VALIDATION_TIMEOUT);
irohaAPI.transaction(transaction, terminalStrategy).blockingSubscribe(status -> {
if (status.getTxStatus().equals(TxStatus.ENOUGH_SIGNATURES_COLLECTED)) {
// Check account is not blocked
assertNull(cacheProvider.getAccountBlockedBy(ValidationUtils.hexHash(transaction)));
}
});

assertEquals(Verdict.REJECTED, transactionVerdictStorage
.getTransactionVerdict(ValidationUtils.hexHash(transaction)).getStatus());
assertEquals(TxStatus.REJECTED,
lastElementStatus.blockingGet().getTxStatus());
irohaAPI.txStatusSync(Utils.hash(transaction)).getTxStatus());

// query Iroha and check that transfer was not committed
AccountAsset accountAsset = irohaAPI.query(new QueryBuilder(receiverId, Instant.now(), 1)
Expand Down
31 changes: 18 additions & 13 deletions config/context/spring-context.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@
<!-- PATH TO THE PROPERTIES FILE -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
<property name="locations" value="classpath:application.properties"/>
<property name="locations">
<list>
<value>classpath:application.properties</value>
<value>classpath:rmq.properties</value>
</list>
</property>
</bean>

<!-- BRVS PORT -->
Expand Down Expand Up @@ -133,7 +138,7 @@

<!-- RULES DEFINITIONS -->
<bean id="sampleRule" class="iroha.validation.rules.impl.core.SampleRule"/>
<bean id="newBrvsRule" class="iroha.validation.rules.impl.NewBrvsRule">
<bean id="newBrvsRule" class="iroha.validation.rules.impl.core.NewBrvsRule">
<constructor-arg name="queryAPI" ref="queryAPI"/>
</bean>
<bean id="updateWhitelistRule" class="iroha.validation.rules.impl.whitelist.UpdateWhitelistRule">
Expand All @@ -160,16 +165,16 @@
<bean id="quorumDivisor" class="iroha.validation.rules.impl.core.QuorumDivisorRule">
<constructor-arg name="divisor" value="${QUORUM_DIVISOR_RULE_VALUE}"/>
</bean>
<!--<bean id="billingRule" class="iroha.validation.rules.impl.billing.BillingRule">-->
<!--<constructor-arg name="getBillingURL" value="${BILLING_URL}"/>-->
<!--<constructor-arg name="rmqHost" value="${RMQ_HOST}"/>-->
<!--<constructor-arg name="rmqPort" value="${RMQ_PORT}"/>-->
<!--<constructor-arg name="rmqExchange" value="${BILLING_RMQ_EXCHANGE}"/>-->
<!--<constructor-arg name="rmqRoutingKey" value="${BILLING_RMQ_ROUTINGKEY}"/>-->
<!--<constructor-arg name="userDomains" value="${BRVS_USERDOMAINS}"/>-->
<!--<constructor-arg name="depositAccounts" value="${BILLING_DEPOSITACCOUNTS}"/>-->
<!--<constructor-arg name="withdrawalAccounts" value="${BILLING_WITHDRAWALACCOUNTS}"/>-->
<!--</bean>-->
<bean id="billingRule" class="iroha.validation.rules.impl.billing.BillingRule">
<constructor-arg name="getBillingURL" value="${BILLING_URL}"/>
<constructor-arg name="rmqHost" value="${rmq.host}"/>
<constructor-arg name="rmqPort" value="${rmq.port}"/>
<constructor-arg name="rmqExchange" value="${BILLING_RMQ_EXCHANGE}"/>
<constructor-arg name="rmqRoutingKey" value="${BILLING_RMQ_ROUTINGKEY}"/>
<constructor-arg name="userDomains" value="${BRVS_USERDOMAINS}"/>
<constructor-arg name="depositAccounts" value="${BILLING_DEPOSITACCOUNTS}"/>
<constructor-arg name="withdrawalAccounts" value="${BILLING_WITHDRAWALACCOUNTS}"/>
</bean>
<util:list id="rules" value-type="iroha.validation.rules.Rule">
<ref bean="sampleRule"/>
<ref bean="newBrvsRule"/>
Expand All @@ -179,7 +184,7 @@
<ref bean="restrictedKeysRule"/>
<ref bean="minKeysRule"/>
<ref bean="quorumDivisor"/>
<!--<ref bean="billingRule"/>-->
<ref bean="billingRule"/>
<!-- More can be added -->
</util:list>

Expand Down

0 comments on commit 11d96d1

Please sign in to comment.