Coverage Summary for Class: BootstrapDataProvider (co.rsk.db.importer.provider)

Class Class, % Method, % Line, %
BootstrapDataProvider 0% (0/1) 0% (0/6) 0% (0/23)


1 /* 2  * This file is part of RskJ 3  * Copyright (C) 2019 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.db.importer.provider; 20  21 import co.rsk.db.importer.BootstrapImportException; 22 import co.rsk.db.importer.provider.index.BootstrapIndexCandidateSelector; 23 import co.rsk.db.importer.provider.index.BootstrapIndexRetriever; 24 import co.rsk.db.importer.provider.index.data.BootstrapDataEntry; 25 import co.rsk.db.importer.provider.index.data.BootstrapDataIndex; 26  27 import java.util.List; 28 import java.util.Map; 29  30 public class BootstrapDataProvider { 31  32  private final BootstrapIndexCandidateSelector bootstrapIndexCandidateSelector; 33  private final BootstrapDataVerifier bootstrapDataVerifier; 34  private final BootstrapFileHandler bootstrapFileHandler; 35  private BootstrapIndexRetriever bootstrapIndexRetriever; 36  private int minimumRequiredVerifications; 37  private long height; 38  39  public BootstrapDataProvider( 40  BootstrapDataVerifier bootstrapDataVerifier, 41  BootstrapFileHandler bootstrapFileHandler, 42  BootstrapIndexCandidateSelector bootstrapIndexCandidateSelector, 43  BootstrapIndexRetriever bootstrapIndexRetriever, 44  int minimumRequiredVerifications) { 45  46  this.bootstrapIndexCandidateSelector = bootstrapIndexCandidateSelector; 47  this.bootstrapDataVerifier = bootstrapDataVerifier; 48  this.bootstrapFileHandler = bootstrapFileHandler; 49  this.bootstrapIndexRetriever = bootstrapIndexRetriever; 50  this.minimumRequiredVerifications = minimumRequiredVerifications; 51  } 52  53  public void retrieveData() { 54  BootstrapIndexCandidateSelector.HeightCandidate heightCandidate = 55  bootstrapIndexCandidateSelector.getHeightData(getIndices()); 56  57  Map<String, BootstrapDataEntry> selectedEntries = heightCandidate.getEntries(); 58  verify(heightCandidate); 59  height = heightCandidate.getHeight(); 60  61  bootstrapFileHandler.setTempDirectory(); 62  bootstrapFileHandler.retrieveAndUnpack(selectedEntries); 63  } 64  65  private void verify(BootstrapIndexCandidateSelector.HeightCandidate mchd) { 66  Map<String, BootstrapDataEntry> selectedEntries = mchd.getEntries(); 67  int verifications = bootstrapDataVerifier.verifyEntries(selectedEntries); 68  if (verifications < minimumRequiredVerifications) { 69  throw new BootstrapImportException(String.format( 70  "Not enough valid signatures: selected height %d doesn't have enough trustworthy sources: %d of %d", 71  mchd.getHeight(), 72  verifications, 73  minimumRequiredVerifications 74  )); 75  } 76  } 77  78  private List<BootstrapDataIndex> getIndices() { 79  return bootstrapIndexRetriever.retrieve(); 80  } 81  82  public byte[] getBootstrapData() { 83  return bootstrapFileHandler.getBootstrapData(); 84  } 85  86  public long getSelectedHeight() { 87  return height; 88  } 89 }