forked from planetarium/libplanet
-
Notifications
You must be signed in to change notification settings - Fork 0
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 planetarium#3911 from s2quake/exp/sdk/swarm
Add Node Service(swarm) to SDK
- Loading branch information
Showing
69 changed files
with
1,830 additions
and
302 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
[*.proto] | ||
indent_size = 2 | ||
|
||
[*.cs] | ||
|
||
# S3903: Types should be defined in named namespaces | ||
|
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
44 changes: 44 additions & 0 deletions
44
sdk/node/Libplanet.Node.Executable/Protos/blockchain.proto
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,44 @@ | ||
syntax = "proto3"; | ||
|
||
option csharp_namespace = "Libplanet.Node.API"; | ||
|
||
package node; | ||
|
||
service BlockChain { | ||
rpc GetGenesisBlock (GetGenesisBlockRequest) returns (GetGenesisBlockReply); | ||
rpc GetTip(Empty) returns (GetTipReply); | ||
rpc GetBlock(GetBlockRequest) returns (GetBlockReply); | ||
} | ||
|
||
message Empty { | ||
} | ||
|
||
message GetGenesisBlockRequest { | ||
} | ||
|
||
message GetGenesisBlockReply { | ||
string hash = 1; | ||
} | ||
|
||
message GetTipReply { | ||
string hash = 1; | ||
int64 height = 2; | ||
} | ||
|
||
message GetBlockRequest { | ||
oneof block_identifier { | ||
int64 height = 1; | ||
string hash = 2; | ||
} | ||
} | ||
|
||
message GetBlockReply { | ||
string hash = 1; | ||
int64 height = 2; | ||
string miner = 3; | ||
string public_key = 4; | ||
string previous_hash = 5; | ||
string state_root_hash = 6; | ||
string signature = 7; | ||
int64 protocol_version = 8; | ||
} |
This file was deleted.
Oops, something went wrong.
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,19 @@ | ||
syntax = "proto3"; | ||
|
||
option csharp_namespace = "Libplanet.Node.API"; | ||
|
||
package node; | ||
|
||
// The greeting service definition. | ||
service Seed { | ||
// Sends a greeting | ||
rpc GetSeed(GetSeedRequest) returns (GetSeedReply); | ||
} | ||
|
||
message GetSeedRequest { | ||
} | ||
|
||
message GetSeedReply { | ||
string blocksync_seed = 1; | ||
string consensus_seed = 2; | ||
} |
64 changes: 64 additions & 0 deletions
64
sdk/node/Libplanet.Node.Executable/Services/BlockChainGrpcService.cs
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,64 @@ | ||
using Grpc.Core; | ||
using Libplanet.Common; | ||
using Libplanet.Node.DependencyInjection; | ||
using Libplanet.Node.Services; | ||
using Libplanet.Types.Blocks; | ||
|
||
namespace Libplanet.Node.API.Services; | ||
|
||
[Grpc] | ||
public class BlockChainGrpcService( | ||
IReadChainService blockChain) | ||
: BlockChain.BlockChainBase | ||
{ | ||
private readonly IReadChainService _blockChain = blockChain; | ||
|
||
public override Task<GetGenesisBlockReply> GetGenesisBlock( | ||
GetGenesisBlockRequest request, | ||
ServerCallContext context) | ||
{ | ||
return Task.FromResult(new GetGenesisBlockReply | ||
{ | ||
Hash = _blockChain.Tip.Hash.ToString(), | ||
}); | ||
} | ||
|
||
public override Task<GetTipReply> GetTip(Empty request, ServerCallContext context) | ||
{ | ||
return Task.FromResult(new GetTipReply | ||
{ | ||
Hash = _blockChain.Tip.Hash.ToString(), | ||
Height = _blockChain.Tip.Index, | ||
}); | ||
} | ||
|
||
public override Task<GetBlockReply> GetBlock(GetBlockRequest request, ServerCallContext context) | ||
{ | ||
return Task.Run(GetResult); | ||
|
||
Block GetBlock() => request.BlockIdentifierCase switch | ||
{ | ||
GetBlockRequest.BlockIdentifierOneofCase.Hash | ||
=> _blockChain.GetBlock(BlockHash.FromString(request.Hash)), | ||
GetBlockRequest.BlockIdentifierOneofCase.Height | ||
=> _blockChain.GetBlock(request.Height), | ||
_ => throw new InvalidOperationException("Invalid block identifier."), | ||
}; | ||
|
||
GetBlockReply GetResult() | ||
{ | ||
var block = GetBlock(); | ||
return new GetBlockReply | ||
{ | ||
Hash = block.Hash.ToString(), | ||
Height = block.Index, | ||
Miner = block.Miner.ToString(), | ||
PublicKey = $"{block.PublicKey}", | ||
PreviousHash = $"{block.PreviousHash}", | ||
StateRootHash = $"{block.StateRootHash}", | ||
Signature = $"{ByteUtil.Hex(block.Signature ?? [])}", | ||
ProtocolVersion = block.ProtocolVersion, | ||
}; | ||
} | ||
} | ||
} |
45 changes: 0 additions & 45 deletions
45
sdk/node/Libplanet.Node.Executable/Services/GreeterService.cs
This file was deleted.
Oops, something went wrong.
28 changes: 28 additions & 0 deletions
28
sdk/node/Libplanet.Node.Executable/Services/SeedGrpcService.cs
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,28 @@ | ||
using Grpc.Core; | ||
using Libplanet.Node.DependencyInjection; | ||
using Libplanet.Node.Services; | ||
|
||
namespace Libplanet.Node.API.Services; | ||
|
||
[Grpc(Scope = "Seed")] | ||
public class SeedGrpcService( | ||
IServiceProvider serviceProvider) | ||
: Seed.SeedBase | ||
{ | ||
public override Task<GetSeedReply> GetSeed(GetSeedRequest request, ServerCallContext context) | ||
{ | ||
var blocksyncSeedService = serviceProvider.GetService<IBlocksyncSeedService>(); | ||
var consensusSeedService = serviceProvider.GetService<IConsensusSeedService>(); | ||
if (blocksyncSeedService is null || consensusSeedService is null) | ||
{ | ||
throw new RpcException( | ||
new Status(StatusCode.Unavailable, "Seed services are not available.")); | ||
} | ||
|
||
return Task.FromResult(new GetSeedReply | ||
{ | ||
BlocksyncSeed = EndPointUtility.ToString(blocksyncSeedService.BoundPeer.EndPoint), | ||
ConsensusSeed = EndPointUtility.ToString(consensusSeedService.BoundPeer.EndPoint), | ||
}); | ||
} | ||
} |
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
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
Oops, something went wrong.