public void initialize(final BlockStore blockStore, final StoredBlock chainHead)
        throws BlockStoreException {
        StoredBlock versionBlock = chainHead;
        final Stack<Long> versions = new Stack<>();

        // We don't know how many blocks back we can go, so load what we can first
        versions.push(versionBlock.getHeader().getVersion());
        for (int headOffset = 0; headOffset < versionWindow.length; headOffset++) {
            versionBlock = versionBlock.getPrev(blockStore);
            if (null == versionBlock) {
                break;
            }
            versions.push(versionBlock.getHeader().getVersion());
        }

        // Replay the versions into the tally
        while (!versions.isEmpty()) {
            add(versions.pop());
        }
    }
@Test
    public void testInitialize() throws BlockStoreException {
        Context.propagate(new Context(100, Transaction.DEFAULT_TX_FEE, false, true));
        final BlockStore blockStore = new MemoryBlockStore(TESTNET.getGenesisBlock());
        final BlockChain chain = new BlockChain(BitcoinNetwork.TESTNET, blockStore);

        // Build a historical chain of version 2 blocks
        Instant time = Instant.ofEpochSecond(1231006505);
        StoredBlock chainHead = null;
        for (int height = 0; height < TESTNET.getMajorityWindow(); height++) {
            chainHead = FakeTxBuilder.createFakeBlock(blockStore, 2, time, height).storedBlock;
            assertEquals(2, chainHead.getHeader().getVersion());
            time = time.plus(1, ChronoUnit.MINUTES);
        }

        VersionTally instance = new VersionTally(TESTNET);
        instance.initialize(blockStore, chainHead);
        assertEquals(TESTNET.getMajorityWindow(), instance.getCountAtOrAbove(2).intValue());
    }