Coverage Summary for Class: Rskip92MerkleProofBuilder (co.rsk.mine)

Class Class, % Method, % Line, %
Rskip92MerkleProofBuilder 0% (0/1) 0% (0/8) 0% (0/15)


1 /* 2  * This file is part of RskJ 3  * Copyright (C) 2018 RSK Labs Ltd. 4  * 5  * This program is free software: you can redistribute it and/or modify 6  * it under the terms of the GNU Lesser General Public License as published by 7  * the Free Software Foundation, either version 3 of the License, or 8  * (at your option) any later version. 9  * 10  * This program is distributed in the hope that it will be useful, 11  * but WITHOUT ANY WARRANTY; without even the implied warranty of 12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13  * GNU Lesser General Public License for more details. 14  * 15  * You should have received a copy of the GNU Lesser General Public License 16  * along with this program. If not, see <http://www.gnu.org/licenses/>. 17  */ 18 package co.rsk.mine; 19  20 import co.rsk.bitcoinj.core.BtcBlock; 21 import co.rsk.bitcoinj.core.Sha256Hash; 22 import co.rsk.bitcoinj.core.Utils; 23 import co.rsk.peg.utils.PartialMerkleTreeFormatUtils; 24 import org.bouncycastle.util.encoders.Hex; 25 import org.ethereum.util.ByteUtil; 26  27 import java.util.List; 28 import java.util.stream.Stream; 29  30 /** 31  * Builds RSKIP 92 Merkle proofs 32  */ 33 public class Rskip92MerkleProofBuilder implements MerkleProofBuilder { 34  35  @Override 36  public byte[] buildFromMerkleHashes( 37  BtcBlock blockWithHeaderOnly, 38  List<String> merkleHashesString, 39  int blockTxnCount) { 40  Stream<byte[]> hashesStream = merkleHashesString.stream() 41  .map(mh -> Utils.reverseBytes(Hex.decode(mh))); 42  return mergeHashes(hashesStream); 43  } 44  45  @Override 46  public byte[] buildFromTxHashes(BtcBlock blockWithHeaderOnly, List<String> txHashesString) { 47  return buildFromFullPmt( 48  new GenesisMerkleProofBuilder().buildFromTxHashes(blockWithHeaderOnly, txHashesString) 49  ); 50  } 51  52  @Override 53  public byte[] buildFromBlock(BtcBlock bitcoinMergedMiningBlock) { 54  return buildFromFullPmt( 55  new GenesisMerkleProofBuilder().buildFromBlock(bitcoinMergedMiningBlock) 56  ); 57  } 58  59  /** 60  * This takes a full PMT and slices and rearranges the needed pieces for the new serialization format. 61  * It would make sense to re-implement this as a standalone algorithm, but reusing the PMT is enough for now. 62  */ 63  private byte[] buildFromFullPmt(byte[] pmtSerialized) { 64  Stream<byte[]> hashesStream = PartialMerkleTreeFormatUtils.streamIntermediateHashes(pmtSerialized) 65  .map(Sha256Hash::getBytes); 66  return mergeHashes(hashesStream); 67  } 68  69  private byte[] mergeHashes(Stream<byte[]> hashesStream) { 70  byte[][] hashes = hashesStream 71  .skip(1) // skip the coinbase 72  .toArray(byte[][]::new); 73  return ByteUtil.merge(hashes); 74  } 75 }