Skip to content

Commit 4718d88

Browse files
committed
Work on #76
1 parent cc48489 commit 4718d88

4 files changed

Lines changed: 225 additions & 64 deletions

File tree

core/src/main/java/eu/bittrade/libs/steemj/apis/network/broadcast/NetworkBroadcastApi.java

Lines changed: 61 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package eu.bittrade.libs.steemj.apis.network.broadcast;
22

3+
import eu.bittrade.libs.steemj.apis.network.broadcast.model.BroadcastTransactionSynchronousReturn;
4+
import eu.bittrade.libs.steemj.base.models.SignedBlock;
35
import eu.bittrade.libs.steemj.base.models.SignedTransaction;
46
import eu.bittrade.libs.steemj.communication.CommunicationHandler;
57
import eu.bittrade.libs.steemj.communication.jrpc.JsonRPCRequest;
@@ -26,8 +28,6 @@ private NetworkBroadcastApi() {
2628
* does not mean that the operation has been accepted and has been
2729
* processed. If you want to make sure that this is the case use the
2830
* {@link #broadcastTransactionSynchronous(CommunicationHandler, SignedTransaction)}
29-
* or the
30-
* {@link #broadcastTransactionWithCallback(CommunicationHandler, SignedTransaction)}
3131
* method.
3232
*
3333
* @param communicationHandler
@@ -71,68 +71,88 @@ public static void broadcastTransaction(CommunicationHandler communicationHandle
7171
}
7272

7373
/**
74+
* Broadcast a transaction on the Steem blockchain. This method will
75+
* validate the transaction and return after it has been accepted and
76+
* applied.
7477
*
78+
* @param communicationHandler
79+
* A
80+
* {@link eu.bittrade.libs.steemj.communication.CommunicationHandler
81+
* CommunicationHandler} instance that should be used to send the
82+
* request.
83+
* @param transaction
84+
* The {@link SignedTransaction} object to broadcast.
85+
* @return A {@link BroadcastTransactionSynchronousReturn} object providing
86+
* information about the block in which the transaction has been
87+
* applied.
88+
* @throws SteemCommunicationException
89+
* <ul>
90+
* <li>If the server was not able to answer the request in the
91+
* given time (see
92+
* {@link eu.bittrade.libs.steemj.configuration.SteemJConfig#setResponseTimeout(int)
93+
* setResponseTimeout}).</li>
94+
* <li>If there is a connection problem.</li>
95+
* </ul>
96+
* @throws SteemResponseException
97+
* <ul>
98+
* <li>If the SteemJ is unable to transform the JSON response
99+
* into a Java object.</li>
100+
* <li>If the Server returned an error object.</li>
101+
* </ul>
102+
* @throws SteemInvalidTransactionException
103+
* In case the provided transaction is not valid.
75104
*/
76-
public static void broadcastTransactionWithCallback(CommunicationHandler communicationHandler,
77-
SignedTransaction transaction)
105+
public static BroadcastTransactionSynchronousReturn broadcastTransactionSynchronous(
106+
CommunicationHandler communicationHandler, SignedTransaction transaction)
78107
throws SteemCommunicationException, SteemResponseException, SteemInvalidTransactionException {
79108
JsonRPCRequest requestObject = new JsonRPCRequest();
80-
requestObject.setApiMethod(RequestMethods.BROADCAST_TRANSACTION_WITH_CALLBACK);
109+
requestObject.setApiMethod(RequestMethods.BROADCAST_TRANSACTION_SYNCHRONOUS);
81110
requestObject.setSteemApi(SteemApiType.NETWORK_BROADCAST_API);
82111

83112
if (transaction.getSignatures() == null || transaction.getSignatures().isEmpty()) {
84113
transaction.sign();
85114
}
86115

87-
Object[] parameters = { 123, transaction };
116+
Object[] parameters = { transaction };
88117
requestObject.setAdditionalParameters(parameters);
89118

90-
communicationHandler.performRequest(requestObject, Object.class);
119+
return communicationHandler.performRequest(requestObject, BroadcastTransactionSynchronousReturn.class).get(0);
91120
}
92121

93122
/**
123+
* Broadcast a whole block.
94124
*
95-
* @param transaction
96-
* @return Nothing
125+
* @param communicationHandler
126+
* A
127+
* {@link eu.bittrade.libs.steemj.communication.CommunicationHandler
128+
* CommunicationHandler} instance that should be used to send the
129+
* request.
130+
* @param signedBlock
131+
* The {@link SignedBlock} object to broadcast.
97132
* @throws SteemCommunicationException
133+
* <ul>
134+
* <li>If the server was not able to answer the request in the
135+
* given time (see
136+
* {@link eu.bittrade.libs.steemj.configuration.SteemJConfig#setResponseTimeout(int)
137+
* setResponseTimeout}).</li>
138+
* <li>If there is a connection problem.</li>
139+
* </ul>
140+
* @throws SteemResponseException
141+
* <ul>
142+
* <li>If the SteemJ is unable to transform the JSON response
143+
* into a Java object.</li>
144+
* <li>If the Server returned an error object.</li>
145+
* </ul>
98146
*/
99-
public static Boolean broadcastTransactionSynchronous(CommunicationHandler communicationHandler,
100-
SignedTransaction transaction)
101-
throws SteemCommunicationException, SteemResponseException, SteemInvalidTransactionException {
147+
public static void broadcastBlock(CommunicationHandler communicationHandler, SignedBlock signedBlock)
148+
throws SteemCommunicationException, SteemResponseException {
102149
JsonRPCRequest requestObject = new JsonRPCRequest();
103-
requestObject.setApiMethod(RequestMethods.BROADCAST_TRANSACTION_SYNCHRONOUS);
150+
requestObject.setApiMethod(RequestMethods.BROADCAST_BLOCK);
104151
requestObject.setSteemApi(SteemApiType.NETWORK_BROADCAST_API);
105152

106-
if (transaction.getSignatures() == null || transaction.getSignatures().isEmpty()) {
107-
transaction.sign();
108-
}
109-
110-
Object[] parameters = { transaction };
153+
Object[] parameters = { signedBlock };
111154
requestObject.setAdditionalParameters(parameters);
112155

113156
communicationHandler.performRequest(requestObject, Object.class);
114-
115-
return null;
116-
}
117-
118-
/**
119-
*
120-
*/
121-
public void broadcastTransactionSynchronous() {
122-
123-
}
124-
125-
/**
126-
*
127-
*/
128-
public void broadcastBlock() {
129-
130-
}
131-
132-
/**
133-
*
134-
*/
135-
public void setMaxBlockAge() {
136-
137157
}
138158
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package eu.bittrade.libs.steemj.apis.network.broadcast.model;
2+
3+
import org.apache.commons.lang3.builder.ToStringBuilder;
4+
5+
import com.fasterxml.jackson.annotation.JsonProperty;
6+
7+
import eu.bittrade.libs.steemj.base.models.TransactionId;
8+
9+
/**
10+
* This class represents a Steem "broadcast_transaction_synchronous_return"
11+
* object.
12+
*
13+
* @author <a href="http://steemit.com/@dez1337">dez1337</a>
14+
*/
15+
public class BroadcastTransactionSynchronousReturn {
16+
@JsonProperty("id")
17+
private TransactionId id;
18+
@JsonProperty("block_num")
19+
private int blockNum = 0;
20+
@JsonProperty("trx_num")
21+
private int trxNum = 0;
22+
@JsonProperty("expired")
23+
private boolean expired = false;
24+
25+
/**
26+
* This object is only used to wrap the JSON response in a POJO, so
27+
* therefore this class should not be instantiated.
28+
*/
29+
protected BroadcastTransactionSynchronousReturn() {
30+
}
31+
32+
/**
33+
* Get the Id of the applied transaction.
34+
*
35+
* @return The transaction Id.
36+
*/
37+
public TransactionId getId() {
38+
return id;
39+
}
40+
41+
/**
42+
* Get the block number the applied transaction has been processed with.
43+
*
44+
* @return The block number.
45+
*/
46+
public int getBlockNum() {
47+
return blockNum;
48+
}
49+
50+
/**
51+
* Get the transaction number inside the block.
52+
*
53+
* @return The transaction number.
54+
*/
55+
public int getTrxNum() {
56+
return trxNum;
57+
}
58+
59+
/**
60+
* Check if the applied transaction is already expired.
61+
*
62+
* @return <code>True</code> if the transaction is already expired or
63+
* <code>false</code> if not.
64+
*/
65+
public boolean isExpired() {
66+
return expired;
67+
}
68+
69+
@Override
70+
public String toString() {
71+
return ToStringBuilder.reflectionToString(this);
72+
}
73+
}

core/src/main/java/eu/bittrade/libs/steemj/enums/RequestMethods.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public enum RequestMethods {
1313
// login_api
1414
LOGIN, GET_API_BY_NAME, GET_VERSION,
1515
// network_broadcast_api
16-
BROADCAST_TRANSACTION, BROADCAST_TRANSACTION_SYNCHRONOUS, BROADCAST_TRANSACTION_WITH_CALLBACK, BROADCAST_BLOCK, SET_MAX_BLOCK_AGE,
16+
BROADCAST_TRANSACTION, BROADCAST_TRANSACTION_SYNCHRONOUS, BROADCAST_BLOCK,
1717
// follow_api
1818
GET_FOLLOWERS, GET_FOLLOWING, GET_FOLLOW_COUNT, GET_FEED_ENTRIES, GET_FEED, GET_BLOG_ENTRIES, GET_BLOG, GET_ACCOUNT_REPUTATIONS, GET_REBLOGGED_BY, GET_BLOG_AUTHORS,
1919
// market_history_api
Lines changed: 90 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,31 @@
11
package eu.bittrade.libs.steemj.apis.network.broadcast;
22

33
import static org.hamcrest.MatcherAssert.assertThat;
4-
import static org.hamcrest.Matchers.equalTo;
4+
import static org.hamcrest.Matchers.greaterThan;
5+
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
6+
import static org.hamcrest.Matchers.isEmptyOrNullString;
7+
import static org.hamcrest.Matchers.not;
8+
import static org.junit.Assert.assertFalse;
59

610
import java.util.ArrayList;
11+
import java.util.Arrays;
712

813
import org.junit.BeforeClass;
914
import org.junit.Test;
1015
import org.junit.experimental.categories.Category;
1116

1217
import eu.bittrade.libs.steemj.BaseTransactionBroadcastIT;
18+
import eu.bittrade.libs.steemj.BaseTransactionalIT;
1319
import eu.bittrade.libs.steemj.IntegrationTest;
20+
import eu.bittrade.libs.steemj.apis.follow.enums.FollowType;
21+
import eu.bittrade.libs.steemj.apis.follow.models.operations.FollowOperation;
22+
import eu.bittrade.libs.steemj.apis.network.broadcast.model.BroadcastTransactionSynchronousReturn;
1423
import eu.bittrade.libs.steemj.base.models.AccountName;
15-
import eu.bittrade.libs.steemj.base.models.Asset;
16-
import eu.bittrade.libs.steemj.base.models.Permlink;
24+
import eu.bittrade.libs.steemj.base.models.DynamicGlobalProperty;
1725
import eu.bittrade.libs.steemj.base.models.SignedTransaction;
18-
import eu.bittrade.libs.steemj.base.models.TimePointSec;
19-
import eu.bittrade.libs.steemj.base.models.operations.CommentOptionsOperation;
26+
import eu.bittrade.libs.steemj.base.models.operations.CustomJsonOperation;
2027
import eu.bittrade.libs.steemj.base.models.operations.Operation;
2128
import eu.bittrade.libs.steemj.communication.CommunicationHandler;
22-
import eu.bittrade.libs.steemj.enums.AssetSymbolType;
2329
import eu.bittrade.libs.steemj.exceptions.SteemCommunicationException;
2430
import eu.bittrade.libs.steemj.exceptions.SteemInvalidTransactionException;
2531
import eu.bittrade.libs.steemj.exceptions.SteemResponseException;
@@ -49,38 +55,100 @@ public static void init() throws SteemCommunicationException {
4955

5056
/**
5157
* Test the
52-
* {@link eu.bittrade.libs.steemj.apis.network.broadcast.NetworkBroadcastApi#broadcastTransactionWithCallback(CommunicationHandler, eu.bittrade.libs.steemj.base.models.SignedTransaction)}
58+
* {@link eu.bittrade.libs.steemj.apis.network.broadcast.NetworkBroadcastApi#broadcastTransactionSynchronous(CommunicationHandler, eu.bittrade.libs.steemj.base.models.SignedTransaction)}
5359
* method.
5460
*
5561
* @throws SteemCommunicationException
5662
* If a communication error occurs.
5763
* @throws SteemResponseException
5864
* If the response is an error.
5965
* @throws SteemInvalidTransactionException
66+
* If the transaction is not valid.
6067
*/
6168
@Category({ IntegrationTest.class })
6269
@Test
63-
public void testBroadcastTransactionWithCallback()
70+
public void testBroadcastTransactionSynchronous()
6471
throws SteemCommunicationException, SteemResponseException, SteemInvalidTransactionException {
65-
AccountName author = new AccountName("dez1337");
66-
Permlink permlink = new Permlink("steemj-v0-2-4-has-been-released-update-9");
67-
boolean allowVotes = true;
68-
boolean allowCurationRewards = true;
69-
short percentSteemDollars = (short) 10000;
70-
Asset maxAcceptedPayout = new Asset(1000000000, AssetSymbolType.SBD);
72+
ArrayList<AccountName> requiredPostingAuths = new ArrayList<>();
73+
requiredPostingAuths.add(BaseTransactionalIT.DEZ_ACCOUNT_NAME);
7174

72-
CommentOptionsOperation commentOptionsOperation = new CommentOptionsOperation(author, permlink,
73-
maxAcceptedPayout, percentSteemDollars, allowVotes, allowCurationRewards, null);
75+
String id = "follow";
76+
String json = (new FollowOperation(BaseTransactionalIT.DEZ_ACCOUNT_NAME,
77+
BaseTransactionalIT.STEEMJ_ACCOUNT_NAME, Arrays.asList(FollowType.BLOG))).toJson();
78+
79+
CustomJsonOperation customJsonOperation = new CustomJsonOperation(null, requiredPostingAuths, id, json);
7480

7581
ArrayList<Operation> operations = new ArrayList<>();
76-
operations.add(commentOptionsOperation);
82+
operations.add(customJsonOperation);
83+
84+
DynamicGlobalProperty globalProperties = steemJ.getDynamicGlobalProperties();
85+
86+
signedTransaction = new SignedTransaction(globalProperties.getHeadBlockId(), operations, null);
87+
88+
signedTransaction.sign();
89+
90+
BroadcastTransactionSynchronousReturn result = NetworkBroadcastApi
91+
.broadcastTransactionSynchronous(COMMUNICATION_HANDLER, signedTransaction);
92+
93+
assertThat(result.getBlockNum(), greaterThan(0));
94+
assertThat(result.getTrxNum(), greaterThanOrEqualTo(0));
95+
assertThat(result.getId().toString(), not(isEmptyOrNullString()));
96+
assertFalse(result.isExpired());
97+
}
98+
99+
/**
100+
* Test the
101+
* {@link eu.bittrade.libs.steemj.apis.network.broadcast.NetworkBroadcastApi#broadcastTransaction(CommunicationHandler, eu.bittrade.libs.steemj.base.models.SignedTransaction)}
102+
* method.
103+
*
104+
* @throws SteemCommunicationException
105+
* If a communication error occurs.
106+
* @throws SteemResponseException
107+
* If the response is an error.
108+
* @throws SteemInvalidTransactionException
109+
* If the transaction is not valid.
110+
*/
111+
@Category({ IntegrationTest.class })
112+
@Test
113+
public void testBroadcastTransaction()
114+
throws SteemCommunicationException, SteemResponseException, SteemInvalidTransactionException {
115+
ArrayList<AccountName> requiredPostingAuths = new ArrayList<>();
116+
requiredPostingAuths.add(BaseTransactionalIT.STEEMJ_ACCOUNT_NAME);
77117

78-
signedTransaction = new SignedTransaction(REF_BLOCK_NUM, REF_BLOCK_PREFIX, new TimePointSec(EXPIRATION_DATE),
79-
operations, null);
118+
String id = "follow";
119+
String json = (new FollowOperation(BaseTransactionalIT.STEEMJ_ACCOUNT_NAME,
120+
BaseTransactionalIT.DEZ_ACCOUNT_NAME, Arrays.asList(FollowType.BLOG))).toJson();
80121

81-
NetworkBroadcastApi.broadcastTransactionSynchronous(COMMUNICATION_HANDLER, signedTransaction);
82-
NetworkBroadcastApi.broadcastTransactionWithCallback(COMMUNICATION_HANDLER, signedTransaction);
122+
CustomJsonOperation customJsonOperation = new CustomJsonOperation(null, requiredPostingAuths, id, json);
83123

84-
assertThat(1, equalTo(2));
124+
ArrayList<Operation> operations = new ArrayList<>();
125+
operations.add(customJsonOperation);
126+
127+
DynamicGlobalProperty globalProperties = steemJ.getDynamicGlobalProperties();
128+
129+
signedTransaction = new SignedTransaction(globalProperties.getHeadBlockId(), operations, null);
130+
131+
signedTransaction.sign();
132+
133+
NetworkBroadcastApi.broadcastTransaction(COMMUNICATION_HANDLER, signedTransaction);
134+
}
135+
136+
/**
137+
* Test the
138+
* {@link eu.bittrade.libs.steemj.apis.network.broadcast.NetworkBroadcastApi#broadcastBlock(CommunicationHandler, eu.bittrade.libs.steemj.base.models.SignedBlock)}
139+
* method.
140+
*
141+
* @throws SteemCommunicationException
142+
* If a communication error occurs.
143+
* @throws SteemResponseException
144+
* If the response is an error.
145+
* @throws SteemInvalidTransactionException
146+
* If the transaction is not valid.
147+
*/
148+
@Category({ IntegrationTest.class })
149+
@Test
150+
public void testBroadcastBlock()
151+
throws SteemCommunicationException, SteemResponseException, SteemInvalidTransactionException {
152+
// TODO: Implement.
85153
}
86154
}

0 commit comments

Comments
 (0)