Skip to content

Commit

Permalink
Add timestamp after txn verification when proposing block
Browse files Browse the repository at this point in the history
Signed-off-by: Yilun <zyl.skysniper@gmail.com>
  • Loading branch information
yilunzhang committed Jul 2, 2019
1 parent ffd2246 commit 9a1d63a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 12 deletions.
21 changes: 13 additions & 8 deletions chain/mining.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import (
)

type Mining interface {
BuildBlock(ctx context.Context, height uint32, chordID []byte, winnerHash common.Uint256, winnerType pb.WinnerType, timestamp int64) (*block.Block, error)
BuildBlock(ctx context.Context, height uint32, chordID []byte, winnerHash common.Uint256, winnerType pb.WinnerType) (*block.Block, error)
SignBlock(b *block.Block, timestamp int64) error
}

type BuiltinMining struct {
Expand All @@ -32,7 +33,7 @@ func NewBuiltinMining(account *vault.Account, txnCollector *TxnCollector) *Built
}
}

func (bm *BuiltinMining) BuildBlock(ctx context.Context, height uint32, chordID []byte, winnerHash common.Uint256, winnerType pb.WinnerType, timestamp int64) (*block.Block, error) {
func (bm *BuiltinMining) BuildBlock(ctx context.Context, height uint32, chordID []byte, winnerHash common.Uint256, winnerType pb.WinnerType) (*block.Block, error) {
var txnList []*transaction.Transaction
var txnHashList []common.Uint256

Expand Down Expand Up @@ -145,7 +146,6 @@ func (bm *BuiltinMining) BuildBlock(ctx context.Context, height uint32, chordID
UnsignedHeader: &pb.UnsignedHeader{
Version: config.HeaderVersion,
PrevBlockHash: curBlockHash.ToArray(),
Timestamp: timestamp,
Height: height,
RandomBeacon: randomBeacon,
TransactionsRoot: txnRoot.ToArray(),
Expand All @@ -170,14 +170,19 @@ func (bm *BuiltinMining) BuildBlock(ctx context.Context, height uint32, chordID

header.UnsignedHeader.StateRoot = curStateHash.ToArray()

hash := signature.GetHashForSigning(header)
return block, nil
}

func (bm *BuiltinMining) SignBlock(b *block.Block, timestamp int64) error {
b.Header.UnsignedHeader.Timestamp = timestamp

hash := signature.GetHashForSigning(b.Header)
sig, err := crypto.Sign(bm.account.PrivateKey, hash)
if err != nil {
return nil, err
return err
}
header.Signature = append(header.Signature, sig...)

return block, nil
b.Header.Signature = append(b.Header.Signature, sig...)
return nil
}

func (bm *BuiltinMining) CreateCoinbaseTransaction(reward common.Fixed64) *transaction.Transaction {
Expand Down
3 changes: 2 additions & 1 deletion consensus/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package moca
import (
"time"

"github.com/nknorg/nkn/chain"
"github.com/nknorg/nkn/pb"
"github.com/nknorg/nkn/util/config"
)
Expand All @@ -14,7 +15,7 @@ const (
minVotingInterval = 500 * time.Millisecond
maxVotingInterval = 2 * time.Second
proposingInterval = 500 * time.Millisecond
proposingTimeout = config.ConsensusDuration / 10
proposingTimeout = chain.ProposingTimeTolerance * 4 / 5
cacheExpiration = 3600 * time.Second
cacheCleanupInterval = 600 * time.Second
proposingStartDelay = config.ConsensusTimeout + time.Second
Expand Down
17 changes: 14 additions & 3 deletions consensus/proposing.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,25 @@ func (consensus *Consensus) startProposing() {

ctx, cancel = context.WithTimeout(context.Background(), proposingTimeout)

block, err := consensus.proposeBlock(ctx, expectedHeight, timestamp)
block, err := consensus.proposeBlock(ctx, expectedHeight)
if err != nil {
log.Errorf("Propose block %d at %v error: %v", expectedHeight, timestamp, err)
break
}

cancel()

timestamp = time.Now().Unix()
if !consensus.isBlockProposer(currentHeight, timestamp) {
log.Errorf("I'm no longer the block proposer at height %d at %v", expectedHeight, timestamp)
break
}
err = consensus.mining.SignBlock(block, timestamp)
if err != nil {
log.Errorf("Sign block with timestamp %v error: %v", timestamp, err)
break
}

blockHash := block.Hash()
log.Infof("Propose block %s at height %d", blockHash.ToHexString(), expectedHeight)

Expand Down Expand Up @@ -82,11 +93,11 @@ func (consensus *Consensus) isBlockProposer(height uint32, timestamp int64) bool
}

// proposeBlock proposes a new block at give height and timestamp
func (consensus *Consensus) proposeBlock(ctx context.Context, height uint32, timestamp int64) (*block.Block, error) {
func (consensus *Consensus) proposeBlock(ctx context.Context, height uint32) (*block.Block, error) {
winnerHash, winnerType, err := chain.GetNextMiningSigChainTxnHash(height)
if err != nil {
return nil, err
}

return consensus.mining.BuildBlock(ctx, height, consensus.localNode.GetChordID(), winnerHash, winnerType, timestamp)
return consensus.mining.BuildBlock(ctx, height, consensus.localNode.GetChordID(), winnerHash, winnerType)
}

0 comments on commit 9a1d63a

Please sign in to comment.