From 6db4ca874d144fb85f1147f5006a5b877d147ba8 Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Thu, 23 Apr 2020 11:52:31 +0300 Subject: [PATCH] consensus: replace NewBlock() with NewBlockFromContext(ctx *Context) We have to set ConsensusData.PrimaryIndex field of Block, so this value can be retrieved from consensus context. --- go.mod | 2 +- go.sum | 2 ++ pkg/consensus/block.go | 5 +++++ pkg/consensus/consensus.go | 30 +++++++++++++++++++++++++++++- 4 files changed, 37 insertions(+), 2 deletions(-) diff --git a/go.mod b/go.mod index 636d7071e..72ea01ca8 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,7 @@ require ( github.com/go-redis/redis v6.10.2+incompatible github.com/go-yaml/yaml v2.1.0+incompatible github.com/mr-tron/base58 v1.1.2 - github.com/nspcc-dev/dbft v0.0.0-20200427132226-15a7927772a4 + github.com/nspcc-dev/dbft v0.0.0-20200427132226-342f23599814 github.com/nspcc-dev/rfc6979 v0.2.0 github.com/pkg/errors v0.8.1 github.com/prometheus/client_golang v1.2.1 diff --git a/go.sum b/go.sum index 7f8283738..27dd966ef 100644 --- a/go.sum +++ b/go.sum @@ -133,6 +133,8 @@ github.com/nspcc-dev/dbft v0.0.0-20200303183127-36d3da79c682 h1:63OWUolW4GcjJR7c github.com/nspcc-dev/dbft v0.0.0-20200303183127-36d3da79c682/go.mod h1:1FYQXSbb6/9HQIkoF8XO7W/S8N7AZRkBsgwbcXRvk0E= github.com/nspcc-dev/dbft v0.0.0-20200427132226-15a7927772a4 h1:3cFSp4v2u9+S7K1GdLUOP1680EiGEHSBvSI6G2n8XzY= github.com/nspcc-dev/dbft v0.0.0-20200427132226-15a7927772a4/go.mod h1:1FYQXSbb6/9HQIkoF8XO7W/S8N7AZRkBsgwbcXRvk0E= +github.com/nspcc-dev/dbft v0.0.0-20200427132226-342f23599814 h1:iNqBioi0RU2VX9UiGl/GfQKBbZrDWq5KSxQG+dgTaqo= +github.com/nspcc-dev/dbft v0.0.0-20200427132226-342f23599814/go.mod h1:1FYQXSbb6/9HQIkoF8XO7W/S8N7AZRkBsgwbcXRvk0E= github.com/nspcc-dev/dbft v0.0.0-20200427132226-660464796c11 h1:sledsmRo0wzgWNCZir5/CeM0PjhHVP8khnGtOfBCFWk= github.com/nspcc-dev/dbft v0.0.0-20200427132226-660464796c11/go.mod h1:1FYQXSbb6/9HQIkoF8XO7W/S8N7AZRkBsgwbcXRvk0E= github.com/nspcc-dev/neo-go v0.73.1-pre.0.20200303142215-f5a1b928ce09/go.mod h1:pPYwPZ2ks+uMnlRLUyXOpLieaDQSEaf4NM3zHVbRjmg= diff --git a/pkg/consensus/block.go b/pkg/consensus/block.go index cd6ccb5d6..820cd8979 100644 --- a/pkg/consensus/block.go +++ b/pkg/consensus/block.go @@ -99,3 +99,8 @@ func (n *neoBlock) SetNextConsensus(h util.Uint160) { n.Block.NextConsensus = h // Signature implements block.Block interface. func (n *neoBlock) Signature() []byte { return n.signature } + +// SetPrimaryIndex is an auxiliary setter for ConsensusData's PrimaryIndex +func (n *neoBlock) SetPrimaryIndex(primaryIndex uint32) { + n.Block.ConsensusData.PrimaryIndex = primaryIndex +} diff --git a/pkg/consensus/consensus.go b/pkg/consensus/consensus.go index 06774e0fb..d31b9a8a9 100644 --- a/pkg/consensus/consensus.go +++ b/pkg/consensus/consensus.go @@ -8,6 +8,7 @@ import ( "github.com/nspcc-dev/dbft" "github.com/nspcc-dev/dbft/block" "github.com/nspcc-dev/dbft/crypto" + "github.com/nspcc-dev/dbft/merkle" "github.com/nspcc-dev/dbft/payload" coreb "github.com/nspcc-dev/neo-go/pkg/core/block" "github.com/nspcc-dev/neo-go/pkg/core/blockchainer" @@ -134,7 +135,7 @@ func NewService(cfg Config) (Service, error) { dbft.WithVerifyBlock(srv.verifyBlock), dbft.WithGetBlock(srv.getBlock), dbft.WithWatchOnly(func() bool { return false }), - dbft.WithNewBlock(func() block.Block { return new(neoBlock) }), + dbft.WithNewBlockFromContext(newBlockFromContext), dbft.WithCurrentHeight(cfg.Chain.BlockHeight), dbft.WithCurrentBlockHash(cfg.Chain.CurrentBlockHash), dbft.WithGetValidators(srv.getValidators), @@ -478,3 +479,30 @@ func convertKeys(validators []crypto.PublicKey) (pubs []*keys.PublicKey) { return } + +func newBlockFromContext(ctx *dbft.Context) block.Block { + block := new(neoBlock) + if ctx.TransactionHashes == nil { + return nil + } + + block.Block.Timestamp = ctx.Timestamp / 1000000 + block.Block.Index = ctx.BlockIndex + block.Block.NextConsensus = ctx.NextConsensus + block.Block.PrevHash = ctx.PrevHash + block.Block.Version = ctx.Version + block.Block.ConsensusData.Nonce = ctx.Nonce + + primaryIndex := uint32(ctx.PrimaryIndex) + block.Block.ConsensusData.PrimaryIndex = primaryIndex + consensusData := coreb.ConsensusData{ + PrimaryIndex: primaryIndex, + Nonce: ctx.Nonce, + } + + if len(ctx.TransactionHashes) != 0 { + mt := merkle.NewMerkleTree(append([]util.Uint256{consensusData.Hash()}, ctx.TransactionHashes...)...) + block.Block.MerkleRoot = mt.Root().Hash + } + return block +}