public void Proposer_slashing_there_and_back() { BeaconBlockHeader header1 = new BeaconBlockHeader(); header1.Slot = new Slot(1); header1.ParentRoot = Sha256.OfAnEmptyString; header1.BodyRoot = Sha256.OfAnEmptyString; header1.StateRoot = Sha256.OfAnEmptyString; header1.Signature = BlsSignature.TestSig1; BeaconBlockHeader header2 = new BeaconBlockHeader(); header2.Slot = new Slot(2); header2.ParentRoot = Sha256.OfAnEmptyString; header2.BodyRoot = Sha256.OfAnEmptyString; header2.StateRoot = Sha256.OfAnEmptyString; header2.Signature = BlsSignature.TestSig1; ProposerSlashing container = new ProposerSlashing(); container.ProposerIndex = new ValidatorIndex(1); container.Header1 = header1; container.Header2 = header2; Span <byte> encoded = new byte[ProposerSlashing.SszLength]; Ssz.Encode(encoded, container); ProposerSlashing?decoded = Ssz.DecodeProposerSlashing(encoded); Assert.AreEqual(container, decoded); Merkle.Ize(out UInt256 root, container); }
public void Proposer_slashing_there_and_back() { BeaconBlockHeader header1 = new BeaconBlockHeader( new Slot(1), Sha256.RootOfAnEmptyString, Sha256.RootOfAnEmptyString, Sha256.RootOfAnEmptyString); BeaconBlockHeader header2 = new BeaconBlockHeader( new Slot(2), Sha256.RootOfAnEmptyString, Sha256.RootOfAnEmptyString, Sha256.RootOfAnEmptyString); ProposerSlashing container = new ProposerSlashing( new ValidatorIndex(1), new SignedBeaconBlockHeader(header1, TestSig1), new SignedBeaconBlockHeader(header2, TestSig1)); Span <byte> encoded = new byte[Ssz.ProposerSlashingLength]; Ssz.Encode(encoded, container); ProposerSlashing?decoded = Ssz.DecodeProposerSlashing(encoded); Assert.AreEqual(container, decoded); Merkle.Ize(out UInt256 root, container); }
private static IEnumerable <SszElement> GetValues(ProposerSlashing item) { yield return(item.ProposerIndex.ToSszBasicElement()); yield return(item.Header1.ToSszContainer()); yield return(item.Header2.ToSszContainer()); }
public static void Ize(out UInt256 root, ProposerSlashing container) { Merkleizer merkleizer = new Merkleizer(2); merkleizer.Feed(container.ProposerIndex); merkleizer.Feed(container.SignedHeader1); merkleizer.Feed(container.SignedHeader2); merkleizer.CalculateRoot(out root); }
private static void TestProposerSlashingSsz(byte[] serialized, UInt256 expectedMerkleRoot, string testCaseDir) { ProposerSlashing container = Nethermind.Ssz.Ssz.DecodeProposerSlashing(serialized); byte[] again = new byte[serialized.Length]; Nethermind.Ssz.Ssz.Encode(again, container); Assert.AreEqual(serialized.ToHexString(), again.ToHexString(), testCaseDir); Merkle.Ize(out UInt256 root, container); Assert.AreEqual(expectedMerkleRoot, root); }
public static ProposerSlashing DecodeProposerSlashing(ReadOnlySpan <byte> span) { if (span.Length != Ssz.ProposerSlashingLength) { ThrowSourceLength <ProposerSlashing>(span.Length, Ssz.ProposerSlashingLength); } int offset = 0; ValidatorIndex proposerIndex = DecodeValidatorIndex(span, ref offset); SignedBeaconBlockHeader signedHeader1 = DecodeSignedBeaconBlockHeader(span, ref offset); SignedBeaconBlockHeader signedHeader2 = DecodeSignedBeaconBlockHeader(span, ref offset); ProposerSlashing container = new ProposerSlashing(proposerIndex, signedHeader1, signedHeader2); return(container); }
public static void Encode(Span <byte> span, ProposerSlashing container) { if (span.Length != Ssz.ProposerSlashingLength) { ThrowTargetLength <ProposerSlashing>(span.Length, Ssz.ProposerSlashingLength); } int offset = 0; Encode(span.Slice(0, Ssz.ValidatorIndexLength), container.ProposerIndex); offset += Ssz.ValidatorIndexLength; Encode(span.Slice(offset, Ssz.SignedBeaconBlockHeaderLength), container.SignedHeader1); offset += Ssz.SignedBeaconBlockHeaderLength; Encode(span.Slice(offset, Ssz.SignedBeaconBlockHeaderLength), container.SignedHeader2); }
public static void Ize(out UInt256 root, ProposerSlashing container) { if (container == null) { root = RootOfNull; return; } Merkleizer merkleizer = new Merkleizer(2); merkleizer.Feed(container.ProposerIndex); merkleizer.Feed(container.Header1); merkleizer.Feed(container.Header2); merkleizer.CalculateRoot(out root); }
public void ProcessProposerSlashing(BeaconState state, ProposerSlashing proposerSlashing) { _logger.LogInformation(Event.ProcessProposerSlashing, "Process block operation proposer slashing {ProposerSlashing}", proposerSlashing); var proposer = state.Validators[(int)(ulong)proposerSlashing.ProposerIndex]; // Verify slots match if (proposerSlashing.Header1.Slot != proposerSlashing.Header2.Slot) { throw new Exception($"Proposer slashing header 1 slot {proposerSlashing.Header1.Slot} must match header 2 slot {proposerSlashing.Header2.Slot}."); } // But the headers are different if (proposerSlashing.Header1.Equals(proposerSlashing.Header2)) { throw new Exception($"Proposer slashing must be for two different headers."); } // Check proposer is slashable var currentEpoch = _beaconStateAccessor.GetCurrentEpoch(state); var isSlashable = _beaconChainUtility.IsSlashableValidator(proposer, currentEpoch); if (!isSlashable) { throw new Exception($"Proposer {proposerSlashing.ProposerIndex} is not slashable at epoch {currentEpoch}."); } // Signatures are valid var slashingEpoch = _beaconChainUtility.ComputeEpochAtSlot(proposerSlashing.Header1.Slot); var domain = _beaconStateAccessor.GetDomain(state, _signatureDomainOptions.CurrentValue.BeaconProposer, slashingEpoch); var signingRoot1 = proposerSlashing.Header1.SigningRoot(); var signature1 = proposerSlashing.Header1.Signature; var header1Valid = _cryptographyService.BlsVerify(proposer.PublicKey, signingRoot1, signature1, domain); if (!header1Valid) { throw new Exception("Proposer slashing header 1 signature is not valid."); } var signingRoot2 = proposerSlashing.Header2.SigningRoot(); var signature2 = proposerSlashing.Header2.Signature; var header2Valid = _cryptographyService.BlsVerify(proposer.PublicKey, signingRoot2, signature2, domain); if (!header2Valid) { throw new Exception("Proposer slashing header 2 signature is not valid."); } _beaconStateMutator.SlashValidator(state, proposerSlashing.ProposerIndex, ValidatorIndex.None); }
public static ProposerSlashing?[] DecodeProposerSlashings(Span <byte> span) { if (span.Length % ProposerSlashing.SszLength != 0) { ThrowInvalidSourceArrayLength <ProposerSlashing>(span.Length, ProposerSlashing.SszLength); } int count = span.Length / ProposerSlashing.SszLength; ProposerSlashing?[] containers = new ProposerSlashing[count]; for (int i = 0; i < count; i++) { containers[i] = DecodeProposerSlashing(span.Slice(i * ProposerSlashing.SszLength, ProposerSlashing.SszLength)); } return(containers); }
public static ProposerSlashing?DecodeProposerSlashing(Span <byte> span) { if (span.Length != ProposerSlashing.SszLength) { ThrowSourceLength <ProposerSlashing>(span.Length, ProposerSlashing.SszLength); } if (span.SequenceEqual(_nullProposerSlashing)) { return(null); } int offset = 0; ProposerSlashing container = new ProposerSlashing(); container.ProposerIndex = DecodeValidatorIndex(span, ref offset); container.Header1 = DecodeBeaconBlockHeader(span, ref offset); container.Header2 = DecodeBeaconBlockHeader(span, ref offset); return(container); }
public static ProposerSlashing GetValidProposerSlashing(IServiceProvider testServiceProvider, BeaconState state, bool signed1, bool signed2) { var timeParameters = testServiceProvider.GetService <IOptions <TimeParameters> >().Value; var beaconStateAccessor = testServiceProvider.GetService <BeaconStateAccessor>(); var currentEpoch = beaconStateAccessor.GetCurrentEpoch(state); var validatorIndex = beaconStateAccessor.GetActiveValidatorIndices(state, currentEpoch).Last(); var validator = state.Validators[(int)(ulong)validatorIndex]; var privateKey = TestKeys.PublicKeyToPrivateKey(validator.PublicKey, timeParameters); var slot = state.Slot; var header1 = new BeaconBlockHeader( slot, new Root(Enumerable.Repeat((byte)0x33, 32).ToArray()), new Root(Enumerable.Repeat((byte)0x44, 32).ToArray()), new Root(Enumerable.Repeat((byte)0x45, 32).ToArray()) ); var header2 = new BeaconBlockHeader( slot, new Root(Enumerable.Repeat((byte)0x99, 32).ToArray()), new Root(Enumerable.Repeat((byte)0x44, 32).ToArray()), new Root(Enumerable.Repeat((byte)0x45, 32).ToArray()) ); SignedBeaconBlockHeader signedHeader1 = new SignedBeaconBlockHeader(header1, BlsSignature.Zero); if (signed1) { signedHeader1 = TestBlockHeader.SignBlockHeader(testServiceProvider, state, header1, privateKey); } SignedBeaconBlockHeader signedHeader2 = new SignedBeaconBlockHeader(header2, BlsSignature.Zero); if (signed2) { signedHeader2 = TestBlockHeader.SignBlockHeader(testServiceProvider, state, header2, privateKey); } var proposerSlashing = new ProposerSlashing(validatorIndex, signedHeader1, signedHeader2); return(proposerSlashing); }
public static void Encode(Span <byte> span, ProposerSlashing container) { if (span.Length != ProposerSlashing.SszLength) { ThrowTargetLength <ProposerSlashing>(span.Length, ProposerSlashing.SszLength); } if (container == null) { return; } int offset = 0; Encode(span.Slice(0, ValidatorIndex.SszLength), container.ProposerIndex); offset += ValidatorIndex.SszLength; Encode(span.Slice(offset, BeaconBlockHeader.SszLength), container.Header1); offset += BeaconBlockHeader.SszLength; Encode(span.Slice(offset, BeaconBlockHeader.SszLength), container.Header2); }
public static SszContainer ToSszContainer(this ProposerSlashing item) { return(new SszContainer(GetValues(item))); }
public SszBeaconBlockBodyBenchmark() { AttestationData data = new AttestationData( new Slot(1), new CommitteeIndex(4), Sha256.RootOfAnEmptyString, new Checkpoint(new Epoch(2), Sha256.RootOfAnEmptyString), new Checkpoint(new Epoch(3), Sha256.RootOfAnEmptyString)); Attestation attestation = new Attestation( new BitArray(new byte[5]), data, TestSig1 ); DepositData depositData = new DepositData( TestKey1, Sha256.Bytes32OfAnEmptyString, new Gwei(7), TestSig1); Deposit deposit = new Deposit( new Bytes32[Ssz.DepositContractTreeDepth + 1], depositData); IndexedAttestation indexedAttestation1 = new IndexedAttestation( new ValidatorIndex[8], data, TestSig1); IndexedAttestation indexedAttestation2 = new IndexedAttestation( new ValidatorIndex[8], data, TestSig1); AttesterSlashing slashing = new AttesterSlashing(indexedAttestation1, indexedAttestation2); Eth1Data eth1Data = new Eth1Data( Sha256.RootOfAnEmptyString, 9, Sha256.Bytes32OfAnEmptyString); Attestation[] attestations = new Attestation[3]; attestations[1] = attestation; Deposit[] deposits = new Deposit[3]; deposits[2] = deposit; Bytes32 graffiti = new Bytes32(new byte[32]); AttesterSlashing[] attesterSlashings = new AttesterSlashing[3]; attesterSlashings[0] = slashing; ProposerSlashing[] proposerSlashings = new ProposerSlashing[10]; BlsSignature randaoReveal = TestSig1; SignedVoluntaryExit[] signedVoluntaryExits = new SignedVoluntaryExit[11]; _body = new BeaconBlockBody(randaoReveal, eth1Data, graffiti, proposerSlashings, attesterSlashings, attestations, deposits, signedVoluntaryExits); _encoded = new byte[Ssz.BeaconBlockBodyLength(_body)]; }
//Run ``process_proposer_slashing``, yielding: //- pre-state('pre') //- proposer_slashing('proposer_slashing') //- post-state('post'). //If ``valid == False``, run expecting ``AssertionError`` private void RunProposerSlashingProcessing(IServiceProvider testServiceProvider, BeaconState state, ProposerSlashing proposerSlashing, bool expectValid) { var chainConstants = testServiceProvider.GetService <ChainConstants>(); var beaconStateTransition = testServiceProvider.GetService <BeaconStateTransition>(); if (!expectValid) { Should.Throw <Exception>(() => { beaconStateTransition.ProcessProposerSlashing(state, proposerSlashing); }); return; } var preProposerBalance = TestState.GetBalance(state, proposerSlashing.ProposerIndex); beaconStateTransition.ProcessProposerSlashing(state, proposerSlashing); var slashedValidator = state.Validators[(int)(ulong)proposerSlashing.ProposerIndex]; slashedValidator.IsSlashed.ShouldBeTrue(); slashedValidator.ExitEpoch.ShouldBeLessThan(chainConstants.FarFutureEpoch); slashedValidator.WithdrawableEpoch.ShouldBeLessThan(chainConstants.FarFutureEpoch); var balance = TestState.GetBalance(state, proposerSlashing.ProposerIndex); balance.ShouldBeLessThan(preProposerBalance); }
public void Beacon_block_body_more_detailed() { AttestationData data = new AttestationData( new Slot(1), new CommitteeIndex(4), Sha256.OfAnEmptyString, new Checkpoint(new Epoch(2), Sha256.OfAnEmptyString), new Checkpoint(new Epoch(3), Sha256.OfAnEmptyString)); Attestation attestation = new Attestation( new BitArray(new byte[5]), data, SszTest.TestSig1); DepositData depositData = new DepositData( SszTest.TestKey1, Sha256.OfAnEmptyString, new Gwei(7), SszTest.TestSig1); Deposit deposit = new Deposit(new Hash32[Ssz.DepositContractTreeDepth + 1], depositData); IndexedAttestation indexedAttestation1 = new IndexedAttestation( new ValidatorIndex[8], data, SszTest.TestSig1); IndexedAttestation indexedAttestation2 = new IndexedAttestation( new ValidatorIndex[8], data, SszTest.TestSig1); AttesterSlashing slashing = new AttesterSlashing(indexedAttestation1, indexedAttestation2); Eth1Data eth1Data = new Eth1Data( Sha256.OfAnEmptyString, 9, Sha256.OfAnEmptyString); Attestation[] attestations = new Attestation[3]; attestations[1] = attestation; Deposit[] deposits = new Deposit[3]; deposits[2] = deposit; Bytes32 graffiti = new Bytes32(new byte[32]); AttesterSlashing[] attesterSlashings = new AttesterSlashing[3]; attesterSlashings[0] = slashing; ProposerSlashing[] proposerSlashings = new ProposerSlashing[10]; VoluntaryExit[] voluntaryExits = new VoluntaryExit[11]; BeaconBlockBody body = new BeaconBlockBody( SszTest.TestSig1, eth1Data, graffiti, proposerSlashings, attesterSlashings, attestations, deposits, voluntaryExits ); byte[] encoded = new byte[Ssz.BeaconBlockBodyLength(body)]; Ssz.Encode(encoded, body); }