-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #62 from yggdrash/feature/validator-pbft-implemant…
…ation Feature/validator pbft implemantation
- Loading branch information
Showing
28 changed files
with
2,393 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
syntax = "proto3"; | ||
|
||
import "Yggdrash.proto"; | ||
import "BlockChain.proto"; | ||
import "EBFT.proto"; | ||
|
||
option java_package = "io.yggdrash.proto"; | ||
option java_outer_classname = "PbftProto"; | ||
|
||
message PbftMessage { | ||
string type = 1; | ||
int64 viewNumber = 2; | ||
int64 seqNumber = 3; | ||
bytes hash = 4; | ||
bytes clientId = 5; | ||
bytes result = 6; | ||
bytes signature = 7; | ||
Block block = 8; | ||
} | ||
|
||
message PbftMessageList { | ||
repeated PbftMessage pbftMessageList = 1; | ||
} | ||
|
||
message PbftMessageSet { | ||
PbftMessage prePrepare = 1; | ||
PbftMessageList prepareList = 2; | ||
PbftMessageList commitList = 3; | ||
} | ||
|
||
message PbftBlock { | ||
Block block = 1; | ||
PbftMessageSet pbftMessageSet = 2; | ||
} | ||
|
||
message PbftBlockList { | ||
repeated PbftBlock pbftBlock = 1; | ||
} | ||
|
||
message PbftStatus { | ||
int64 index = 1; | ||
PbftMessageList pbftMessageList = 2; | ||
int64 timestamp = 3; | ||
bytes signature = 4; | ||
} | ||
|
||
service PbftService { | ||
rpc PingPongTime (PingTime) returns (PongTime) { | ||
} | ||
|
||
rpc exchangePbftStatus (PbftStatus) returns (PbftStatus) { | ||
} | ||
|
||
rpc getPbftBlockList (Offset) returns (PbftBlockList) { | ||
} | ||
|
||
rpc multicastPbftMessage (PbftMessage) returns (Empty) { | ||
} | ||
|
||
rpc multicastPbftBlock (PbftBlock) returns (Empty) { | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
150 changes: 150 additions & 0 deletions
150
yggdrash-validator/src/main/java/io/yggdrash/validator/data/PbftBlock.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
/* | ||
* Copyright 2019 Akashic Foundation | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.yggdrash.validator.data; | ||
|
||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonObject; | ||
import io.yggdrash.common.util.JsonUtil; | ||
import io.yggdrash.core.blockchain.Block; | ||
import io.yggdrash.proto.PbftProto; | ||
import io.yggdrash.proto.Proto; | ||
import io.yggdrash.validator.data.pbft.PbftMessageSet; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.spongycastle.util.encoders.Hex; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.util.List; | ||
|
||
public class PbftBlock { | ||
private static final Logger log = LoggerFactory.getLogger(PbftBlock.class); | ||
|
||
private final Block block; | ||
private final PbftMessageSet pbftMessageSet; | ||
|
||
public PbftBlock(Block block, PbftMessageSet pbftMessageSet) { | ||
this.block = block; | ||
this.pbftMessageSet = pbftMessageSet; | ||
} | ||
|
||
public PbftBlock(byte[] bytes) { | ||
this(JsonUtil.parseJsonObject(new String(bytes, StandardCharsets.UTF_8))); | ||
} | ||
|
||
public PbftBlock(JsonObject jsonObject) { | ||
this.block = new Block(jsonObject.get("block").getAsJsonObject()); | ||
|
||
JsonElement pbftMessageSetJsonElement = jsonObject.get("pbftMessageSet"); | ||
if (pbftMessageSetJsonElement != null) { | ||
this.pbftMessageSet = new PbftMessageSet(pbftMessageSetJsonElement.getAsJsonObject()); | ||
} else { | ||
this.pbftMessageSet = null; | ||
} | ||
} | ||
|
||
public PbftBlock(PbftProto.PbftBlock protoBlock) { | ||
this.block = Block.toBlock(protoBlock.getBlock()); | ||
this.pbftMessageSet = new PbftMessageSet(protoBlock.getPbftMessageSet()); | ||
} | ||
|
||
public byte[] toBinary() { | ||
return this.toJsonObject().toString().getBytes(StandardCharsets.UTF_8); | ||
} | ||
|
||
public long getIndex() { | ||
return this.block.getIndex(); | ||
} | ||
|
||
public byte[] getHash() { | ||
return this.block.getHash(); | ||
} | ||
|
||
public String getHashHex() { | ||
return Hex.toHexString(getHash()); | ||
} | ||
|
||
public byte[] getPrevBlockHash() { | ||
return this.block.getHeader().getPrevBlockHash(); | ||
} | ||
|
||
public Block getBlock() { | ||
return block; | ||
} | ||
|
||
public PbftMessageSet getPbftMessageSet() { | ||
return pbftMessageSet; | ||
} | ||
|
||
public static boolean verify(PbftBlock block) { | ||
if (block == null || block.getBlock() == null || block.getPbftMessageSet() == null) { | ||
return false; | ||
} | ||
|
||
if (block.getIndex() == 0) { // genesis block | ||
if (block.getBlock().verify()) { | ||
return true; | ||
} | ||
} else { | ||
if (block.getBlock().verify() | ||
&& block.getPbftMessageSet().verify(block.getPbftMessageSet())) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public JsonObject toJsonObject() { | ||
JsonObject jsonObject = new JsonObject(); | ||
jsonObject.add("block", this.block.toJsonObject()); | ||
if (this.pbftMessageSet != null) { | ||
jsonObject.add("pbftMessageSet", this.pbftMessageSet.toJsonObject()); | ||
} | ||
return jsonObject; | ||
} | ||
|
||
public static PbftProto.PbftBlock toProto(PbftBlock pbftBlock) { | ||
Proto.Block protoBlock = pbftBlock.getBlock().toProtoBlock(); | ||
PbftProto.PbftMessageSet protoPbftMessageSet = | ||
PbftMessageSet.toProto(pbftBlock.getPbftMessageSet()); | ||
|
||
PbftProto.PbftBlock.Builder protoPbftBlockBuilder = PbftProto.PbftBlock.newBuilder(); | ||
if (protoBlock != null) { | ||
protoPbftBlockBuilder.setBlock(protoBlock); | ||
} | ||
if (protoPbftMessageSet != null) { | ||
protoPbftBlockBuilder.setPbftMessageSet(protoPbftMessageSet); | ||
} | ||
|
||
return protoPbftBlockBuilder.build(); | ||
} | ||
|
||
public static PbftProto.PbftBlockList toProtoList(List<PbftBlock> pbftBlockList) { | ||
if (pbftBlockList == null) { | ||
return null; | ||
} | ||
|
||
PbftProto.PbftBlockList.Builder protoPbftBlockListBuilder = | ||
PbftProto.PbftBlockList.newBuilder(); | ||
for (PbftBlock pbftBlock : pbftBlockList) { | ||
protoPbftBlockListBuilder.addPbftBlock(PbftBlock.toProto(pbftBlock)); | ||
} | ||
|
||
return protoPbftBlockListBuilder.build(); | ||
} | ||
|
||
} |
Oops, something went wrong.