Add validation of tx hash when syncing

This commit is contained in:
ChronosX88 2021-05-27 00:29:37 +03:00
parent b51a5ffe75
commit 43871c6141
Signed by: ChronosXYZ
GPG Key ID: 085A69A82C8C511A
2 changed files with 12 additions and 0 deletions

View File

@ -158,6 +158,9 @@ func (sm *syncManager) processReceivedBlock(block types2.Block) error {
if !txProofVerified { if !txProofVerified {
return fmt.Errorf("transaction doesn't present in block hash merkle tree") return fmt.Errorf("transaction doesn't present in block hash merkle tree")
} }
if !tx.ValidateHash() {
return fmt.Errorf("transaction hash is invalid")
}
} }
err = sm.blockPool.StoreBlock(&block) err = sm.blockPool.StoreBlock(&block)

View File

@ -1,6 +1,7 @@
package types package types
import ( import (
"bytes"
"encoding/hex" "encoding/hex"
"fmt" "fmt"
"time" "time"
@ -27,3 +28,11 @@ func CreateTransaction(data []byte) *Transaction {
Data: data, Data: data,
} }
} }
func (tx *Transaction) ValidateHash() bool {
h := crypto.Keccak256([]byte(fmt.Sprintf("%d_%s", tx.Timestamp.Unix(), tx.Hash)))
if bytes.Compare(h, tx.Hash) != 0 {
return false
}
return true
}