From 695e473784cfc8841049e263eac9ea30c203fc2a Mon Sep 17 00:00:00 2001 From: Rongjian Lan Date: Mon, 3 Sep 2018 16:48:49 -0700 Subject: [PATCH] Create state block from utxo pool --- blockchain/block.go | 18 ++++++++++++++++++ blockchain/utxopool.go | 5 +++++ 2 files changed, 23 insertions(+) diff --git a/blockchain/block.go b/blockchain/block.go index 805631d19..696630db8 100644 --- a/blockchain/block.go +++ b/blockchain/block.go @@ -112,3 +112,21 @@ func NewBlock(transactions []*Transaction, prevBlockHash [32]byte, shardId uint3 func NewGenesisBlock(coinbase *Transaction, shardId uint32) *Block { return NewBlock([]*Transaction{coinbase}, [32]byte{}, shardId) } + +// NewStateBlock creates and returns a state Block based on utxo pool. +func NewStateBlock(utxoPool *UTXOPool) *Block { + stateTransactions := []*Transaction{} + for address, txHash2Vout2AmountMap := range utxoPool.UtxoMap { + stateTransaction := Transaction{} + for _, vout2AmountMap := range txHash2Vout2AmountMap { + for _, amount := range vout2AmountMap { + stateTransaction.TxOutput = append(stateTransaction.TxOutput, TXOutput{Amount: amount, Address: address, ShardID: utxoPool.ShardID}) + } + } + if len(stateTransaction.TxOutput) != 0 { + stateTransaction.SetID() + stateTransactions = append(stateTransactions, &stateTransaction) + } + } + return NewBlock(stateTransactions, [32]byte{}, utxoPool.ShardID) +} diff --git a/blockchain/utxopool.go b/blockchain/utxopool.go index 0b08fbe83..184027362 100644 --- a/blockchain/utxopool.go +++ b/blockchain/utxopool.go @@ -456,3 +456,8 @@ 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) +}