Implement state block verification

pull/69/head
Rongjian Lan 6 years ago
parent 17fe3fd35a
commit d0d6715ccd
  1. 13
      blockchain/block.go
  2. 2
      blockchain/transaction.go
  3. 24
      blockchain/utxopool.go
  4. 2
      node/node_handler.go

@ -128,14 +128,21 @@ func NewGenesisBlock(coinbase *Transaction, shardId uint32) *Block {
}
// NewStateBlock creates and returns a state Block based on utxo pool.
// TODO(RJ): take care of dangling cross shard transaction
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 {
for _, amount := range vout2AmountMap {
stateTransaction.TxOutput = append(stateTransaction.TxOutput, TXOutput{Amount: amount, Address: address, ShardID: utxoPool.ShardID})
for txHash, vout2AmountMap := range txHash2Vout2AmountMap {
for index, amount := range vout2AmountMap {
txHashBytes, err := utils.Get32BytesFromString(txHash)
if err == nil {
stateTransaction.TxInput = append(stateTransaction.TxInput, *NewTXInput(NewOutPoint(&txHashBytes, index), address, utxoPool.ShardID))
stateTransaction.TxOutput = append(stateTransaction.TxOutput, TXOutput{Amount: amount, Address: address, ShardID: utxoPool.ShardID})
} else {
return nil
}
}
}
if len(stateTransaction.TxOutput) != 0 {

@ -33,7 +33,7 @@ type Transaction struct {
// TXOutput is the struct of transaction output in a transaction.
type TXOutput struct {
Amount int
Amount int // TODO: Switch to big int or uint32
Address [20]byte // last 20 bytes of the hash of public key
ShardID uint32 // The Id of the shard where this UTXO belongs
}

@ -87,7 +87,29 @@ func (utxoPool *UTXOPool) VerifyTransactions(transactions []*Transaction) bool {
// VerifyStateBlock verifies if the given state block matches the current utxo pool.
func (utxoPool *UTXOPool) VerifyStateBlock(stateBlock *Block) bool {
// TODO: implement this
accountBalanceInUtxoPool := make(map[[20]byte]int)
for address, txHash2Vout2AmountMap := range utxoPool.UtxoMap {
for _, vout2AmountMap := range txHash2Vout2AmountMap {
for _, amount := range vout2AmountMap {
accountBalanceInUtxoPool[address] = accountBalanceInUtxoPool[address] + amount
}
}
}
for _, transaction := range stateBlock.Transactions {
for _, txOutput := range transaction.TxOutput {
if txOutput.ShardID != utxoPool.ShardID {
return false
}
accountBalanceInUtxoPool[txOutput.Address] = accountBalanceInUtxoPool[txOutput.Address] - txOutput.Amount
}
}
for _, amount := range accountBalanceInUtxoPool {
if amount != 0 {
return false
}
}
return true
}

@ -21,7 +21,7 @@ const (
// The max number of transaction per a block.
MaxNumberOfTransactionsPerBlock = 3000
// The number of blocks allowed before generating state block
NumBlocksBeforeStateBlock = 100
NumBlocksBeforeStateBlock = 10
)
// NodeHandler handles a new incoming connection.

Loading…
Cancel
Save