Skip to content

Commit a4862ec

Browse files
committed
Make validation for max description length configurable
Signed-off-by: Takeda <[email protected]>
1 parent de2e942 commit a4862ec

30 files changed

+251
-231
lines changed

client/src/main/java/jp/co/soramitsu/iroha/java/BlocksQuery.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,19 +52,19 @@ public Queries.BlocksQuery buildUnsigned() {
5252
return q.build();
5353
}
5454

55-
public static BlocksQueryBuilder builder(String accountId, Instant time, long counter) {
56-
return new BlocksQueryBuilder(accountId, time, counter);
55+
public static BlocksQueryBuilder builder(String accountId, Instant time, long counter, FieldValidator.Config config) {
56+
return new BlocksQueryBuilder(accountId, time, counter, config);
5757
}
5858

59-
public static BlocksQueryBuilder builder(String accountId, Date time, long counter) {
60-
return new BlocksQueryBuilder(accountId, time, counter);
59+
public static BlocksQueryBuilder builder(String accountId, Date time, long counter, FieldValidator.Config config) {
60+
return new BlocksQueryBuilder(accountId, time, counter, config);
6161
}
6262

63-
public static BlocksQueryBuilder builder(String accountId, Long time, long counter) {
64-
return new BlocksQueryBuilder(accountId, time, counter);
63+
public static BlocksQueryBuilder builder(String accountId, Long time, long counter, FieldValidator.Config config) {
64+
return new BlocksQueryBuilder(accountId, time, counter, config);
6565
}
6666

67-
public static BlocksQueryBuilder builder(String accountId, long counter) {
68-
return builder(accountId, System.currentTimeMillis(), counter);
67+
public static BlocksQueryBuilder builder(String accountId, long counter, FieldValidator.Config config) {
68+
return builder(accountId, System.currentTimeMillis(), counter, config);
6969
}
7070
}

client/src/main/java/jp/co/soramitsu/iroha/java/BlocksQueryBuilder.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import jp.co.soramitsu.crypto.ed25519.Ed25519Sha3.CryptoException;
1111

1212
public class BlocksQueryBuilder {
13-
13+
private final FieldValidator.Config config;
1414
private FieldValidator validator;
1515

1616
private QueryPayloadMeta.Builder meta = QueryPayloadMeta.newBuilder();
@@ -25,20 +25,21 @@ private void init(String accountId, Long time, long counter) {
2525
setCounter(counter);
2626
}
2727

28-
public BlocksQueryBuilder(String accountId, Instant time, long counter) {
29-
init(accountId, time.toEpochMilli(), counter);
28+
public BlocksQueryBuilder(String accountId, Instant time, long counter, FieldValidator.Config config) {
29+
this(accountId, time.toEpochMilli(), counter, config);
3030
}
3131

32-
public BlocksQueryBuilder(String accountId, Date time, long counter) {
33-
init(accountId, time.getTime(), counter);
32+
public BlocksQueryBuilder(String accountId, Date time, long counter, FieldValidator.Config config) {
33+
this(accountId, time.getTime(), counter, config);
3434
}
3535

36-
public BlocksQueryBuilder(String accountId, Long time, long counter) {
36+
public BlocksQueryBuilder(String accountId, Long time, long counter, FieldValidator.Config config) {
37+
this.config = config;
3738
init(accountId, time, counter);
3839
}
3940

4041
public BlocksQueryBuilder enableValidation() {
41-
this.validator = new FieldValidator();
42+
this.validator = new FieldValidator(this.config);
4243
return this;
4344
}
4445

client/src/main/java/jp/co/soramitsu/iroha/java/FieldValidator.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,20 @@
1414
* Stateless validator for transaction and query fields.
1515
*/
1616
public class FieldValidator {
17+
private final int maxDescriptionLength;
18+
19+
static final class Config {
20+
int maxDescriptionLength;
21+
Config(int maxDescriptionLength) {
22+
this.maxDescriptionLength = maxDescriptionLength;
23+
}
24+
}
25+
26+
public static final Config defaultConfig = new Config(64);
27+
28+
FieldValidator(Config config){
29+
this.maxDescriptionLength = config.maxDescriptionLength;
30+
}
1731

1832
public void checkAmount(@NonNull String amount) {
1933
BigDecimal am;
@@ -154,7 +168,7 @@ public void checkEvmAddress(@NonNull String address) {
154168
}
155169
}
156170

157-
public void checkPublicKey(@NonNull byte[] peerKey) {
171+
public void checkPublicKey(byte[] peerKey) {
158172
if (peerKey.length != 32 && peerKey.length != 35) {
159173
throw new ValidationException(PUBKEY, "Public key must be 32 or 35 bytes length, got '%d'",
160174
peerKey.length);
@@ -205,9 +219,9 @@ public void checkDescription(String description) {
205219
}
206220

207221
int len = description.length();
208-
if (len > 64) {
209-
throw new ValidationException(DESCRIPTION, "Max length is 64, given string length is '%d'",
210-
len);
222+
if (len > this.maxDescriptionLength) {
223+
throw new ValidationException(DESCRIPTION, String.format("Max length is '%d', given string length is '%d'",
224+
this.maxDescriptionLength, len));
211225
}
212226
}
213227

client/src/main/java/jp/co/soramitsu/iroha/java/Query.java

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -63,35 +63,35 @@ public Queries.Query buildUnsigned() {
6363
return q.build();
6464
}
6565

66-
public static QueryBuilder builder(String accountId, Long time, long counter) {
67-
return new QueryBuilder(accountId, time, counter);
66+
public static QueryBuilder builder(String accountId, Long time, long counter, FieldValidator.Config config) {
67+
return new QueryBuilder(accountId, time, counter, config);
6868
}
6969

70-
public static QueryBuilder builder(String accountId, Date time, long counter) {
71-
return new QueryBuilder(accountId, time, counter);
70+
public static QueryBuilder builder(String accountId, Date time, long counter, FieldValidator.Config config) {
71+
return new QueryBuilder(accountId, time, counter, config);
7272
}
7373

74-
public static QueryBuilder builder(String accountId, Instant time, long counter) {
75-
return new QueryBuilder(accountId, time, counter);
74+
public static QueryBuilder builder(String accountId, Instant time, long counter, FieldValidator.Config config) {
75+
return new QueryBuilder(accountId, time, counter, config);
7676
}
7777

78-
public static QueryBuilder builder(String accountId, long counter) {
79-
return new QueryBuilder(accountId, Instant.now(), counter);
78+
public static QueryBuilder builder(String accountId, long counter, FieldValidator.Config config) {
79+
return new QueryBuilder(accountId, Instant.now(), counter, config);
8080
}
8181

82-
public static QueryBuilder builder(String accountId, Long time, long counter, SignatureBuilder signatureBuilder) {
83-
return new QueryBuilder(accountId, time, counter, signatureBuilder);
82+
public static QueryBuilder builder(String accountId, Long time, long counter, SignatureBuilder signatureBuilder, FieldValidator.Config config) {
83+
return new QueryBuilder(accountId, time, counter, signatureBuilder, config);
8484
}
8585

86-
public static QueryBuilder builder(String accountId, Date time, long counter, SignatureBuilder signatureBuilder) {
87-
return new QueryBuilder(accountId, time, counter, signatureBuilder);
86+
public static QueryBuilder builder(String accountId, Date time, long counter, SignatureBuilder signatureBuilder, FieldValidator.Config config) {
87+
return new QueryBuilder(accountId, time, counter, signatureBuilder, config);
8888
}
8989

90-
public static QueryBuilder builder(String accountId, Instant time, long counter, SignatureBuilder signatureBuilder) {
91-
return new QueryBuilder(accountId, time, counter, signatureBuilder);
90+
public static QueryBuilder builder(String accountId, Instant time, long counter, SignatureBuilder signatureBuilder, FieldValidator.Config config) {
91+
return new QueryBuilder(accountId, time, counter, signatureBuilder, config);
9292
}
9393

94-
public static QueryBuilder builder(String accountId, long counter, SignatureBuilder signatureBuilder) {
95-
return new QueryBuilder(accountId, Instant.now(), counter, signatureBuilder);
94+
public static QueryBuilder builder(String accountId, long counter, SignatureBuilder signatureBuilder, FieldValidator.Config config) {
95+
return new QueryBuilder(accountId, Instant.now(), counter, signatureBuilder, config);
9696
}
9797
}

client/src/main/java/jp/co/soramitsu/iroha/java/QueryAPI.java

Lines changed: 27 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -35,36 +35,29 @@ public class QueryAPI {
3535
private String accountId;
3636
@NonNull
3737
private KeyPair keyPair;
38-
38+
@NonNull
39+
private final FieldValidator.Config config;
3940
// default signature builder
4041
private SignatureBuilder signatureBuilder;
4142

42-
public QueryAPI(IrohaAPI api, String accountId, KeyPair keyPair) {
43-
this.api = api;
44-
this.accountId = accountId;
45-
this.keyPair = keyPair;
46-
signatureBuilder = Ed25519Sha3SignatureBuilder.getInstance();
43+
public QueryAPI(IrohaAPI api, String accountId, KeyPair keyPair, FieldValidator.Config config) {
44+
this(api, accountId, keyPair, Ed25519Sha3SignatureBuilder.getInstance(), config);
4745
}
4846

49-
public QueryAPI(IrohaAPI api, String accountId, KeyPair keyPair, SignatureBuilder signatureBuilder) {
47+
public QueryAPI(IrohaAPI api, String accountId, KeyPair keyPair, SignatureBuilder signatureBuilder, FieldValidator.Config config) {
5048
this.api = api;
5149
this.accountId = accountId;
5250
this.keyPair = keyPair;
5351
this.signatureBuilder = signatureBuilder;
52+
this.config = config;
5453
}
5554

56-
public QueryAPI(IrohaAPI api, Account account) {
57-
this.api = api;
58-
this.accountId = account.getId();
59-
this.keyPair = account.getKeyPair();
60-
signatureBuilder = Ed25519Sha3SignatureBuilder.getInstance();
55+
public QueryAPI(IrohaAPI api, Account account, FieldValidator.Config config) {
56+
this(api, account.getId(), account.getKeyPair(), Ed25519Sha3SignatureBuilder.getInstance(), config);
6157
}
6258

63-
public QueryAPI(IrohaAPI api, Account account, SignatureBuilder signatureBuilder) {
64-
this.api = api;
65-
this.accountId = account.getId();
66-
this.keyPair = account.getKeyPair();
67-
this.signatureBuilder = signatureBuilder;
59+
public QueryAPI(IrohaAPI api, Account account, SignatureBuilder signatureBuilder, FieldValidator.Config config) {
60+
this(api, account.getId(), account.getKeyPair(), signatureBuilder, config);
6861
}
6962

7063
private static AtomicInteger counter = new AtomicInteger(1);
@@ -77,7 +70,7 @@ private void checkErrorResponse(QueryResponse response) {
7770
}
7871

7972
public EngineReceiptsResponse getEngineReceipts(String txHash) {
80-
val q = Query.builder(this.accountId, counter.getAndIncrement(), signatureBuilder)
73+
val q = Query.builder(this.accountId, counter.getAndIncrement(), signatureBuilder, this.config)
8174
.getEngineReceipts(txHash)
8275
.buildSigned(keyPair);
8376

@@ -89,7 +82,7 @@ public EngineReceiptsResponse getEngineReceipts(String txHash) {
8982
}
9083

9184
public PeersResponse getPeers() {
92-
val q = Query.builder(this.accountId, counter.getAndIncrement(), signatureBuilder)
85+
val q = Query.builder(this.accountId, counter.getAndIncrement(), signatureBuilder, this.config)
9386
.getPeers()
9487
.buildSigned(keyPair);
9588

@@ -111,7 +104,7 @@ public String getAccountDetails(
111104
String writer,
112105
String key
113106
) {
114-
val q = Query.builder(this.accountId, counter.getAndIncrement(), signatureBuilder)
107+
val q = Query.builder(this.accountId, counter.getAndIncrement(), signatureBuilder, this.config)
115108
.getAccountDetail(accountId, writer, key)
116109
.buildSigned(keyPair);
117110

@@ -132,7 +125,7 @@ public AccountDetailResponse getAccountDetails(
132125
String accountDetailRecordIdWriter,
133126
String accountDetailRecordIdKey
134127
) {
135-
val q = Query.builder(this.accountId, counter.getAndIncrement(), signatureBuilder)
128+
val q = Query.builder(this.accountId, counter.getAndIncrement(), signatureBuilder, this.config)
136129
.getAccountDetail(
137130
accountId,
138131
writer,
@@ -160,7 +153,7 @@ public AccountDetailResponse getAccountDetails(
160153
}
161154

162155
public AccountResponse getAccount(String accountId) {
163-
val q = Query.builder(this.accountId, counter.getAndIncrement(), signatureBuilder)
156+
val q = Query.builder(this.accountId, counter.getAndIncrement(), signatureBuilder, this.config)
164157
.getAccount(accountId)
165158
.buildSigned(keyPair);
166159

@@ -172,7 +165,7 @@ public AccountResponse getAccount(String accountId) {
172165
}
173166

174167
public BlockResponse getBlock(Long height) {
175-
val q = Query.builder(this.accountId, counter.getAndIncrement(), signatureBuilder)
168+
val q = Query.builder(this.accountId, counter.getAndIncrement(), signatureBuilder, this.config)
176169
.getBlock(height)
177170
.buildSigned(keyPair);
178171

@@ -190,7 +183,7 @@ public TransactionsPageResponse getAccountTransactions(String accountId,
190183
Timestamp lastTxTime,
191184
Integer firstTxHeight,
192185
Integer lastTxHeight) {
193-
val q = Query.builder(this.accountId, counter.getAndIncrement(), signatureBuilder)
186+
val q = Query.builder(this.accountId, counter.getAndIncrement(), signatureBuilder, this.config)
194187
.getAccountTransactions(accountId, pageSize, firstHashHex, ordering, firstTxTime, lastTxTime, firstTxHeight, lastTxHeight)
195188
.buildSigned(keyPair);
196189

@@ -243,7 +236,7 @@ public TransactionsPageResponse getAccountAssetTransactions(String accountId,
243236
Integer firstTxHeight,
244237
Integer lastTxHeight) {
245238

246-
val q = Query.builder(this.accountId, counter.getAndIncrement(), signatureBuilder)
239+
val q = Query.builder(this.accountId, counter.getAndIncrement(), signatureBuilder, this.config)
247240
.getAccountAssetTransactions(accountId, assetId, pageSize, firstHashHex, ordering, firstTxTime, lastTxTime, firstTxHeight, lastTxHeight)
248241
.buildSigned(keyPair);
249242

@@ -283,7 +276,7 @@ public TransactionsResponse getTransactions(List<byte[]> hashes) {
283276
}
284277

285278
public TransactionsResponse getTransactions(Iterable<String> hashes) {
286-
val q = Query.builder(this.accountId, counter.getAndIncrement(), signatureBuilder)
279+
val q = Query.builder(this.accountId, counter.getAndIncrement(), signatureBuilder, this.config)
287280
.getTransactions(hashes)
288281
.buildSigned(keyPair);
289282

@@ -295,7 +288,7 @@ public TransactionsResponse getTransactions(Iterable<String> hashes) {
295288
}
296289

297290
public AssetResponse getAssetInfo(String assetId) {
298-
val q = Query.builder(this.accountId, counter.getAndIncrement(), signatureBuilder)
291+
val q = Query.builder(this.accountId, counter.getAndIncrement(), signatureBuilder, this.config)
299292
.getAssetInfo(assetId)
300293
.buildSigned(keyPair);
301294

@@ -313,7 +306,7 @@ public AssetResponse getAssetInfo(String assetId) {
313306
*/
314307
@Deprecated
315308
public AccountAssetResponse getAccountAssets(String accountId) {
316-
val q = Query.builder(this.accountId, counter.getAndIncrement(), signatureBuilder)
309+
val q = Query.builder(this.accountId, counter.getAndIncrement(), signatureBuilder, this.config)
317310
.getAccountAssets(accountId)
318311
.buildSigned(keyPair);
319312

@@ -329,7 +322,7 @@ public AccountAssetResponse getAccountAssets(
329322
Integer pageSize,
330323
String firstAssetId
331324
) {
332-
val q = Query.builder(this.accountId, counter.getAndIncrement(), signatureBuilder)
325+
val q = Query.builder(this.accountId, counter.getAndIncrement(), signatureBuilder, this.config)
333326
.getAccountAssets(accountId, pageSize, firstAssetId)
334327
.buildSigned(keyPair);
335328

@@ -348,7 +341,7 @@ public AccountAssetResponse getAccountAssets(
348341
}
349342

350343
public SignatoriesResponse getSignatories(String accountId) {
351-
val q = Query.builder(this.accountId, counter.getAndIncrement(), signatureBuilder)
344+
val q = Query.builder(this.accountId, counter.getAndIncrement(), signatureBuilder, this.config)
352345
.getSignatories(accountId)
353346
.buildSigned(keyPair);
354347

@@ -366,7 +359,7 @@ public SignatoriesResponse getSignatories(String accountId) {
366359
*/
367360
@Deprecated
368361
public TransactionsResponse getPendingTransactions() {
369-
val q = Query.builder(this.accountId, counter.getAndIncrement(), signatureBuilder)
362+
val q = Query.builder(this.accountId, counter.getAndIncrement(), signatureBuilder, this.config)
370363
.getPendingTransactions()
371364
.buildSigned(keyPair);
372365

@@ -386,7 +379,7 @@ public TransactionsResponse getPendingTransactions(
386379
Integer firstTxHeight,
387380
Integer lastTxHeight
388381
) {
389-
val q = Query.builder(this.accountId, counter.getAndIncrement(), signatureBuilder)
382+
val q = Query.builder(this.accountId, counter.getAndIncrement(), signatureBuilder, this.config)
390383
.getPendingTransactions(pageSize, firstHashHex, ordering, firstTxTime, lastTxTime, firstTxHeight, lastTxHeight)
391384
.buildSigned(keyPair);
392385

@@ -426,7 +419,7 @@ public TransactionsResponse getPendingTransactions(
426419
}
427420

428421
public RolesResponse getRoles() {
429-
val q = Query.builder(this.accountId, counter.getAndIncrement(), signatureBuilder)
422+
val q = Query.builder(this.accountId, counter.getAndIncrement(), signatureBuilder, this.config)
430423
.getRoles()
431424
.buildSigned(keyPair);
432425

@@ -438,7 +431,7 @@ public RolesResponse getRoles() {
438431
}
439432

440433
public RolePermissionsResponse getRolePermissions(String roleId) {
441-
val q = Query.builder(this.accountId, counter.getAndIncrement(), signatureBuilder)
434+
val q = Query.builder(this.accountId, counter.getAndIncrement(), signatureBuilder, this.config)
442435
.getRolePermissions(roleId)
443436
.buildSigned(keyPair);
444437

0 commit comments

Comments
 (0)