Skip to content

Commit

Permalink
Merge pull request #282 from multiversx/header_proof_on_chain_handler…
Browse files Browse the repository at this point in the history
…_interface

Added header proof for the ChainHandler interface
  • Loading branch information
ssd04 authored Sep 17, 2024
2 parents d9127b8 + 3ea8d78 commit 99280b4
Show file tree
Hide file tree
Showing 11 changed files with 509 additions and 292 deletions.
10 changes: 5 additions & 5 deletions data/block/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -627,13 +627,13 @@ func (h *Header) CheckFieldsForNil() error {
return nil
}

// GetPreviousAggregatedSignatureAndBitmap returns the previous aggregated signature and the previous pubkeys bitmap
func (h *Header) GetPreviousAggregatedSignatureAndBitmap() ([]byte, []byte) {
// GetPreviousProof returns the previous proof
func (h *Header) GetPreviousProof() data.HeaderProofHandler {
// no proof for the initial header
return nil, nil
return nil
}

// SetPreviousAggregatedSignatureAndBitmap sets the previous aggregated signature and the previous pubkeys bitmap
func (h *Header) SetPreviousAggregatedSignatureAndBitmap(_ []byte, _ []byte) {
// SetPreviousProof sets the previous proof
func (h *Header) SetPreviousProof(_ data.HeaderProofHandler) {
// no proof for the initial header
}
27 changes: 14 additions & 13 deletions data/block/blockV2.go
Original file line number Diff line number Diff line change
Expand Up @@ -660,18 +660,19 @@ func (hv2 *HeaderV2) CheckFieldsForNil() error {
return nil
}

// GetPreviousAggregatedSignatureAndBitmap returns the previous aggregated signature and the previous pubkeys bitmap
func (hv2 *HeaderV2) GetPreviousAggregatedSignatureAndBitmap() ([]byte, []byte) {
if hv2.PreviousHeaderProof == nil {
return nil, nil
}
return hv2.PreviousHeaderProof.AggregatedSignature, hv2.PreviousHeaderProof.PubKeysBitmap
}

// SetPreviousAggregatedSignatureAndBitmap sets the previous aggregated signature and the previous pubkeys bitmap
func (hv2 *HeaderV2) SetPreviousAggregatedSignatureAndBitmap(aggregatedSignature []byte, pubKeysBitmap []byte) {
hv2.PreviousHeaderProof = &PreviousHeaderProof{
PubKeysBitmap: pubKeysBitmap,
AggregatedSignature: aggregatedSignature,
// GetPreviousProof returns the previous proof
func (hv2 *HeaderV2) GetPreviousProof() data.HeaderProofHandler {
return hv2.PreviousHeaderProof
}

// SetPreviousProof sets the previous proof
func (hv2 *HeaderV2) SetPreviousProof(proof data.HeaderProofHandler) {
hv2.PreviousHeaderProof = &HeaderProof{
PubKeysBitmap: proof.GetPubKeysBitmap(),
AggregatedSignature: proof.GetAggregatedSignature(),
HeaderHash: proof.GetHeaderHash(),
HeaderEpoch: proof.GetHeaderEpoch(),
HeaderNonce: proof.GetHeaderNonce(),
HeaderShardId: proof.GetHeaderShardId(),
}
}
359 changes: 271 additions & 88 deletions data/block/blockV2.pb.go

Large diffs are not rendered by default.

26 changes: 15 additions & 11 deletions data/block/blockV2.proto
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ import "block.proto";

// HeaderV2 extends the Header structure with extra fields for version 2
message HeaderV2 {
Header Header = 1 [(gogoproto.jsontag) = "header,omitempty"];
bytes ScheduledRootHash = 2 [(gogoproto.jsontag) = "scheduledRootHash,omitempty"];
Header Header = 1 [(gogoproto.jsontag) = "header,omitempty"];
bytes ScheduledRootHash = 2 [(gogoproto.jsontag) = "scheduledRootHash,omitempty"];
bytes ScheduledAccumulatedFees = 3 [(gogoproto.jsontag) = "scheduledAccumulatedFees,omitempty", (gogoproto.casttypewith) = "math/big.Int;github.com/multiversx/mx-chain-core-go/data.BigIntCaster"];
bytes ScheduledDeveloperFees = 4 [(gogoproto.jsontag) = "scheduledDeveloperFees,omitempty", (gogoproto.casttypewith) = "math/big.Int;github.com/multiversx/mx-chain-core-go/data.BigIntCaster"];
uint64 ScheduledGasProvided = 5 [(gogoproto.jsontag) = "scheduledGasProvided"];
uint64 ScheduledGasPenalized = 6 [(gogoproto.jsontag) = "scheduledGasPenalized"];
uint64 ScheduledGasRefunded = 7 [(gogoproto.jsontag) = "scheduledGasRefunded"];
PreviousHeaderProof PreviousHeaderProof = 8 [(gogoproto.jsontag) = "previousHeaderProof"];
bytes ScheduledDeveloperFees = 4 [(gogoproto.jsontag) = "scheduledDeveloperFees,omitempty", (gogoproto.casttypewith) = "math/big.Int;github.com/multiversx/mx-chain-core-go/data.BigIntCaster"];
uint64 ScheduledGasProvided = 5 [(gogoproto.jsontag) = "scheduledGasProvided"];
uint64 ScheduledGasPenalized = 6 [(gogoproto.jsontag) = "scheduledGasPenalized"];
uint64 ScheduledGasRefunded = 7 [(gogoproto.jsontag) = "scheduledGasRefunded"];
HeaderProof PreviousHeaderProof = 8 [(gogoproto.jsontag) = "previousHeaderProof"];
}

message MiniBlockReserved {
Expand All @@ -33,8 +33,12 @@ message MiniBlockHeaderReserved {
int32 IndexOfLastTxProcessed = 4 [(gogoproto.jsontag) = "indexOfLastTxProcessed"];
}

// PreviousHeaderProof defines a header proof
message PreviousHeaderProof {
bytes PubKeysBitmap = 1 [(gogoproto.jsontag) = "pubKeysBitmap"];
bytes AggregatedSignature = 2 [(gogoproto.jsontag) = "aggregatedSignature"];
// HeaderProof defines a proof for a specific header
message HeaderProof {
bytes PubKeysBitmap = 1 [(gogoproto.jsontag) = "pubKeysBitmap"];
bytes AggregatedSignature = 2 [(gogoproto.jsontag) = "aggregatedSignature"];
bytes HeaderHash = 3 [(gogoproto.jsontag) = "headerHash"];
uint32 HeaderEpoch = 4 [(gogoproto.jsontag) = "headerEpoch"];
uint64 HeaderNonce = 5 [(gogoproto.jsontag) = "headerNonce"];
uint32 HeaderShardId = 6 [(gogoproto.jsontag) = "headerShardId"];
}
2 changes: 1 addition & 1 deletion data/block/blockV2Checks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func TestBlockHeaderV2_Checks(t *testing.T) {

objectToTest := &HeaderV2{
Header: &Header{},
PreviousHeaderProof: &PreviousHeaderProof{},
PreviousHeaderProof: &HeaderProof{},
}

fieldsForHeaderV1 := prepareFieldsList(objectToTest.Header, headerV1ExceptionFields...)
Expand Down
34 changes: 22 additions & 12 deletions data/block/blockV2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1244,19 +1244,29 @@ func TestHeaderV2_SetBlockBodyTypeInt32(t *testing.T) {
})
}

func TestHeaderV2_GetPreviousAggregatedSignatureAndBitmap(t *testing.T) {
func TestHeaderV2_GetPreviousProof(t *testing.T) {
t.Parallel()

shardBlock := &block.HeaderV2{}
sig, bitmap := shardBlock.GetPreviousAggregatedSignatureAndBitmap()
require.Nil(t, sig)
require.Nil(t, bitmap)

previousPubkeysBitmap := []byte("previous pub keys bitmap")
previousAggregatedSignature := []byte("previous aggregated signature")

shardBlock.SetPreviousAggregatedSignatureAndBitmap(previousAggregatedSignature, previousPubkeysBitmap)
sig, bitmap = shardBlock.GetPreviousAggregatedSignatureAndBitmap()
require.Equal(t, previousPubkeysBitmap, bitmap)
require.Equal(t, previousAggregatedSignature, sig)
proof := shardBlock.GetPreviousProof()
require.Nil(t, proof)

previousProof := &block.HeaderProof{
PubKeysBitmap: []byte("previous pub keys bitmap"),
AggregatedSignature: []byte("previous aggregated signature"),
HeaderHash: []byte("previous hash"),
HeaderEpoch: 123,
HeaderNonce: 234,
HeaderShardId: 0,
}

shardBlock.SetPreviousProof(previousProof)
proof = shardBlock.GetPreviousProof()
require.Equal(t, previousProof, proof) // pointer testing
require.Equal(t, previousProof.AggregatedSignature, proof.GetAggregatedSignature())
require.Equal(t, previousProof.PubKeysBitmap, proof.GetPubKeysBitmap())
require.Equal(t, previousProof.HeaderHash, proof.GetHeaderHash())
require.Equal(t, previousProof.HeaderEpoch, proof.GetHeaderEpoch())
require.Equal(t, previousProof.HeaderNonce, proof.GetHeaderNonce())
require.Equal(t, previousProof.HeaderShardId, proof.GetHeaderShardId())
}
27 changes: 14 additions & 13 deletions data/block/metaBlock.go
Original file line number Diff line number Diff line change
Expand Up @@ -567,18 +567,19 @@ func (m *MetaBlock) CheckFieldsForNil() error {
return nil
}

// GetPreviousAggregatedSignatureAndBitmap returns the previous aggregated signature and the previous pubkeys bitmap
func (m *MetaBlock) GetPreviousAggregatedSignatureAndBitmap() ([]byte, []byte) {
if m.PreviousHeaderProof == nil {
return nil, nil
}
return m.PreviousHeaderProof.AggregatedSignature, m.PreviousHeaderProof.PubKeysBitmap
}

// SetPreviousAggregatedSignatureAndBitmap sets the previous aggregated signature and the previous pubkeys bitmap
func (m *MetaBlock) SetPreviousAggregatedSignatureAndBitmap(aggregatedSignature []byte, pubKeysBitmap []byte) {
m.PreviousHeaderProof = &PreviousHeaderProof{
PubKeysBitmap: pubKeysBitmap,
AggregatedSignature: aggregatedSignature,
// GetPreviousProof returns the previous proof
func (m *MetaBlock) GetPreviousProof() data.HeaderProofHandler {
return m.PreviousHeaderProof
}

// SetPreviousProof sets the previous proof
func (m *MetaBlock) SetPreviousProof(proof data.HeaderProofHandler) {
m.PreviousHeaderProof = &HeaderProof{
PubKeysBitmap: proof.GetPubKeysBitmap(),
AggregatedSignature: proof.GetAggregatedSignature(),
HeaderHash: proof.GetHeaderHash(),
HeaderEpoch: proof.GetHeaderEpoch(),
HeaderNonce: proof.GetHeaderNonce(),
HeaderShardId: proof.GetHeaderShardId(),
}
}
Loading

0 comments on commit 99280b4

Please sign in to comment.