neo-go/pkg/consensus/prepare_request.go

58 lines
1.7 KiB
Go
Raw Permalink Normal View History

2019-11-08 15:40:21 +00:00
package consensus
import (
"github.com/nspcc-dev/dbft"
"github.com/nspcc-dev/neo-go/pkg/core/block"
"github.com/nspcc-dev/neo-go/pkg/io"
"github.com/nspcc-dev/neo-go/pkg/util"
2019-11-08 15:40:21 +00:00
)
2019-11-15 10:32:40 +00:00
// prepareRequest represents dBFT prepareRequest message.
2019-11-08 15:40:21 +00:00
type prepareRequest struct {
version uint32
prevHash util.Uint256
timestamp uint64
nonce uint64
2019-11-15 10:32:40 +00:00
transactionHashes []util.Uint256
stateRootEnabled bool
stateRoot util.Uint256
2019-11-08 15:40:21 +00:00
}
var _ dbft.PrepareRequest[util.Uint256] = (*prepareRequest)(nil)
2019-11-15 10:32:40 +00:00
// EncodeBinary implements the io.Serializable interface.
2019-11-08 15:40:21 +00:00
func (p *prepareRequest) EncodeBinary(w *io.BinWriter) {
w.WriteU32LE(p.version)
w.WriteBytes(p.prevHash[:])
w.WriteU64LE(p.timestamp)
w.WriteU64LE(p.nonce)
w.WriteVarUint(uint64(len(p.transactionHashes)))
for i := range p.transactionHashes {
w.WriteBytes(p.transactionHashes[i][:])
}
if p.stateRootEnabled {
w.WriteBytes(p.stateRoot[:])
}
2019-11-08 15:40:21 +00:00
}
// DecodeBinary implements the io.Serializable interface.
2019-11-08 15:40:21 +00:00
func (p *prepareRequest) DecodeBinary(r *io.BinReader) {
p.version = r.ReadU32LE()
r.ReadBytes(p.prevHash[:])
p.timestamp = r.ReadU64LE()
p.nonce = r.ReadU64LE()
r.ReadArray(&p.transactionHashes, block.MaxTransactionsPerBlock)
if p.stateRootEnabled {
r.ReadBytes(p.stateRoot[:])
}
2019-11-08 15:40:21 +00:00
}
2019-11-15 10:32:40 +00:00
// Timestamp implements the payload.PrepareRequest interface.
func (p *prepareRequest) Timestamp() uint64 { return p.timestamp * nsInMs }
2019-11-15 10:32:40 +00:00
// Nonce implements the payload.PrepareRequest interface.
func (p *prepareRequest) Nonce() uint64 { return p.nonce }
2019-11-15 10:32:40 +00:00
// TransactionHashes implements the payload.PrepareRequest interface.
2019-11-15 10:32:40 +00:00
func (p *prepareRequest) TransactionHashes() []util.Uint256 { return p.transactionHashes }