2828import org .tron .common .crypto .ECKey .ECDSASignature ;
2929import org .tron .common .utils .Sha256Hash ;
3030import org .tron .core .capsule .utils .MerkleTree ;
31+ import org .tron .core .exception .BadItemException ;
3132import org .tron .core .exception .ValidateSignatureException ;
3233import org .tron .protos .Protocol .Block ;
3334import org .tron .protos .Protocol .BlockHeader ;
@@ -108,26 +109,25 @@ public long getNum() {
108109
109110 private BlockId blockId = new BlockId (Sha256Hash .ZERO_HASH , 0 );
110111
111- private byte [] data ;
112-
113112 private Block block ;
114-
115- private boolean unpacked ;
116-
117113 public boolean generatedByMyself = false ;
118114
119- private synchronized void unPack () {
120- if (unpacked ) {
121- return ;
122- }
115+ public BlockCapsule (long number , Sha256Hash hash , long when , ByteString witnessAddress ) {
116+ // blockheader raw
117+ BlockHeader .raw .Builder blockHeaderRawBuild = BlockHeader .raw .newBuilder ();
118+ BlockHeader .raw blockHeaderRaw = blockHeaderRawBuild
119+ .setNumber (number )
120+ .setParentHash (hash .getByteString ())
121+ .setTimestamp (when )
122+ .setWitnessAddress (witnessAddress ).build ();
123123
124- try {
125- this .block = Block .parseFrom (data );
126- } catch (InvalidProtocolBufferException e ) {
127- logger .debug (e .getMessage ());
128- }
124+ // block header
125+ BlockHeader .Builder blockHeaderBuild = BlockHeader .newBuilder ();
126+ BlockHeader blockHeader = blockHeaderBuild .setRawData (blockHeaderRaw ).build ();
129127
130- unpacked = true ;
128+ // block
129+ Block .Builder blockBuild = Block .newBuilder ();
130+ this .block = blockBuild .setBlockHeader (blockHeader ).build ();
131131 }
132132
133133 public BlockCapsule (long number , ByteString hash , long when , ByteString witnessAddress ) {
@@ -146,7 +146,6 @@ public BlockCapsule(long number, ByteString hash, long when, ByteString witnessA
146146 // block
147147 Block .Builder blockBuild = Block .newBuilder ();
148148 this .block = blockBuild .setBlockHeader (blockHeader ).build ();
149- unpacked = true ;
150149 }
151150
152151 public BlockCapsule (long timestamp , ByteString parentHash , long number ,
@@ -167,7 +166,7 @@ public BlockCapsule(long timestamp, ByteString parentHash, long number,
167166 Block .Builder blockBuild = Block .newBuilder ();
168167 transactionList .forEach (trx -> blockBuild .addTransactions (trx ));
169168 this .block = blockBuild .setBlockHeader (blockHeader ).build ();
170- unpacked = true ;
169+
171170 }
172171
173172 public void addTransaction (TransactionCapsule pendingTrx ) {
@@ -193,7 +192,6 @@ public void sign(byte[] privateKey) {
193192 }
194193
195194 private Sha256Hash getRawHash () {
196- unPack ();
197195 return Sha256Hash .of (this .block .getBlockHeader ().getRawData ().toByteArray ());
198196 }
199197
@@ -210,15 +208,10 @@ public boolean validateSignature() throws ValidateSignatureException {
210208 }
211209
212210 public BlockId getBlockId () {
213- unPack ();
214211 if (blockId .equals (Sha256Hash .ZERO_HASH )) {
215212 blockId = new BlockId (Sha256Hash .of (this .block .getBlockHeader ().toByteArray ()), getNum ());
216213 }
217-
218214 return blockId ;
219- // return blockId.equals(Sha256Hash.ZERO_HASH)
220- // ? blockId = new BlockId(Sha256Hash.of(this.block.getBlockHeader().toByteArray()), getNum())
221- // : blockId;
222215 }
223216
224217 public Sha256Hash calcMerkleRoot () {
@@ -246,41 +239,29 @@ public void setMerkleRoot() {
246239 }
247240
248241 public Sha256Hash getMerkleRoot () {
249- unPack ();
250242 return Sha256Hash .wrap (this .block .getBlockHeader ().getRawData ().getTxTrieRoot ());
251243 }
252244
253- public ByteString getWitnessAddress (){
254- unPack ();
245+ public ByteString getWitnessAddress () {
255246 return this .block .getBlockHeader ().getRawData ().getWitnessAddress ();
256247 }
257248
258249
259- private void pack () {
260- if (data == null ) {
261- this .data = this .block .toByteArray ();
262- }
263- }
264-
265- public boolean validate () {
266- unPack ();
267- return true ;
268- }
269-
270250 public BlockCapsule (Block block ) {
271251 this .block = block ;
272- unpacked = true ;
273252 }
274253
275- public BlockCapsule (byte [] data ) {
276- this .data = data ;
277- unPack ();
254+ public BlockCapsule (byte [] data ) throws BadItemException {
255+ try {
256+ this .block = Block .parseFrom (data );
257+ } catch (InvalidProtocolBufferException e ) {
258+ throw new BadItemException ();
259+ }
278260 }
279261
280262 @ Override
281263 public byte [] getData () {
282- pack ();
283- return data ;
264+ return this .block .toByteArray ();
284265 }
285266
286267 @ Override
@@ -289,28 +270,23 @@ public Block getInstance() {
289270 }
290271
291272 public Sha256Hash getParentHash () {
292- unPack ();
293273 return Sha256Hash .wrap (this .block .getBlockHeader ().getRawData ().getParentHash ());
294274 }
295275
296276 public ByteString getParentHashStr () {
297- unPack ();
298277 return this .block .getBlockHeader ().getRawData ().getParentHash ();
299278 }
300279
301280 public long getNum () {
302- unPack ();
303281 return this .block .getBlockHeader ().getRawData ().getNumber ();
304282 }
305283
306284 public long getTimeStamp () {
307- unPack ();
308285 return this .block .getBlockHeader ().getRawData ().getTimestamp ();
309286 }
310287
311288 @ Override
312289 public String toString () {
313- unPack ();
314290 return "BlockCapsule{" +
315291 "blockId=" + blockId +
316292 ", num=" + getNum () +
0 commit comments