From 5a2daff8b418a42f834248e2acd69ec3afb0ee14 Mon Sep 17 00:00:00 2001 From: CocoaHuke Date: Thu, 21 Jun 2018 16:32:09 -0700 Subject: [PATCH] Update light/odr_util.go --- light/odr_util.go | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/light/odr_util.go b/light/odr_util.go index 620af63835..c7276b7818 100644 --- a/light/odr_util.go +++ b/light/odr_util.go @@ -230,3 +230,42 @@ func GetBloomBits(ctx context.Context, odr OdrBackend, bitIdx uint, sectionIdxLi return result, nil } } + +//GetTxStatus requests full nodes to retrieve the status of transaction corresponding to the hash. +//Message of the LES/2 protocol version (LPV2) +func GetTxStatus(ctx context.Context, odr OdrBackend, txHash common.Hash) (core.TxStatus, error) { + + // Return "Included" status if query success from database + bHash, bIndex, _ := rawdb.ReadTxLookupEntry(odr.Database(), txHash) + if bHash != (common.Hash{}) { + return core.TxStatusIncluded, nil + } + + // Send on-demand request message + r := &TxStatusRequest{TxHash: txHash} + if err := odr.Retrieve(ctx, r); err != nil { + return core.TxStatusUnknown, err + } + + if r.TxStatus != core.TxStatusIncluded { + return r.TxStatus, nil + } + + // Another on-demand request message for retrieve block body, verify block body internally + bHash, bIndex, _ = r.TxEntry.BlockHash, r.TxEntry.BlockIndex, r.TxEntry.Index + bBody, err := GetBody(ctx, odr, bHash, bIndex) + if err != nil { + return r.TxStatus, err + } + + // Try to find matched transaction by given hash, verifying transaction by given hash whether exist + for _, tx := range bBody.Transactions { + if tx.Hash() == r.TxHash { + // Write into database if hash matched, link given hash to verified block hash and index + r.StoreResult(odr.Database()) + break + } + } + + return r.TxStatus, nil +}