forked from 33cn/chain33-sdk-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTransactions.java
More file actions
165 lines (149 loc) · 5.4 KB
/
Transactions.java
File metadata and controls
165 lines (149 loc) · 5.4 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
package cn.chain33.javasdk.model.protobuf;
import cn.chain33.javasdk.model.enums.SignType;
import cn.chain33.javasdk.utils.HexUtil;
import com.google.protobuf.ByteString;
import java.util.ArrayList;
import java.util.List;
/**
* @authoer lhl
* @date 2022/7/1 上午8:30
*/
public class Transactions {
private List<Transaction> list;
//提供一些常用的构造函数
public Transactions(TransactionAllProtobuf.Transactions txs) throws Exception {
if (txs.getTxsCount() > 20 || txs.getTxsCount() <= 1) {
throw new Exception("ErrTxGroupCount");
}
ArrayList<Transaction> arrayList = new ArrayList<Transaction>();
boolean isPara = false;
boolean isMain = false;
for (int i = 0; i < txs.getTxsCount(); i++) {
if (txs.getTxs(i).getExecer().startsWith(ByteString.copyFrom("user.p.".getBytes()))) {
isPara = true;
} else {
isMain = true;
}
arrayList.add(new Transaction(txs.getTxs(i)));
}
if (isMain && isPara) {
throw new Exception("ErrTxGroupParaMainMixed");
}
this.list = arrayList;
}
//使用时需要重构交易组
public Transactions(TransactionAllProtobuf.Transaction... txs) throws Exception {
if (txs.length > 20 || txs.length <= 1) {
throw new Exception("ErrTxGroupCount");
}
boolean isPara = false;
boolean isMain = false;
ArrayList<Transaction> arrayList = new ArrayList<Transaction>();
for (int i = 0; i < txs.length; i++) {
if (txs[i].getExecer().startsWith(ByteString.copyFrom("user.p.".getBytes()))) {
isPara = true;
} else {
isMain = true;
}
arrayList.add(new Transaction(txs[i]));
}
if (isMain && isPara) {
throw new Exception("ErrTxGroupParaMainMixed");
}
this.list = arrayList;
}
//使用时需要重构交易组
public Transactions(String... txs) throws Exception {
if (txs.length > 20 || txs.length <= 1) {
throw new Exception("ErrTxGroupCount");
}
boolean isPara = false;
boolean isMain = false;
ArrayList<Transaction> arrayList = new ArrayList<Transaction>();
for (int i = 0; i < txs.length; i++) {
TransactionAllProtobuf.Transaction tx = TransactionAllProtobuf.Transaction.parseFrom(HexUtil.fromHexString(txs[i]));
if (tx.getExecer().startsWith(ByteString.copyFrom("user.p.".getBytes()))) {
isPara = true;
} else {
isMain = true;
}
arrayList.add(new Transaction(tx));
}
if (isMain && isPara) {
throw new Exception("ErrTxGroupParaMainMixed");
}
this.list = arrayList;
}
public List<Transaction> getTxList() {
return this.list;
}
//重新构建交易组
public void reBuild(long feeRate) throws Exception {
long totalFee = 0;
long minFee = 0;
byte[] header = list.get(0).hash();
for (int i = list.size() - 1; i >= 0; i--) {
list.get(i).setGroupCount(list.size());
totalFee += list.get(i).getFee();
list.get(i).setHeader(header);
if (i == 0) {
list.get(i).setFee(1 << 62);
} else {
list.get(i).setFee(0);
}
long realFee = list.get(i).getRealFee(feeRate);
minFee += realFee;
if (i == 0) {
if (totalFee < minFee) {
totalFee = minFee;
}
list.get(i).setFee(totalFee);
header = list.get(i).hash();
} else {
list.get(i).setFee(0);
list.get(i - 1).setNext(getTxList().get(i).hash());
}
}
for (int i = 0; i < list.size(); i++) {
list.get(i).setHeader(header);
}
}
/**
* 对交易组中交易签名
*
* @param n 当n小于0时表示对所有交易签名
* @param signType
* @param privateKey
* @throws Exception
*/
public void signN(int n, SignType signType, String privateKey) throws Exception {
if (n >= list.size()) {
throw new Exception("ErrIndex");
}
if (n < 0) {
for (int i = 0; i < list.size(); i++) {
list.get(i).sign(signType, privateKey);
}
return;
}
list.get(n).sign(signType, privateKey);
}
public Transaction toTransaction() throws Exception {
if (getTxList().size() < 2) {
throw new Exception("ErrInvalidParam");
}
//利用序列化进行深拷贝
TransactionAllProtobuf.Transaction copytx = TransactionAllProtobuf.Transaction.parseFrom(getTxList().get(0).getTx().toByteArray());
//放到header中不影响交易的Hash
return new Transaction(copytx.toBuilder().setHeader(ByteString.copyFrom(getTxGroup().toByteArray())).build());
}
public TransactionAllProtobuf.Transactions getTxGroup() {
TransactionAllProtobuf.Transactions.Builder builder = TransactionAllProtobuf.Transactions.newBuilder();
getTxList().forEach(
tx -> {
builder.addTxs(tx.getTx());
}
);
return builder.build();
}
}