From e25a5dfb8a1df6e60dc0280754bd0e06d7ac0362 Mon Sep 17 00:00:00 2001
From: Denis Ermolin <denis.ermolin@matic.network>
Date: Thu, 29 Jul 2021 19:54:10 +0700
Subject: [PATCH] fix: use block hash from the query to get bor tx

---
 core/rawdb/bor_receipt.go  | 20 +++++++++++++++++++-
 eth/bor_api_backend.go     |  5 +++++
 internal/ethapi/backend.go |  1 +
 internal/ethapi/bor_api.go |  2 +-
 les/api_backend.go         |  4 ++++
 5 files changed, 30 insertions(+), 2 deletions(-)

diff --git a/core/rawdb/bor_receipt.go b/core/rawdb/bor_receipt.go
index 676406a76..63c4be12a 100644
--- a/core/rawdb/bor_receipt.go
+++ b/core/rawdb/bor_receipt.go
@@ -146,7 +146,25 @@ func DeleteBorReceipt(db ethdb.KeyValueWriter, hash common.Hash, number uint64)
 	}
 }
 
-// ReadBorTransaction retrieves a specific bor (fake) transaction, along with
+// ReadBorTransactionWithBlockHash retrieves a specific bor (fake) transaction by tx hash and block hash, along with
+// its added positional metadata.
+func ReadBorTransactionWithBlockHash(db ethdb.Reader, txHash common.Hash, blockHash common.Hash) (*types.Transaction, common.Hash, uint64, uint64) {
+	blockNumber := ReadBorTxLookupEntry(db, txHash)
+	if blockNumber == nil {
+		return nil, common.Hash{}, 0, 0
+	}
+
+	body := ReadBody(db, blockHash, *blockNumber)
+	if body == nil {
+		log.Error("Transaction referenced missing", "number", blockNumber, "hash", blockHash)
+		return nil, common.Hash{}, 0, 0
+	}
+
+	// fetch receipt and return it
+	return types.NewBorTransaction(), blockHash, *blockNumber, uint64(len(body.Transactions))
+}
+
+// ReadBorTransaction retrieves a specific bor (fake) transaction by hash, along with
 // its added positional metadata.
 func ReadBorTransaction(db ethdb.Reader, hash common.Hash) (*types.Transaction, common.Hash, uint64, uint64) {
 	blockNumber := ReadBorTxLookupEntry(db, hash)
diff --git a/eth/bor_api_backend.go b/eth/bor_api_backend.go
index 7f17c5b50..a6ff090c3 100644
--- a/eth/bor_api_backend.go
+++ b/eth/bor_api_backend.go
@@ -58,6 +58,11 @@ func (b *EthAPIBackend) GetBorBlockTransaction(ctx context.Context, hash common.
 	return tx, blockHash, blockNumber, index, nil
 }
 
+func (b *EthAPIBackend) GetBorBlockTransactionWithBlockHash(ctx context.Context, txHash common.Hash, blockHash common.Hash) (*types.Transaction, common.Hash, uint64, uint64, error) {
+	tx, blockHash, blockNumber, index := rawdb.ReadBorTransactionWithBlockHash(b.eth.ChainDb(), txHash, blockHash)
+	return tx, blockHash, blockNumber, index, nil
+}
+
 // SubscribeStateSyncEvent subscribes to state sync event
 func (b *EthAPIBackend) SubscribeStateSyncEvent(ch chan<- core.StateSyncEvent) event.Subscription {
 	return b.eth.BlockChain().SubscribeStateSyncEvent(ch)
diff --git a/internal/ethapi/backend.go b/internal/ethapi/backend.go
index 8fe8d2d01..e8c263f71 100644
--- a/internal/ethapi/backend.go
+++ b/internal/ethapi/backend.go
@@ -92,6 +92,7 @@ type Backend interface {
 	GetBorBlockReceipt(ctx context.Context, hash common.Hash) (*types.Receipt, error)
 	GetBorBlockLogs(ctx context.Context, hash common.Hash) ([]*types.Log, error)
 	GetBorBlockTransaction(ctx context.Context, txHash common.Hash) (*types.Transaction, common.Hash, uint64, uint64, error)
+	GetBorBlockTransactionWithBlockHash(ctx context.Context, txHash common.Hash, blockHash common.Hash) (*types.Transaction, common.Hash, uint64, uint64, error)
 
 	ChainConfig() *params.ChainConfig
 	Engine() consensus.Engine
diff --git a/internal/ethapi/bor_api.go b/internal/ethapi/bor_api.go
index 384cc7741..adcfe3902 100644
--- a/internal/ethapi/bor_api.go
+++ b/internal/ethapi/bor_api.go
@@ -27,7 +27,7 @@ func (s *PublicBlockChainAPI) GetBorBlockReceipt(ctx context.Context, hash commo
 func (s *PublicBlockChainAPI) appendRPCMarshalBorTransaction(ctx context.Context, block *types.Block, fields map[string]interface{}, fullTx bool) map[string]interface{} {
 	if block != nil {
 		txHash := types.GetDerivedBorTxHash(types.BorReceiptKey(block.Number().Uint64(), block.Hash()))
-		borTx, blockHash, blockNumber, txIndex, _ := s.b.GetBorBlockTransaction(ctx, txHash)
+		borTx, blockHash, blockNumber, txIndex, _ := s.b.GetBorBlockTransactionWithBlockHash(ctx, txHash, block.Hash())
 		if borTx != nil {
 			formattedTxs := fields["transactions"].([]interface{})
 			if fullTx {
diff --git a/les/api_backend.go b/les/api_backend.go
index e4e97d384..53d925b65 100644
--- a/les/api_backend.go
+++ b/les/api_backend.go
@@ -324,3 +324,7 @@ func (b *LesApiBackend) GetBorBlockLogs(ctx context.Context, hash common.Hash) (
 func (b *LesApiBackend) GetBorBlockTransaction(ctx context.Context, txHash common.Hash) (*types.Transaction, common.Hash, uint64, uint64, error) {
 	return nil, common.Hash{}, 0, 0, errors.New("Not implemented")
 }
+
+func (b *LesApiBackend) GetBorBlockTransactionWithBlockHash(ctx context.Context, txHash common.Hash, blockHash common.Hash) (*types.Transaction, common.Hash, uint64, uint64, error) {
+	return nil, common.Hash{}, 0, 0, errors.New("Not implemented")
+}
-- 
GitLab