Skip to content

Commit

Permalink
[api] Remove ReceiptByAction() in coreservice.go (#3347)
Browse files Browse the repository at this point in the history
* [api] remove ReceiptByAction() in coreservice.go

* fix commit

* rename GetBlockHashByActionHash

* get block hash by block height

* add getBlockHashByActionHash

* fix commit
  • Loading branch information
LuckyPigeon authored May 26, 2022
1 parent bf98850 commit b1a3c88
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 22 deletions.
20 changes: 18 additions & 2 deletions api/coreservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,6 @@ type (
ServerMeta() (packageVersion string, packageCommitID string, gitStatus string, goVersion string, buildTime string)
// SendAction is the API to send an action to blockchain.
SendAction(ctx context.Context, in *iotextypes.Action) (string, error)
// ReceiptByAction gets receipt with corresponding action hash
ReceiptByAction(actHash hash.Hash256) (*action.Receipt, string, error)
// ReadContract reads the state in a contract address specified by the slot
ReadContract(ctx context.Context, callerAddr address.Address, sc *action.Execution) (string, *iotextypes.Receipt, error)
// ReadState reads state on blockchain
Expand Down Expand Up @@ -147,6 +145,10 @@ type (
PendingNonce(address.Address) (uint64, error)
// ReceiveBlock broadcasts the block to api subscribers
ReceiveBlock(blk *block.Block) error
// BlockHashByActionHash returns block hash by action hash
BlockHashByActionHash(h hash.Hash256) (hash.Hash256, error)
// BlockHashByBlockHeight returns block hash by block height
BlockHashByBlockHeight(blkHeight uint64) (hash.Hash256, error)
}

// coreService implements the CoreService interface
Expand Down Expand Up @@ -1004,6 +1006,20 @@ func (core *coreService) getBlockHashByActionHash(h hash.Hash256) (hash.Hash256,
return core.dao.GetBlockHash(actIndex.BlockHeight())
}

// BlockHashByActionHash returns block hash by action hash
func (core *coreService) BlockHashByActionHash(h hash.Hash256) (hash.Hash256, error) {
actIndex, err := core.indexer.GetActionIndex(h[:])
if err != nil {
return hash.ZeroHash256, err
}
return core.dao.GetBlockHash(actIndex.BlockHeight())
}

// BlockHashByBlockHeight returns block hash by block height
func (core *coreService) BlockHashByBlockHeight(blkHeight uint64) (hash.Hash256, error) {
return core.dao.GetBlockHash(blkHeight)
}

// ActionByActionHash returns action by action hash
func (core *coreService) ActionByActionHash(h hash.Hash256) (action.SealedEnvelope, hash.Hash256, uint64, uint32, error) {
if err := core.checkActionIndex(); err != nil {
Expand Down
18 changes: 15 additions & 3 deletions api/grpcserver.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
// Copyright (c) 2022 IoTeX Foundation
// This is an alpha (internal) release and is not suitable for production. This source code is provided 'as is' and no
// warranties are given as to title or non-infringement, merchantability or fitness for purpose and, to the extent
// permitted by law, all liability for your use of the code is disclaimed. This source code is governed by Apache
// License 2.0 that can be found in the LICENSE file.

package api

import (
"context"
"encoding/hex"
"fmt"
"math/big"
"net"
Expand Down Expand Up @@ -228,14 +235,19 @@ func (svr *GRPCServer) GetReceiptByAction(ctx context.Context, in *iotexapi.GetR
if err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}
receipt, blkHash, err := svr.coreService.ReceiptByAction(actHash)
receipt, err := svr.coreService.ReceiptByActionHash(actHash)
if err != nil {
return nil, err
return nil, status.Error(codes.NotFound, err.Error())
}
blkHash, err := svr.coreService.BlockHashByBlockHeight(receipt.BlockHeight)
if err != nil {
return nil, status.Error(codes.NotFound, err.Error())
}

return &iotexapi.GetReceiptByActionResponse{
ReceiptInfo: &iotexapi.ReceiptInfo{
Receipt: receipt.ConvertToReceiptPb(),
BlkHash: blkHash,
BlkHash: hex.EncodeToString(blkHash[:]),
},
}, nil
}
Expand Down
46 changes: 30 additions & 16 deletions test/mock/mock_apicoreservice/mock_apicoreservice.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tools/util/injectorutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -848,7 +848,7 @@ func GetActionByActionHash(api api.CoreService, actHash hash.Hash256) (*iotexapi

// GetReceiptByAction acquires receipt by calling coreService
func GetReceiptByAction(api api.CoreService, actHash hash.Hash256) (*iotextypes.Receipt, error) {
receipt, _, err := api.ReceiptByAction(actHash)
receipt, err := api.ReceiptByActionHash(actHash)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit b1a3c88

Please sign in to comment.