Coverage Summary for Class: BridgeState (co.rsk.peg)

Class Class, % Method, % Line, %
BridgeState 0% (0/1) 0% (0/14) 0% (0/51)


1 /* 2  * This file is part of RskJ 3  * Copyright (C) 2017 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  19 package co.rsk.peg; 20  21 import co.rsk.bitcoinj.core.BtcTransaction; 22 import co.rsk.bitcoinj.core.UTXO; 23 import co.rsk.config.BridgeConstants; 24 import co.rsk.crypto.Keccak256; 25 import org.ethereum.config.blockchain.upgrades.ActivationConfig; 26 import org.ethereum.config.blockchain.upgrades.ConsensusRule; 27 import org.ethereum.util.RLP; 28 import org.ethereum.util.RLPList; 29  30 import javax.annotation.Nullable; 31 import java.io.IOException; 32 import java.math.BigInteger; 33 import java.util.*; 34  35 /** 36  * DTO to send the contract state. 37  * Not production code, just used for debugging. 38  * 39  * Created by mario on 27/09/2016. 40  */ 41 public class BridgeState { 42  private final int btcBlockchainBestChainHeight; 43  private final List<UTXO> activeFederationBtcUTXOs; 44  private final SortedMap<Keccak256, BtcTransaction> rskTxsWaitingForSignatures; 45  private final ReleaseRequestQueue releaseRequestQueue; 46  private final ReleaseTransactionSet releaseTransactionSet; 47  private final ActivationConfig.ForBlock activations; 48  49  private BridgeState(int btcBlockchainBestChainHeight, 50  List<UTXO> activeFederationBtcUTXOs, 51  SortedMap<Keccak256, 52  BtcTransaction> rskTxsWaitingForSignatures, 53  ReleaseRequestQueue releaseRequestQueue, 54  ReleaseTransactionSet releaseTransactionSet, 55  @Nullable ActivationConfig.ForBlock activations) { 56  this.btcBlockchainBestChainHeight = btcBlockchainBestChainHeight; 57  this.activeFederationBtcUTXOs = activeFederationBtcUTXOs; 58  this.rskTxsWaitingForSignatures = rskTxsWaitingForSignatures; 59  this.releaseRequestQueue = releaseRequestQueue; 60  this.releaseTransactionSet = releaseTransactionSet; 61  this.activations = activations; 62  } 63  64  public BridgeState(int btcBlockchainBestChainHeight, BridgeStorageProvider provider, ActivationConfig.ForBlock activations) throws IOException { 65  this(btcBlockchainBestChainHeight, 66  provider.getNewFederationBtcUTXOs(), 67  provider.getRskTxsWaitingForSignatures(), 68  provider.getReleaseRequestQueue(), 69  provider.getReleaseTransactionSet(), 70  activations); 71  } 72  73  public int getBtcBlockchainBestChainHeight() { 74  return this.btcBlockchainBestChainHeight; 75  } 76  77  public List<UTXO> getActiveFederationBtcUTXOs() { 78  return activeFederationBtcUTXOs; 79  } 80  81  public SortedMap<Keccak256, BtcTransaction> getRskTxsWaitingForSignatures() { 82  return rskTxsWaitingForSignatures; 83  } 84  85  public ReleaseRequestQueue getReleaseRequestQueue() { 86  return releaseRequestQueue; 87  } 88  89  public ReleaseTransactionSet getReleaseTransactionSet() { 90  return releaseTransactionSet; 91  } 92  93  @Override 94  public String toString() { 95  return "StateForDebugging{" + "\n" + 96  "btcBlockchainBestChainHeight=" + btcBlockchainBestChainHeight + "\n" + 97  ", activeFederationBtcUTXOs=" + activeFederationBtcUTXOs + "\n" + 98  ", rskTxsWaitingForSignatures=" + rskTxsWaitingForSignatures + "\n" + 99  ", releaseRequestQueue=" + releaseRequestQueue + "\n" + 100  ", releaseTransactionSet=" + releaseTransactionSet + "\n" + 101  '}'; 102  } 103  104  public Map<String, Object> stateToMap() { 105  Map<String, Object> result = new HashMap<>(); 106  result.put("rskTxsWaitingForSignatures", this.toStringList(rskTxsWaitingForSignatures.keySet())); 107  result.put("btcBlockchainBestChainHeight", this.btcBlockchainBestChainHeight); 108  return result; 109  } 110  111  public byte[] getEncoded() throws IOException { 112  byte[] rlpBtcBlockchainBestChainHeight = RLP.encodeBigInteger(BigInteger.valueOf(this.btcBlockchainBestChainHeight)); 113  byte[] rlpActiveFederationBtcUTXOs = RLP.encodeElement(BridgeSerializationUtils.serializeUTXOList(activeFederationBtcUTXOs)); 114  byte[] rlpRskTxsWaitingForSignatures = RLP.encodeElement(BridgeSerializationUtils.serializeMap(rskTxsWaitingForSignatures)); 115  byte[] serializedReleaseRequestQueue = shouldUsePapyrusEncoding(this.activations) ? 116  BridgeSerializationUtils.serializeReleaseRequestQueueWithTxHash(releaseRequestQueue): 117  BridgeSerializationUtils.serializeReleaseRequestQueue(releaseRequestQueue); 118  byte[] rlpReleaseRequestQueue = RLP.encodeElement(serializedReleaseRequestQueue); 119  byte[] serializedReleaseTransactionSet = shouldUsePapyrusEncoding(this.activations) ? 120  BridgeSerializationUtils.serializeReleaseTransactionSetWithTxHash(releaseTransactionSet): 121  BridgeSerializationUtils.serializeReleaseTransactionSet(releaseTransactionSet); 122  byte[] rlpReleaseTransactionSet = RLP.encodeElement(serializedReleaseTransactionSet); 123  124  return RLP.encodeList(rlpBtcBlockchainBestChainHeight, rlpActiveFederationBtcUTXOs, rlpRskTxsWaitingForSignatures, rlpReleaseRequestQueue, rlpReleaseTransactionSet); 125  } 126  127  public static BridgeState create(BridgeConstants bridgeConstants, byte[] data, @Nullable ActivationConfig.ForBlock activations) throws IOException { 128  RLPList rlpList = (RLPList)RLP.decode2(data).get(0); 129  130  byte[] btcBlockchainBestChainHeightBytes = rlpList.get(0).getRLPData(); 131  int btcBlockchainBestChainHeight = btcBlockchainBestChainHeightBytes == null ? 0 : (new BigInteger(1, btcBlockchainBestChainHeightBytes)).intValue(); 132  byte[] btcUTXOsBytes = rlpList.get(1).getRLPData(); 133  List<UTXO> btcUTXOs = BridgeSerializationUtils.deserializeUTXOList(btcUTXOsBytes); 134  byte[] rskTxsWaitingForSignaturesBytes = rlpList.get(2).getRLPData(); 135  SortedMap<Keccak256, BtcTransaction> rskTxsWaitingForSignatures = BridgeSerializationUtils.deserializeMap(rskTxsWaitingForSignaturesBytes, bridgeConstants.getBtcParams(), false); 136  byte[] releaseRequestQueueBytes = rlpList.get(3).getRLPData(); 137  ReleaseRequestQueue releaseRequestQueue = new ReleaseRequestQueue(BridgeSerializationUtils.deserializeReleaseRequestQueue(releaseRequestQueueBytes, bridgeConstants.getBtcParams(), shouldUsePapyrusEncoding(activations))); 138  byte[] releaseTransactionSetBytes = rlpList.get(4).getRLPData(); 139  ReleaseTransactionSet releaseTransactionSet = BridgeSerializationUtils.deserializeReleaseTransactionSet(releaseTransactionSetBytes, bridgeConstants.getBtcParams(), shouldUsePapyrusEncoding(activations)); 140  141  return new BridgeState( 142  btcBlockchainBestChainHeight, 143  btcUTXOs, 144  rskTxsWaitingForSignatures, 145  releaseRequestQueue, 146  releaseTransactionSet, 147  activations 148  ); 149  } 150  151  private List<String> toStringList(Set<Keccak256> keys) { 152  List<String> hashes = new ArrayList<>(); 153  if(keys != null) { 154  keys.forEach(s -> hashes.add(s.toHexString())); 155  } 156  157  return hashes; 158  } 159  160  public static boolean shouldUsePapyrusEncoding(ActivationConfig.ForBlock activations) { 161  return activations != null && activations.isActive(ConsensusRule.RSKIP146); 162  } 163 }