Search in sources :

Example 16 with ByteArrayWrapper

use of org.aion.base.util.ByteArrayWrapper in project aion by aionnetwork.

the class TxPoolA0 method removeTimeoutTxn.

private void removeTimeoutTxn() {
    long ts = TimeInstant.now().toEpochSec() - txn_timeout;
    List<TX> txl = Collections.synchronizedList(new ArrayList<>());
    this.getTimeView().entrySet().parallelStream().forEach(e -> {
        if (e.getKey() < ts) {
            for (ByteArrayWrapper bw : e.getValue()) {
                txl.add(this.getMainMap().get(bw).getTx());
            }
        }
    });
    if (txl.isEmpty()) {
        return;
    }
    this.addOutDatedList(txl);
    this.remove(txl);
    if (LOG.isDebugEnabled()) {
        LOG.debug("TxPoolA0.remove return [{}] TX, poolSize[{}]", txl.size(), getMainMap().size());
    }
}
Also used : ByteArrayWrapper(org.aion.base.util.ByteArrayWrapper)

Example 17 with ByteArrayWrapper

use of org.aion.base.util.ByteArrayWrapper in project aion by aionnetwork.

the class TxPoolA0 method remove.

@Override
public synchronized List<TX> remove(Map<Address, BigInteger> accNonce) {
    List<ByteArrayWrapper> bwList = new ArrayList<>();
    for (Map.Entry<Address, BigInteger> en1 : accNonce.entrySet()) {
        AccountState as = this.getAccView(en1.getKey());
        Iterator<Map.Entry<BigInteger, AbstractMap.SimpleEntry<ByteArrayWrapper, BigInteger>>> it = as.getMap().entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry<BigInteger, AbstractMap.SimpleEntry<ByteArrayWrapper, BigInteger>> en = it.next();
            if (en1.getValue().compareTo(en.getKey()) > 0) {
                bwList.add(en.getValue().getKey());
                it.remove();
            } else {
                break;
            }
        }
        Set<BigInteger> fee = Collections.synchronizedSet(new HashSet<>());
        if (this.getPoolStateView(en1.getKey()) != null) {
            this.getPoolStateView(en1.getKey()).parallelStream().forEach(ps -> fee.add(ps.getFee()));
        }
        fee.parallelStream().forEach(bi -> {
            if (this.getFeeView().get(bi) != null) {
                this.getFeeView().get(bi).entrySet().removeIf(byteArrayWrapperTxDependListEntry -> byteArrayWrapperTxDependListEntry.getValue().getAddress().equals(en1.getKey()));
                if (this.getFeeView().get(bi).isEmpty()) {
                    this.getFeeView().remove(bi);
                }
            }
        });
        as.setDirty();
    }
    List<TX> removedTxl = Collections.synchronizedList(new ArrayList<>());
    bwList.parallelStream().forEach(bw -> {
        if (this.getMainMap().get(bw) != null) {
            ITransaction tx = this.getMainMap().get(bw).getTx().clone();
            removedTxl.add((TX) tx);
            long timestamp = tx.getTimeStampBI().longValue() / multiplyM;
            synchronized (this.getTimeView().get(timestamp)) {
                if (this.getTimeView().get(timestamp) == null) {
                    LOG.error("Txpool.remove can't find the timestamp in the map [{}]", tx.toString());
                    return;
                }
                this.getTimeView().get(timestamp).remove(bw);
                if (this.getTimeView().get(timestamp).isEmpty()) {
                    this.getTimeView().remove(timestamp);
                }
            }
            this.getMainMap().remove(bw);
        }
    });
    this.updateAccPoolState();
    this.updateFeeMap();
    if (LOG.isInfoEnabled()) {
        LOG.info("TxPoolA0.remove {} TX", removedTxl.size());
    }
    return removedTxl;
}
Also used : Address(org.aion.base.type.Address) ITransaction(org.aion.base.type.ITransaction) AccountState(org.aion.txpool.common.AccountState) Entry(java.util.Map.Entry) ByteArrayWrapper(org.aion.base.util.ByteArrayWrapper) BigInteger(java.math.BigInteger)

Example 18 with ByteArrayWrapper

use of org.aion.base.util.ByteArrayWrapper in project aion by aionnetwork.

the class TxPoolA0 method remove.

@Override
@Deprecated
public synchronized List<TX> remove(List<TX> txs) {
    List<TX> removedTxl = Collections.synchronizedList(new ArrayList<>());
    Set<Address> checkedAddress = Collections.synchronizedSet(new HashSet<>());
    for (TX tx : txs) {
        ByteArrayWrapper bw = ByteArrayWrapper.wrap(tx.getHash());
        if (this.getMainMap().remove(bw) == null) {
            continue;
        }
        // noinspection unchecked
        removedTxl.add((TX) tx.clone());
        if (LOG.isTraceEnabled()) {
            LOG.trace("TxPoolA0.remove:[{}] nonce:[{}]", ByteUtils.toHexString(tx.getHash()), tx.getNonceBI().toString());
        }
        long timestamp = tx.getTimeStampBI().longValue() / multiplyM;
        if (this.getTimeView().get(timestamp).remove(bw)) {
            if (this.getTimeView().get(timestamp).isEmpty()) {
                this.getTimeView().remove(timestamp);
            }
        }
        // remove the all transactions belong to the given address in the feeView
        Address address = tx.getFrom();
        Set<BigInteger> fee = Collections.synchronizedSet(new HashSet<>());
        if (!checkedAddress.contains(address)) {
            this.getPoolStateView(tx.getFrom()).parallelStream().forEach(ps -> fee.add(ps.getFee()));
            fee.parallelStream().forEach(bi -> {
                this.getFeeView().get(bi).entrySet().removeIf(byteArrayWrapperTxDependListEntry -> byteArrayWrapperTxDependListEntry.getValue().getAddress().equals(address));
                if (this.getFeeView().get(bi).isEmpty()) {
                    this.getFeeView().remove(bi);
                }
            });
            checkedAddress.add(address);
        }
        AccountState as = this.getAccView(tx.getFrom());
        as.getMap().remove(tx.getNonceBI());
        as.setDirty();
    }
    this.updateAccPoolState();
    this.updateFeeMap();
    if (LOG.isInfoEnabled()) {
        LOG.info("TxPoolA0.remove TX remove [{}] removed [{}]", txs.size(), removedTxl.size());
    }
    return removedTxl;
}
Also used : Address(org.aion.base.type.Address) ByteArrayWrapper(org.aion.base.util.ByteArrayWrapper) BigInteger(java.math.BigInteger) AccountState(org.aion.txpool.common.AccountState)

Example 19 with ByteArrayWrapper

use of org.aion.base.util.ByteArrayWrapper in project aion by aionnetwork.

the class BlockchainForkingTest method testHigherDifficultyBlockFork.

/*-
     * Test the general forking case, where an incoming block (b) has a greater total
     * difficulty than our current block. In this scenario, we should switch to
     * the branch (sub-tree) that has (b) at its tip.
     *
     * This is the simplest case, where the distance between (a) (our current head) and
     * (b) is 2. This implies that the common ancestor is directly adjacent to both blocks.
     *
     *          (common ancestor)
     *          /               \
     *         /                 \
     *        /                   \
     *       (a)x(low td)           (b)o(higher td)
     *
     * In this simple case:
     * b.td > a.td
     * a_worldState === b_worldState
     *
     */
@Test
public void testHigherDifficultyBlockFork() {
    StandaloneBlockchain.Builder builder = new StandaloneBlockchain.Builder();
    StandaloneBlockchain.Bundle b = builder.withValidatorConfiguration("simple").build();
    StandaloneBlockchain bc = b.bc;
    AionBlock bestBlock = bc.getBestBlock();
    AionBlock standardBlock = bc.createNewBlock(bc.getBestBlock(), Collections.emptyList(), true);
    ChainConfiguration cc = new ChainConfiguration();
    AionBlock higherDifficultyBlock = new AionBlock(standardBlock);
    higherDifficultyBlock.getHeader().setTimestamp(bestBlock.getTimestamp() + 1);
    BigInteger difficulty = cc.getDifficultyCalculator().calculateDifficulty(higherDifficultyBlock.getHeader(), bestBlock.getHeader());
    assertThat(difficulty).isGreaterThan(standardBlock.getDifficultyBI());
    higherDifficultyBlock.getHeader().setDifficulty(difficulty.toByteArray());
    System.out.println("before any processing: " + new ByteArrayWrapper(bc.getRepository().getRoot()));
    System.out.println("trie: " + ((AionRepositoryImpl) bc.getRepository()).getWorldState().getTrieDump());
    ImportResult result = bc.tryToConnect(standardBlock);
    assertThat(result).isEqualTo(ImportResult.IMPORTED_BEST);
    // assert that the block we just inserted (best) is the instance that is returned
    assertThat(bc.getBestBlock() == standardBlock).isTrue();
    System.out.println(new ByteArrayWrapper(bc.getRepository().getRoot()));
    ImportResult higherDifficultyResult = bc.tryToConnect(higherDifficultyBlock);
    assertThat(higherDifficultyResult).isEqualTo(ImportResult.IMPORTED_BEST);
    assertThat(bc.getBestBlockHash()).isEqualTo(higherDifficultyBlock.getHash());
    // the object reference here is intentional
    assertThat(bc.getBestBlock() == higherDifficultyBlock).isTrue();
}
Also used : ImportResult(org.aion.mcf.core.ImportResult) ByteArrayWrapper(org.aion.base.util.ByteArrayWrapper) ChainConfiguration(org.aion.zero.impl.blockchain.ChainConfiguration) BigInteger(java.math.BigInteger) AionBlock(org.aion.zero.impl.types.AionBlock) Test(org.junit.Test)

Example 20 with ByteArrayWrapper

use of org.aion.base.util.ByteArrayWrapper in project aion by aionnetwork.

the class EquihashSolutionRuleTest method testProperSolution.

// perhaps we should generate a solution by hand
@Test
public void testProperSolution() {
    // given that all our inputs are deterministic, for given nonce, there should always
    // be a valid output solution
    final int n = 210;
    final int k = 9;
    final BigInteger givenNonce = new BigInteger("21");
    // assume a 32-byte nonce (fixed)
    byte[] unpaddedNonceBytes = givenNonce.toByteArray();
    byte[] nonceBytes = new byte[32];
    System.arraycopy(unpaddedNonceBytes, 0, nonceBytes, 32 - unpaddedNonceBytes.length, unpaddedNonceBytes.length);
    System.out.println(new ByteArrayWrapper(nonceBytes));
    A0BlockHeader header = new A0BlockHeader.Builder().build();
    header.setNonce(nonceBytes);
    byte[] headerBytes = header.getHeaderBytes(true);
    Equihash equihash = new Equihash(n, k);
    int[][] solutions = equihash.getSolutionsForNonce(headerBytes, header.getNonce());
    byte[] compressedSolution = (new EquiUtils()).getMinimalFromIndices(solutions[0], n / (k + 1));
    header.setSolution(compressedSolution);
// EquihashSolutionRule rule = new EquihashSolutionRule(new OptimizedEquiValidator(n, k));
// boolean result = rule.validate(header);
// assertThat(result).isTrue();
// assertThat(rule.getErrors()).isEmpty();
}
Also used : A0BlockHeader(org.aion.zero.types.A0BlockHeader) EquiUtils(org.aion.equihash.EquiUtils) ByteArrayWrapper(org.aion.base.util.ByteArrayWrapper) BigInteger(java.math.BigInteger) Equihash(org.aion.equihash.Equihash) Test(org.junit.Test)

Aggregations

ByteArrayWrapper (org.aion.base.util.ByteArrayWrapper)45 BigInteger (java.math.BigInteger)10 Address (org.aion.base.type.Address)5 ITransaction (org.aion.base.type.ITransaction)4 Entry (java.util.Map.Entry)3 DataWord (org.aion.mcf.vm.types.DataWord)3 AionBlock (org.aion.zero.impl.types.AionBlock)3 Test (org.junit.Test)3 java.util (java.util)2 SimpleEntry (java.util.AbstractMap.SimpleEntry)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 LinkedHashMap (java.util.LinkedHashMap)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 ImportResult (org.aion.mcf.core.ImportResult)2 Value (org.aion.rlp.Value)2 AccountState (org.aion.txpool.common.AccountState)2 TxDependList (org.aion.txpool.common.TxDependList)2 AionTxReceipt (org.aion.zero.types.AionTxReceipt)2