SPVBlockStore: make method initNewStore() take genesisBlock

This makes the method independent of `NetworkParameters`.
This commit is contained in:
Andreas Schildbach 2023-05-03 12:49:51 +02:00
parent f62259b780
commit 8fe1d84a59

View file

@ -159,7 +159,7 @@ public class SPVBlockStore implements BlockStore {
if (!new String(header, StandardCharsets.US_ASCII).equals(HEADER_MAGIC)) if (!new String(header, StandardCharsets.US_ASCII).equals(HEADER_MAGIC))
throw new BlockStoreException("Header bytes do not equal " + HEADER_MAGIC); throw new BlockStoreException("Header bytes do not equal " + HEADER_MAGIC);
} else { } else {
initNewStore(params); initNewStore(params.getGenesisBlock());
} }
} catch (Exception e) { } catch (Exception e) {
try { try {
@ -171,7 +171,7 @@ public class SPVBlockStore implements BlockStore {
} }
} }
private void initNewStore(NetworkParameters params) throws Exception { private void initNewStore(Block genesisBlock) throws Exception {
byte[] header; byte[] header;
header = HEADER_MAGIC.getBytes(StandardCharsets.US_ASCII); header = HEADER_MAGIC.getBytes(StandardCharsets.US_ASCII);
buffer.put(header); buffer.put(header);
@ -182,8 +182,7 @@ public class SPVBlockStore implements BlockStore {
} finally { } finally {
lock.unlock(); lock.unlock();
} }
Block genesis = params.getGenesisBlock().cloneAsHeader(); StoredBlock storedGenesis = new StoredBlock(genesisBlock.cloneAsHeader(), genesisBlock.getWork(), 0);
StoredBlock storedGenesis = new StoredBlock(genesis, genesis.getWork(), 0);
put(storedGenesis); put(storedGenesis);
setChainHead(storedGenesis); setChainHead(storedGenesis);
} }
@ -350,7 +349,7 @@ public class SPVBlockStore implements BlockStore {
} }
// Initialize store again // Initialize store again
((Buffer) buffer).position(0); ((Buffer) buffer).position(0);
initNewStore(params); initNewStore(params.getGenesisBlock());
} finally { lock.unlock(); } } finally { lock.unlock(); }
} }
} }