From 8e055e371433df574503b0f7685635b0f7ef318b Mon Sep 17 00:00:00 2001 From: sunhongtao Date: Tue, 14 Nov 2023 14:55:23 +0800 Subject: [PATCH] fix: tx.time is empty with tx task for evm --- collect/service/cmd/chain/bnb/service.go | 14 ++++++++++---- collect/service/cmd/chain/ether/service.go | 14 ++++++++++---- collect/service/cmd/chain/ether/service_test.go | 6 +++++- collect/service/cmd/chain/polygonpos/service.go | 14 ++++++++++---- collect/type.go | 1 + 5 files changed, 36 insertions(+), 13 deletions(-) diff --git a/collect/service/cmd/chain/bnb/service.go b/collect/service/cmd/chain/bnb/service.go index 334d1aa..2fdabaf 100644 --- a/collect/service/cmd/chain/bnb/service.go +++ b/collect/service/cmd/chain/bnb/service.go @@ -87,7 +87,7 @@ func (s *Service) GetBlockByHash(blockHash string, eLog *logrus.Entry, flag bool return nil, nil } - r := &collect.BlockInterface{BlockHash: block.BlockHash, BlockNumber: block.BlockNumber, Block: block} + r := &collect.BlockInterface{BlockHash: block.BlockHash, BlockNumber: block.BlockNumber, Block: block, BlockTime: block.BlockTime} if !flag { //仅区块,不涉及交易 return r, nil } @@ -118,7 +118,6 @@ func (s *Service) GetBlockByHash(blockHash string, eLog *logrus.Entry, flag bool txs = append(txs, t) } } - //r := &service.BlockInterface{BlockHash: block.BlockHash, BlockNumber: block.BlockNumber, Block: block} return r, txs } @@ -158,7 +157,7 @@ func (s *Service) GetBlockByNumber(blockNumber string, eLog *logrus.Entry, flag return nil, nil } - r := &collect.BlockInterface{BlockHash: block.BlockHash, BlockNumber: block.BlockNumber, Block: block} + r := &collect.BlockInterface{BlockHash: block.BlockHash, BlockNumber: block.BlockNumber, Block: block, BlockTime: block.BlockTime} if !flag { //仅区块数据,不涉及交易 return r, nil } @@ -191,7 +190,6 @@ func (s *Service) GetBlockByNumber(blockNumber string, eLog *logrus.Entry, flag eLog.Warnf("the tx is ignored,hash=%v", tx.TxHash) } } - //r := &service.BlockInterface{BlockHash: block.BlockHash, BlockNumber: block.BlockNumber, Block: block} return r, txs } @@ -220,6 +218,14 @@ func (s *Service) GetTx(txHash string, eLog *logrus.Entry) *collect.TxInterface return nil } + //fix tx.time + if len(tx.TxTime) < 1 { + b, _ := s.GetBlockByNumber(tx.BlockNumber, eLog, false) + if b != nil { + tx.TxTime = b.BlockTime + } + } + receipt, err := s.GetReceipt(tx.TxHash, eLog) if err != nil { eLog.Errorf("GetTx|BlockChainCode=%v,err=%v,txHash=%v", s.chain.BlockChainCode, err.Error(), tx.TxHash) diff --git a/collect/service/cmd/chain/ether/service.go b/collect/service/cmd/chain/ether/service.go index 19dadf5..65fea83 100644 --- a/collect/service/cmd/chain/ether/service.go +++ b/collect/service/cmd/chain/ether/service.go @@ -87,7 +87,7 @@ func (s *Service) GetBlockByHash(blockHash string, eLog *logrus.Entry, flag bool return nil, nil } - r := &collect.BlockInterface{BlockHash: block.BlockHash, BlockNumber: block.BlockNumber, Block: block} + r := &collect.BlockInterface{BlockHash: block.BlockHash, BlockNumber: block.BlockNumber, Block: block, BlockTime: block.BlockTime} if !flag { //仅区块,不涉及交易 return r, nil } @@ -118,7 +118,6 @@ func (s *Service) GetBlockByHash(blockHash string, eLog *logrus.Entry, flag bool txs = append(txs, t) } } - //r := &service.BlockInterface{BlockHash: block.BlockHash, BlockNumber: block.BlockNumber, Block: block} return r, txs } @@ -158,7 +157,7 @@ func (s *Service) GetBlockByNumber(blockNumber string, eLog *logrus.Entry, flag return nil, nil } - r := &collect.BlockInterface{BlockHash: block.BlockHash, BlockNumber: block.BlockNumber, Block: block} + r := &collect.BlockInterface{BlockHash: block.BlockHash, BlockNumber: block.BlockNumber, Block: block, BlockTime: block.BlockTime} if !flag { //仅区块数据,不涉及交易 return r, nil } @@ -191,7 +190,6 @@ func (s *Service) GetBlockByNumber(blockNumber string, eLog *logrus.Entry, flag eLog.Warnf("the tx is ignored,hash=%v", tx.TxHash) } } - //r := &service.BlockInterface{BlockHash: block.BlockHash, BlockNumber: block.BlockNumber, Block: block} return r, txs } @@ -220,6 +218,14 @@ func (s *Service) GetTx(txHash string, eLog *logrus.Entry) *collect.TxInterface return nil } + //fix tx.time + if len(tx.TxTime) < 1 { + b, _ := s.GetBlockByNumber(tx.BlockNumber, eLog, false) + if b != nil { + tx.TxTime = b.BlockTime + } + } + receipt, err := s.GetReceipt(tx.TxHash, eLog) if err != nil { eLog.Errorf("GetTx|BlockChainCode=%v,err=%v,txHash=%v", s.chain.BlockChainCode, err.Error(), tx.TxHash) diff --git a/collect/service/cmd/chain/ether/service_test.go b/collect/service/cmd/chain/ether/service_test.go index bd22f71..5b1c40e 100644 --- a/collect/service/cmd/chain/ether/service_test.go +++ b/collect/service/cmd/chain/ether/service_test.go @@ -26,7 +26,11 @@ func TestService_GetBlockByNumber(t *testing.T) { func TestService_GetTx(t *testing.T) { s, _, x := Init() tx := s.GetTx("0xb7959ab3ed0f8b424f897ba8ec28168358079be0030a9337d20252f7d4c18cfd", x.WithFields(logrus.Fields{})) - t.Log(tx) + if tx == nil { + t.Error("fail") + } else { + t.Logf("%+v", tx.Tx) + } } func TestService_GetReceipt(t *testing.T) { diff --git a/collect/service/cmd/chain/polygonpos/service.go b/collect/service/cmd/chain/polygonpos/service.go index 5cb7f37..5f1e763 100644 --- a/collect/service/cmd/chain/polygonpos/service.go +++ b/collect/service/cmd/chain/polygonpos/service.go @@ -87,7 +87,7 @@ func (s *Service) GetBlockByHash(blockHash string, eLog *logrus.Entry, flag bool return nil, nil } - r := &collect.BlockInterface{BlockHash: block.BlockHash, BlockNumber: block.BlockNumber, Block: block} + r := &collect.BlockInterface{BlockHash: block.BlockHash, BlockNumber: block.BlockNumber, Block: block, BlockTime: block.BlockTime} if !flag { //仅区块,不涉及交易 return r, nil } @@ -118,7 +118,6 @@ func (s *Service) GetBlockByHash(blockHash string, eLog *logrus.Entry, flag bool txs = append(txs, t) } } - //r := &service.BlockInterface{BlockHash: block.BlockHash, BlockNumber: block.BlockNumber, Block: block} return r, txs } @@ -158,7 +157,7 @@ func (s *Service) GetBlockByNumber(blockNumber string, eLog *logrus.Entry, flag return nil, nil } - r := &collect.BlockInterface{BlockHash: block.BlockHash, BlockNumber: block.BlockNumber, Block: block} + r := &collect.BlockInterface{BlockHash: block.BlockHash, BlockNumber: block.BlockNumber, Block: block, BlockTime: block.BlockTime} if !flag { //仅区块数据,不涉及交易 return r, nil } @@ -191,7 +190,6 @@ func (s *Service) GetBlockByNumber(blockNumber string, eLog *logrus.Entry, flag eLog.Warnf("the tx is ignored,hash=%v", tx.TxHash) } } - //r := &service.BlockInterface{BlockHash: block.BlockHash, BlockNumber: block.BlockNumber, Block: block} return r, txs } @@ -220,6 +218,14 @@ func (s *Service) GetTx(txHash string, eLog *logrus.Entry) *collect.TxInterface return nil } + //fix tx.time + if len(tx.TxTime) < 1 { + b, _ := s.GetBlockByNumber(tx.BlockNumber, eLog, false) + if b != nil { + tx.TxTime = b.BlockTime + } + } + receipt, err := s.GetReceipt(tx.TxHash, eLog) if err != nil { eLog.Errorf("GetTx|BlockChainCode=%v,err=%v,txHash=%v", s.chain.BlockChainCode, err.Error(), tx.TxHash) diff --git a/collect/type.go b/collect/type.go index f1e538f..e28d8f3 100644 --- a/collect/type.go +++ b/collect/type.go @@ -291,6 +291,7 @@ type Block struct { type BlockInterface struct { BlockHash string `json:"hash" gorm:"column:hash"` + BlockTime string `json:"blockTime"` BlockNumber string `json:"number" gorm:"column:block_number"` Block interface{} `json:"block"` }