org.bitcoinj.coinjoin.CoinJoinBaseSession Maven / Gradle / Ivy
/*
* Copyright (c) 2022 Dash Core Group
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.bitcoinj.coinjoin;
import com.google.common.collect.Lists;
import org.bitcoinj.core.Coin;
import org.bitcoinj.core.Context;
import org.bitcoinj.core.Transaction;
import org.bitcoinj.core.TransactionInput;
import org.bitcoinj.core.TransactionOutput;
import org.bitcoinj.core.Utils;
import org.bitcoinj.script.Script;
import org.bitcoinj.script.ScriptPattern;
import org.bitcoinj.utils.Threading;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.concurrent.GuardedBy;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.ReentrantLock;
import static org.bitcoinj.coinjoin.CoinJoinConstants.COINJOIN_EXTRA;
import static org.bitcoinj.coinjoin.PoolMessage.ERR_ALREADY_HAVE;
import static org.bitcoinj.coinjoin.PoolMessage.ERR_DENOM;
import static org.bitcoinj.coinjoin.PoolMessage.ERR_FEES;
import static org.bitcoinj.coinjoin.PoolMessage.ERR_INVALID_INPUT;
import static org.bitcoinj.coinjoin.PoolMessage.ERR_INVALID_SCRIPT;
import static org.bitcoinj.coinjoin.PoolMessage.ERR_SIZE_MISMATCH;
import static org.bitcoinj.coinjoin.PoolMessage.MSG_NOERR;
import static org.bitcoinj.coinjoin.PoolState.POOL_STATE_IDLE;
public class CoinJoinBaseSession {
private final Logger log = LoggerFactory.getLogger(CoinJoinBaseSession.class);
protected final Context context;
protected final ReentrantLock lock = Threading.lock("coinjoin_base_session");
@GuardedBy("lock")
protected final ArrayList entries; // Masternode/clients entries
protected final AtomicReference state = new AtomicReference<>(POOL_STATE_IDLE); // should be one of the POOL_STATE_XXX values
protected final AtomicReference status = new AtomicReference<>(PoolStatus.WARMUP);
protected final AtomicLong timeLastSuccessfulStep = new AtomicLong(0); // the time when last successful mixing step was performed
protected final AtomicInteger sessionID = new AtomicInteger(0); // 0 if no mixing session is active
@GuardedBy("lock")
protected Transaction finalMutableTransaction; // the finalized transaction ready for signing
protected void setNull() {
// Both sides
lock.lock();
try {
state.set(POOL_STATE_IDLE);
sessionID.set(0);
sessionDenom = 0;
entries.clear();
finalMutableTransaction.clearInputs();
finalMutableTransaction.clearOutputs();
timeLastSuccessfulStep.set(Utils.currentTimeSeconds());
} finally {
lock.unlock();
}
}
interface CheckTxOutputs {
ValidInOuts check(TransactionOutput txout);
}
protected ValidInOuts isValidInOuts(List vin, List vout) {
HashSet