Skip to content

Commit bd55453

Browse files
Update QA service for DS [ECR-3586]: (#1224)
1. Added an ability to set key pair in TransactionMessage.Builder, so that the builder with a _set_ key pair can be passed around. #signedWith(keys) is provided to just set the keys (then can be followed by any other setters and completed with #build); #sign — as a shortcut to builder.signedWith(keys).build(). 2. Added a Testkit#port method returning the TCP port on which the server providing service transport listens 3. Added a system-time provider for cases when the tests don’t need to control the current time. 4. QA service: 1. Removed conversion of transactions into JSON, as it is no longer supported by the core. 2. Removed conversion of transactions into RawTransactions 3. Removed usages of RequiresNativeLibrary as it is no longer used in filtering 4. Removed operations submitting a self-signed transaction except the two enabling Node testing. The rest of transactions shall be submitted through the usual channel. 5. Moved transaction decoding tests to the transaction converter. 5. Add BOM to build scripts
1 parent f20fe67 commit bd55453

File tree

51 files changed

+1104
-1523
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+1104
-1523
lines changed

exonum-java-binding/common/src/main/java/com/exonum/binding/common/message/TransactionMessage.java

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package com.exonum.binding.common.message;
1919

2020
import static com.google.common.base.Preconditions.checkArgument;
21+
import static com.google.common.base.Preconditions.checkNotNull;
2122

2223
import com.exonum.binding.common.crypto.CryptoFunction;
2324
import com.exonum.binding.common.crypto.CryptoFunctions;
@@ -110,9 +111,12 @@ static TransactionMessage fromBytes(byte[] bytes) {
110111
* Builder for the binary transaction message.
111112
*/
112113
class Builder {
114+
private static CryptoFunction DEFAULT_CRYPTO_FUNCTION = CryptoFunctions.ed25519();
113115
private Integer serviceId;
114116
private Integer transactionId;
115117
private ByteString payload;
118+
private KeyPair keys;
119+
private CryptoFunction cryptoFunction;
116120

117121
/**
118122
* Sets service identifier to the transaction message.
@@ -160,16 +164,51 @@ public Builder payload(ByteString payload) {
160164
}
161165

162166
/**
163-
* Signs the message, creating a new signed binary transaction message.
167+
* Sets the Ed25519 key pair to use to sign the message.
168+
*
169+
* @param keys a key pair with a private and public keys. The public key is included
170+
* in the message as an author key of the message. The private key is used for signing
171+
* the message, but not included in it
172+
*/
173+
public Builder signedWith(KeyPair keys) {
174+
return signedWith(keys, DEFAULT_CRYPTO_FUNCTION);
175+
}
176+
177+
/**
178+
* Sets the key pair and the crypto function to use to sign the message.
164179
*
165-
* @param keys key pair with private and public keys. Public key is used as an author key of the
166-
* message and private key is used for signing the message.
180+
* @param keys a key pair with a private and public keys. The public key is included
181+
* in the message as an author key of the message. The private key is used for signing
182+
* the message, but not included in it
167183
* @param crypto a cryptographic function to use
184+
*/
185+
public Builder signedWith(KeyPair keys, CryptoFunction crypto) {
186+
this.keys = checkNotNull(keys);
187+
this.cryptoFunction = checkNotNull(crypto);
188+
return this;
189+
}
190+
191+
/**
192+
* Signs the message with the given Ed25519 keys, creating a new signed binary
193+
* transaction message. A shorthand for {@code signedWith(keys).build()}.
194+
*
195+
* @param keys a key pair to {@linkplain #signedWith(KeyPair) sign} the message
196+
* @return a new signed binary transaction message
197+
* @throws IllegalStateException if any field weren't set
198+
* @throws IllegalArgumentException if the public key has wrong size
199+
*/
200+
public TransactionMessage sign(KeyPair keys) {
201+
return signedWith(keys).build();
202+
}
203+
204+
/**
205+
* Signs the message, creating a new signed binary transaction message.
206+
*
168207
* @return a new signed binary transaction message
169-
* @throws IllegalStateException if serviceId or transactionId or payload weren't set
170-
* @throws IllegalArgumentException if public key has wrong size
208+
* @throws IllegalStateException if any field weren't set
209+
* @throws IllegalArgumentException if the public key has wrong size
171210
*/
172-
public TransactionMessage sign(KeyPair keys, CryptoFunction crypto) {
211+
public TransactionMessage build() {
173212
checkRequiredFieldsSet();
174213
PublicKey authorPublicKey = keys.getPublicKey();
175214
checkArgument(authorPublicKey.size() == Ed25519.PUBLIC_KEY_BYTES,
@@ -187,7 +226,7 @@ public TransactionMessage sign(KeyPair keys, CryptoFunction crypto) {
187226
.build()
188227
.toByteArray();
189228

190-
byte[] signature = crypto.signMessage(exonumMessage, keys.getPrivateKey());
229+
byte[] signature = cryptoFunction.signMessage(exonumMessage, keys.getPrivateKey());
191230

192231
Consensus.SignedMessage signedMessage = Consensus.SignedMessage.newBuilder()
193232
.setAuthor(Types.PublicKey.newBuilder()
@@ -219,6 +258,7 @@ private void checkRequiredFieldsSet() {
219258
undefinedFields =
220259
transactionId == null ? undefinedFields + " transactionId" : undefinedFields;
221260
undefinedFields = payload == null ? undefinedFields + " payload" : undefinedFields;
261+
undefinedFields = keys == null ? undefinedFields + " keys" : undefinedFields;
222262
if (!undefinedFields.isEmpty()) {
223263
throw new IllegalStateException(
224264
"Following field(s) are required but weren't set: " + undefinedFields);

exonum-java-binding/common/src/test/java/com/exonum/binding/common/message/TransactionMessageBuilderTest.java

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ void messageBuilderTest() {
6161
.serviceId(SERVICE_ID)
6262
.transactionId(TRANSACTION_ID)
6363
.payload(payload)
64-
.sign(keys, cryptoFunction);
64+
.signedWith(keys, cryptoFunction)
65+
.build();
6566

6667
// Check the message has correct attributes
6768
assertThat(message.getServiceId(), is(SERVICE_ID));
@@ -82,7 +83,7 @@ void invalidKeyLengthTest() {
8283
KeyPair keys = KeyPair.createKeyPair(Bytes.bytes(0x00, 0x01), publicKeyBytes);
8384

8485
Exception e = assertThrows(IllegalArgumentException.class,
85-
() -> messageBuilder.sign(keys, CRYPTO));
86+
() -> messageBuilder.sign(keys));
8687

8788
assertThat(e.getMessage(), allOf(
8889
containsString(String.valueOf(publicKeyBytes.length)),
@@ -105,7 +106,7 @@ void payloadFromByteBufferWithCustomPositionAndLimitTest() {
105106
.serviceId(SERVICE_ID)
106107
.transactionId(TRANSACTION_ID)
107108
.payload(payloadBuffer)
108-
.sign(CRYPTO.generateKeyPair(), CRYPTO);
109+
.sign(CRYPTO.generateKeyPair());
109110

110111
assertThat(message.getPayload().toByteArray(), is(payload));
111112
}
@@ -123,25 +124,31 @@ private static List<Executable> notProperlyFilledMessagesSource() {
123124
return ImmutableList.of(
124125
() -> TransactionMessage.builder()
125126
.serviceId(SERVICE_ID)
126-
.sign(keyPair, CRYPTO),
127+
.signedWith(keyPair)
128+
.build(),
127129
() -> TransactionMessage.builder()
128130
.serviceId(SERVICE_ID)
129131
.transactionId(TRANSACTION_ID)
130-
.sign(keyPair, CRYPTO),
132+
.signedWith(keyPair)
133+
.build(),
131134
() -> TransactionMessage.builder()
132135
.transactionId(TRANSACTION_ID)
133-
.sign(keyPair, CRYPTO),
136+
.signedWith(keyPair)
137+
.build(),
134138
() -> TransactionMessage.builder()
135139
.serviceId(SERVICE_ID)
136140
.payload(payload)
137-
.sign(keyPair, CRYPTO),
141+
.signedWith(keyPair)
142+
.build(),
138143
() -> TransactionMessage.builder()
139144
.transactionId(TRANSACTION_ID)
140145
.payload(payload)
141-
.sign(keyPair, CRYPTO),
146+
.signedWith(keyPair)
147+
.build(),
142148
() -> TransactionMessage.builder()
143149
.payload(payload)
144-
.sign(keyPair, CRYPTO)
150+
.signedWith(keyPair)
151+
.build()
145152
);
146153
}
147154

exonum-java-binding/common/src/test/java/com/exonum/binding/common/serialization/TransactionMessageSerializerTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ void roundTrip() {
4646
.serviceId((short) 1)
4747
.transactionId((short) 2)
4848
.payload(payload)
49-
.sign(keys, cryptoFunction);
49+
.signedWith(keys, cryptoFunction)
50+
.build();
5051

5152
roundTripTest(message, serializer);
5253
}

exonum-java-binding/common/src/test/java/com/exonum/binding/common/serialization/json/JsonSerializerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ private static TransactionMessage aMessage() {
8383
.serviceId(1)
8484
.transactionId(2)
8585
.payload(bytes())
86-
.sign(keyPair, cryptoFunction);
86+
.sign(keyPair);
8787
}
8888

8989
private static class Wrapper<T> {

exonum-java-binding/common/src/test/java/com/exonum/binding/common/serialization/json/TransactionMessageGsonSerializerTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,12 @@ private static List<TransactionMessage> source() {
6060
.serviceId(Short.MIN_VALUE)
6161
.transactionId(Short.MAX_VALUE)
6262
.payload(bytes())
63-
.sign(keys, ed25519()),
63+
.sign(keys),
6464
TransactionMessage.builder()
6565
.serviceId((short) 0)
6666
.transactionId((short) 127)
6767
.payload(bytes(0x00, 0x01, 0x02))
68-
.sign(keys, ed25519())
68+
.sign(keys)
6969
);
7070
}
7171

exonum-java-binding/core/rust/src/testkit/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use exonum::{
2828
use exonum_testkit::{TestKit, TestKitBuilder};
2929
use exonum_time::{time_provider::TimeProvider, TimeServiceFactory};
3030
use jni::{
31-
objects::{JObject, JValue},
31+
objects::{JClass, JObject, JValue},
3232
sys::{jboolean, jbyteArray, jobjectArray, jshort},
3333
Executor, JNIEnv,
3434
};
@@ -100,7 +100,7 @@ pub extern "system" fn Java_com_exonum_binding_testkit_TestKit_nativeCreateTestK
100100
#[no_mangle]
101101
pub extern "system" fn Java_com_exonum_binding_testkit_TestKit_nativeFreeTestKit(
102102
env: JNIEnv,
103-
_: JObject,
103+
_: JClass,
104104
handle: Handle,
105105
) {
106106
drop_handle::<TestKit>(&env, handle)

exonum-java-binding/core/src/main/java/com/exonum/binding/core/runtime/RuntimeTransport.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ private void logApiMountEvent(ServiceWrapper service, String serviceApiPath, Rou
9494
String serviceName = service.getName();
9595
int port = server.getActualPort().orElse(0);
9696
// Currently the API is mounted on *all* interfaces, see VertxServer#start
97-
logger.info("Service {} API is mounted at :{}{}", serviceName, port, serviceApiPath);
97+
logger.info("Service {} API is mounted at <host>::{}{}", serviceName, port, serviceApiPath);
9898

9999
// Log the full path to one of the service endpoint
100100
serviceRoutes.stream()

exonum-java-binding/cryptocurrency-demo/src/test/java/com/exonum/binding/cryptocurrency/transactions/TransactionUtils.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
package com.exonum.binding.cryptocurrency.transactions;
1818

19-
import com.exonum.binding.common.crypto.CryptoFunction;
20-
import com.exonum.binding.common.crypto.CryptoFunctions;
2119
import com.exonum.binding.common.crypto.KeyPair;
2220
import com.exonum.binding.common.crypto.PublicKey;
2321
import com.exonum.binding.common.message.TransactionMessage;
@@ -28,8 +26,6 @@
2826
*/
2927
public final class TransactionUtils {
3028

31-
private static final CryptoFunction CRYPTO_FUNCTION = CryptoFunctions.ed25519();
32-
3329
static final long DEFAULT_INITIAL_BALANCE = 100L;
3430

3531
/**
@@ -42,7 +38,7 @@ static TransactionMessage newCreateWalletTransaction(
4238
.payload(newCreateWalletTxPayload(initialBalance))
4339
.serviceId(serviceId)
4440
.transactionId(CreateWalletTx.ID)
45-
.sign(ownerKeyPair, CRYPTO_FUNCTION);
41+
.sign(ownerKeyPair);
4642
}
4743

4844
/**
@@ -65,7 +61,7 @@ static TransactionMessage newTransferTransaction(
6561
.payload(newTransferTxPayload(seed, receiverKey, sum))
6662
.serviceId(serviceId)
6763
.transactionId(TransferTx.ID)
68-
.sign(ownerKeyPair, CRYPTO_FUNCTION);
64+
.sign(ownerKeyPair);
6965
}
7066

7167
/**

exonum-java-binding/integration-tests/src/test/java/com/exonum/binding/test/BlockchainIntegrationTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ private static TransactionMessage createTestTransactionMessage(String key, Strin
450450
.setKey(key)
451451
.setValue(value)
452452
.build())
453-
.sign(KEY_PAIR, CRYPTO_FUNCTION);
453+
.sign(KEY_PAIR);
454454
}
455455

456456
/**
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Auto-generated sources for proto messages
2+
exonum_java_runtime_plugin/proto/

0 commit comments

Comments
 (0)