From 9fc97e43b008e86698ed5f2c6609f68c2aa6fee4 Mon Sep 17 00:00:00 2001 From: Eugene Kim Date: Fri, 22 Mar 2019 17:42:32 -0700 Subject: [PATCH 1/2] Store group sigs in the real header, not in a copy (*Block) Header() returns a (shallow) copy of the header, where we were storing group signatures and signer bitmaps. This had no effect, and the on-chain block was still missing these signatures. --- consensus/consensus_leader.go | 10 ++++++---- consensus/consensus_validator.go | 10 ++++++---- core/types/block.go | 24 ++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 8 deletions(-) diff --git a/consensus/consensus_leader.go b/consensus/consensus_leader.go index 96c1ce8ff..3ffae0feb 100644 --- a/consensus/consensus_leader.go +++ b/consensus/consensus_leader.go @@ -308,10 +308,12 @@ func (consensus *Consensus) processCommitMessage(message *msg_pb.Message) { } // Sign the block - copy(blockObj.Header().PrepareSignature[:], consensus.aggregatedPrepareSig.Serialize()[:]) - copy(blockObj.Header().PrepareBitmap[:], consensus.prepareBitmap.Bitmap) - copy(blockObj.Header().CommitSignature[:], consensus.aggregatedCommitSig.Serialize()[:]) - copy(blockObj.Header().CommitBitmap[:], consensus.commitBitmap.Bitmap) + blockObj.SetPrepareSig( + consensus.aggregatedPrepareSig.Serialize(), + consensus.prepareBitmap.Bitmap) + blockObj.SetCommitSig( + consensus.aggregatedCommitSig.Serialize(), + consensus.commitBitmap.Bitmap) consensus.state = targetState diff --git a/consensus/consensus_validator.go b/consensus/consensus_validator.go index c67dd3dbe..b4f42fd19 100644 --- a/consensus/consensus_validator.go +++ b/consensus/consensus_validator.go @@ -282,10 +282,12 @@ func (consensus *Consensus) processCommittedMessage(message *msg_pb.Message) { } // Put the signatures into the block - copy(blockObj.Header().PrepareSignature[:], consensus.aggregatedPrepareSig.Serialize()[:]) - copy(blockObj.Header().PrepareBitmap[:], consensus.prepareBitmap.Bitmap) - copy(blockObj.Header().CommitSignature[:], consensus.aggregatedCommitSig.Serialize()[:]) - copy(blockObj.Header().CommitBitmap[:], consensus.commitBitmap.Bitmap) + blockObj.SetPrepareSig( + consensus.aggregatedPrepareSig.Serialize(), + consensus.prepareBitmap.Bitmap) + blockObj.SetCommitSig( + consensus.aggregatedCommitSig.Serialize(), + consensus.commitBitmap.Bitmap) utils.GetLogInstance().Info("Adding block to chain", "numTx", len(blockObj.Transactions())) consensus.OnConsensusDone(&blockObj) consensus.ResetState() diff --git a/core/types/block.go b/core/types/block.go index eb3e70323..408ac5549 100644 --- a/core/types/block.go +++ b/core/types/block.go @@ -30,6 +30,8 @@ import ( "github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/rlp" "golang.org/x/crypto/sha3" + + "github.com/harmony-one/harmony/internal/utils" ) // Constants for block. @@ -159,6 +161,28 @@ type Block struct { 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 // code solely to facilitate upgrading the database from the old format to the // new, after which it should be deleted. Do not use! From 2876ac5d99642112ffb025bb90dc7592aed2ef9f Mon Sep 17 00:00:00 2001 From: Eugene Kim Date: Sat, 23 Mar 2019 11:46:09 -0700 Subject: [PATCH 2/2] Add UTs Suggested-by: Leo Chen --- core/types/block_test.go | 112 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 core/types/block_test.go diff --git a/core/types/block_test.go b/core/types/block_test.go new file mode 100644 index 000000000..5d4f7e9e4 --- /dev/null +++ b/core/types/block_test.go @@ -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 . + +// 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) + } + }) + } +}