Search in sources :

Example 1 with ECPublicKey

use of com.radixdlt.crypto.ECPublicKey in project radixdlt by radixdlt.

the class EpochUpdateConstraintScrypt method main.

@Override
public void main(Loader os) {
    os.substate(new SubstateDefinition<>(ValidatorStakeData.class, SubstateTypeId.VALIDATOR_STAKE_DATA.id(), buf -> {
        REFieldSerialization.deserializeReservedByte(buf);
        var isRegistered = REFieldSerialization.deserializeBoolean(buf);
        var amount = REFieldSerialization.deserializeUInt256(buf);
        var delegate = REFieldSerialization.deserializeKey(buf);
        var ownership = REFieldSerialization.deserializeUInt256(buf);
        var rakePercentage = REFieldSerialization.deserializeInt(buf);
        var ownerAddress = REFieldSerialization.deserializeAccountREAddr(buf);
        return ValidatorStakeData.create(delegate, amount, ownership, rakePercentage, ownerAddress, isRegistered);
    }, (s, buf) -> {
        REFieldSerialization.serializeReservedByte(buf);
        REFieldSerialization.serializeBoolean(buf, s.isRegistered());
        buf.put(s.amount().toByteArray());
        REFieldSerialization.serializeKey(buf, s.validatorKey());
        buf.put(s.totalOwnership().toByteArray());
        buf.putInt(s.rakePercentage());
        REFieldSerialization.serializeREAddr(buf, s.ownerAddr());
    }, buf -> REFieldSerialization.deserializeKey(buf), (k, buf) -> REFieldSerialization.serializeKey(buf, (ECPublicKey) k), k -> ValidatorStakeData.createVirtual((ECPublicKey) k)));
    os.substate(new SubstateDefinition<>(StakeOwnership.class, SubstateTypeId.STAKE_OWNERSHIP.id(), buf -> {
        REFieldSerialization.deserializeReservedByte(buf);
        var delegate = REFieldSerialization.deserializeKey(buf);
        var owner = REFieldSerialization.deserializeAccountREAddr(buf);
        var amount = REFieldSerialization.deserializeNonZeroUInt256(buf);
        return new StakeOwnership(delegate, owner, amount);
    }, (s, buf) -> {
        REFieldSerialization.serializeReservedByte(buf);
        REFieldSerialization.serializeKey(buf, s.delegateKey());
        REFieldSerialization.serializeREAddr(buf, s.owner());
        buf.put(s.amount().toByteArray());
    }));
    os.substate(new SubstateDefinition<>(ExitingStake.class, SubstateTypeId.EXITING_STAKE.id(), buf -> {
        REFieldSerialization.deserializeReservedByte(buf);
        var epochUnlocked = REFieldSerialization.deserializeNonNegativeLong(buf);
        var delegate = REFieldSerialization.deserializeKey(buf);
        var owner = REFieldSerialization.deserializeAccountREAddr(buf);
        var amount = REFieldSerialization.deserializeNonZeroUInt256(buf);
        return new ExitingStake(epochUnlocked, delegate, owner, amount);
    }, (s, buf) -> {
        REFieldSerialization.serializeReservedByte(buf);
        buf.putLong(s.epochUnlocked());
        REFieldSerialization.serializeKey(buf, s.delegateKey());
        REFieldSerialization.serializeREAddr(buf, s.owner());
        buf.put(s.amount().toByteArray());
    }));
    registerGenesisTransitions(os);
    // Epoch update
    epochUpdate(os);
}
Also used : SubstateTypeId(com.radixdlt.atom.SubstateTypeId) java.util(java.util) StakeOwnership(com.radixdlt.application.system.state.StakeOwnership) PreparedUnstakeOwnership(com.radixdlt.application.tokens.state.PreparedUnstakeOwnership) ValidatorStakeData(com.radixdlt.application.system.state.ValidatorStakeData) NextValidatorSetEvent(com.radixdlt.constraintmachine.REEvent.NextValidatorSetEvent) ValidatorRegisteredCopy(com.radixdlt.application.validators.state.ValidatorRegisteredCopy) MismatchException(com.radixdlt.constraintmachine.exceptions.MismatchException) HasEpochData(com.radixdlt.application.system.state.HasEpochData) ExitingStake(com.radixdlt.application.tokens.state.ExitingStake) SubstateDefinition(com.radixdlt.atomos.SubstateDefinition) ECPublicKey(com.radixdlt.crypto.ECPublicKey) Function(java.util.function.Function) Supplier(java.util.function.Supplier) RoundData(com.radixdlt.application.system.state.RoundData) Longs(com.radixdlt.utils.Longs) ConstraintScrypt(com.radixdlt.atomos.ConstraintScrypt) ValidatorOwnerCopy(com.radixdlt.application.validators.state.ValidatorOwnerCopy) ProcedureException(com.radixdlt.constraintmachine.exceptions.ProcedureException) EpochData(com.radixdlt.application.system.state.EpochData) ValidatorBFTData(com.radixdlt.application.system.state.ValidatorBFTData) UInt256(com.radixdlt.utils.UInt256) UnsignedBytes(com.google.common.primitives.UnsignedBytes) PreparedStake(com.radixdlt.application.tokens.state.PreparedStake) Loader(com.radixdlt.atomos.Loader) REFieldSerialization(com.radixdlt.atom.REFieldSerialization) TokensInAccount(com.radixdlt.application.tokens.state.TokensInAccount) REAddr(com.radixdlt.identifiers.REAddr) Streams(com.google.common.collect.Streams) Collectors(java.util.stream.Collectors) com.radixdlt.constraintmachine(com.radixdlt.constraintmachine) RAKE_MAX(com.radixdlt.application.validators.scrypt.ValidatorUpdateRakeConstraintScrypt.RAKE_MAX) ValidatorFeeCopy(com.radixdlt.application.validators.state.ValidatorFeeCopy) KeyComparator(com.radixdlt.utils.KeyComparator) ValidatorBFTDataEvent(com.radixdlt.constraintmachine.REEvent.ValidatorBFTDataEvent) StakeOwnership(com.radixdlt.application.system.state.StakeOwnership) ValidatorStakeData(com.radixdlt.application.system.state.ValidatorStakeData) ExitingStake(com.radixdlt.application.tokens.state.ExitingStake)

Example 2 with ECPublicKey

use of com.radixdlt.crypto.ECPublicKey in project radixdlt by radixdlt.

the class RoundUpdateConstraintScrypt method main.

@Override
public void main(Loader os) {
    os.substate(new SubstateDefinition<>(ValidatorBFTData.class, SubstateTypeId.VALIDATOR_BFT_DATA.id(), buf -> {
        REFieldSerialization.deserializeReservedByte(buf);
        var key = REFieldSerialization.deserializeKey(buf);
        var proposalsCompleted = REFieldSerialization.deserializeNonNegativeLong(buf);
        var proposalsMissed = REFieldSerialization.deserializeNonNegativeLong(buf);
        return new ValidatorBFTData(key, proposalsCompleted, proposalsMissed);
    }, (s, buf) -> {
        REFieldSerialization.serializeReservedByte(buf);
        REFieldSerialization.serializeKey(buf, s.validatorKey());
        buf.putLong(s.completedProposals());
        buf.putLong(s.missedProposals());
    }, (k, buf) -> REFieldSerialization.serializeKey(buf, (ECPublicKey) k)));
    os.procedure(new DownProcedure<>(VoidReducerState.class, RoundData.class, d -> new Authorization(PermissionLevel.SUPER_USER, (r, c) -> {
    }), (d, s, r, c) -> ReducerResult.incomplete(new EndPrevRound(d))));
    os.procedure(new DownProcedure<>(EndPrevRound.class, ValidatorBFTData.class, d -> new Authorization(PermissionLevel.SUPER_USER, (r, c) -> {
    }), (d, s, r, c) -> {
        var closedRound = s.getClosedRound().view();
        var next = new StartValidatorBFTUpdate(closedRound);
        next.beginUpdate(d);
        return ReducerResult.incomplete(next);
    }));
    os.procedure(new DownProcedure<>(StartValidatorBFTUpdate.class, ValidatorBFTData.class, d -> new Authorization(PermissionLevel.SUPER_USER, (r, c) -> {
    }), (d, s, r, c) -> ReducerResult.incomplete(s.beginUpdate(d))));
    os.procedure(new UpProcedure<>(StartValidatorBFTUpdate.class, ValidatorBFTData.class, u -> new Authorization(PermissionLevel.SUPER_USER, (r, c) -> {
    }), (s, u, c, r) -> {
        var next = s.exit();
        return ReducerResult.incomplete(next.update(u, c));
    }));
    os.procedure(new UpProcedure<>(UpdatingValidatorBFTData.class, ValidatorBFTData.class, u -> new Authorization(PermissionLevel.SUPER_USER, (r, c) -> {
    }), (s, u, c, r) -> ReducerResult.incomplete(s.update(u, c))));
    os.procedure(new UpProcedure<>(StartNextRound.class, RoundData.class, u -> new Authorization(PermissionLevel.SUPER_USER, (r, c) -> {
    }), (s, u, c, r) -> {
        s.update(u);
        return ReducerResult.complete();
    }));
}
Also used : SubstateTypeId(com.radixdlt.atom.SubstateTypeId) ReducerResult(com.radixdlt.constraintmachine.ReducerResult) VoidReducerState(com.radixdlt.constraintmachine.VoidReducerState) Loader(com.radixdlt.atomos.Loader) ReducerState(com.radixdlt.constraintmachine.ReducerState) REFieldSerialization(com.radixdlt.atom.REFieldSerialization) Authorization(com.radixdlt.constraintmachine.Authorization) SubstateDefinition(com.radixdlt.atomos.SubstateDefinition) PermissionLevel(com.radixdlt.constraintmachine.PermissionLevel) ECPublicKey(com.radixdlt.crypto.ECPublicKey) RoundData(com.radixdlt.application.system.state.RoundData) UpProcedure(com.radixdlt.constraintmachine.UpProcedure) TreeMap(java.util.TreeMap) ConstraintScrypt(com.radixdlt.atomos.ConstraintScrypt) KeyComparator(com.radixdlt.utils.KeyComparator) DownProcedure(com.radixdlt.constraintmachine.DownProcedure) ProcedureException(com.radixdlt.constraintmachine.exceptions.ProcedureException) ValidatorBFTData(com.radixdlt.application.system.state.ValidatorBFTData) VoidReducerState(com.radixdlt.constraintmachine.VoidReducerState) RoundData(com.radixdlt.application.system.state.RoundData) ValidatorBFTData(com.radixdlt.application.system.state.ValidatorBFTData) Authorization(com.radixdlt.constraintmachine.Authorization)

Example 3 with ECPublicKey

use of com.radixdlt.crypto.ECPublicKey in project radixdlt by radixdlt.

the class MessageCentralValidatorSyncTest method when_send_error_response__then_message_central_will_send_error_response.

@Test
public void when_send_error_response__then_message_central_will_send_error_response() {
    QuorumCertificate qc = mock(QuorumCertificate.class);
    HighQC highQC = mock(HighQC.class);
    when(highQC.highestQC()).thenReturn(qc);
    when(highQC.highestCommittedQC()).thenReturn(qc);
    BFTNode node = mock(BFTNode.class);
    ECPublicKey ecPublicKey = mock(ECPublicKey.class);
    when(node.getKey()).thenReturn(ecPublicKey);
    final var request = new GetVerticesRequest(HashUtils.random256(), 3);
    sync.verticesErrorResponseDispatcher().dispatch(node, new GetVerticesErrorResponse(highQC, request));
    verify(messageCentral, times(1)).send(eq(NodeId.fromPublicKey(ecPublicKey)), any(GetVerticesErrorResponseMessage.class));
}
Also used : HighQC(com.radixdlt.hotstuff.HighQC) BFTNode(com.radixdlt.hotstuff.bft.BFTNode) GetVerticesRequest(com.radixdlt.hotstuff.sync.GetVerticesRequest) ECPublicKey(com.radixdlt.crypto.ECPublicKey) QuorumCertificate(com.radixdlt.hotstuff.QuorumCertificate) GetVerticesErrorResponse(com.radixdlt.hotstuff.sync.GetVerticesErrorResponse) Test(org.junit.Test)

Example 4 with ECPublicKey

use of com.radixdlt.crypto.ECPublicKey in project radixdlt by radixdlt.

the class MessageCentralValidatorSyncTest method setUp.

@Before
public void setUp() {
    this.self = mock(BFTNode.class);
    ECPublicKey pubKey = mock(ECPublicKey.class);
    when(self.getKey()).thenReturn(pubKey);
    this.messageCentral = MessageCentralMockProvider.get();
    this.hasher = new RandomHasher();
    this.sync = new MessageCentralValidatorSync(messageCentral, hasher);
}
Also used : BFTNode(com.radixdlt.hotstuff.bft.BFTNode) RandomHasher(com.radixdlt.utils.RandomHasher) ECPublicKey(com.radixdlt.crypto.ECPublicKey) Before(org.junit.Before)

Example 5 with ECPublicKey

use of com.radixdlt.crypto.ECPublicKey in project radixdlt by radixdlt.

the class KeyVerifyTestScenarioRunner method doRunTestVector.

@Override
public void doRunTestVector(KeyVerifyTestVector testVector) throws AssertionError {
    ECPublicKey publicKey = null;
    try {
        publicKey = ECPublicKey.fromBytes(fromHexString(testVector.input.publicKeyUncompressed));
    } catch (Exception e) {
        throw new AssertionError("Failed to construct public key from hex", e);
    }
    ECDSASignature signature = ECDSASignature.decodeFromDER(fromHexString(testVector.input.signatureDerEncoded));
    byte[] hashedMessageToVerify = sha256Hash(fromHexString(testVector.input.msg));
    assertEquals(testVector.expected.isValid, publicKey.verify(hashedMessageToVerify, signature));
}
Also used : ECPublicKey(com.radixdlt.crypto.ECPublicKey) ECDSASignature(com.radixdlt.crypto.ECDSASignature)

Aggregations

ECPublicKey (com.radixdlt.crypto.ECPublicKey)13 REFieldSerialization (com.radixdlt.atom.REFieldSerialization)5 SubstateTypeId (com.radixdlt.atom.SubstateTypeId)5 ConstraintScrypt (com.radixdlt.atomos.ConstraintScrypt)5 Loader (com.radixdlt.atomos.Loader)5 SubstateDefinition (com.radixdlt.atomos.SubstateDefinition)5 ProcedureException (com.radixdlt.constraintmachine.exceptions.ProcedureException)5 BFTNode (com.radixdlt.hotstuff.bft.BFTNode)5 EpochData (com.radixdlt.application.system.state.EpochData)4 Authorization (com.radixdlt.constraintmachine.Authorization)4 DownProcedure (com.radixdlt.constraintmachine.DownProcedure)4 PermissionLevel (com.radixdlt.constraintmachine.PermissionLevel)4 ReducerResult (com.radixdlt.constraintmachine.ReducerResult)4 ReducerState (com.radixdlt.constraintmachine.ReducerState)4 UpProcedure (com.radixdlt.constraintmachine.UpProcedure)4 VoidReducerState (com.radixdlt.constraintmachine.VoidReducerState)4 AbstractModule (com.google.inject.AbstractModule)3 Provides (com.google.inject.Provides)3 ReadProcedure (com.radixdlt.constraintmachine.ReadProcedure)3 AuthorizationException (com.radixdlt.constraintmachine.exceptions.AuthorizationException)3