Add State struct in Block and populate them in state block

pull/69/head
Rongjian Lan 6 years ago
parent 8a709bcf35
commit ea6b76e1b3
  1. 16
      blockchain/block.go
  2. 16
      blockchain/blockchain.go
  3. 5
      blockchain/utxopool.go
  4. 2
      node/node_handler.go

@ -23,13 +23,19 @@ type Block struct {
ShardId uint32
Hash [32]byte
MerkleRootData []byte
State *State // If present, this block is state block
// Signature...
Bitmap []byte // Contains which validator signed the block.
Signature [66]byte // Schnorr collective signature
}
type State struct {
NumBlocks int32 // Total number of blocks
NumTransactions int32 // Total number of transactions
}
func (b *Block) IsStateBlock() bool {
return bytes.Equal(b.PrevBlockHash[:], (&[32]byte{})[:]) // TODO: think of a better indicator to check
return b.State != nil && bytes.Equal(b.PrevBlockHash[:], (&[32]byte{})[:]) // TODO: think of a better indicator to check
}
// Serialize serializes the block
@ -122,8 +128,9 @@ func NewGenesisBlock(coinbase *Transaction, shardId uint32) *Block {
}
// NewStateBlock creates and returns a state Block based on utxo pool.
func NewStateBlock(utxoPool *UTXOPool) *Block {
func NewStateBlock(utxoPool *UTXOPool, numBlocks, numTxs int32) *Block {
stateTransactions := []*Transaction{}
stateTransactionIds := [][32]byte{}
for address, txHash2Vout2AmountMap := range utxoPool.UtxoMap {
stateTransaction := Transaction{}
for _, vout2AmountMap := range txHash2Vout2AmountMap {
@ -133,8 +140,11 @@ func NewStateBlock(utxoPool *UTXOPool) *Block {
}
if len(stateTransaction.TxOutput) != 0 {
stateTransaction.SetID()
stateTransactionIds = append(stateTransactionIds, stateTransaction.ID)
stateTransactions = append(stateTransactions, &stateTransaction)
}
}
return NewBlock(stateTransactions, [32]byte{}, utxoPool.ShardID)
newBlock := NewBlock(stateTransactions, [32]byte{}, utxoPool.ShardID)
newBlock.State = &State{NumBlocks: numBlocks, NumTransactions: numTxs}
return newBlock
}

@ -195,3 +195,19 @@ func CreateBlockchain(address [20]byte, shardId uint32) *Blockchain {
return &bc
}
// Create state block based on the utxos.
func (bc *Blockchain) CreateStateBlock(utxoPool *UTXOPool) *Block {
var numBlocks int32 = 0
var numTxs int32 = 0
for _, block := range bc.Blocks {
if block.IsStateBlock() {
numBlocks += block.State.NumBlocks
numTxs += block.State.NumTransactions
} else {
numBlocks += 1
numTxs += block.NumTransactions
}
}
return NewStateBlock(utxoPool, numBlocks, numTxs)
}

@ -462,8 +462,3 @@ func (utxoPool *UTXOPool) GetSizeInByteOfUtxoMap() int {
encoder.Encode(utxoPool.UtxoMap)
return len(byteBuffer.Bytes())
}
// Create state block based on the utxos.
func (utxoPool *UTXOPool) CreateStateBlock() *Block {
return NewStateBlock(utxoPool)
}

@ -210,7 +210,7 @@ func (node *Node) WaitForConsensusReady(readySignal chan int) {
if !retry {
if len(node.blockchain.Blocks) > NumBlocksBeforeStateBlock {
// Generate state block and run consensus on it
newBlock = node.UtxoPool.CreateStateBlock()
newBlock = node.blockchain.CreateStateBlock(node.UtxoPool)
} else {
// Normal tx block consensus
for {

Loading…
Cancel
Save