Search in sources :

Example 1 with BootstrapImportException

use of co.rsk.db.importer.BootstrapImportException in project rskj by rsksmart.

the class BootstrapFileHandler method unzipFile.

private void unzipFile(Path sourcePath, Path destinationPath) {
    try (InputStream inputStream = Files.newInputStream(sourcePath)) {
        unzipper.unzip(inputStream, destinationPath);
        logger.info("Bootstrap data extracted");
    } catch (IOException e) {
        throw new BootstrapImportException("The file is corrupted or incomplete. Please start again the import process", e);
    }
}
Also used : BootstrapImportException(co.rsk.db.importer.BootstrapImportException)

Example 2 with BootstrapImportException

use of co.rsk.db.importer.BootstrapImportException in project rskj by rsksmart.

the class BootstrapFileHandler method checkFileHash.

private void checkFileHash(Path databasePath, String expectedHash) {
    try {
        byte[] fileContent = Files.readAllBytes(databasePath);
        byte[] hash = HashUtil.sha256(fileContent);
        if (!Arrays.equals(hash, Hex.decode(expectedHash))) {
            throw new BootstrapImportException(String.format("File: %s does not match with expected hash: %s", databasePath, expectedHash));
        }
        logger.info("Bootstrap data hash checked");
    } catch (IOException e) {
        throw new BootstrapImportException("Error trying to read bootstrap data contents. Please start again the import process", e);
    }
}
Also used : BootstrapImportException(co.rsk.db.importer.BootstrapImportException)

Example 3 with BootstrapImportException

use of co.rsk.db.importer.BootstrapImportException in project rskj by rsksmart.

the class BootstrapFileHandler method downloadFile.

private void downloadFile(Path downloadPath, URL url) {
    try (InputStream is = url.openStream()) {
        Files.copy(is, downloadPath, StandardCopyOption.REPLACE_EXISTING);
        logger.info("Bootstrap data downloaded");
    } catch (IOException e) {
        throw new BootstrapImportException(String.format("Error downloading bootstrap data from %s. Please start again the import process", url), e);
    }
}
Also used : BootstrapImportException(co.rsk.db.importer.BootstrapImportException)

Example 4 with BootstrapImportException

use of co.rsk.db.importer.BootstrapImportException in project rskj by rsksmart.

the class BootstrapIndexCandidateSelector method getEntriesPerHeight.

private Map<Long, Map<String, BootstrapDataEntry>> getEntriesPerHeight(List<BootstrapDataIndex> indexes) {
    // the outer map represents the tuples (height, heightEntries)
    // the inner map represents the tuples (source, dbEntry)
    Map<Long, Map<String, BootstrapDataEntry>> entriesPerHeight = new HashMap<>();
    // each iteration is an index from a different source, each index contains many entries
    for (int i = 0; i < indexes.size(); i++) {
        BootstrapDataIndex bdi = indexes.get(i);
        String publicKey = publicKeys.get(i);
        // all the items for this index belongs to the same source
        for (BootstrapDataEntry bde : bdi.getDbs()) {
            Map<String, BootstrapDataEntry> entries = entriesPerHeight.computeIfAbsent(bde.getHeight(), k -> new HashMap<>());
            // if any height is duplicated on a single file the process is stopped
            if (entries.get(publicKey) != null) {
                throw new BootstrapImportException(String.format("There is an invalid file from %s: it has 2 entries from same height %d", publicKey, bde.getHeight()));
            }
            entries.put(publicKey, bde);
        }
    }
    if (entriesPerHeight.isEmpty()) {
        throw new BootstrapImportException("Downloaded files contain no height entries");
    }
    return entriesPerHeight;
}
Also used : BootstrapDataEntry(co.rsk.db.importer.provider.index.data.BootstrapDataEntry) BootstrapImportException(co.rsk.db.importer.BootstrapImportException) BootstrapDataIndex(co.rsk.db.importer.provider.index.data.BootstrapDataIndex)

Example 5 with BootstrapImportException

use of co.rsk.db.importer.BootstrapImportException in project rskj by rsksmart.

the class BootstrapDataVerifier method verifyEntries.

public int verifyEntries(Map<String, BootstrapDataEntry> selectedEntries) {
    int verifications = 0;
    if (selectedEntries.isEmpty()) {
        return 0;
    }
    String hashToVerify = selectedEntries.values().iterator().next().getHash();
    byte[] dbHash = Hex.decode(hashToVerify);
    for (Map.Entry<String, BootstrapDataEntry> entry : selectedEntries.entrySet()) {
        BootstrapDataEntry bde = entry.getValue();
        String currentHash = bde.getHash();
        if (!hashToVerify.equals(currentHash)) {
            throw new BootstrapImportException(String.format("Error trying to verify different hashes: %s vs %s", hashToVerify, currentHash));
        }
        BootstrapDataSignature bds = bde.getSig();
        // to use the public key we need to have an extra byte according to x9.62 declaring
        // which format is using. The current format from signer is uncompressed
        byte[] publicKey = Hex.decode(entry.getKey());
        // 1 is for forcing to interpret the values as unsigned integers
        BigInteger r = new BigInteger(1, Hex.decode(bds.getR()));
        BigInteger s = new BigInteger(1, Hex.decode(bds.getS()));
        ECDSASignature signature = new ECDSASignature(r, s);
        if (Secp256k1.getInstance().verify(dbHash, signature, publicKey)) {
            verifications++;
        }
    }
    return verifications;
}
Also used : BootstrapDataSignature(co.rsk.db.importer.provider.index.data.BootstrapDataSignature) BootstrapDataEntry(co.rsk.db.importer.provider.index.data.BootstrapDataEntry) BootstrapImportException(co.rsk.db.importer.BootstrapImportException) BigInteger(java.math.BigInteger) ECDSASignature(org.ethereum.crypto.signature.ECDSASignature) Map(java.util.Map)

Aggregations

BootstrapImportException (co.rsk.db.importer.BootstrapImportException)5 BootstrapDataEntry (co.rsk.db.importer.provider.index.data.BootstrapDataEntry)2 BootstrapDataIndex (co.rsk.db.importer.provider.index.data.BootstrapDataIndex)1 BootstrapDataSignature (co.rsk.db.importer.provider.index.data.BootstrapDataSignature)1 BigInteger (java.math.BigInteger)1 Map (java.util.Map)1 ECDSASignature (org.ethereum.crypto.signature.ECDSASignature)1