Add processing after state block consensus

pull/69/head
Rongjian Lan 6 years ago
parent 3919360ef6
commit c09dc508f3
  1. 8
      blockchain/block.go
  2. 19
      node/node_handler.go

@ -28,6 +28,10 @@ type Block struct {
Signature [66]byte // Schnorr collective signature
}
func (b *Block) IsStateBlock() bool {
return bytes.Equal(b.PrevBlockHash[:], (&[32]byte{})[:]) // TODO: think of a better indicator to check
}
// Serialize serializes the block
func (b *Block) Serialize() []byte {
var result bytes.Buffer
@ -75,6 +79,10 @@ func (b *Block) Write(db db.Database, key string) error {
return db.Put([]byte(key), b.Serialize())
}
func Delete(db db.Database, key string) error {
return db.Delete([]byte(key))
}
// CalculateBlockHash returns a hash of the block
func (b *Block) CalculateBlockHash() []byte {
var hashes [][]byte

@ -271,7 +271,7 @@ func (node *Node) BroadcastNewBlock(newBlock *blockchain.Block) {
// This is called by consensus participants to verify the block they are running consensus on
func (node *Node) VerifyNewBlock(newBlock *blockchain.Block) bool {
if bytes.Equal(newBlock.PrevBlockHash[:], (&[32]byte{})[:]) {
if newBlock.IsStateBlock() {
return node.UtxoPool.VerifyStateBlock(newBlock)
} else {
return node.UtxoPool.VerifyTransactions(newBlock.Transactions)
@ -282,7 +282,19 @@ func (node *Node) VerifyNewBlock(newBlock *blockchain.Block) bool {
// 1. add the new block to blockchain
// 2. [leader] move cross shard tx and proof to the list where they wait to be sent to the client
func (node *Node) PostConsensusProcessing(newBlock *blockchain.Block) {
if newBlock.IsStateBlock() {
// Clear out old tx blocks and put state block as genesis
if node.db != nil {
node.log.Info("Deleting old blocks.")
for i := 1; i <= len(node.blockchain.Blocks); i++ {
blockchain.Delete(node.db, strconv.Itoa(i))
}
}
node.blockchain.Blocks = []*blockchain.Block{}
node.AddNewBlock(newBlock)
} else {
node.AddNewBlock(newBlock)
node.UpdateUtxoAndState(newBlock)
if node.Consensus.IsLeader {
// Move crossTx-in-consensus into the list to be returned to client
@ -298,6 +310,8 @@ func (node *Node) PostConsensusProcessing(newBlock *blockchain.Block) {
node.SendBackProofOfAcceptOrReject()
node.BroadcastNewBlock(newBlock)
}
}
}
func (node *Node) AddNewBlock(newBlock *blockchain.Block) {
@ -308,6 +322,9 @@ func (node *Node) AddNewBlock(newBlock *blockchain.Block) {
node.log.Info("Writing new block into disk.")
newBlock.Write(node.db, strconv.Itoa(len(node.blockchain.Blocks)))
}
}
func (node *Node) UpdateUtxoAndState(newBlock *blockchain.Block) {
// Update UTXO pool
node.UtxoPool.Update(newBlock.Transactions)
// Clear transaction-in-Consensus list

Loading…
Cancel
Save