Search in sources :

Example 1 with CloseableCursor

use of com.radixdlt.atom.CloseableCursor in project radixdlt by radixdlt.

the class BerkeleyLedgerEntryStore method forkVotingResultsCursor.

@Override
public CloseableCursor<ForkVotingResult> forkVotingResultsCursor(long fromEpoch, long toEpoch, HashCode candidateForkId) {
    final Cursor underlyingCursor = forksVotingResultsDatabase.openCursor(null, null);
    OperationStatus cursorStatus;
    try {
        cursorStatus = underlyingCursor.getSearchKeyRange(new DatabaseEntry(Longs.toByteArray(fromEpoch)), null, DEFAULT);
    } catch (Exception e) {
        // close the cursor and re-throw
        underlyingCursor.close();
        throw e;
    }
    if (cursorStatus != SUCCESS) {
        underlyingCursor.close();
        return CloseableCursor.empty();
    }
    return new CloseableCursor<>() {

        private Optional<ForkVotingResult> nextOpt = findNextForkVotingResult(toEpoch, candidateForkId, underlyingCursor, true);

        @Override
        public boolean hasNext() {
            return nextOpt.isPresent();
        }

        @Override
        public ForkVotingResult next() {
            final var res = nextOpt.orElseThrow(NoSuchElementException::new);
            nextOpt = findNextForkVotingResult(toEpoch, candidateForkId, underlyingCursor, false);
            return res;
        }

        @Override
        public void close() {
            underlyingCursor.close();
        }
    };
}
Also used : CloseableCursor(com.radixdlt.atom.CloseableCursor) Optional(java.util.Optional) OperationStatus(com.sleepycat.je.OperationStatus) DatabaseEntry(com.sleepycat.je.DatabaseEntry) Cursor(com.sleepycat.je.Cursor) CloseableCursor(com.radixdlt.atom.CloseableCursor) SecondaryCursor(com.sleepycat.je.SecondaryCursor) RadixEngineException(com.radixdlt.engine.RadixEngineException) VirtualSubstateAlreadyDownException(com.radixdlt.constraintmachine.exceptions.VirtualSubstateAlreadyDownException) NoSuchElementException(java.util.NoSuchElementException) IOException(java.io.IOException) DeserializeException(com.radixdlt.serialization.DeserializeException) NoSuchElementException(java.util.NoSuchElementException)

Example 2 with CloseableCursor

use of com.radixdlt.atom.CloseableCursor in project radixdlt by radixdlt.

the class InMemoryEngineStore method transaction.

@Override
public <R> R transaction(TransactionEngineStoreConsumer<M, R> consumer) throws RadixEngineException {
    return consumer.start(new EngineStoreInTransaction<>() {

        @Override
        public void storeTxn(REProcessedTxn txn) {
            synchronized (lock) {
                txn.stateUpdates().forEach(update -> {
                    store.storedState.put(update.getId(), update);
                    // FIXME: Superhack
                    if (update.isBootUp()) {
                        if (update.getParsed() instanceof TokenResource) {
                            var tokenDef = (TokenResource) update.getParsed();
                            store.resources.put(tokenDef.addr(), update::getStateBuf);
                        } else if (update.getParsed() instanceof VirtualParent) {
                            var p = (VirtualParent) update.getParsed();
                            var typeByte = p.data()[0];
                            var mapKey = SystemMapKey.ofSystem(typeByte);
                            store.maps.put(mapKey, update.getRawSubstateBytes());
                        } else if (update.getParsed() instanceof ValidatorData) {
                            var data = (ValidatorData) update.getParsed();
                            var mapKey = SystemMapKey.ofSystem(update.typeByte(), data.validatorKey().getCompressedBytes());
                            store.maps.put(mapKey, update.getRawSubstateBytes());
                        } else if (update.getParsed() instanceof SystemData) {
                            var mapKey = SystemMapKey.ofSystem(update.typeByte());
                            store.maps.put(mapKey, update.getRawSubstateBytes());
                        }
                    } else if (update.isShutDown()) {
                        if (update.getParsed() instanceof ValidatorData) {
                            var data = (ValidatorData) update.getParsed();
                            var mapKey = SystemMapKey.ofSystem(update.typeByte(), data.validatorKey().getCompressedBytes());
                            store.maps.remove(mapKey);
                        } else if (update.getParsed() instanceof SystemData) {
                            var mapKey = SystemMapKey.ofSystem(update.typeByte());
                            store.maps.remove(mapKey);
                        }
                    }
                });
            }
        }

        @Override
        public void storeMetadata(M metadata) {
            store.metadata = metadata;
        }

        @Override
        public ByteBuffer verifyVirtualSubstate(SubstateId substateId) throws VirtualSubstateAlreadyDownException, VirtualParentStateDoesNotExist {
            synchronized (lock) {
                var parent = substateId.getVirtualParent().orElseThrow();
                var update = store.storedState.get(parent);
                if (update == null || !(update.getParsed() instanceof VirtualParent)) {
                    throw new VirtualParentStateDoesNotExist(parent);
                }
                var inst = store.storedState.get(substateId);
                if (inst != null && inst.isShutDown()) {
                    throw new VirtualSubstateAlreadyDownException(substateId);
                }
                return update.getStateBuf();
            }
        }

        @Override
        public Optional<ByteBuffer> loadSubstate(SubstateId substateId) {
            synchronized (lock) {
                var inst = store.storedState.get(substateId);
                if (inst == null || !inst.isBootUp()) {
                    return Optional.empty();
                }
                return Optional.of(inst.getStateBuf());
            }
        }

        @Override
        public CloseableCursor<RawSubstateBytes> openIndexedCursor(SubstateIndex<?> index) {
            return InMemoryEngineStore.this.openIndexedCursor(index);
        }

        @Override
        public Optional<ByteBuffer> loadResource(REAddr addr) {
            synchronized (lock) {
                var supplier = store.resources.get(addr);
                return supplier == null ? Optional.empty() : Optional.of(supplier.get());
            }
        }
    });
}
Also used : SubstateId(com.radixdlt.atom.SubstateId) REStateUpdate(com.radixdlt.constraintmachine.REStateUpdate) RadixEngineException(com.radixdlt.engine.RadixEngineException) Inject(com.google.inject.Inject) HashMap(java.util.HashMap) Supplier(java.util.function.Supplier) ByteBuffer(java.nio.ByteBuffer) RawSubstateBytes(com.radixdlt.constraintmachine.RawSubstateBytes) ArrayList(java.util.ArrayList) Map(java.util.Map) SystemMapKey(com.radixdlt.constraintmachine.SystemMapKey) REProcessedTxn(com.radixdlt.constraintmachine.REProcessedTxn) UnsignedBytes(com.google.common.primitives.UnsignedBytes) SubstateIndex(com.radixdlt.constraintmachine.SubstateIndex) TokenResource(com.radixdlt.application.tokens.state.TokenResource) REAddr(com.radixdlt.identifiers.REAddr) ValidatorData(com.radixdlt.application.validators.state.ValidatorData) VirtualParent(com.radixdlt.application.system.state.VirtualParent) VirtualSubstateAlreadyDownException(com.radixdlt.constraintmachine.exceptions.VirtualSubstateAlreadyDownException) List(java.util.List) CloseableCursor(com.radixdlt.atom.CloseableCursor) VirtualParentStateDoesNotExist(com.radixdlt.constraintmachine.exceptions.VirtualParentStateDoesNotExist) Optional(java.util.Optional) Comparator(java.util.Comparator) SystemData(com.radixdlt.application.system.state.SystemData) ValidatorData(com.radixdlt.application.validators.state.ValidatorData) CloseableCursor(com.radixdlt.atom.CloseableCursor) TokenResource(com.radixdlt.application.tokens.state.TokenResource) Optional(java.util.Optional) SystemData(com.radixdlt.application.system.state.SystemData) REProcessedTxn(com.radixdlt.constraintmachine.REProcessedTxn) ByteBuffer(java.nio.ByteBuffer) VirtualParentStateDoesNotExist(com.radixdlt.constraintmachine.exceptions.VirtualParentStateDoesNotExist) SubstateId(com.radixdlt.atom.SubstateId) VirtualSubstateAlreadyDownException(com.radixdlt.constraintmachine.exceptions.VirtualSubstateAlreadyDownException) VirtualParent(com.radixdlt.application.system.state.VirtualParent) REAddr(com.radixdlt.identifiers.REAddr)

Example 3 with CloseableCursor

use of com.radixdlt.atom.CloseableCursor in project radixdlt by radixdlt.

the class BerkeleyLedgerEntryStore method transaction.

@Override
public <R> R transaction(TransactionEngineStoreConsumer<LedgerAndBFTProof, R> consumer) throws RadixEngineException {
    var dbTxn = createTransaction();
    try {
        var result = consumer.start(new EngineStoreInTransaction<>() {

            @Override
            public void storeTxn(REProcessedTxn txn) {
                BerkeleyLedgerEntryStore.this.storeTxn(dbTxn, txn);
            }

            @Override
            public void storeMetadata(LedgerAndBFTProof metadata) {
                BerkeleyLedgerEntryStore.this.storeMetadata(dbTxn, metadata);
            }

            @Override
            public ByteBuffer verifyVirtualSubstate(SubstateId substateId) throws VirtualSubstateAlreadyDownException, VirtualParentStateDoesNotExist {
                var parent = substateId.getVirtualParent().orElseThrow();
                var parentState = BerkeleyLedgerEntryStore.this.loadSubstate(dbTxn, parent);
                if (parentState.isEmpty()) {
                    throw new VirtualParentStateDoesNotExist(parent);
                }
                var buf = parentState.get();
                if (buf.get() != SubstateTypeId.VIRTUAL_PARENT.id()) {
                    throw new VirtualParentStateDoesNotExist(parent);
                }
                buf.position(buf.position() - 1);
                if (BerkeleyLedgerEntryStore.this.isVirtualDown(dbTxn, substateId)) {
                    throw new VirtualSubstateAlreadyDownException(substateId);
                }
                return buf;
            }

            @Override
            public Optional<ByteBuffer> loadSubstate(SubstateId substateId) {
                return BerkeleyLedgerEntryStore.this.loadSubstate(dbTxn, substateId);
            }

            @Override
            public CloseableCursor<RawSubstateBytes> openIndexedCursor(SubstateIndex<?> index) {
                return BerkeleyLedgerEntryStore.this.openIndexedCursor(dbTxn, index);
            }

            @Override
            public Optional<ByteBuffer> loadResource(REAddr addr) {
                return BerkeleyLedgerEntryStore.this.loadAddr(dbTxn, addr);
            }
        });
        dbTxn.commit();
        return result;
    } catch (Exception e) {
        dbTxn.abort();
        throw e;
    }
}
Also used : CloseableCursor(com.radixdlt.atom.CloseableCursor) Optional(java.util.Optional) LedgerAndBFTProof(com.radixdlt.statecomputer.LedgerAndBFTProof) REProcessedTxn(com.radixdlt.constraintmachine.REProcessedTxn) ByteBuffer(java.nio.ByteBuffer) RadixEngineException(com.radixdlt.engine.RadixEngineException) VirtualSubstateAlreadyDownException(com.radixdlt.constraintmachine.exceptions.VirtualSubstateAlreadyDownException) NoSuchElementException(java.util.NoSuchElementException) IOException(java.io.IOException) DeserializeException(com.radixdlt.serialization.DeserializeException) VirtualParentStateDoesNotExist(com.radixdlt.constraintmachine.exceptions.VirtualParentStateDoesNotExist) SubstateId(com.radixdlt.atom.SubstateId) VirtualSubstateAlreadyDownException(com.radixdlt.constraintmachine.exceptions.VirtualSubstateAlreadyDownException) REAddr(com.radixdlt.identifiers.REAddr)

Aggregations

CloseableCursor (com.radixdlt.atom.CloseableCursor)3 VirtualSubstateAlreadyDownException (com.radixdlt.constraintmachine.exceptions.VirtualSubstateAlreadyDownException)3 RadixEngineException (com.radixdlt.engine.RadixEngineException)3 Optional (java.util.Optional)3 SubstateId (com.radixdlt.atom.SubstateId)2 REProcessedTxn (com.radixdlt.constraintmachine.REProcessedTxn)2 VirtualParentStateDoesNotExist (com.radixdlt.constraintmachine.exceptions.VirtualParentStateDoesNotExist)2 REAddr (com.radixdlt.identifiers.REAddr)2 DeserializeException (com.radixdlt.serialization.DeserializeException)2 IOException (java.io.IOException)2 ByteBuffer (java.nio.ByteBuffer)2 NoSuchElementException (java.util.NoSuchElementException)2 UnsignedBytes (com.google.common.primitives.UnsignedBytes)1 Inject (com.google.inject.Inject)1 SystemData (com.radixdlt.application.system.state.SystemData)1 VirtualParent (com.radixdlt.application.system.state.VirtualParent)1 TokenResource (com.radixdlt.application.tokens.state.TokenResource)1 ValidatorData (com.radixdlt.application.validators.state.ValidatorData)1 REStateUpdate (com.radixdlt.constraintmachine.REStateUpdate)1 RawSubstateBytes (com.radixdlt.constraintmachine.RawSubstateBytes)1