Skip to content

Commit

Permalink
fix(grpc): encode data and signature propoerly (#1538)
Browse files Browse the repository at this point in the history
  • Loading branch information
b00f authored Oct 10, 2024
1 parent 080686b commit 2bcb8d6
Show file tree
Hide file tree
Showing 17 changed files with 500 additions and 494 deletions.
40 changes: 19 additions & 21 deletions www/grpc/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ func (s *blockchainServer) GetConsensusInfo(_ context.Context,
_ *pactus.GetConsensusInfoRequest,
) (*pactus.GetConsensusInfoResponse, error) {
instances := make([]*pactus.ConsensusInfo, 0)
var proposal *pactus.Proposal

for _, cons := range s.consMgr.Instances() {
height, round := cons.HeightRound()
Expand All @@ -69,31 +68,30 @@ func (s *blockchainServer) GetConsensusInfo(_ context.Context,
Round: int32(round),
Votes: voteInfos,
})
}

if proposal == nil && cons.Proposal() != nil {
// All instances have the same proposal, so we just need to fill proposal once.
p := cons.Proposal()

var blockData string
if p.Block() != nil {
data, err := p.Block().Bytes()
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}

blockData = string(data)
}
var proposalInfo *pactus.ProposalInfo
prop := s.consMgr.Proposal()
if prop != nil {
var blockData string
data, err := prop.Block().Bytes()
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
blockData = hex.EncodeToString(data)

proposal = &pactus.Proposal{
Height: p.Height(),
Round: int32(p.Round()),
BlockData: blockData,
SignatureData: string(p.Signature().Bytes()),
}
proposalInfo = &pactus.ProposalInfo{
Height: prop.Height(),
Round: int32(prop.Round()),
BlockData: blockData,
Signature: prop.Signature().String(),
}
}

return &pactus.GetConsensusInfoResponse{Instances: instances, Proposal: proposal}, nil
return &pactus.GetConsensusInfoResponse{
Instances: instances,
Proposal: proposalInfo,
}, nil
}

func (s *blockchainServer) GetBlockHash(_ context.Context,
Expand Down
6 changes: 6 additions & 0 deletions www/grpc/blockchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,12 +339,14 @@ func TestConsensusInfo(t *testing.T) {
consRound := td.RandRound()
vote1, _ := td.GenerateTestPrepareVote(consHeight, consRound)
vote2, _ := td.GenerateTestPrecommitVote(consHeight, consRound)
prop, _ := td.GenerateTestProposal(consHeight, consRound)

td.consMocks[0].Active = true
td.consMocks[0].Height = consHeight
td.consMocks[0].Round = consRound
td.consMocks[0].AddVote(vote1)
td.consMocks[0].AddVote(vote2)
td.consMocks[0].SetProposal(prop)

td.consMocks[1].Active = false
td.consMocks[1].Height = consHeight
Expand All @@ -366,6 +368,10 @@ func TestConsensusInfo(t *testing.T) {
assert.False(t, res.Instances[1].Active)
assert.Equal(t, consHeight, res.Instances[1].Height)
assert.Equal(t, int32(consRound), res.Instances[1].Round)

assert.Equal(t, consHeight, res.Proposal.Height)
assert.Equal(t, int32(consRound), res.Proposal.Round)
assert.Equal(t, prop.Signature().String(), res.Proposal.Signature)
})

assert.Nil(t, conn.Close(), "Error closing connection")
Expand Down
52 changes: 26 additions & 26 deletions www/grpc/gen/dart/blockchain.pb.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1026,14 +1026,14 @@ class GetConsensusInfoRequest extends $pb.GeneratedMessage {

class GetConsensusInfoResponse extends $pb.GeneratedMessage {
static final $pb.BuilderInfo _i = $pb.BuilderInfo(const $core.bool.fromEnvironment('protobuf.omit_message_names') ? '' : 'GetConsensusInfoResponse', package: const $pb.PackageName(const $core.bool.fromEnvironment('protobuf.omit_message_names') ? '' : 'pactus'), createEmptyInstance: create)
..aOM<Proposal>(1, const $core.bool.fromEnvironment('protobuf.omit_field_names') ? '' : 'proposal', subBuilder: Proposal.create)
..aOM<ProposalInfo>(1, const $core.bool.fromEnvironment('protobuf.omit_field_names') ? '' : 'proposal', subBuilder: ProposalInfo.create)
..pc<ConsensusInfo>(2, const $core.bool.fromEnvironment('protobuf.omit_field_names') ? '' : 'instances', $pb.PbFieldType.PM, subBuilder: ConsensusInfo.create)
..hasRequiredFields = false
;

GetConsensusInfoResponse._() : super();
factory GetConsensusInfoResponse({
Proposal? proposal,
ProposalInfo? proposal,
$core.Iterable<ConsensusInfo>? instances,
}) {
final _result = create();
Expand Down Expand Up @@ -1067,15 +1067,15 @@ class GetConsensusInfoResponse extends $pb.GeneratedMessage {
static GetConsensusInfoResponse? _defaultInstance;

@$pb.TagNumber(1)
Proposal get proposal => $_getN(0);
ProposalInfo get proposal => $_getN(0);
@$pb.TagNumber(1)
set proposal(Proposal v) { setField(1, v); }
set proposal(ProposalInfo v) { setField(1, v); }
@$pb.TagNumber(1)
$core.bool hasProposal() => $_has(0);
@$pb.TagNumber(1)
void clearProposal() => clearField(1);
@$pb.TagNumber(1)
Proposal ensureProposal() => $_ensure(0);
ProposalInfo ensureProposal() => $_ensure(0);

@$pb.TagNumber(2)
$core.List<ConsensusInfo> get instances => $_getList(1);
Expand Down Expand Up @@ -1853,21 +1853,21 @@ class ConsensusInfo extends $pb.GeneratedMessage {
$core.List<VoteInfo> get votes => $_getList(4);
}

class Proposal extends $pb.GeneratedMessage {
static final $pb.BuilderInfo _i = $pb.BuilderInfo(const $core.bool.fromEnvironment('protobuf.omit_message_names') ? '' : 'Proposal', package: const $pb.PackageName(const $core.bool.fromEnvironment('protobuf.omit_message_names') ? '' : 'pactus'), createEmptyInstance: create)
class ProposalInfo extends $pb.GeneratedMessage {
static final $pb.BuilderInfo _i = $pb.BuilderInfo(const $core.bool.fromEnvironment('protobuf.omit_message_names') ? '' : 'ProposalInfo', package: const $pb.PackageName(const $core.bool.fromEnvironment('protobuf.omit_message_names') ? '' : 'pactus'), createEmptyInstance: create)
..a<$core.int>(1, const $core.bool.fromEnvironment('protobuf.omit_field_names') ? '' : 'height', $pb.PbFieldType.OU3)
..a<$core.int>(2, const $core.bool.fromEnvironment('protobuf.omit_field_names') ? '' : 'round', $pb.PbFieldType.O3)
..aOS(3, const $core.bool.fromEnvironment('protobuf.omit_field_names') ? '' : 'blockData')
..aOS(4, const $core.bool.fromEnvironment('protobuf.omit_field_names') ? '' : 'signatureData')
..aOS(4, const $core.bool.fromEnvironment('protobuf.omit_field_names') ? '' : 'signature')
..hasRequiredFields = false
;

Proposal._() : super();
factory Proposal({
ProposalInfo._() : super();
factory ProposalInfo({
$core.int? height,
$core.int? round,
$core.String? blockData,
$core.String? signatureData,
$core.String? signature,
}) {
final _result = create();
if (height != null) {
Expand All @@ -1879,31 +1879,31 @@ class Proposal extends $pb.GeneratedMessage {
if (blockData != null) {
_result.blockData = blockData;
}
if (signatureData != null) {
_result.signatureData = signatureData;
if (signature != null) {
_result.signature = signature;
}
return _result;
}
factory Proposal.fromBuffer($core.List<$core.int> i, [$pb.ExtensionRegistry r = $pb.ExtensionRegistry.EMPTY]) => create()..mergeFromBuffer(i, r);
factory Proposal.fromJson($core.String i, [$pb.ExtensionRegistry r = $pb.ExtensionRegistry.EMPTY]) => create()..mergeFromJson(i, r);
factory ProposalInfo.fromBuffer($core.List<$core.int> i, [$pb.ExtensionRegistry r = $pb.ExtensionRegistry.EMPTY]) => create()..mergeFromBuffer(i, r);
factory ProposalInfo.fromJson($core.String i, [$pb.ExtensionRegistry r = $pb.ExtensionRegistry.EMPTY]) => create()..mergeFromJson(i, r);
@$core.Deprecated(
'Using this can add significant overhead to your binary. '
'Use [GeneratedMessageGenericExtensions.deepCopy] instead. '
'Will be removed in next major version')
Proposal clone() => Proposal()..mergeFromMessage(this);
ProposalInfo clone() => ProposalInfo()..mergeFromMessage(this);
@$core.Deprecated(
'Using this can add significant overhead to your binary. '
'Use [GeneratedMessageGenericExtensions.rebuild] instead. '
'Will be removed in next major version')
Proposal copyWith(void Function(Proposal) updates) => super.copyWith((message) => updates(message as Proposal)) as Proposal; // ignore: deprecated_member_use
ProposalInfo copyWith(void Function(ProposalInfo) updates) => super.copyWith((message) => updates(message as ProposalInfo)) as ProposalInfo; // ignore: deprecated_member_use
$pb.BuilderInfo get info_ => _i;
@$core.pragma('dart2js:noInline')
static Proposal create() => Proposal._();
Proposal createEmptyInstance() => create();
static $pb.PbList<Proposal> createRepeated() => $pb.PbList<Proposal>();
static ProposalInfo create() => ProposalInfo._();
ProposalInfo createEmptyInstance() => create();
static $pb.PbList<ProposalInfo> createRepeated() => $pb.PbList<ProposalInfo>();
@$core.pragma('dart2js:noInline')
static Proposal getDefault() => _defaultInstance ??= $pb.GeneratedMessage.$_defaultFor<Proposal>(create);
static Proposal? _defaultInstance;
static ProposalInfo getDefault() => _defaultInstance ??= $pb.GeneratedMessage.$_defaultFor<ProposalInfo>(create);
static ProposalInfo? _defaultInstance;

@$pb.TagNumber(1)
$core.int get height => $_getIZ(0);
Expand Down Expand Up @@ -1933,13 +1933,13 @@ class Proposal extends $pb.GeneratedMessage {
void clearBlockData() => clearField(3);

@$pb.TagNumber(4)
$core.String get signatureData => $_getSZ(3);
$core.String get signature => $_getSZ(3);
@$pb.TagNumber(4)
set signatureData($core.String v) { $_setString(3, v); }
set signature($core.String v) { $_setString(3, v); }
@$pb.TagNumber(4)
$core.bool hasSignatureData() => $_has(3);
$core.bool hasSignature() => $_has(3);
@$pb.TagNumber(4)
void clearSignatureData() => clearField(4);
void clearSignature() => clearField(4);
}

class BlockchainApi {
Expand Down
18 changes: 9 additions & 9 deletions www/grpc/gen/dart/blockchain.pbjson.dart
Original file line number Diff line number Diff line change
Expand Up @@ -226,13 +226,13 @@ final $typed_data.Uint8List getConsensusInfoRequestDescriptor = $convert.base64D
const GetConsensusInfoResponse$json = const {
'1': 'GetConsensusInfoResponse',
'2': const [
const {'1': 'proposal', '3': 1, '4': 1, '5': 11, '6': '.pactus.Proposal', '10': 'proposal'},
const {'1': 'proposal', '3': 1, '4': 1, '5': 11, '6': '.pactus.ProposalInfo', '10': 'proposal'},
const {'1': 'instances', '3': 2, '4': 3, '5': 11, '6': '.pactus.ConsensusInfo', '10': 'instances'},
],
};

/// Descriptor for `GetConsensusInfoResponse`. Decode as a `google.protobuf.DescriptorProto`.
final $typed_data.Uint8List getConsensusInfoResponseDescriptor = $convert.base64Decode('ChhHZXRDb25zZW5zdXNJbmZvUmVzcG9uc2USLAoIcHJvcG9zYWwYASABKAsyEC5wYWN0dXMuUHJvcG9zYWxSCHByb3Bvc2FsEjMKCWluc3RhbmNlcxgCIAMoCzIVLnBhY3R1cy5Db25zZW5zdXNJbmZvUglpbnN0YW5jZXM=');
final $typed_data.Uint8List getConsensusInfoResponseDescriptor = $convert.base64Decode('ChhHZXRDb25zZW5zdXNJbmZvUmVzcG9uc2USMAoIcHJvcG9zYWwYASABKAsyFC5wYWN0dXMuUHJvcG9zYWxJbmZvUghwcm9wb3NhbBIzCglpbnN0YW5jZXMYAiADKAsyFS5wYWN0dXMuQ29uc2Vuc3VzSW5mb1IJaW5zdGFuY2Vz');
@$core.Deprecated('Use getTxPoolContentRequestDescriptor instead')
const GetTxPoolContentRequest$json = const {
'1': 'GetTxPoolContentRequest',
Expand Down Expand Up @@ -343,19 +343,19 @@ const ConsensusInfo$json = const {

/// Descriptor for `ConsensusInfo`. Decode as a `google.protobuf.DescriptorProto`.
final $typed_data.Uint8List consensusInfoDescriptor = $convert.base64Decode('Cg1Db25zZW5zdXNJbmZvEhgKB2FkZHJlc3MYASABKAlSB2FkZHJlc3MSFgoGYWN0aXZlGAIgASgIUgZhY3RpdmUSFgoGaGVpZ2h0GAMgASgNUgZoZWlnaHQSFAoFcm91bmQYBCABKAVSBXJvdW5kEiYKBXZvdGVzGAUgAygLMhAucGFjdHVzLlZvdGVJbmZvUgV2b3Rlcw==');
@$core.Deprecated('Use proposalDescriptor instead')
const Proposal$json = const {
'1': 'Proposal',
@$core.Deprecated('Use proposalInfoDescriptor instead')
const ProposalInfo$json = const {
'1': 'ProposalInfo',
'2': const [
const {'1': 'height', '3': 1, '4': 1, '5': 13, '10': 'height'},
const {'1': 'round', '3': 2, '4': 1, '5': 5, '10': 'round'},
const {'1': 'block_data', '3': 3, '4': 1, '5': 9, '10': 'blockData'},
const {'1': 'signature_data', '3': 4, '4': 1, '5': 9, '10': 'signatureData'},
const {'1': 'signature', '3': 4, '4': 1, '5': 9, '10': 'signature'},
],
};

/// Descriptor for `Proposal`. Decode as a `google.protobuf.DescriptorProto`.
final $typed_data.Uint8List proposalDescriptor = $convert.base64Decode('CghQcm9wb3NhbBIWCgZoZWlnaHQYASABKA1SBmhlaWdodBIUCgVyb3VuZBgCIAEoBVIFcm91bmQSHQoKYmxvY2tfZGF0YRgDIAEoCVIJYmxvY2tEYXRhEiUKDnNpZ25hdHVyZV9kYXRhGAQgASgJUg1zaWduYXR1cmVEYXRh');
/// Descriptor for `ProposalInfo`. Decode as a `google.protobuf.DescriptorProto`.
final $typed_data.Uint8List proposalInfoDescriptor = $convert.base64Decode('CgxQcm9wb3NhbEluZm8SFgoGaGVpZ2h0GAEgASgNUgZoZWlnaHQSFAoFcm91bmQYAiABKAVSBXJvdW5kEh0KCmJsb2NrX2RhdGEYAyABKAlSCWJsb2NrRGF0YRIcCglzaWduYXR1cmUYBCABKAlSCXNpZ25hdHVyZQ==');
const $core.Map<$core.String, $core.dynamic> BlockchainServiceBase$json = const {
'1': 'Blockchain',
'2': const [
Expand Down Expand Up @@ -394,7 +394,7 @@ const $core.Map<$core.String, $core.Map<$core.String, $core.dynamic>> Blockchain
'.pactus.ValidatorInfo': ValidatorInfo$json,
'.pactus.GetConsensusInfoRequest': GetConsensusInfoRequest$json,
'.pactus.GetConsensusInfoResponse': GetConsensusInfoResponse$json,
'.pactus.Proposal': Proposal$json,
'.pactus.ProposalInfo': ProposalInfo$json,
'.pactus.ConsensusInfo': ConsensusInfo$json,
'.pactus.VoteInfo': VoteInfo$json,
'.pactus.GetAccountRequest': GetAccountRequest$json,
Expand Down
6 changes: 3 additions & 3 deletions www/grpc/gen/docs/grpc.md
Original file line number Diff line number Diff line change
Expand Up @@ -1560,7 +1560,7 @@ Message has no fields.
<tbody class="table-group-divider">
<tr>
<td class="fw-bold">proposal</td>
<td> Proposal</td>
<td> ProposalInfo</td>
<td>
The proposal of the consensus info.
</td>
Expand All @@ -1587,10 +1587,10 @@ Message has no fields.
</td>
</tr>
<tr>
<td class="fw-bold">proposal.signature_data</td>
<td class="fw-bold">proposal.signature</td>
<td> string</td>
<td>
The signature data of the proposal.
The signature of the proposal, signed by the proposer.
</td>
</tr>
<tr>
Expand Down
4 changes: 2 additions & 2 deletions www/grpc/gen/docs/json-rpc.md
Original file line number Diff line number Diff line change
Expand Up @@ -1638,10 +1638,10 @@ Parameters has no fields.
</td>
</tr>
<tr>
<td class="fw-bold">proposal.signature_data</td>
<td class="fw-bold">proposal.signature</td>
<td> string</td>
<td>
The signature data of the proposal.
The signature of the proposal, signed by the proposer.
</td>
</tr>
<tr>
Expand Down
Loading

0 comments on commit 2bcb8d6

Please sign in to comment.