-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSyncTest.java
More file actions
174 lines (148 loc) · 6.16 KB
/
SyncTest.java
File metadata and controls
174 lines (148 loc) · 6.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package cn.dmlab.bitxhub;
import cn.dmlab.crypto.ecdsa.ECKeyS256;
import cn.dmlab.utils.ByteUtil;
import cn.dmlab.utils.Utils;
import com.alibaba.fastjson.JSONObject;
import com.google.protobuf.ByteString;
import io.grpc.stub.StreamObserver;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.junit.*;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import pb.*;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;
@RunWith(JUnit4.class)
@Slf4j
public class SyncTest {
private GrpcClient client;
private Config config = Config.defaultConfig();
private Config config1 = Config.defaultConfig();
byte[] from, to;
@Before
public void setUp() {
config.setEcKey(ECKeyS256.fromPrivate(ByteUtil.hexStringToBytes("b6477143e17f889263044f6cf463dc37177ac4526c4c39a7a344198457024a2f")));
from = config.getAddress();
config1.setEcKey(ECKeyS256.fromPrivate(ByteUtil.hexStringToBytes("05c3708d30c2c72c4b36314a41f30073ab18ea226cf8c6b9f566720bfe2e8631")));
to = config1.getAddress();
client = new GrpcClientImpl(config);
}
@After
public void tearDown() throws Exception {
client.stop();
}
public void sendTransaction() {
Transaction.BxhTransaction unsignedTx = Transaction.BxhTransaction.newBuilder()
.setFrom(ByteString.copyFrom(from))
.setTo(ByteString.copyFrom(to))
.setTimestamp(Utils.genTimestamp())
.setPayload(Transaction.TransactionData.newBuilder().setAmount("100000").build().toByteString())
.build();
String txHash = client.sendTransaction(unsignedTx, null);
Assert.assertNotNull(txHash);
}
@Test
@Ignore
public void getInterchainTxWrapper() throws InterruptedException {
CountDownLatch asyncLatch = new CountDownLatch(1);
StreamObserver<Broker.InterchainTxWrappers> observer = new StreamObserver<Broker.InterchainTxWrappers>() {
@Override
public void onNext(Broker.InterchainTxWrappers interchainTxWrapper) {
Assert.assertNotNull(interchainTxWrapper);
asyncLatch.countDown();
}
@Override
public void onError(Throwable throwable) {
throwable.printStackTrace();
asyncLatch.countDown();
}
@Override
public void onCompleted() {
asyncLatch.countDown();
}
};
Chain.ChainMeta chainMeta = client.getChainMeta();
client.getInterchainTxWrappers("node1", chainMeta.getHeight(), chainMeta.getHeight() + 100, observer);
sendInterchaintx();
asyncLatch.await();
}
@Test
public void getBlockHeaders() throws InterruptedException {
CountDownLatch asyncLatch = new CountDownLatch(1);
StreamObserver<BlockOuterClass.BlockHeader> observer = new StreamObserver<BlockOuterClass.BlockHeader>() {
@Override
public void onNext(BlockOuterClass.BlockHeader blockHeader) {
Assert.assertNotNull(blockHeader);
asyncLatch.countDown();
}
@Override
public void onError(Throwable throwable) {
throwable.printStackTrace();
asyncLatch.countDown();
}
@Override
public void onCompleted() {
asyncLatch.countDown();
}
};
client.getBlockHeaders(1L, 2L, observer);
sendTransaction();
asyncLatch.await();
}
void sendInterchaintx() {
ArgOuterClass.Arg[] args = Types.toArgArray(
Types.string(""), //validators
Types.i32(0), //consensus_type
Types.string("hyperchain"), //chain_type
Types.string("税务链"), //name
Types.string("趣链税务链"), //desc
Types.string("1.8"),//version
Types.string("")); //public key
ReceiptOuterClass.Receipt receipt = client.invokeBVMContract(BVMAddr.APPCHAIN_MANAGER_CONTRACT_ADDR, "Register", args);
Assert.assertNotNull(receipt);
String ret = receipt.getRet().toStringUtf8();
JSONObject jsonObject = JSONObject.parseObject(ret);
String appchainID = jsonObject.getString("id");
byte[] contractBytes = new byte[0];
try {
contractBytes = IOUtils.toByteArray(
new FileInputStream("target/test-classes/testdata/example.wasm"));
} catch (IOException e) {
e.printStackTrace();
}
String contractAddress = client.deployContract(contractBytes);
ArgOuterClass.Arg[] ruleArgs = Types.toArgArray(
Types.string(appchainID),
Types.string(contractAddress));
ReceiptOuterClass.Receipt ruleReceipt = client.invokeBVMContract(BVMAddr.RULE_MANAGER_CONTRACT_ADDR, "RegisterRule", ruleArgs);
Assert.assertNotNull(ruleReceipt);
Ibtp.IBTP ibtp = getIBTP();
ArgOuterClass.Arg[] ibtpArgs = Types.toArgArray(
Types.bytes(ibtp.toByteArray())
);
client.invokeBVMContract(BVMAddr.INTER_CHAIN_CONTRACT_ADDR, "HandleIBTP", ibtpArgs);
}
Ibtp.IBTP getIBTP() {
Ibtp.content content = Ibtp.content.newBuilder()
// .setSrcContractId(ByteUtil.toHexStringWithOx(from))
// .setDstContractId(ByteUtil.toHexStringWithOx(to))
// .setFunc("set")
// .addArgs(Types.string("Alice").toByteString())
.build();
Ibtp.payload payload = Ibtp.payload.newBuilder()
.setContent(content.toByteString())
.setEncrypted(false)
.build();
Ibtp.IBTP ib = Ibtp.IBTP.newBuilder()
.setFrom(ByteUtil.toHexStringWithOx(from))
.setTo(ByteUtil.toHexStringWithOx(to))
.setIndex(1)
.setPayload(payload.toByteString())
.setType(Ibtp.IBTP.Type.INTERCHAIN)
// .setTimestamp(Utils.genTimestamp())
.build();
return ib;
}
}