From 8407031605a6fdf8c642c7e81ce57d73d54d455b Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Thu, 25 Jun 2020 11:16:42 +0300 Subject: [PATCH] consensus: build MerkleRoot from hashes on block creation Use transaction hashes for merkle root calculation as transactions itself can not yet be available. --- pkg/consensus/consensus.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/pkg/consensus/consensus.go b/pkg/consensus/consensus.go index 6113753a9..783501d7f 100644 --- a/pkg/consensus/consensus.go +++ b/pkg/consensus/consensus.go @@ -13,6 +13,7 @@ import ( coreb "github.com/nspcc-dev/neo-go/pkg/core/block" "github.com/nspcc-dev/neo-go/pkg/core/blockchainer" "github.com/nspcc-dev/neo-go/pkg/core/transaction" + "github.com/nspcc-dev/neo-go/pkg/crypto/hash" "github.com/nspcc-dev/neo-go/pkg/crypto/keys" "github.com/nspcc-dev/neo-go/pkg/io" "github.com/nspcc-dev/neo-go/pkg/smartcontract" @@ -478,6 +479,15 @@ func (s *service) newBlockFromContext(ctx *dbft.Context) block.Block { primaryIndex := uint32(ctx.PrimaryIndex) block.Block.ConsensusData.PrimaryIndex = primaryIndex - block.Block.RebuildMerkleRoot() + hashes := make([]util.Uint256, len(ctx.TransactionHashes)+1) + hashes[0] = block.Block.ConsensusData.Hash() + copy(hashes[1:], ctx.TransactionHashes) + mt, err := hash.NewMerkleTree(hashes) + if err != nil { + s.log.Fatal("can't calculate merkle root for the new block") + return nil + } + block.Block.MerkleRoot = mt.Root() + return block }