package org.bitcoinj.quorums;

import org.bitcoinj.core.Context;
import org.bitcoinj.core.Sha256Hash;
import org.bitcoinj.core.StoredBlock;
import org.bitcoinj.evolution.SimplifiedMasternodeListManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;

public class SPVQuorumManager extends QuorumManager {

    private static final Logger log = LoggerFactory.getLogger(SPVQuorumManager.class);
    SimplifiedMasternodeListManager masternodeListManager;

    public SPVQuorumManager(Context context, SimplifiedMasternodeListManager masternodeListManager) {
        super(context);
        this.masternodeListManager = masternodeListManager;
    }

    @Override
    public Quorum getQuorum(LLMQParameters.LLMQType llmqType, Sha256Hash quorumHash) {
        return null;
    }

    @Override
    public Quorum getNewestQuorum(LLMQParameters.LLMQType llmqType) {
        return null;
    }

    @Override
    public ArrayList<Quorum> scanQuorums(final LLMQParameters.LLMQType llmqType, final long maxCount) {
        final ArrayList<Quorum> result = new ArrayList<Quorum>();
        SimplifiedQuorumList list = masternodeListManager.getQuorumListAtTip();
        if(list == null)
            return result;  // return empty list
        final LLMQParameters llmqParameters = context.getParams().getLlmqs().get(llmqType);
        list.forEachQuorum(true, new SimplifiedQuorumList.ForeachQuorumCallback() {
            int found = 0;
            @Override
            public void processQuorum(FinalCommitment fqc) {
                if(fqc.llmqType == llmqType.getValue() && found < maxCount) {
                    found++;
                    Quorum quorum = new Quorum(llmqParameters, fqc);
                    result.add(quorum);
                }

            }
        });
        return result;
    }

    // this one is cs_main-free
    @Override
    public ArrayList<Quorum> scanQuorums(LLMQParameters.LLMQType llmqType, StoredBlock start, long maxCount) {
        if(start != null && start.getHeight() > masternodeListManager.getQuorumListAtTip().getHeight())
            log.warn("quorum list is old, the quorums may not match");
        return scanQuorums(llmqType, maxCount);
    }

    boolean isQuorumActive(LLMQParameters.LLMQType llmqType, Sha256Hash quorumHash) {

        final LLMQParameters llmqParameters = context.getParams().getLlmqs().get(llmqType);

        // sig shares and recovered sigs are only accepted from recent/active quorums
        // we allow one more active quorum as specified in consensus, as otherwise there is a small window where things could
        // fail while we are on the brink of a new quorum
        ArrayList<Quorum> quorums = scanQuorums(llmqType, (int)llmqParameters.signingActiveQuorumCount + 1);
        for (Quorum q : quorums) {
            if (q.commitment.quorumHash.equals(quorumHash)) {
                return true;
            }
        }
        return false;
    }

    @Override
    public void close() {
    }
}

@@ -1029,32 +1029,458 @@
 ong maxCount) %7B%0A
+        return scanQuorums(llmqType, blockChain.getChainHead(), maxCount);%0A    %7D%0A%0A    // this one is cs_main-free%0A    @Override%0A    public ArrayList%3CQuorum%3E scanQuorums(final LLMQParameters.LLMQType llmqType, StoredBlock start, final long maxCount) %7B%0A        if(start != null && start.getHeight() %3E masternodeListManager.getQuorumListAtTip().getHeight())%0A            log.warn(%22quorum list is old, the quorums may not match%22);%0A
         final Ar
@@ -1600,22 +1600,52 @@
 orumList
-AtTip(
+ForBlock(start.getHeader().getHash()
 );%0A     
@@ -2294,395 +2294,8 @@
  %7D%0A%0A
-    // this one is cs_main-free%0A    @Override%0A    public ArrayList%3CQuorum%3E scanQuorums(LLMQParameters.LLMQType llmqType, StoredBlock start, long maxCount) %7B%0A        if(start != null && start.getHeight() %3E masternodeListManager.getQuorumListAtTip().getHeight())%0A            log.warn(%22quorum list is old, the quorums may not match%22);%0A        return scanQuorums(llmqType, maxCount);%0A    %7D%0A%0A
     

Use startBlock in scanQuorums to get correct quorum list