Skip to content

Commit

Permalink
Fix microblocks network messages for 1.2 and miner for 1.1 (#299)
Browse files Browse the repository at this point in the history
Co-authored-by: Alexey Kiselev <alexey.kiselev@gmail.com>
  • Loading branch information
Frozen and alexeykiselev committed Apr 23, 2020
1 parent 06707d7 commit 88a6969
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 13 deletions.
2 changes: 1 addition & 1 deletion pkg/miner/micro_miner.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func (a *MicroMiner) Micro(
}
sk := keyPair.Secret

err = newBlock.SetTransactionsRoot(a.scheme)
err = newBlock.SetTransactionsRootIfPossible(a.scheme)
if err != nil {
return nil, nil, rest, err
}
Expand Down
114 changes: 107 additions & 7 deletions pkg/miner/scheduler/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"github.com/pkg/errors"
"github.com/wavesplatform/gowaves/pkg/consensus"
"github.com/wavesplatform/gowaves/pkg/crypto"
"github.com/wavesplatform/gowaves/pkg/proto"
"github.com/wavesplatform/gowaves/pkg/settings"
"github.com/wavesplatform/gowaves/pkg/state"
Expand Down Expand Up @@ -47,7 +46,17 @@ type internalImpl struct {
}

func (a internalImpl) schedule(storage state.StateInfo, keyPairs []proto.KeyPair, schema proto.Scheme, AverageBlockDelaySeconds uint64, confirmedBlock *proto.Block, confirmedBlockHeight uint64) ([]Emit, error) {
vrfActivated, err := storage.IsActivated(int16(settings.BlockV5))
if err != nil {
return nil, errors.Wrap(err, "failed get vrfActivated")
}
if vrfActivated {
return a.scheduleWithVrf(storage, keyPairs, schema, AverageBlockDelaySeconds, confirmedBlock, confirmedBlockHeight)
}
return a.scheduleWithoutVrf(storage, keyPairs, schema, AverageBlockDelaySeconds, confirmedBlock, confirmedBlockHeight)
}

func (a internalImpl) scheduleWithVrf(storage state.StateInfo, keyPairs []proto.KeyPair, schema proto.Scheme, AverageBlockDelaySeconds uint64, confirmedBlock *proto.Block, confirmedBlockHeight uint64) ([]Emit, error) {
var greatGrandParentTimestamp proto.Timestamp = 0
if confirmedBlockHeight > 2 {
greatGrandParent, err := storage.BlockByHeight(confirmedBlockHeight - 2)
Expand Down Expand Up @@ -85,11 +94,7 @@ func (a internalImpl) schedule(storage state.StateInfo, keyPairs []proto.KeyPair

var out []Emit
for _, keyPair := range keyPairs {
var key [crypto.KeySize]byte = keyPair.Public
if blockV5Activated { // In case of VRF generation signature we need a SK of miner to produce it
key = keyPair.Secret
}

key := keyPair.Secret
HitSourceAtHeight, err := storage.HitSourceAtHeight(heightForHit)
if err != nil {
zap.S().Error("scheduler, internalImpl", err)
Expand Down Expand Up @@ -143,7 +148,102 @@ func (a internalImpl) schedule(storage state.StateInfo, keyPairs []proto.KeyPair
}

out = append(out, Emit{
Timestamp: confirmedBlock.Timestamp + delay + 10,
Timestamp: confirmedBlock.Timestamp + delay,
KeyPair: keyPair,
GenSignature: genSig,
VRF: vrf,
BaseTarget: baseTarget,
Parent: confirmedBlock.BlockID(),
})
}
return out, nil
}

func (a internalImpl) scheduleWithoutVrf(storage state.StateInfo, keyPairs []proto.KeyPair, schema proto.Scheme, AverageBlockDelaySeconds uint64, confirmedBlock *proto.Block, confirmedBlockHeight uint64) ([]Emit, error) {
var greatGrandParentTimestamp proto.Timestamp = 0
if confirmedBlockHeight > 2 {
greatGrandParent, err := storage.BlockByHeight(confirmedBlockHeight - 2)
if err != nil {
zap.S().Error(err)
return nil, err
}
greatGrandParentTimestamp = greatGrandParent.Timestamp
}

fairPosActivated, err := storage.IsActiveAtHeight(int16(settings.FairPoS), confirmedBlockHeight)
if err != nil {
return nil, errors.Wrap(err, "failed get fairPosActivated")
}
blockV5Activated, err := storage.IsActivated(int16(settings.BlockV5))
if err != nil {
return nil, errors.Wrap(err, "failed get blockV5Activated")
}
var pos consensus.PosCalculator = &consensus.NxtPosCalculator{}
if fairPosActivated {
if blockV5Activated {
pos = &consensus.FairPosCalculatorV2{}
} else {
pos = &consensus.FairPosCalculatorV1{}
}
}
var gsp consensus.GenerationSignatureProvider = &consensus.NXTGenerationSignatureProvider{}
hitSourceHeader, err := storage.HeaderByHeight(pos.HeightForHit(confirmedBlockHeight))
if err != nil {
zap.S().Error("scheduler, internalImpl HeaderByHeight", err)
return nil, err
}

zap.S().Infof("Scheduler: topBlock: id %s, gensig: %s, topBlockHeight: %d", confirmedBlock.BlockID().String(), confirmedBlock.GenSignature, confirmedBlockHeight)

var out []Emit
for _, keyPair := range keyPairs {
genSigBlock := confirmedBlock.BlockHeader
genSig, err := gsp.GenerationSignature(keyPair.Public, genSigBlock.GenSignature)
if err != nil {
zap.S().Error("scheduler, internalImpl", err)
continue
}
source, err := gsp.HitSource(keyPair.Public, hitSourceHeader.GenSignature)
if err != nil {
zap.S().Error("scheduler, internalImpl HitSource", err)
continue
}
var vrf []byte = nil
hit, err := consensus.GenHit(source)
if err != nil {
zap.S().Error("scheduler, internalImpl GenHit", err)
continue
}

addr, err := keyPair.Addr(schema)
if err != nil {
zap.S().Error("scheduler, internalImpl keyPair.Addr", err)
continue
}
var startHeight proto.Height = 1
if confirmedBlockHeight > 1000 {
startHeight = confirmedBlockHeight - 1000
}
effectiveBalance, err := storage.EffectiveBalanceStable(proto.NewRecipientFromAddress(addr), startHeight, confirmedBlockHeight)
if err != nil {
zap.S().Debug("scheduler, internalImpl effectiveBalance, err", effectiveBalance, err, addr.String())
continue
}

delay, err := pos.CalculateDelay(hit, confirmedBlock.BlockHeader.BaseTarget, effectiveBalance)
if err != nil {
zap.S().Error("scheduler, internalImpl pos.CalculateDelay", err)
continue
}

baseTarget, err := pos.CalculateBaseTarget(AverageBlockDelaySeconds, confirmedBlockHeight, confirmedBlock.BlockHeader.BaseTarget, confirmedBlock.Timestamp, greatGrandParentTimestamp, delay+confirmedBlock.Timestamp)
if err != nil {
zap.S().Error("scheduler, internalImpl pos.CalculateBaseTarget", err)
continue
}

out = append(out, Emit{
Timestamp: confirmedBlock.Timestamp + delay,
KeyPair: keyPair,
GenSignature: genSig,
VRF: vrf,
Expand Down
3 changes: 2 additions & 1 deletion pkg/node/actions_by_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net"
"reflect"

"github.com/pkg/errors"
"github.com/wavesplatform/gowaves/pkg/crypto"
"github.com/wavesplatform/gowaves/pkg/node/state_fsm"
"github.com/wavesplatform/gowaves/pkg/p2p/peer"
Expand Down Expand Up @@ -184,7 +185,7 @@ func PBMicroBlockAction(services services.Services, mess peer.ProtoMessage, fsm
micro := &proto.MicroBlock{}
err := micro.UnmarshalFromProtobuf(mess.Message.(*proto.PBMicroBlockMessage).MicroBlockBytes)
if err != nil {
return fsm, nil, err
return fsm, nil, errors.Wrap(err, "PBMicroBlockAction")
}
return fsm.MicroBlock(mess.ID, micro)
}
Expand Down
9 changes: 8 additions & 1 deletion pkg/proto/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,13 @@ func (b *Block) SetTransactionsRoot(scheme Scheme) error {
return nil
}

func (b *Block) SetTransactionsRootIfPossible(scheme Scheme) error {
if b.Version < ProtoBlockVersion {
return nil
}
return b.SetTransactionsRoot(scheme)
}

func (b *Block) VerifySignature(scheme Scheme) (bool, error) {
var bb []byte
if b.Version >= ProtoBlockVersion {
Expand Down Expand Up @@ -692,7 +699,7 @@ func (b *Block) UnmarshalBinary(data []byte, scheme Scheme) (err error) {

func (b *Block) transactionsRoot(scheme Scheme) ([]byte, error) {
if b.Version < ProtoBlockVersion {
return nil, errors.Errorf("no transactions root prior block version %d", ProtoBlockVersion)
return nil, errors.Errorf("no transactions root prior block version %d, current version %d", ProtoBlockVersion, b.Version)
}
tree, err := crypto.NewMerkleTree()
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions pkg/proto/microblock.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ type MicroblockTotalSig = crypto.Signature
func (a *MicroBlock) UnmarshalFromProtobuf(b []byte) error {
var pbMicroBlock g.SignedMicroBlock
if err := protobuf.Unmarshal(b, &pbMicroBlock); err != nil {
return err
return errors.Wrap(err, "SignedMicroBlock: failed to unmarshal")
}
var c ProtobufConverter
res, err := c.MicroBlock(&pbMicroBlock)
if err != nil {
return err
return errors.Wrap(err, "ProtobufConverter")
}
*a = res
return nil
Expand Down Expand Up @@ -549,7 +549,7 @@ func (a *PBMicroBlockMessage) UnmarshalBinary(data []byte) error {
if uint32(len(data)) < h.PayloadLength {
return errors.New("invalid data size")
}
mbBytes := data[:h.PayloadLength-crypto.DigestSize]
mbBytes := data[:h.PayloadLength]
a.MicroBlockBytes = make([]byte, len(mbBytes))
copy(a.MicroBlockBytes, mbBytes)
return nil
Expand Down

0 comments on commit 88a6969

Please sign in to comment.