Skip to content
This repository was archived by the owner on Aug 30, 2022. It is now read-only.

Commit 6a3e509

Browse files
committed
Initial changes for defaulting tapos calculation to last irreversible block.
Still allows blocksBehind if the user wishes to use it. Fix several bad javadoc references from the protocol changes.
1 parent a4cb53f commit 6a3e509

11 files changed

Lines changed: 87 additions & 46 deletions

File tree

eosiojava/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ test {
4747

4848
def libraryGroupId = 'one.block'
4949
def libraryArtifactId = 'eosiojava'
50-
def libraryVersion = '0.1.5-eosio2.1'
50+
def libraryVersion = '0.1.6-eosio2.1'
5151

5252
task sourcesJar(type: Jar, dependsOn: classes){
5353
classifier = 'sources'

eosiojava/src/main/java/one/block/eosiojava/error/ErrorConstants.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -266,11 +266,6 @@ private ErrorConstants(){
266266
*/
267267
public static final String TRANSACTION_PROCESSOR_GET_SIGN_DESERIALIZE_TRANS_ERROR = "Error happened on calling deserializeTransaction to refresh transaction object with new values";
268268

269-
/**
270-
* Error message get thrown if {@link IRPCProvider#pushTransaction(PushTransactionRequest)} returns error.
271-
*/
272-
public static final String TRANSACTION_PROCESSOR_RPC_PUSH_TRANSACTION = "Error happened on calling pushTransaction RPC call";
273-
274269
/**
275270
* Error message get thrown if {@link IRPCProvider#sendTransaction(SendTransactionRequest)} returns error.
276271
*/

eosiojava/src/main/java/one/block/eosiojava/models/rpcProvider/TransactionConfig.java

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package one.block.eosiojava.models.rpcProvider;
22

3+
import one.block.eosiojava.interfaces.IRPCProvider;
4+
import one.block.eosiojava.models.rpcProvider.request.GetBlockInfoRequest;
35
import one.block.eosiojava.models.rpcProvider.request.GetBlockRequest;
46
import one.block.eosiojava.models.rpcProvider.response.GetInfoResponse;
57

@@ -21,6 +23,12 @@ public class TransactionConfig {
2123
*/
2224
private static final int DEFAULT_EXPIRES_SECONDS = 5 * 60;
2325

26+
/**
27+
* Default useLastIrreversible to use last irreversible block rather than blocksBehind when
28+
* calculating TAPOS
29+
*/
30+
private static final boolean DEFAULT_USE_LAST_IRREVERSIBLE = true;
31+
2432
/**
2533
* The Expires seconds.
2634
* <br>
@@ -37,6 +45,15 @@ public class TransactionConfig {
3745
*/
3846
private int blocksBehind = DEFAULT_BLOCKS_BEHIND;
3947

48+
/**
49+
* Use the last irreversible block when calculating TAPOS rather than blocks behind.
50+
* <br>
51+
* Mutually exclusive with {@link TransactionConfig#setBlocksBehind(int)}. If set, TAPOS will be calculated
52+
* by fetching the last irreversible block with {@link IRPCProvider#getInfo()} and expire the transaction
53+
* {@link TransactionConfig#setExpiresSeconds(int)} after that block's time.
54+
*/
55+
private boolean useLastIrreversible = DEFAULT_USE_LAST_IRREVERSIBLE;
56+
4057
/**
4158
* Gets the expiration time for the transaction.
4259
* <br>
@@ -64,8 +81,7 @@ public void setExpiresSeconds(int expiresSeconds) {
6481
/**
6582
* Gets blocks behind.
6683
* <br>
67-
* It is an argument to calculate head block number to call {@link
68-
* one.block.eosiojava.interfaces.IRPCProvider#getBlock(GetBlockRequest)}
84+
* It is an argument to calculate head block number to call {@link one.block.eosiojava.interfaces.IRPCProvider#getBlockInfo(GetBlockInfoRequest)}
6985
* @return the blocks behind
7086
*/
7187
public int getBlocksBehind() {
@@ -76,10 +92,28 @@ public int getBlocksBehind() {
7692
* Sets blocks behind.
7793
* <br>
7894
* It is an argument to calculate head block number to call {@link
79-
* one.block.eosiojava.interfaces.IRPCProvider#getBlock(GetBlockRequest)}
95+
* one.block.eosiojava.interfaces.IRPCProvider#getBlockInfo(GetBlockInfoRequest)}
8096
* @param blocksBehind the blocks behind
8197
*/
8298
public void setBlocksBehind(int blocksBehind) {
8399
this.blocksBehind = blocksBehind;
84100
}
101+
102+
/**
103+
* Gets useLastIrreversible.
104+
* <br>
105+
* It is an argument to calculate TAPOS from the last irreversible block rather than blocks behind the head block
106+
* @return useLastIrreversible whether to use the last irreversible block for calculating TAPOS
107+
*/
108+
public boolean getUseLastIrreversible() { return useLastIrreversible; }
109+
110+
/**
111+
* Sets useLastIrreversible.
112+
* <br>
113+
* It is an argument to calculate TAPOS from the last irreversible block rather than blocks behind the head block
114+
* @param useLastIrreversible whether to use the last irreversible block
115+
*/
116+
public void setUseLastIrreversible(boolean useLastIrreversible) {
117+
this.useLastIrreversible = useLastIrreversible;
118+
}
85119
}

eosiojava/src/main/java/one/block/eosiojava/models/rpcProvider/request/GetBlockInfoRequest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import java.math.BigInteger;
77

88
/**
9-
* The request class for getBlockInfo() RPC call {@link one.block.eosiojava.interfaces.IRPCProvider#getBlock(GetBlockInfoRequest)}
9+
* The request class for getBlockInfo() RPC call {@link one.block.eosiojava.interfaces.IRPCProvider#getBlockInfo(GetBlockInfoRequest)}
1010
*/
1111
public class GetBlockInfoRequest {
1212

eosiojava/src/main/java/one/block/eosiojava/models/rpcProvider/request/GetBlockRequest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import org.jetbrains.annotations.NotNull;
55

66
/**
7-
* The request class for getBlock() RPC call {@link one.block.eosiojava.interfaces.IRPCProvider#getBlock(GetBlockRequest)}
7+
* The request class for getBlock() RPC call {@link one.block.eosiojava.interfaces.IRPCProvider#getBlockInfo(GetBlockInfoRequest)}
88
*/
99
public class GetBlockRequest {
1010

eosiojava/src/main/java/one/block/eosiojava/models/rpcProvider/response/GetBlockResponse.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
import java.math.BigInteger;
55
import java.util.List;
66
import java.util.Map;
7+
8+
import one.block.eosiojava.models.rpcProvider.request.GetBlockInfoRequest;
79
import one.block.eosiojava.models.rpcProvider.request.GetBlockRequest;
810

911
/**
10-
* The response of getBlock() RPC call {@link one.block.eosiojava.interfaces.IRPCProvider#getBlock(GetBlockRequest)}
12+
* The response of getBlock() RPC call {@link one.block.eosiojava.interfaces.IRPCProvider#getBlockInfo(GetBlockInfoRequest)}
1113
*/
1214
public class GetBlockResponse {
1315

eosiojava/src/main/java/one/block/eosiojava/models/rpcProvider/response/GetInfoResponse.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.util.List;
66
import one.block.eosiojava.interfaces.IRPCProvider;
77
import one.block.eosiojava.models.rpcProvider.Transaction;
8+
import one.block.eosiojava.models.rpcProvider.request.GetBlockInfoRequest;
89
import one.block.eosiojava.models.rpcProvider.request.GetBlockRequest;
910

1011
/**
@@ -88,7 +89,7 @@ public String getChainId() {
8889

8990
/**
9091
* Gets the head block number. It is an argument used to specify the reference block to call {@link
91-
* one.block.eosiojava.interfaces.IRPCProvider#getBlock(GetBlockRequest)} at {@link
92+
* one.block.eosiojava.interfaces.IRPCProvider#getBlockInfo(GetBlockInfoRequest)} at {@link
9293
* one.block.eosiojava.session.TransactionProcessor#prepare(List)}
9394
* @return the head block number.
9495
*/

eosiojava/src/main/java/one/block/eosiojava/models/rpcProvider/response/PushTransactionResponse.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
/**
1212
* The response of the pushTransaction() RPC call
13-
* {@link one.block.eosiojava.interfaces.IRPCProvider#pushTransaction(PushTransactionRequest)}
1413
*/
1514
public class PushTransactionResponse extends SendTransactionResponse {
1615

eosiojava/src/main/java/one/block/eosiojava/models/rpcProvider/response/SendTransactionResponse.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ public ArrayList<Object> getActionValues() {
6464
* Get the action value at the specified index, if it exists and return it as the passed in type.
6565
* @param index The index of the action value returns to retrieve.
6666
* @param clazz The class type to cast the action value to, if found.
67+
* @param <T> Typed return.
6768
* @return The action value as the desired type or null if not found or is the wrong type.
6869
* @throws ClassCastException if the value cannot be cast to the requested type.
6970
* @throws IndexOutOfBoundsException if an incorrect index is requested.

eosiojava/src/main/java/one/block/eosiojava/session/TransactionProcessor.java

Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,9 @@ public class TransactionProcessor {
175175
* <p>
176176
* - The expiration period for the transaction in seconds
177177
* <p>
178-
* - How many blocks behind
178+
* - Whether to use the last irreversible block or
179+
* <p>
180+
* - How many blocks behind the head block
179181
*/
180182
@NotNull
181183
private TransactionConfig transactionConfig = new TransactionConfig();
@@ -265,7 +267,7 @@ public TransactionProcessor(
265267
* {@link TransactionPrepareInputError} thrown if input is invalid
266268
* <br>
267269
* {@link TransactionPrepareRpcError} thrown if any RPC call ({@link IRPCProvider#getInfo()}
268-
* and {@link IRPCProvider#getBlock(GetBlockRequest)}) return or throw an error
270+
* and {@link IRPCProvider#getBlockInfo(GetBlockInfoRequest)}) return or throw an error
269271
*/
270272
public void prepare(@NotNull List<Action> actions) throws TransactionPrepareError {
271273
this.prepare(actions, new ArrayList<Action>());
@@ -294,7 +296,7 @@ public void prepare(@NotNull List<Action> actions) throws TransactionPrepareErro
294296
* {@link TransactionPrepareInputError} thrown if input is invalid
295297
* <br>
296298
* {@link TransactionPrepareRpcError} thrown if any RPC call ({@link IRPCProvider#getInfo()}
297-
* and {@link IRPCProvider#getBlock(GetBlockRequest)}) return or throw an error
299+
* and {@link IRPCProvider#getBlockInfo(GetBlockInfoRequest)}) return or throw an error
298300
*/
299301
public void prepare(@NotNull List<Action> actions, @NotNull List<Action> contextFreeActions) throws TransactionPrepareError {
300302
prepare(actions, contextFreeActions, new ArrayList<String>());
@@ -322,7 +324,7 @@ public void prepare(@NotNull List<Action> actions, @NotNull List<Action> context
322324
* {@link TransactionPrepareInputError} thrown if inputs are invalid
323325
* <br>
324326
* {@link TransactionPrepareRpcError} thrown if any RPC call ({@link IRPCProvider#getInfo()}
325-
* and {@link IRPCProvider#getBlock(GetBlockRequest)}) return or throw an error
327+
* and {@link IRPCProvider#getBlockInfo(GetBlockInfoRequest)}) return or throw an error
326328
*/
327329
public void prepare(@NotNull List<Action> actions, @NotNull List<Action> contextFreeActions, @NotNull List<String> contextFreeData) throws TransactionPrepareError {
328330
if (actions.isEmpty()) {
@@ -370,48 +372,55 @@ public void prepare(@NotNull List<Action> actions, @NotNull List<Action> context
370372
getInfoResponse.getChainId()));
371373
}
372374

373-
if (preparingTransaction.getExpiration().isEmpty()) {
374-
String strHeadBlockTime = getInfoResponse.getHeadBlockTime();
375-
376-
long headBlockTime;
377-
378-
try {
379-
headBlockTime = DateFormatter.convertBackendTimeToMilli(strHeadBlockTime);
380-
} catch (ParseException e) {
381-
throw new TransactionPrepareError(
382-
ErrorConstants.TRANSACTION_PROCESSOR_HEAD_BLOCK_TIME_PARSE_ERROR, e);
383-
}
384-
385-
int expiresSeconds = this.transactionConfig.getExpiresSeconds();
386-
387-
long expirationTimeInMilliseconds = headBlockTime + expiresSeconds * 1000;
388-
preparingTransaction.setExpiration(DateFormatter
389-
.convertMilliSecondToBackendTimeString(expirationTimeInMilliseconds));
390-
}
391-
392375
// Assigning value to refBlockNum and refBlockPrefix
393376

394-
BigInteger headBlockNum;
377+
BigInteger taposBlockNum;
395378

379+
boolean useLastIrreversibleConfig = this.transactionConfig.getUseLastIrreversible();
396380
int blockBehindConfig = this.transactionConfig.getBlocksBehind();
397381

398-
if (getInfoResponse.getHeadBlockNum().compareTo(BigInteger.valueOf(blockBehindConfig))
399-
> 0) {
400-
headBlockNum = getInfoResponse.getHeadBlockNum()
401-
.subtract(BigInteger.valueOf(blockBehindConfig));
382+
if (useLastIrreversibleConfig) {
383+
taposBlockNum = getInfoResponse.getLastIrreversibleBlockNum();
402384
} else {
403-
headBlockNum = BigInteger.valueOf(blockBehindConfig);
385+
if (getInfoResponse.getHeadBlockNum().compareTo(BigInteger.valueOf(blockBehindConfig))
386+
> 0) {
387+
taposBlockNum = getInfoResponse.getHeadBlockNum()
388+
.subtract(BigInteger.valueOf(blockBehindConfig));
389+
} else {
390+
taposBlockNum = BigInteger.valueOf(blockBehindConfig);
391+
}
404392
}
405393

406394
GetBlockInfoResponse getBlockInfoResponse;
407395
try {
408396
getBlockInfoResponse = this.rpcProvider
409-
.getBlockInfo(new GetBlockInfoRequest(headBlockNum));
397+
.getBlockInfo(new GetBlockInfoRequest(taposBlockNum));
410398
} catch (GetBlockInfoRpcError getBlockInfoRpcError) {
411399
throw new TransactionPrepareRpcError(
412400
ErrorConstants.TRANSACTION_PROCESSOR_PREPARE_RPC_GET_BLOCK_INFO, getBlockInfoRpcError);
413401
}
414402

403+
// Calculate the expiration based on the taposBlockNum expiration
404+
if (preparingTransaction.getExpiration().isEmpty()) {
405+
406+
String strHeadBlockTime = getBlockInfoResponse.getTimestamp();
407+
408+
long taposBlockTime;
409+
410+
try {
411+
taposBlockTime = DateFormatter.convertBackendTimeToMilli(strHeadBlockTime);
412+
} catch (ParseException e) {
413+
throw new TransactionPrepareError(
414+
ErrorConstants.TRANSACTION_PROCESSOR_HEAD_BLOCK_TIME_PARSE_ERROR, e);
415+
}
416+
417+
int expiresSeconds = this.transactionConfig.getExpiresSeconds();
418+
419+
long expirationTimeInMilliseconds = taposBlockTime + expiresSeconds * 1000;
420+
preparingTransaction.setExpiration(DateFormatter
421+
.convertMilliSecondToBackendTimeString(expirationTimeInMilliseconds));
422+
}
423+
415424
// Restrict the refBlockNum to 32 bit unsigned value
416425
BigInteger refBlockNum = getBlockInfoResponse.getBlockNum().and(BigInteger.valueOf(0xffff));
417426
BigInteger refBlockPrefix = getBlockInfoResponse.getRefBlockPrefix();

0 commit comments

Comments
 (0)