consensus: add Nonce
to PrepareRequest
Signed-off-by: Evgeniy Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
parent
8077f9232d
commit
85ce207f40
2 changed files with 18 additions and 2 deletions
|
@ -1,6 +1,8 @@
|
||||||
package consensus
|
package consensus
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"crypto/rand"
|
||||||
|
"encoding/binary"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"sort"
|
"sort"
|
||||||
|
@ -239,9 +241,19 @@ func (s *service) newPrepareRequest() payload.PrepareRequest {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
r.nonce = s.getNonce()
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *service) getNonce() uint64 {
|
||||||
|
b := make([]byte, 8)
|
||||||
|
_, err := rand.Read(b)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return binary.LittleEndian.Uint64(b)
|
||||||
|
}
|
||||||
|
|
||||||
func (s *service) Start() {
|
func (s *service) Start() {
|
||||||
if s.started.CAS(false, true) {
|
if s.started.CAS(false, true) {
|
||||||
s.log.Info("starting consensus service")
|
s.log.Info("starting consensus service")
|
||||||
|
@ -649,6 +661,7 @@ func (s *service) newBlockFromContext(ctx *dbft.Context) block.Block {
|
||||||
block := &neoBlock{network: s.ProtocolConfiguration.Magic}
|
block := &neoBlock{network: s.ProtocolConfiguration.Magic}
|
||||||
|
|
||||||
block.Block.Timestamp = ctx.Timestamp / nsInMs
|
block.Block.Timestamp = ctx.Timestamp / nsInMs
|
||||||
|
block.Block.Nonce = ctx.PreparationPayloads[ctx.PrimaryIndex].GetPrepareRequest().Nonce()
|
||||||
block.Block.Index = ctx.BlockIndex
|
block.Block.Index = ctx.BlockIndex
|
||||||
if s.ProtocolConfiguration.StateRootInHeader {
|
if s.ProtocolConfiguration.StateRootInHeader {
|
||||||
sr, err := s.Chain.GetStateModule().GetStateRoot(ctx.BlockIndex - 1)
|
sr, err := s.Chain.GetStateModule().GetStateRoot(ctx.BlockIndex - 1)
|
||||||
|
|
|
@ -12,6 +12,7 @@ type prepareRequest struct {
|
||||||
version uint32
|
version uint32
|
||||||
prevHash util.Uint256
|
prevHash util.Uint256
|
||||||
timestamp uint64
|
timestamp uint64
|
||||||
|
nonce uint64
|
||||||
transactionHashes []util.Uint256
|
transactionHashes []util.Uint256
|
||||||
stateRootEnabled bool
|
stateRootEnabled bool
|
||||||
stateRoot util.Uint256
|
stateRoot util.Uint256
|
||||||
|
@ -24,6 +25,7 @@ func (p *prepareRequest) EncodeBinary(w *io.BinWriter) {
|
||||||
w.WriteU32LE(p.version)
|
w.WriteU32LE(p.version)
|
||||||
w.WriteBytes(p.prevHash[:])
|
w.WriteBytes(p.prevHash[:])
|
||||||
w.WriteU64LE(p.timestamp)
|
w.WriteU64LE(p.timestamp)
|
||||||
|
w.WriteU64LE(p.nonce)
|
||||||
w.WriteArray(p.transactionHashes)
|
w.WriteArray(p.transactionHashes)
|
||||||
if p.stateRootEnabled {
|
if p.stateRootEnabled {
|
||||||
w.WriteBytes(p.stateRoot[:])
|
w.WriteBytes(p.stateRoot[:])
|
||||||
|
@ -35,6 +37,7 @@ func (p *prepareRequest) DecodeBinary(r *io.BinReader) {
|
||||||
p.version = r.ReadU32LE()
|
p.version = r.ReadU32LE()
|
||||||
r.ReadBytes(p.prevHash[:])
|
r.ReadBytes(p.prevHash[:])
|
||||||
p.timestamp = r.ReadU64LE()
|
p.timestamp = r.ReadU64LE()
|
||||||
|
p.nonce = r.ReadU64LE()
|
||||||
r.ReadArray(&p.transactionHashes, block.MaxTransactionsPerBlock)
|
r.ReadArray(&p.transactionHashes, block.MaxTransactionsPerBlock)
|
||||||
if p.stateRootEnabled {
|
if p.stateRootEnabled {
|
||||||
r.ReadBytes(p.stateRoot[:])
|
r.ReadBytes(p.stateRoot[:])
|
||||||
|
@ -68,10 +71,10 @@ func (p *prepareRequest) Timestamp() uint64 { return p.timestamp * nsInMs }
|
||||||
func (p *prepareRequest) SetTimestamp(ts uint64) { p.timestamp = ts / nsInMs }
|
func (p *prepareRequest) SetTimestamp(ts uint64) { p.timestamp = ts / nsInMs }
|
||||||
|
|
||||||
// Nonce implements payload.PrepareRequest interface.
|
// Nonce implements payload.PrepareRequest interface.
|
||||||
func (p *prepareRequest) Nonce() uint64 { return 0 }
|
func (p *prepareRequest) Nonce() uint64 { return p.nonce }
|
||||||
|
|
||||||
// SetNonce implements payload.PrepareRequest interface.
|
// SetNonce implements payload.PrepareRequest interface.
|
||||||
func (p *prepareRequest) SetNonce(nonce uint64) {}
|
func (p *prepareRequest) SetNonce(nonce uint64) { p.nonce = nonce }
|
||||||
|
|
||||||
// TransactionHashes implements payload.PrepareRequest interface.
|
// TransactionHashes implements payload.PrepareRequest interface.
|
||||||
func (p *prepareRequest) TransactionHashes() []util.Uint256 { return p.transactionHashes }
|
func (p *prepareRequest) TransactionHashes() []util.Uint256 { return p.transactionHashes }
|
||||||
|
|
Loading…
Reference in a new issue