Merge pull request #628 from harmony-ek/fix_group_sigs_in_real_header

Store group sigs in the real header, not in a copy
pull/644/head
Eugene Kim 6 years ago committed by GitHub
commit d089c8dbf7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 10
      consensus/consensus_leader.go
  2. 10
      consensus/consensus_validator.go
  3. 24
      core/types/block.go
  4. 112
      core/types/block_test.go

@ -308,10 +308,12 @@ func (consensus *Consensus) processCommitMessage(message *msg_pb.Message) {
} }
// Sign the block // Sign the block
copy(blockObj.Header().PrepareSignature[:], consensus.aggregatedPrepareSig.Serialize()[:]) blockObj.SetPrepareSig(
copy(blockObj.Header().PrepareBitmap[:], consensus.prepareBitmap.Bitmap) consensus.aggregatedPrepareSig.Serialize(),
copy(blockObj.Header().CommitSignature[:], consensus.aggregatedCommitSig.Serialize()[:]) consensus.prepareBitmap.Bitmap)
copy(blockObj.Header().CommitBitmap[:], consensus.commitBitmap.Bitmap) blockObj.SetCommitSig(
consensus.aggregatedCommitSig.Serialize(),
consensus.commitBitmap.Bitmap)
consensus.state = targetState consensus.state = targetState

@ -253,10 +253,12 @@ func (consensus *Consensus) processCommittedMessage(message *msg_pb.Message) {
} }
// Put the signatures into the block // Put the signatures into the block
copy(blockObj.Header().PrepareSignature[:], consensus.aggregatedPrepareSig.Serialize()[:]) blockObj.SetPrepareSig(
copy(blockObj.Header().PrepareBitmap[:], consensus.prepareBitmap.Bitmap) consensus.aggregatedPrepareSig.Serialize(),
copy(blockObj.Header().CommitSignature[:], consensus.aggregatedCommitSig.Serialize()[:]) consensus.prepareBitmap.Bitmap)
copy(blockObj.Header().CommitBitmap[:], consensus.commitBitmap.Bitmap) blockObj.SetCommitSig(
consensus.aggregatedCommitSig.Serialize(),
consensus.commitBitmap.Bitmap)
utils.GetLogInstance().Info("Adding block to chain", "numTx", len(blockObj.Transactions())) utils.GetLogInstance().Info("Adding block to chain", "numTx", len(blockObj.Transactions()))
consensus.OnConsensusDone(&blockObj) consensus.OnConsensusDone(&blockObj)
consensus.ResetState() consensus.ResetState()

@ -30,6 +30,8 @@ import (
"github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/rlp"
"golang.org/x/crypto/sha3" "golang.org/x/crypto/sha3"
"github.com/harmony-one/harmony/internal/utils"
) )
// Constants for block. // Constants for block.
@ -159,6 +161,28 @@ type Block struct {
ReceivedFrom interface{} ReceivedFrom interface{}
} }
// SetPrepareSig sets the block's prepare group signature.
func (b *Block) SetPrepareSig(sig []byte, signers []byte) {
if len(sig) != len(b.header.PrepareSignature) {
utils.GetLogInstance().Warn("SetPrepareSig: sig size mismatch",
"srcLen", len(sig),
"dstLen", len(b.header.PrepareSignature))
}
copy(b.header.PrepareSignature[:], sig[:])
b.header.PrepareBitmap = append(signers[:0:0], signers...)
}
// SetCommitSig sets the block's commit group signature.
func (b *Block) SetCommitSig(sig []byte, signers []byte) {
if len(sig) != len(b.header.CommitSignature) {
utils.GetLogInstance().Warn("SetCommitSig: sig size mismatch",
"srcLen", len(sig),
"dstLen", len(b.header.CommitSignature))
}
copy(b.header.CommitSignature[:], sig[:])
b.header.CommitBitmap = append(signers[:0:0], signers...)
}
// DeprecatedTd is an old relic for extracting the TD of a block. It is in the // DeprecatedTd is an old relic for extracting the TD of a block. It is in the
// code solely to facilitate upgrading the database from the old format to the // code solely to facilitate upgrading the database from the old format to the
// new, after which it should be deleted. Do not use! // new, after which it should be deleted. Do not use!

@ -0,0 +1,112 @@
// Copyright 2014 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum 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 go-ethereum 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 go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
// Package types contains data types related to Ethereum consensus.
package types
import (
"bytes"
"testing"
)
var (
pat48Hex55 = []byte{
0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
}
pat48HexAA = []byte{
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
}
pat48Pi = []byte{
0x03, 0x14, 0x15, 0x92, 0x65, 0x35, 0x89, 0x79,
0x32, 0x38, 0x46, 0x26, 0x43, 0x38, 0x32, 0x79,
0x50, 0x28, 0x84, 0x19, 0x71, 0x69, 0x39, 0x93,
0x75, 0x10, 0x58, 0x20, 0x97, 0x49, 0x44, 0x59,
0x23, 0x07, 0x81, 0x64, 0x06, 0x28, 0x62, 0x08,
0x99, 0x86, 0x28, 0x03, 0x48, 0x25, 0x34, 0x21,
}
pat48E = []byte{
0x02, 0x71, 0x82, 0x81, 0x82, 0x84, 0x59, 0x04,
0x52, 0x35, 0x36, 0x02, 0x87, 0x47, 0x13, 0x52,
0x66, 0x24, 0x97, 0x75, 0x72, 0x47, 0x09, 0x36,
0x99, 0x95, 0x95, 0x74, 0x96, 0x69, 0x67, 0x62,
0x77, 0x24, 0x07, 0x66, 0x30, 0x35, 0x35, 0x47,
0x59, 0x45, 0x71, 0x38, 0x21, 0x78, 0x52, 0x51,
}
)
func TestBlock_SetPrepareSig(t *testing.T) {
tests := []struct {
name string
sig []byte
signers []byte
}{
{"55AA", pat48Hex55, pat48HexAA},
{"AA55", pat48HexAA, pat48Hex55},
{"PiE", pat48Pi, pat48E},
{"EPi", pat48E, pat48Pi},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
b := &Block{header: &Header{}}
b.SetPrepareSig(tt.sig, tt.signers)
if !bytes.Equal(tt.sig, b.header.PrepareSignature[:]) {
t.Errorf("signature mismatch: expected %+v, actual %+v",
tt.sig, b.header.PrepareSignature)
}
if !bytes.Equal(tt.signers, b.header.PrepareBitmap) {
t.Errorf("signature mismatch: expected %+v, actual %+v",
tt.signers, b.header.PrepareBitmap)
}
})
}
}
func TestBlock_SetCommitSig(t *testing.T) {
tests := []struct {
name string
sig []byte
signers []byte
}{
{"5AAa", pat48Hex55, pat48HexAA},
{"AA55", pat48HexAA, pat48Hex55},
{"PiE", pat48Pi, pat48E},
{"EPi", pat48E, pat48Pi},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
b := &Block{header: &Header{}}
b.SetCommitSig(tt.sig, tt.signers)
if !bytes.Equal(tt.sig, b.header.CommitSignature[:]) {
t.Errorf("signature mismatch: expected %+v, actual %+v",
tt.sig, b.header.CommitSignature)
}
if !bytes.Equal(tt.signers, b.header.CommitBitmap) {
t.Errorf("signature mismatch: expected %+v, actual %+v",
tt.signers, b.header.CommitBitmap)
}
})
}
}
Loading…
Cancel
Save