Skip to content

Commit

Permalink
Merge d4f2f02 into b423bdf
Browse files Browse the repository at this point in the history
  • Loading branch information
zilm13 committed Oct 15, 2018
2 parents b423bdf + d4f2f02 commit 559e160
Show file tree
Hide file tree
Showing 26 changed files with 1,087 additions and 42 deletions.
Expand Up @@ -35,6 +35,8 @@
import org.ethereum.db.TransactionStore;
import org.ethereum.facade.Ethereum;
import org.ethereum.manager.WorldManager;
import org.ethereum.sharding.crypto.DummySign;
import org.ethereum.sharding.crypto.Sign;
import org.ethereum.sharding.pubsub.Publisher;
import org.ethereum.sharding.manager.ShardingWorldManager;
import org.ethereum.sharding.processing.BeaconChain;
Expand All @@ -43,6 +45,8 @@
import org.ethereum.sharding.processing.db.IndexedBeaconStore;
import org.ethereum.sharding.processing.state.BeaconStateRepository;
import org.ethereum.sharding.processing.state.StateRepository;
import org.ethereum.sharding.validator.BeaconAttester;
import org.ethereum.sharding.validator.BeaconAttesterImpl;
import org.ethereum.sharding.validator.BeaconProposer;
import org.ethereum.sharding.validator.BeaconProposerImpl;
import org.ethereum.sharding.validator.ValidatorService;
Expand Down Expand Up @@ -127,8 +131,8 @@ public ValidatorRegistrationService validatorRegistrationService() {
@Bean
public ValidatorService validatorService() {
if (validatorConfig().isEnabled()) {
ValidatorService validatorService = new ValidatorServiceImpl(beaconProposer(), beaconChain(),
publisher(), validatorConfig(), ethereum, blockStore);
ValidatorService validatorService = new ValidatorServiceImpl(beaconProposer(), beaconAttester(),
beaconChain(), publisher(), validatorConfig(), ethereum, blockStore);
shardingWorldManager.setProposerService(validatorService);
return validatorService;
} else {
Expand Down Expand Up @@ -160,7 +164,8 @@ public StateRepository beaconStateRepository() {
Source<byte[], byte[]> validatorSrc = cachedBeaconChainSource("validator_set");
Source<byte[], byte[]> validatorIndexSrc = cachedBeaconChainSource("validator_index");
Source<byte[], byte[]> crystallizedSrc = cachedBeaconChainSource("crystallized_state");
return new BeaconStateRepository(src, crystallizedSrc, validatorSrc, validatorIndexSrc);
Source<byte[], byte[]> activeSrc = cachedBeaconChainSource("active_state");
return new BeaconStateRepository(src, crystallizedSrc, activeSrc, validatorSrc, validatorIndexSrc);
}

@Bean
Expand Down Expand Up @@ -216,10 +221,15 @@ public Publisher publisher() {

@Bean
public BeaconProposer beaconProposer() {
return new BeaconProposerImpl(randao(), beaconStateRepository(),
return new BeaconProposerImpl(randao(), beaconStateRepository(), beaconStore(),
BeaconChainFactory.stateTransition(validatorRepository()), validatorConfig());
}

@Bean
public BeaconAttester beaconAttester() {
return new BeaconAttesterImpl(beaconStateRepository(), beaconStore(), validatorConfig(), sign());
}

@Bean
public Randao randao() {
DbSource<byte[]> src = commonConfig.keyValueDataSource("randao");
Expand All @@ -244,6 +254,10 @@ public DepositAuthority depositAuthority() {
return new UnsecuredDepositAuthority(validatorConfig());
}

public Sign sign() {
return new DummySign();
}

public static class Enabled implements Condition {
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
Expand Down
@@ -0,0 +1,66 @@
/*
* Copyright (c) [2016] [ <ether.camp> ]
* This file is part of the ethereumJ library.
*
* The ethereumJ library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The ethereumJ library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with the ethereumJ library. If not, see <http://www.gnu.org/licenses/>.
*/
package org.ethereum.sharding.crypto;

import java.math.BigInteger;

import static org.ethereum.crypto.HashUtil.sha3;

/**
* Dummy signature implementation without real crypto underneath
*/
public class DummySign implements Sign {

/**
* Sign the message
*/
public Signature sign(byte[] msg, byte[] privateKey) {
byte[] rSource = sha3(privateKey);
byte[] sSource = sha3(msg, privateKey);
Signature res = new Signature();
res.r = new BigInteger(rSource);
res.s = new BigInteger(sSource);

return res;
}

/**
* Verifies whether signature is made by signer with publicKey
*/
public boolean verify(Signature signature, byte[] publicKey) {
return true;
}

/**
* Aggregates several signatures in one
*/
public Signature aggSigns(Signature[] signatures) {
int signatureLen = signatures.length;
Signature aggSignature = new Signature();
aggSignature.r = BigInteger.ZERO;
aggSignature.s = BigInteger.ZERO;
for (int i = 0; i < signatureLen; ++i) {
for (Signature signature : signatures) {
aggSignature.r = aggSignature.r.xor(signature.r);
aggSignature.s = aggSignature.s.xor(signature.s);
}
}

return aggSignature;
}
}
@@ -0,0 +1,48 @@
package org.ethereum.sharding.crypto;

import java.math.BigInteger;
import java.util.Objects;

/**
* Signature utilities
* Signature should be implemented using BLS
*/
public interface Sign {

/**
* Sign the message
*/
Signature sign(byte[] msg, byte[] privateKey);

/**
* Verifies whether signature is made by signer with publicKey
*/
boolean verify(Signature signature, byte[] publicKey);

/**
* Aggregates several signatures in one
*/
Signature aggSigns(Signature[] signatures);

class Signature {
public BigInteger r;
public BigInteger s;

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Signature signature = (Signature) o;
return Objects.equals(r, signature.r) &&
Objects.equals(s, signature.s);
}

@Override
public String toString() {
return "Signature{" +
"r=" + r +
", s=" + s +
'}';
}
}
}
Expand Up @@ -18,15 +18,21 @@
package org.ethereum.sharding.domain;

import org.ethereum.datasource.Serializer;
import org.ethereum.sharding.processing.state.AttestationRecord;
import org.ethereum.util.ByteUtil;
import org.ethereum.util.FastByteComparisons;
import org.ethereum.util.RLP;
import org.ethereum.util.RLPElement;
import org.ethereum.util.RLPList;
import org.spongycastle.util.encoders.Hex;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

import static org.ethereum.crypto.HashUtil.blake2b;
import static org.ethereum.util.ByteUtil.ZERO_BYTE_ARRAY;
import static org.ethereum.util.ByteUtil.isSingleZero;

/**
* Beacon chain block structure.
Expand All @@ -46,13 +52,17 @@ public class Beacon {
private byte[] stateHash;
/* Slot number */
private long slotNumber;
/* Attestations */
private List<AttestationRecord> attestations;

public Beacon(byte[] parentHash, byte[] randaoReveal, byte[] mainChainRef, byte[] stateHash, long slotNumber) {
public Beacon(byte[] parentHash, byte[] randaoReveal, byte[] mainChainRef, byte[] stateHash,
long slotNumber,List<AttestationRecord> attestations) {
this.parentHash = parentHash;
this.randaoReveal = randaoReveal;
this.mainChainRef = mainChainRef;
this.stateHash = stateHash;
this.slotNumber = slotNumber;
this.attestations = attestations;
}

public Beacon(byte[] rlp) {
Expand All @@ -62,11 +72,24 @@ public Beacon(byte[] rlp) {
this.mainChainRef = items.get(2).getRLPData();
this.stateHash = items.get(3).getRLPData();
this.slotNumber = ByteUtil.bytesToBigInteger(items.get(4).getRLPData()).longValue();

this.attestations = new ArrayList<>();
if (!isSingleZero(items.get(5).getRLPData())) {
RLPList attestationsRlp = RLP.unwrapList(items.get(5).getRLPData());
for (RLPElement anAttestationsRlp : attestationsRlp) {
attestations.add(new AttestationRecord(anAttestationsRlp.getRLPData()));
}
}
}

public byte[] getEncoded() {
byte[][] encodedAttestations = new byte[attestations.size()][];
for (int i = 0; i < attestations.size(); i++)
encodedAttestations[i] = attestations.get(i).getEncoded();

return RLP.wrapList(parentHash, randaoReveal, mainChainRef, stateHash,
BigInteger.valueOf(slotNumber).toByteArray());
BigInteger.valueOf(slotNumber).toByteArray(),
encodedAttestations.length == 0 ? ZERO_BYTE_ARRAY : RLP.wrapList(encodedAttestations));
}

public byte[] getHash() {
Expand All @@ -93,6 +116,10 @@ public long getSlotNumber() {
return slotNumber;
}

public List<AttestationRecord> getAttestations() {
return attestations;
}

public boolean isParentOf(Beacon other) {
return FastByteComparisons.equal(this.getHash(), other.getParentHash());
}
Expand All @@ -101,10 +128,14 @@ public void setStateHash(byte[] stateHash) {
this.stateHash = stateHash;
}

public void setAttestations(List<AttestationRecord> attestations) {
this.attestations = attestations;
}

@Override
public boolean equals(Object other) {
if (this == other) return true;
if (other == null || !(other instanceof Beacon)) return false;
if (!(other instanceof Beacon)) return false;

return FastByteComparisons.equal(this.getHash(), ((Beacon) other).getHash());
}
Expand Down
Expand Up @@ -53,7 +53,7 @@ public class BeaconGenesis extends Beacon {
private List<String> initialValidators = new ArrayList<>();

BeaconGenesis(Json json) {
super(json.parentHash(), json.randaoReveal(), json.mainChainRef(), EMPTY, SLOT);
super(json.parentHash(), json.randaoReveal(), json.mainChainRef(), EMPTY, SLOT, new ArrayList<>());
this.timestamp = json.timestamp();
this.initialValidators = json.validatorSet();
}
Expand Down
Expand Up @@ -69,6 +69,6 @@ public BeaconState applyBlock(Beacon block, BeaconState to) {
.withFinality(finality);
}

return new BeaconState(crystallized);
return new BeaconState(crystallized, to.getActiveState());
}
}
Expand Up @@ -80,7 +80,7 @@ public BeaconState applyBlock(Beacon block, BeaconState to) {
.withLastStateRecalc(0L)
.withCrosslinks(Crosslink.empty(SHARD_COUNT));

return new BeaconState(crystallizedState);
return new BeaconState(crystallizedState, to.getActiveState());
}

class ValidatorSetInitiator implements StateTransition<ValidatorSet> {
Expand Down

0 comments on commit 559e160

Please sign in to comment.