move index error into core module

This commit is contained in:
maskpp 2024-04-23 10:48:46 +08:00
parent acd1eaae2c
commit 234161e42b
5 changed files with 28 additions and 37 deletions

View file

@ -277,12 +277,12 @@ func (bc *BlockChain) GetTransactionLookup(hash common.Hash) (*rawdb.LegacyTxLoo
if tx == nil { if tx == nil {
progress, err := bc.TxIndexProgress() progress, err := bc.TxIndexProgress()
if err != nil { if err != nil {
return nil, nil, nil return nil, nil, err
} }
// The transaction indexing is not finished yet, returning an // The transaction indexing is not finished yet, returning an
// error to explicitly indicate it. // error to explicitly indicate it.
if !progress.Done() { if !progress.Done() {
return nil, nil, errors.New("transaction indexing still in progress") return nil, nil, NewTxIndexingError()
} }
// The transaction is already indexed, the transaction is either // The transaction is already indexed, the transaction is either
// not existent or not in the range of index, returning null. // not existent or not in the range of index, returning null.

View file

@ -108,3 +108,24 @@ var (
// ErrBlobTxCreate is returned if a blob transaction has no explicit to field. // ErrBlobTxCreate is returned if a blob transaction has no explicit to field.
ErrBlobTxCreate = errors.New("blob transaction of type create") ErrBlobTxCreate = errors.New("blob transaction of type create")
) )
// TxIndexingError is an API error that indicates the transaction indexing is not
// fully finished yet with JSON error code and a binary data blob.
type TxIndexingError struct{}
// NewTxIndexingError creates a TxIndexingError instance.
func NewTxIndexingError() *TxIndexingError { return &TxIndexingError{} }
// Error implement error interface, returning the error message.
func (e *TxIndexingError) Error() string {
return "transaction indexing is in progress"
}
// ErrorCode returns the JSON error code for a revert.
// See: https://github.com/ethereum/wiki/wiki/JSON-RPC-Error-Codes-Improvement-Proposal
func (e *TxIndexingError) ErrorCode() int {
return -32000 // to be decided
}
// ErrorData returns the hex encoded revert reason.
func (e *TxIndexingError) ErrorData() interface{} { return "transaction indexing is in progress" }

View file

@ -830,7 +830,7 @@ func containsTx(block *types.Block, hash common.Hash) bool {
func (api *API) TraceTransaction(ctx context.Context, hash common.Hash, config *TraceConfig) (interface{}, error) { func (api *API) TraceTransaction(ctx context.Context, hash common.Hash, config *TraceConfig) (interface{}, error) {
found, _, blockHash, blockNumber, index, err := api.backend.GetTransaction(ctx, hash) found, _, blockHash, blockNumber, index, err := api.backend.GetTransaction(ctx, hash)
if err != nil { if err != nil {
return nil, ethapi.NewTxIndexingError() return nil, err
} }
// Only mined txes are supported // Only mined txes are supported
if !found { if !found {

View file

@ -1642,10 +1642,7 @@ func (s *TransactionAPI) GetTransactionByHash(ctx context.Context, hash common.H
if tx := s.b.GetPoolTransaction(hash); tx != nil { if tx := s.b.GetPoolTransaction(hash); tx != nil {
return NewRPCPendingTransaction(tx, s.b.CurrentHeader(), s.b.ChainConfig()), nil return NewRPCPendingTransaction(tx, s.b.CurrentHeader(), s.b.ChainConfig()), nil
} }
if err == nil { return nil, err
return nil, nil
}
return nil, NewTxIndexingError()
} }
header, err := s.b.HeaderByHash(ctx, blockHash) header, err := s.b.HeaderByHash(ctx, blockHash)
if err != nil { if err != nil {
@ -1662,10 +1659,7 @@ func (s *TransactionAPI) GetRawTransactionByHash(ctx context.Context, hash commo
if tx = s.b.GetPoolTransaction(hash); tx != nil { if tx = s.b.GetPoolTransaction(hash); tx != nil {
return tx.MarshalBinary() return tx.MarshalBinary()
} }
if err == nil { return nil, err
return nil, nil
}
return nil, NewTxIndexingError()
} }
return tx.MarshalBinary() return tx.MarshalBinary()
} }
@ -1674,7 +1668,7 @@ func (s *TransactionAPI) GetRawTransactionByHash(ctx context.Context, hash commo
func (s *TransactionAPI) GetTransactionReceipt(ctx context.Context, hash common.Hash) (map[string]interface{}, error) { func (s *TransactionAPI) GetTransactionReceipt(ctx context.Context, hash common.Hash) (map[string]interface{}, error) {
found, tx, blockHash, blockNumber, index, err := s.b.GetTransaction(ctx, hash) found, tx, blockHash, blockNumber, index, err := s.b.GetTransaction(ctx, hash)
if err != nil { if err != nil {
return nil, NewTxIndexingError() // transaction is not fully indexed return nil, err
} }
if !found { if !found {
return nil, nil // transaction is not existent or reachable return nil, nil // transaction is not existent or reachable
@ -2082,10 +2076,7 @@ func (s *DebugAPI) GetRawTransaction(ctx context.Context, hash common.Hash) (hex
if tx = s.b.GetPoolTransaction(hash); tx != nil { if tx = s.b.GetPoolTransaction(hash); tx != nil {
return tx.MarshalBinary() return tx.MarshalBinary()
} }
if err == nil { return nil, err
return nil, nil
}
return nil, NewTxIndexingError()
} }
return tx.MarshalBinary() return tx.MarshalBinary()
} }

View file

@ -55,24 +55,3 @@ func newRevertError(revert []byte) *revertError {
reason: hexutil.Encode(revert), reason: hexutil.Encode(revert),
} }
} }
// TxIndexingError is an API error that indicates the transaction indexing is not
// fully finished yet with JSON error code and a binary data blob.
type TxIndexingError struct{}
// NewTxIndexingError creates a TxIndexingError instance.
func NewTxIndexingError() *TxIndexingError { return &TxIndexingError{} }
// Error implement error interface, returning the error message.
func (e *TxIndexingError) Error() string {
return "transaction indexing is in progress"
}
// ErrorCode returns the JSON error code for a revert.
// See: https://github.com/ethereum/wiki/wiki/JSON-RPC-Error-Codes-Improvement-Proposal
func (e *TxIndexingError) ErrorCode() int {
return -32000 // to be decided
}
// ErrorData returns the hex encoded revert reason.
func (e *TxIndexingError) ErrorData() interface{} { return "transaction indexing is in progress" }