From 43871c6141652ba0ee5f7f0fe5575e31c47c1806 Mon Sep 17 00:00:00 2001 From: ChronosX88 Date: Thu, 27 May 2021 00:29:37 +0300 Subject: [PATCH] Add validation of tx hash when syncing --- blockchain/sync/sync_mgr.go | 3 +++ blockchain/types/transaction.go | 9 +++++++++ 2 files changed, 12 insertions(+) diff --git a/blockchain/sync/sync_mgr.go b/blockchain/sync/sync_mgr.go index 91c2f8e..2a6cbef 100644 --- a/blockchain/sync/sync_mgr.go +++ b/blockchain/sync/sync_mgr.go @@ -158,6 +158,9 @@ func (sm *syncManager) processReceivedBlock(block types2.Block) error { if !txProofVerified { 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) diff --git a/blockchain/types/transaction.go b/blockchain/types/transaction.go index 3be0e0d..abb4c6d 100644 --- a/blockchain/types/transaction.go +++ b/blockchain/types/transaction.go @@ -1,6 +1,7 @@ package types import ( + "bytes" "encoding/hex" "fmt" "time" @@ -27,3 +28,11 @@ func CreateTransaction(data []byte) *Transaction { 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 +}