Search in sources :

Example 16 with UInt256

use of com.radixdlt.utils.UInt256 in project radixdlt by radixdlt.

the class BerkeleyLedgerEntryStore method open.

private void open() {
    var primaryConfig = buildPrimaryConfig();
    var rriConfig = buildRriConfig();
    var pendingConfig = buildPendingConfig();
    try {
        // This SuppressWarnings here is valid, as ownership of the underlying
        // resource is not changed here, the resource is just accessed.
        @SuppressWarnings("resource") var env = dbEnv.getEnvironment();
        txnDatabase = env.openDatabase(null, TXN_DB_NAME, primaryConfig);
        resourceDatabase = env.openDatabase(null, RESOURCE_DB_NAME, rriConfig);
        mapDatabase = env.openDatabase(null, MAP_DB_NAME, rriConfig);
        substatesDatabase = env.openDatabase(null, SUBSTATE_DB_NAME, primaryConfig);
        indexedSubstatesDatabase = env.openSecondaryDatabase(null, INDEXED_SUBSTATE_DB_NAME, substatesDatabase, (SecondaryConfig) new SecondaryConfig().setKeyCreator((secondary, key, data, result) -> {
            if (entryToSpin(data) != REOp.UP) {
                return false;
            }
            var substateTypeId = data.getData()[data.getOffset()];
            final int prefixIndexSize;
            if (substateTypeId == SubstateTypeId.TOKENS.id()) {
                // Indexing not necessary for verification at the moment but useful
                // for construction
                // 0: Type Byte
                // 1: Reserved Byte
                // 2-37: Account Address
                prefixIndexSize = 2 + (1 + ECPublicKey.COMPRESSED_BYTES);
            } else if (substateTypeId == SubstateTypeId.STAKE_OWNERSHIP.id()) {
                // Indexing not necessary for verification at the moment but useful
                // for construction
                // This should have had validator keys and account addresses switched
                // so that
                // prefix indexing could be done against account addresses rather than
                // validators
                // so that actions like "Unstake Everything" could be implemented and
                // queries against
                // accounts. A later to do...
                // 0: Type Byte
                // 1: Reserved Byte
                // 2-36: Validator Key
                // 37-69: Account Address
                prefixIndexSize = 2 + ECPublicKey.COMPRESSED_BYTES + (1 + ECPublicKey.COMPRESSED_BYTES);
            } else if (substateTypeId == SubstateTypeId.EXITING_STAKE.id()) {
                // 0: Type Byte
                // 1: Reserved Byte
                // 2-5: Epoch
                // 6-40: Validator Key
                // 41-73: Account Address
                prefixIndexSize = 2 + Long.BYTES + ECPublicKey.COMPRESSED_BYTES + (1 + ECPublicKey.COMPRESSED_BYTES);
            } else if (substateTypeId == SubstateTypeId.PREPARED_STAKE.id()) {
                // 0: Type Byte
                // 1: Reserved Byte
                // 2-36: Validator Key
                // 37-69: Account Address
                prefixIndexSize = 2 + ECPublicKey.COMPRESSED_BYTES + (1 + ECPublicKey.COMPRESSED_BYTES);
            } else if (substateTypeId == SubstateTypeId.PREPARED_UNSTAKE.id()) {
                // 0: Type Byte
                // 1: Reserved Byte
                // 2-36: Validator Key
                // 37-69: Account Address
                prefixIndexSize = 2 + ECPublicKey.COMPRESSED_BYTES + (1 + ECPublicKey.COMPRESSED_BYTES);
            } else if (substateTypeId == SubstateTypeId.VALIDATOR_OWNER_COPY.id()) {
                // 0: Type Byte
                // 1: Reserved Byte
                // 2: Optional flag
                // 3-6: Epoch
                // 7-41: Validator Key
                prefixIndexSize = 3 + Long.BYTES + ECPublicKey.COMPRESSED_BYTES;
            } else if (substateTypeId == SubstateTypeId.VALIDATOR_REGISTERED_FLAG_COPY.id()) {
                // 0: Type Byte
                // 1: Reserved Byte
                // 2: Optional flag
                // 3-6: Epoch
                // 7-41: Validator Key
                prefixIndexSize = 3 + Long.BYTES + ECPublicKey.COMPRESSED_BYTES;
            } else if (substateTypeId == SubstateTypeId.VALIDATOR_RAKE_COPY.id()) {
                // 0: Type Byte
                // 1: Reserved Byte
                // 2: Optional flag
                // 3-6: Epoch
                // 7-41: Validator Key
                prefixIndexSize = 3 + Long.BYTES + ECPublicKey.COMPRESSED_BYTES;
            } else if (substateTypeId == SubstateTypeId.VALIDATOR_STAKE_DATA.id()) {
                // 0: Type Byte
                // 1: Reserved Byte
                // 2: Registered Byte
                // 3-34: Stake amount
                // 35-67: Validator key
                prefixIndexSize = 3 + UInt256.BYTES + ECPublicKey.COMPRESSED_BYTES;
            } else {
                // 0: Type Byte
                prefixIndexSize = 1;
            }
            // Index by substate type
            result.setData(data.getData(), data.getOffset(), prefixIndexSize);
            return true;
        }).setBtreeComparator(lexicographicalComparator()).setSortedDuplicates(true).setAllowCreate(true).setTransactional(true));
        proofDatabase = env.openDatabase(null, PROOF_DB_NAME, primaryConfig);
        vertexStoreDatabase = env.openDatabase(null, VERTEX_STORE_DB_NAME, pendingConfig);
        epochProofDatabase = env.openSecondaryDatabase(null, EPOCH_PROOF_DB_NAME, proofDatabase, buildEpochProofConfig());
        forkConfigDatabase = env.openDatabase(null, FORK_CONFIG_DB, primaryConfig);
        forksVotingResultsDatabase = env.openDatabase(null, FORKS_VOTING_RESULTS_DB, primaryConfig.clone().setSortedDuplicates(true));
        txnLog = AppendLog.openCompressed(new File(env.getHome(), LEDGER_NAME).getAbsolutePath(), systemCounters);
    } catch (Exception e) {
        throw new BerkeleyStoreException("Error while opening databases", e);
    }
    this.additionalStores.forEach(b -> b.open(dbEnv));
    if (System.getProperty("db.check_integrity", "1").equals("1")) {
    // TODO implement integrity check
    // TODO perhaps we should implement recovery instead?
    // TODO recovering should be integrated with recovering of ClientApiStore
    }
}
Also used : AppendLog(com.radixdlt.store.berkeley.atom.AppendLog) Arrays(java.util.Arrays) RadixEngineException(com.radixdlt.engine.RadixEngineException) LedgerAndBFTProof(com.radixdlt.statecomputer.LedgerAndBFTProof) Inject(com.google.inject.Inject) VerifiedVertexStoreState(com.radixdlt.hotstuff.bft.VerifiedVertexStoreState) ByteBuffer(java.nio.ByteBuffer) DEFAULT(com.sleepycat.je.LockMode.DEFAULT) VerifiedTxnsAndProof(com.radixdlt.ledger.VerifiedTxnsAndProof) PersistentVertexStore(com.radixdlt.hotstuff.bft.PersistentVertexStore) ForkConfig(com.radixdlt.statecomputer.forks.ForkConfig) BigInteger(java.math.BigInteger) SystemMapKey(com.radixdlt.constraintmachine.SystemMapKey) Get(com.sleepycat.je.Get) SystemCounters(com.radixdlt.counters.SystemCounters) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) DatabaseEnvironment(com.radixdlt.store.DatabaseEnvironment) DatabaseEntry(com.sleepycat.je.DatabaseEntry) Set(java.util.Set) Output(com.radixdlt.serialization.DsonOutput.Output) ResourceStore(com.radixdlt.store.ResourceStore) REAddr(com.radixdlt.identifiers.REAddr) Streams(com.google.common.collect.Streams) EngineStore(com.radixdlt.store.EngineStore) Bytes(com.google.common.primitives.Bytes) ForksEpochStore(com.radixdlt.statecomputer.forks.ForksEpochStore) Objects(java.util.Objects) VirtualSubstateAlreadyDownException(com.radixdlt.constraintmachine.exceptions.VirtualSubstateAlreadyDownException) List(java.util.List) DatabaseConfig(com.sleepycat.je.DatabaseConfig) Logger(org.apache.logging.log4j.Logger) VirtualParentStateDoesNotExist(com.radixdlt.constraintmachine.exceptions.VirtualParentStateDoesNotExist) StoreConfig(com.radixdlt.store.StoreConfig) CommittedReader(com.radixdlt.sync.CommittedReader) ResourceData(com.radixdlt.application.tokens.state.ResourceData) Optional(java.util.Optional) SystemData(com.radixdlt.application.system.state.SystemData) CounterType(com.radixdlt.counters.SystemCounters.CounterType) SubstateId(com.radixdlt.atom.SubstateId) SubstateTypeId(com.radixdlt.atom.SubstateTypeId) REStateUpdate(com.radixdlt.constraintmachine.REStateUpdate) OperationStatus(com.sleepycat.je.OperationStatus) AID(com.radixdlt.identifiers.AID) LedgerProof(com.radixdlt.hotstuff.LedgerProof) Stopwatch(com.google.common.base.Stopwatch) Serialization(com.radixdlt.serialization.Serialization) UnsignedBytes.lexicographicalComparator(com.google.common.primitives.UnsignedBytes.lexicographicalComparator) ECPublicKey(com.radixdlt.crypto.ECPublicKey) ForkVotingResult(com.radixdlt.statecomputer.forks.ForkVotingResult) Supplier(java.util.function.Supplier) RawSubstateBytes(com.radixdlt.constraintmachine.RawSubstateBytes) Longs(com.radixdlt.utils.Longs) OptionalLong(java.util.OptionalLong) ImmutableList(com.google.common.collect.ImmutableList) REOp(com.radixdlt.constraintmachine.REOp) Database(com.sleepycat.je.Database) SecondaryConfig(com.sleepycat.je.SecondaryConfig) NoSuchElementException(java.util.NoSuchElementException) UInt256(com.radixdlt.utils.UInt256) REProcessedTxn(com.radixdlt.constraintmachine.REProcessedTxn) Shorts(com.radixdlt.utils.Shorts) Iterator(java.util.Iterator) SubstateIndex(com.radixdlt.constraintmachine.SubstateIndex) HashCode(com.google.common.hash.HashCode) DtoLedgerProof(com.radixdlt.ledger.DtoLedgerProof) TokenResource(com.radixdlt.application.tokens.state.TokenResource) Txn(com.radixdlt.atom.Txn) IOException(java.io.IOException) SUCCESS(com.sleepycat.je.OperationStatus.SUCCESS) CandidateForkVote(com.radixdlt.statecomputer.forks.CandidateForkVote) Longs.fromByteArray(com.radixdlt.utils.Longs.fromByteArray) SecondaryDatabase(com.sleepycat.je.SecondaryDatabase) File(java.io.File) NOTFOUND(com.sleepycat.je.OperationStatus.NOTFOUND) ValidatorData(com.radixdlt.application.validators.state.ValidatorData) Cursor(com.sleepycat.je.Cursor) VirtualParent(com.radixdlt.application.system.state.VirtualParent) TimeUnit(java.util.concurrent.TimeUnit) CloseableCursor(com.radixdlt.atom.CloseableCursor) SecondaryCursor(com.sleepycat.je.SecondaryCursor) DeserializeException(com.radixdlt.serialization.DeserializeException) Transaction(com.sleepycat.je.Transaction) LogManager(org.apache.logging.log4j.LogManager) SecondaryConfig(com.sleepycat.je.SecondaryConfig) File(java.io.File) RadixEngineException(com.radixdlt.engine.RadixEngineException) VirtualSubstateAlreadyDownException(com.radixdlt.constraintmachine.exceptions.VirtualSubstateAlreadyDownException) NoSuchElementException(java.util.NoSuchElementException) IOException(java.io.IOException) DeserializeException(com.radixdlt.serialization.DeserializeException)

Example 17 with UInt256

use of com.radixdlt.utils.UInt256 in project radixdlt by radixdlt.

the class TokensConstraintScryptV3 method registerParticles.

private void registerParticles(Loader os) {
    os.substate(new SubstateDefinition<>(TokenResource.class, SubstateTypeId.TOKEN_RESOURCE.id(), buf -> {
        REFieldSerialization.deserializeReservedByte(buf);
        var addr = REFieldSerialization.deserializeResourceAddr(buf);
        var granularity = REFieldSerialization.deserializeNonZeroUInt256(buf);
        if (!granularity.equals(UInt256.ONE)) {
            throw new DeserializeException("Granularity must be one.");
        }
        var isMutable = REFieldSerialization.deserializeBoolean(buf);
        var minter = REFieldSerialization.deserializeOptionalKey(buf);
        return new TokenResource(addr, granularity, isMutable, minter.orElse(null));
    }, (s, buf) -> {
        REFieldSerialization.serializeReservedByte(buf);
        REFieldSerialization.serializeREAddr(buf, s.addr());
        REFieldSerialization.serializeUInt256(buf, UInt256.ONE);
        REFieldSerialization.serializeBoolean(buf, s.isMutable());
        REFieldSerialization.serializeOptionalKey(buf, s.optionalOwner());
    }));
    os.substate(new SubstateDefinition<>(TokenResourceMetadata.class, SubstateTypeId.TOKEN_RESOURCE_METADATA.id(), buf -> {
        REFieldSerialization.deserializeReservedByte(buf);
        var addr = REFieldSerialization.deserializeResourceAddr(buf);
        var symbol = REFieldSerialization.deserializeString(buf);
        var name = REFieldSerialization.deserializeString(buf);
        var description = REFieldSerialization.deserializeString(buf);
        var url = REFieldSerialization.deserializeUrl(buf);
        var iconUrl = REFieldSerialization.deserializeUrl(buf);
        return new TokenResourceMetadata(addr, symbol, name, description, iconUrl, url);
    }, (s, buf) -> {
        REFieldSerialization.serializeReservedByte(buf);
        REFieldSerialization.serializeREAddr(buf, s.addr());
        REFieldSerialization.serializeString(buf, s.symbol());
        REFieldSerialization.serializeString(buf, s.name());
        REFieldSerialization.serializeString(buf, s.description());
        REFieldSerialization.serializeString(buf, s.url());
        REFieldSerialization.serializeString(buf, s.iconUrl());
    }));
    os.substate(new SubstateDefinition<>(TokensInAccount.class, SubstateTypeId.TOKENS.id(), buf -> {
        REFieldSerialization.deserializeReservedByte(buf);
        var holdingAddr = REFieldSerialization.deserializeAccountREAddr(buf);
        var addr = REFieldSerialization.deserializeResourceAddr(buf);
        var amount = REFieldSerialization.deserializeNonZeroUInt256(buf);
        return new TokensInAccount(holdingAddr, addr, amount);
    }, (s, buf) -> {
        REFieldSerialization.serializeReservedByte(buf);
        REFieldSerialization.serializeREAddr(buf, s.holdingAddress());
        REFieldSerialization.serializeREAddr(buf, s.resourceAddr());
        buf.put(s.amount().toByteArray());
    }));
}
Also used : SubstateTypeId(com.radixdlt.atom.SubstateTypeId) TokenResourceMetadata(com.radixdlt.application.tokens.state.TokenResourceMetadata) Loader(com.radixdlt.atomos.Loader) REFieldSerialization(com.radixdlt.atom.REFieldSerialization) TokenResource(com.radixdlt.application.tokens.state.TokenResource) Set(java.util.Set) TokensInAccount(com.radixdlt.application.tokens.state.TokensInAccount) SubstateDefinition(com.radixdlt.atomos.SubstateDefinition) StandardCharsets(java.nio.charset.StandardCharsets) com.radixdlt.constraintmachine(com.radixdlt.constraintmachine) ResourceCreatedEvent(com.radixdlt.constraintmachine.REEvent.ResourceCreatedEvent) ReservedSymbolException(com.radixdlt.constraintmachine.exceptions.ReservedSymbolException) SystemConstraintScrypt(com.radixdlt.application.system.scrypt.SystemConstraintScrypt) ConstraintScrypt(com.radixdlt.atomos.ConstraintScrypt) ProcedureException(com.radixdlt.constraintmachine.exceptions.ProcedureException) Pattern(java.util.regex.Pattern) DeserializeException(com.radixdlt.serialization.DeserializeException) UInt256(com.radixdlt.utils.UInt256) TokenResource(com.radixdlt.application.tokens.state.TokenResource) TokenResourceMetadata(com.radixdlt.application.tokens.state.TokenResourceMetadata) TokensInAccount(com.radixdlt.application.tokens.state.TokensInAccount) DeserializeException(com.radixdlt.serialization.DeserializeException)

Example 18 with UInt256

use of com.radixdlt.utils.UInt256 in project radixdlt by radixdlt.

the class WeightedRotatingLeadersTest method setUp.

private void setUp(int validatorSetSize, int sizeOfCache) {
    this.validatorsInOrder = Stream.generate(() -> ECKeyPair.generateNew().getPublicKey()).limit(validatorSetSize).map(BFTNode::create).map(node -> BFTValidator.from(node, UInt256.ONE)).sorted(Comparator.comparing(v -> v.getNode().getKey(), KeyComparator.instance().reversed())).collect(ImmutableList.toImmutableList());
    BFTValidatorSet validatorSet = BFTValidatorSet.from(validatorsInOrder);
    this.weightedRotatingLeaders = new WeightedRotatingLeaders(validatorSet, sizeOfCache);
    this.weightedRotatingLeaders2 = new WeightedRotatingLeaders(validatorSet, sizeOfCache);
}
Also used : IntStream(java.util.stream.IntStream) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Collectors.counting(java.util.stream.Collectors.counting) Collectors.groupingBy(java.util.stream.Collectors.groupingBy) Test(org.junit.Test) Collectors.collectingAndThen(java.util.stream.Collectors.collectingAndThen) BFTValidatorSet(com.radixdlt.consensus.bft.BFTValidatorSet) Supplier(java.util.function.Supplier) BFTValidator(com.radixdlt.consensus.bft.BFTValidator) ECKeyPair(com.radixdlt.crypto.ECKeyPair) Stream(java.util.stream.Stream) Collectors.toMap(java.util.stream.Collectors.toMap) ImmutableList(com.google.common.collect.ImmutableList) Map(java.util.Map) BFTNode(com.radixdlt.consensus.bft.BFTNode) KeyComparator(com.radixdlt.utils.KeyComparator) Comparator(java.util.Comparator) View(com.radixdlt.consensus.bft.View) UInt256(com.radixdlt.utils.UInt256) BFTNode(com.radixdlt.consensus.bft.BFTNode) BFTValidatorSet(com.radixdlt.consensus.bft.BFTValidatorSet)

Example 19 with UInt256

use of com.radixdlt.utils.UInt256 in project radixdlt by radixdlt.

the class WeightedRotatingLeadersTest method when_validators_distributed_by_fibonacci__then_leaders_also_distributed_in_fibonacci.

@Test
public void when_validators_distributed_by_fibonacci__then_leaders_also_distributed_in_fibonacci() {
    // fibonacci sequence can quickly explode so keep sizes small
    final int validatorSetSize = 8;
    final int sizeOfCache = 4;
    final Supplier<IntStream> fibonacci = () -> Stream.iterate(new int[] { 1, 1 }, t -> new int[] { t[1], t[0] + t[1] }).mapToInt(t -> t[0]).limit(validatorSetSize);
    final int sumOfPower = fibonacci.get().sum();
    this.validatorsInOrder = fibonacci.get().mapToObj(p -> BFTValidator.from(BFTNode.random(), UInt256.from(p))).collect(ImmutableList.toImmutableList());
    BFTValidatorSet validatorSet = BFTValidatorSet.from(validatorsInOrder);
    this.weightedRotatingLeaders = new WeightedRotatingLeaders(validatorSet, sizeOfCache);
    Map<BFTNode, UInt256> proposerCounts = Stream.iterate(View.of(0), View::next).limit(sumOfPower).map(this.weightedRotatingLeaders::getProposer).collect(groupingBy(p -> p, collectingAndThen(counting(), UInt256::from)));
    Map<BFTNode, UInt256> expected = validatorsInOrder.stream().collect(toMap(BFTValidator::getNode, BFTValidator::getPower));
    assertThat(proposerCounts).isEqualTo(expected);
}
Also used : IntStream(java.util.stream.IntStream) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Collectors.counting(java.util.stream.Collectors.counting) Collectors.groupingBy(java.util.stream.Collectors.groupingBy) Test(org.junit.Test) Collectors.collectingAndThen(java.util.stream.Collectors.collectingAndThen) BFTValidatorSet(com.radixdlt.consensus.bft.BFTValidatorSet) Supplier(java.util.function.Supplier) BFTValidator(com.radixdlt.consensus.bft.BFTValidator) ECKeyPair(com.radixdlt.crypto.ECKeyPair) Stream(java.util.stream.Stream) Collectors.toMap(java.util.stream.Collectors.toMap) ImmutableList(com.google.common.collect.ImmutableList) Map(java.util.Map) BFTNode(com.radixdlt.consensus.bft.BFTNode) KeyComparator(com.radixdlt.utils.KeyComparator) Comparator(java.util.Comparator) View(com.radixdlt.consensus.bft.View) UInt256(com.radixdlt.utils.UInt256) BFTNode(com.radixdlt.consensus.bft.BFTNode) BFTValidatorSet(com.radixdlt.consensus.bft.BFTValidatorSet) IntStream(java.util.stream.IntStream) UInt256(com.radixdlt.utils.UInt256) Test(org.junit.Test)

Aggregations

UInt256 (com.radixdlt.utils.UInt256)19 Test (org.junit.Test)9 ImmutableList (com.google.common.collect.ImmutableList)8 ECKeyPair (com.radixdlt.crypto.ECKeyPair)7 Stream (java.util.stream.Stream)7 View (com.radixdlt.hotstuff.bft.View)6 List (java.util.List)6 Map (java.util.Map)6 IntStream (java.util.stream.IntStream)6 Inject (com.google.inject.Inject)4 NotEnoughNativeTokensForFeesException (com.radixdlt.api.core.model.NotEnoughNativeTokensForFeesException)4 OperationTxBuilder (com.radixdlt.api.core.model.OperationTxBuilder)4 TokenResource (com.radixdlt.api.core.model.TokenResource)4 AccountVaultEntity (com.radixdlt.api.core.model.entities.AccountVaultEntity)4 Supplier (java.util.function.Supplier)4 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)4 AbstractModule (com.google.inject.AbstractModule)3 Guice (com.google.inject.Guice)3 Module (com.google.inject.Module)3 TypeLiteral (com.google.inject.TypeLiteral)3