Skip to content

Commit 69ba93e

Browse files
authored
<feat>(transaction): add extra_data field for transaction (#710)
* <feat>(transaction): add extra_data field for transaction * fix memory leak by jni key pair * modify java-sdk version
1 parent 6538e00 commit 69ba93e

File tree

10 files changed

+180
-18
lines changed

10 files changed

+180
-18
lines changed

build.gradle

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ ext {
2525
junitVersion = '4.13.2'
2626
commonsCollections4Version = "4.4"
2727
guavaVersion = '30.1.1-jre'
28-
bcosSdkJniVersion = "3.0.0"
28+
bcosSdkJniVersion = "3.1.2-SNAPSHOT"
2929
slf4jApiVerison = '2.0.3'
3030
mockitoVersion = '4.8.0'
3131
}
@@ -34,7 +34,8 @@ ext {
3434
// integrationTest.mustRunAfter test
3535
allprojects {
3636
group = 'org.fisco-bcos.java-sdk'
37-
version = '3.1.1'
37+
version = '3.1.2-SNAPSHOT'
38+
3839
apply plugin: 'maven-publish'
3940
apply plugin: 'idea'
4041
apply plugin: 'eclipse'

src/integration-test/java/org/fisco/bcos/sdk/v3/test/BcosSDKTest.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,11 @@ public void testHelloWorldInSolidity() throws ConfigException, JniException, Con
282282
ConfigOption configOption = Config.load(configFile);
283283
Client client = Client.build(GROUP, configOption);
284284

285+
String extraData = "HelloWorld ExtraData";
286+
client.setExtraData(extraData);
287+
288+
Assert.assertTrue(extraData.equals(client.getExtraData()));
289+
285290
// CryptoSuite cryptoSuite = new CryptoSuite(CryptoType.ECDSA_TYPE);
286291
// CryptoSuite cryptoSuite = new CryptoSuite(CryptoType.SM_TYPE);
287292
CryptoSuite cryptoSuite = client.getCryptoSuite();
@@ -316,6 +321,16 @@ public void onError(Response errorResponse) {
316321
TransactionReceipt receipt = helloWorld.set("fisco hello");
317322
System.out.println("helloworld set : fisco hello, status=" + receipt.getStatus());
318323
System.out.println(receipt);
324+
Assert.assertTrue(receipt.isStatusOK());
325+
326+
String txHash = receipt.getTransactionHash();
327+
BcosTransaction transaction1 = client.getTransaction(txHash, false);
328+
Assert.assertEquals(extraData, transaction1.getResult().getExtraData());
329+
Assert.assertTrue(extraData.equals(transaction1.getResult().getExtraData()));
330+
331+
BcosTransactionReceipt transactionReceipt = client.getTransactionReceipt(txHash, false);
332+
Assert.assertTrue(extraData.equals(transactionReceipt.getResult().getExtraData()));
333+
319334
// get 2nd block
320335
block1 =
321336
client.getBlockByNumber(
@@ -373,9 +388,10 @@ public void testTransactionAssemble() throws ConfigException, JniException, Cont
373388
.sign(transactionDataHash, client.getCryptoSuite().getCryptoKeyPair());
374389

375390
String transactionDataHashSignedData2 = Hex.toHexString(sign.encode());
391+
String extraData = "extraData";
376392
String signedMessage =
377393
TransactionBuilderJniObj.createSignedTransaction(
378-
transactionData, transactionDataHashSignedData2, transactionDataHash, 0);
394+
transactionData, transactionDataHashSignedData2, transactionDataHash, 0, extraData);
379395

380396
TransactionPusherService txPusher = new TransactionPusherService(client);
381397
TransactionReceipt receipt2 = txPusher.push(signedMessage);

src/integration-test/java/org/fisco/bcos/sdk/v3/test/transaction/manager/AssembleTransactionProcessorTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import org.junit.FixMethodOrder;
4949
import org.junit.Test;
5050
import org.junit.runners.MethodSorters;
51+
import org.mockito.internal.matchers.Null;
5152

5253
/**
5354
* TransactionProcessorTest @Description: TransactionProcessorTest
@@ -431,6 +432,16 @@ public void test8ComplexSetBytesFuture() throws Exception {
431432
r -> {
432433
Assert.assertEquals(0, response.getTransactionReceipt().getStatus());
433434
});
435+
436+
TxPair txPair0 =
437+
transactionProcessor.createSignedTransaction(
438+
contractAddress, data, this.cryptoKeyPair, 0, "a");
439+
CompletableFuture<TransactionReceipt> future0 =
440+
transactionProcessor.sendTransactionAsync(txPair0.getSignedTx());
441+
future0.thenAccept(
442+
r -> {
443+
Assert.assertEquals(0, response.getTransactionReceipt().getStatus());
444+
});
434445
}
435446

436447
@Test

src/main/java/org/fisco/bcos/sdk/v3/client/Client.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ static Client build(String groupId, ConfigOption configOption, long nativePointe
9999
/** @return native pointer */
100100
long getNativePointer();
101101

102+
String getExtraData();
103+
104+
void setExtraData(String extraData);
105+
102106
/**
103107
* Get CryptoSuite
104108
*

src/main/java/org/fisco/bcos/sdk/v3/client/ClientImpl.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ public class ClientImpl implements Client {
7575
private Boolean authCheck = false;
7676
private boolean serialExecute;
7777
private Boolean smCrypto;
78+
private String extraData = "";
7879
// ------------basic group info --------------
7980

8081
// ------------ runtime info -----------------
@@ -160,6 +161,16 @@ protected ClientImpl(String groupID, ConfigOption configOption, long nativePoint
160161
isWASM());
161162
}
162163

164+
@Override
165+
public String getExtraData() {
166+
return extraData;
167+
}
168+
169+
@Override
170+
public void setExtraData(String extraData) {
171+
this.extraData = extraData;
172+
}
173+
163174
@Override
164175
public long getNativePointer() {
165176
return rpcJniObj.getNativePointer();

src/main/java/org/fisco/bcos/sdk/v3/client/protocol/model/JsonTransactionResponse.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public class JsonTransactionResponse {
3939
private String input;
4040
private String chainID;
4141
private String groupID;
42+
private String extraData;
4243
private String signature;
4344
private long importTime;
4445
private List<MerkleProofUnit> transactionProof;
@@ -184,6 +185,7 @@ public boolean equals(Object o) {
184185
&& Objects.equals(this.blockLimit, that.blockLimit)
185186
&& Objects.equals(this.chainID, that.chainID)
186187
&& Objects.equals(this.groupID, that.groupID)
188+
&& Objects.equals(this.extraData, that.extraData)
187189
&& Objects.equals(this.signature, that.signature);
188190
}
189191

@@ -200,6 +202,7 @@ public int hashCode() {
200202
this.blockLimit,
201203
this.chainID,
202204
this.groupID,
205+
this.extraData,
203206
this.signature);
204207
}
205208

@@ -241,9 +244,19 @@ public String toString() {
241244
+ '\''
242245
+ ", signature="
243246
+ this.signature
247+
+ ", extraData="
248+
+ this.extraData
244249
+ '}';
245250
}
246251

252+
public String getExtraData() {
253+
return extraData;
254+
}
255+
256+
public void setExtraData(String extraData) {
257+
this.extraData = extraData;
258+
}
259+
247260
public long getImportTime() {
248261
return this.importTime;
249262
}

src/main/java/org/fisco/bcos/sdk/v3/crypto/keypair/CryptoKeyPair.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public abstract class CryptoKeyPair {
6969
protected String signatureAlgorithm;
7070

7171
// for jni transaction sign
72-
protected long jniKeyPair;
72+
protected long jniKeyPair = 0;
7373

7474
public CryptoKeyPair() {}
7575

@@ -305,6 +305,29 @@ protected String getKeyStoreFilePath(String address, String postFix) {
305305
}
306306

307307
public void destroy() {
308-
KeyPairJniObj.destroyJniKeyPair(this.jniKeyPair);
308+
releaseJni();
309+
}
310+
311+
public void releaseJni() {
312+
if (this.jniKeyPair != 0) {
313+
KeyPairJniObj.destroyJniKeyPair(this.jniKeyPair);
314+
if (logger.isTraceEnabled()) {
315+
logger.trace("finalize, jni key pair: {}", this.jniKeyPair);
316+
}
317+
318+
this.jniKeyPair = 0;
319+
}
320+
}
321+
322+
@Override
323+
protected void finalize() {
324+
try {
325+
super.finalize();
326+
releaseJni();
327+
} catch (Exception e) {
328+
329+
} catch (Throwable throwable) {
330+
// throwable.printStackTrace();
331+
}
309332
}
310333
}

src/main/java/org/fisco/bcos/sdk/v3/model/TransactionReceipt.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public class TransactionReceipt {
3636
private String input;
3737
private String from;
3838
private String to;
39+
private String extraData;
3940
private List<MerkleProofUnit> transactionProof;
4041
private List<MerkleProofUnit> receiptProof;
4142
private String message;
@@ -247,6 +248,14 @@ public void setTransactionProof(List<MerkleProofUnit> transactionProof) {
247248
this.transactionProof = transactionProof;
248249
}
249250

251+
public String getExtraData() {
252+
return extraData;
253+
}
254+
255+
public void setExtraData(String extraData) {
256+
this.extraData = extraData;
257+
}
258+
250259
@Override
251260
public boolean equals(Object o) {
252261
if (this == o) return true;
@@ -264,6 +273,7 @@ public boolean equals(Object o) {
264273
&& Objects.equals(this.status, that.status)
265274
&& Objects.equals(this.input, that.input)
266275
&& Objects.equals(this.output, that.output)
276+
&& Objects.equals(this.extraData, that.extraData)
267277
&& Objects.equals(this.transactionProof, that.transactionProof)
268278
&& Objects.equals(this.receiptProof, that.receiptProof);
269279
}
@@ -283,6 +293,7 @@ public int hashCode() {
283293
this.status,
284294
this.input,
285295
this.output,
296+
this.extraData,
286297
this.transactionProof,
287298
this.receiptProof);
288299
}
@@ -318,6 +329,9 @@ public String toString() {
318329
+ ", status='"
319330
+ this.status
320331
+ '\''
332+
+ ", extraData='"
333+
+ this.extraData
334+
+ '\''
321335
+ ", input='"
322336
+ this.input
323337
+ '\''

src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/TransactionProcessor.java

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ public TransactionReceipt deployAndGetReceipt(
7070
data,
7171
abi,
7272
cryptoKeyPair == null ? this.cryptoKeyPair : cryptoKeyPair,
73-
txAttribute);
73+
txAttribute,
74+
client.getExtraData());
7475
TransactionReceipt transactionReceipt =
7576
this.client.sendTransaction(txPair.getSignedTx(), false).getTransactionReceipt();
7677
if (Objects.nonNull(transactionReceipt)
@@ -95,7 +96,8 @@ public TransactionReceipt sendTransactionAndGetReceipt(
9596
to,
9697
data,
9798
cryptoKeyPair == null ? this.cryptoKeyPair : cryptoKeyPair,
98-
txAttribute);
99+
txAttribute,
100+
client.getExtraData());
99101
TransactionReceipt transactionReceipt =
100102
this.client.sendTransaction(txPair.getSignedTx(), false).getTransactionReceipt();
101103
if (Objects.nonNull(transactionReceipt)
@@ -106,8 +108,8 @@ public TransactionReceipt sendTransactionAndGetReceipt(
106108
}
107109

108110
@Override
109-
public TransactionReceipt sendTransactionAndGetReceipt(String to, byte[] data, int txAttribute)
110-
throws JniException {
111+
public TransactionReceipt sendTransactionAndGetReceipt(
112+
String to, byte[] data, int txAttribute) {
111113
return sendTransactionAndGetReceipt(to, data, this.cryptoKeyPair, txAttribute);
112114
}
113115

@@ -118,7 +120,9 @@ public String sendTransactionAsync(
118120
CryptoKeyPair cryptoKeyPair,
119121
int txAttribute,
120122
TransactionCallback callback) {
121-
TxPair txPair = this.createSignedTransaction(to, data, cryptoKeyPair, txAttribute);
123+
TxPair txPair =
124+
this.createSignedTransaction(
125+
to, data, cryptoKeyPair, txAttribute, client.getExtraData());
122126
this.client.sendTransactionAsync(txPair.getSignedTx(), false, callback);
123127
return txPair.getTxHash();
124128
}
@@ -158,16 +162,33 @@ public void asyncExecuteCall(CallRequest callRequest, RespCallback<Call> callbac
158162
@Override
159163
public TxPair createDeploySignedTransaction(
160164
String to, byte[] data, String abi, CryptoKeyPair cryptoKeyPair, int txAttribute) {
161-
try {
165+
return createDeploySignedTransaction(
166+
to, data, abi, cryptoKeyPair, txAttribute, client.getExtraData());
167+
}
162168

169+
@Override
170+
public TxPair createSignedTransaction(
171+
String to, byte[] data, CryptoKeyPair cryptoKeyPair, int txAttribute) {
172+
return createSignedTransaction(to, data, cryptoKeyPair, txAttribute, client.getExtraData());
173+
}
174+
175+
@Override
176+
public TxPair createDeploySignedTransaction(
177+
String to,
178+
byte[] data,
179+
String abi,
180+
CryptoKeyPair cryptoKeyPair,
181+
int txAttribute,
182+
String extraData) {
183+
try {
163184
if (log.isDebugEnabled()) {
164185
log.debug(
165-
"createDeploySignedTransaction to: {}, abi: {}, attr: {}",
186+
"createDeploySignedTransaction to: {}, abi: {}, attr: {}, extraData: {}",
166187
to,
167188
abi,
168-
txAttribute);
189+
txAttribute,
190+
extraData);
169191
}
170-
171192
return TransactionBuilderJniObj.createSignedTransaction(
172193
cryptoKeyPair.getJniKeyPair(),
173194
this.groupId,
@@ -176,7 +197,8 @@ public TxPair createDeploySignedTransaction(
176197
Hex.toHexString(data),
177198
Objects.nonNull(abi) ? abi : "",
178199
client.getBlockLimit().longValue(),
179-
txAttribute);
200+
txAttribute,
201+
Objects.nonNull(extraData) ? extraData : "");
180202
} catch (JniException e) {
181203
log.error("jni e: ", e);
182204
return null;
@@ -185,11 +207,19 @@ public TxPair createDeploySignedTransaction(
185207

186208
@Override
187209
public TxPair createSignedTransaction(
188-
String to, byte[] data, CryptoKeyPair cryptoKeyPair, int txAttribute) {
210+
String to,
211+
byte[] data,
212+
CryptoKeyPair cryptoKeyPair,
213+
int txAttribute,
214+
String extraData) {
189215
try {
190216

191217
if (log.isDebugEnabled()) {
192-
log.debug("createSignedTransaction to: {}, attr: {}", to, txAttribute);
218+
log.debug(
219+
"createSignedTransaction to: {}, attr: {}, extraData: {}",
220+
to,
221+
txAttribute,
222+
extraData);
193223
}
194224

195225
return TransactionBuilderJniObj.createSignedTransaction(
@@ -200,7 +230,8 @@ public TxPair createSignedTransaction(
200230
Hex.toHexString(data),
201231
"",
202232
client.getBlockLimit().longValue(),
203-
txAttribute);
233+
txAttribute,
234+
Objects.nonNull(extraData) ? extraData : "");
204235
} catch (JniException e) {
205236
log.error("jni e: ", e);
206237
return null;

0 commit comments

Comments
 (0)