replace block hash by transaction hash

pull/16/head
Minh Doan 7 years ago
parent 26f31e5fbe
commit 39b9a36feb
  1. 26
      blockchain/block.go
  2. 2
      consensus/consensus_validator.go
  3. 1
      p2p/peer.go
  4. 7
      utils/utils.go
  5. 11
      utils/utils_test.go

@ -5,6 +5,7 @@ import (
"crypto/sha256" "crypto/sha256"
"encoding/gob" "encoding/gob"
"fmt" "fmt"
"harmony-benchmark/utils"
"log" "log"
"time" "time"
) )
@ -14,11 +15,11 @@ type Block struct {
// Header // Header
Timestamp int64 Timestamp int64
PrevBlockHash [32]byte PrevBlockHash [32]byte
Hash [32]byte
NumTransactions int32 NumTransactions int32
TransactionIds [][32]byte TransactionIds [][32]byte
Transactions []*Transaction // Transactions Transactions []*Transaction // Transactions
ShardId uint32 ShardId uint32
Hash [32]byte
// Signature... // Signature...
} }
@ -67,6 +68,25 @@ func (b *Block) HashTransactions() []byte {
return txHash[:] return txHash[:]
} }
// CalculateBlockHash returns a hash of the block
func (b *Block) CalculateBlockHash() []byte {
var hashes [][]byte
var blockHash [32]byte
hashes = append(hashes, utils.ConvertFixedDataIntoByteArray(b.Timestamp))
hashes = append(hashes, b.PrevBlockHash[:])
for _, id := range b.TransactionIds {
hashes = append(hashes, id[:])
}
for _, tx := range b.Transactions {
hashes = append(hashes, tx.ID[:])
}
hashes = append(hashes, utils.ConvertFixedDataIntoByteArray(b.ShardId))
blockHash = sha256.Sum256(bytes.Join(hashes, []byte{}))
return blockHash[:]
}
// NewBlock creates and returns a neew block. // NewBlock creates and returns a neew block.
func NewBlock(transactions []*Transaction, prevBlockHash [32]byte, shardId uint32) *Block { func NewBlock(transactions []*Transaction, prevBlockHash [32]byte, shardId uint32) *Block {
numTxs := int32(len(transactions)) numTxs := int32(len(transactions))
@ -75,8 +95,8 @@ func NewBlock(transactions []*Transaction, prevBlockHash [32]byte, shardId uint3
for _, tx := range transactions { for _, tx := range transactions {
txIds = append(txIds, tx.ID) txIds = append(txIds, tx.ID)
} }
block := &Block{time.Now().Unix(), prevBlockHash, [32]byte{}, numTxs, txIds, transactions, shardId} block := &Block{time.Now().Unix(), prevBlockHash, numTxs, txIds, transactions, shardId, [32]byte{}}
copy(block.Hash[:], block.HashTransactions()[:]) // TODO(Minh): the blockhash should be a hash of everything in the block copy(block.Hash[:], block.CalculateBlockHash()[:])
return block return block
} }

@ -106,7 +106,7 @@ func (consensus *Consensus) processAnnounceMessage(payload []byte) {
} }
// check block hash // check block hash
if bytes.Compare(blockHash[:], blockHeaderObj.HashTransactions()[:]) != 0 || bytes.Compare(blockHeaderObj.Hash[:], blockHeaderObj.HashTransactions()[:]) != 0 { if bytes.Compare(blockHash[:], blockHeaderObj.CalculateBlockHash()[:]) != 0 || bytes.Compare(blockHeaderObj.Hash[:], blockHeaderObj.CalculateBlockHash()[:]) != 0 {
consensus.Log.Warn("Block hash doesn't match", "consensus", consensus) consensus.Log.Warn("Block hash doesn't match", "consensus", consensus)
return return
} }

@ -28,6 +28,7 @@ func BroadcastMessage(peers []Peer, msg []byte) {
// Construct broadcast p2p message // Construct broadcast p2p message
content := ConstructP2pMessage(byte(17), msg) content := ConstructP2pMessage(byte(17), msg)
// TODO(rj): Can optimize by calling goroutine.
for _, peer := range peers { for _, peer := range peers {
send(peer.Ip, peer.Port, content) send(peer.Ip, peer.Port, content)
} }

@ -8,14 +8,13 @@ import (
"strings" "strings"
) )
// IntToHex converts an int64 to a byte array // ConvertFixedDataIntoByteArray converts an empty interface data to a byte array
func IntToHex(num int64) []byte { func ConvertFixedDataIntoByteArray(data interface{}) []byte {
buff := new(bytes.Buffer) buff := new(bytes.Buffer)
err := binary.Write(buff, binary.BigEndian, num) err := binary.Write(buff, binary.BigEndian, data)
if err != nil { if err != nil {
log.Panic(err) log.Panic(err)
} }
return buff.Bytes() return buff.Bytes()
} }

@ -15,3 +15,14 @@ func TestConvertIntoInts(t *testing.T) {
} }
} }
} }
func TestConvertFixedDataIntoByteArray(t *testing.T) {
res := ConvertFixedDataIntoByteArray(int16(3))
if len(res) != 2 {
t.Errorf("Conversion incorrect.")
}
res = ConvertFixedDataIntoByteArray(int32(3))
if len(res) != 4 {
t.Errorf("Conversion incorrect.")
}
}

Loading…
Cancel
Save