Skip to content

Commit

Permalink
[api] Move the subscribers of coreService to serverV2 (#3073)
Browse files Browse the repository at this point in the history
* move the subscribers under api to serverV2
  • Loading branch information
Liuhaai authored Mar 24, 2022
1 parent 42131c3 commit faf3c97
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 19 deletions.
13 changes: 7 additions & 6 deletions api/coreservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ type (
TipHeight() uint64
// PendingNonce returns the pending nonce of an account
PendingNonce(address.Address) (uint64, error)
// ReceiveBlock broadcasts the block to api subscribers
ReceiveBlock(blk *block.Block) error
}

// coreService implements the CoreService interface
Expand Down Expand Up @@ -783,12 +785,6 @@ func (core *coreService) TipHeight() uint64 {

// Start starts the API server
func (core *coreService) Start(_ context.Context) error {
if err := core.bc.AddSubscriber(core.readCache); err != nil {
return errors.Wrap(err, "failed to add readCache")
}
if err := core.bc.AddSubscriber(core.chainListener); err != nil {
return errors.Wrap(err, "failed to add chainListener")
}
if err := core.chainListener.Start(); err != nil {
return errors.Wrap(err, "failed to start blockchain listener")
}
Expand Down Expand Up @@ -1618,6 +1614,11 @@ func (core *coreService) ReadContractStorage(ctx context.Context, addr address.A
return core.sf.ReadContractStorage(ctx, addr, key)
}

func (core *coreService) ReceiveBlock(blk *block.Block) error {
core.readCache.Clear()
return core.chainListener.ReceiveBlock(blk)
}

func (core *coreService) SimulateExecution(ctx context.Context, addr address.Address, exec *action.Execution) ([]byte, *action.Receipt, error) {
state, err := accountutil.AccountState(core.sf, addr)
if err != nil {
Expand Down
13 changes: 0 additions & 13 deletions api/read_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/iotexproject/go-pkgs/hash"
"go.uber.org/zap"

"github.com/iotexproject/iotex-core/blockchain/block"
"github.com/iotexproject/iotex-core/pkg/log"
)

Expand Down Expand Up @@ -64,15 +63,3 @@ func (rc *ReadCache) Put(key hash.Hash160, value []byte) {
func (rc *ReadCache) Clear() {
rc.c.Reset()
}

// ReceiveBlock receives the new block
func (rc *ReadCache) ReceiveBlock(*block.Block) error {
// invalidate the cache at every new block
rc.c.Reset()
return nil
}

// Exit implements the Responder interface
func (rc *ReadCache) Exit() {
rc.c.Reset()
}
6 changes: 6 additions & 0 deletions api/serverV2.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/iotexproject/iotex-core/action/protocol"
"github.com/iotexproject/iotex-core/actpool"
"github.com/iotexproject/iotex-core/blockchain"
"github.com/iotexproject/iotex-core/blockchain/block"
"github.com/iotexproject/iotex-core/blockchain/blockdao"
"github.com/iotexproject/iotex-core/blockindex"
"github.com/iotexproject/iotex-core/blocksync"
Expand Down Expand Up @@ -128,3 +129,8 @@ func (svr *ServerV2) Stop(ctx context.Context) error {
}
return nil
}

// ReceiveBlock receives the new block
func (svr *ServerV2) ReceiveBlock(blk *block.Block) error {
return svr.core.ReceiveBlock(blk)
}
3 changes: 3 additions & 0 deletions chainservice/chainservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,9 @@ func New(
if err != nil {
return nil, err
}
if err := chain.AddSubscriber(apiSvr); err != nil {
log.L().Warn("Failed to add subscriber: api server.", zap.Error(err))
}
accountProtocol := account.NewProtocol(rewarding.DepositGas)
if accountProtocol != nil {
if err = accountProtocol.Register(registry); err != nil {
Expand Down
53 changes: 53 additions & 0 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.

0 comments on commit faf3c97

Please sign in to comment.