github.com/klaytn/klaytn@v1.12.1/node/sc/local_backend.go (about)

     1  // Modifications Copyright 2019 The klaytn Authors
     2  // Copyright 2015 The go-ethereum Authors
     3  // This file is part of the go-ethereum library.
     4  //
     5  // The go-ethereum library is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Lesser General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // The go-ethereum library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  // GNU Lesser General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Lesser General Public License
    16  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    17  //
    18  // This file is derived from accounts/abi/bind/backends/simulated.go (2018/06/04).
    19  // Modified and improved for the klaytn development.
    20  
    21  package sc
    22  
    23  import (
    24  	"context"
    25  	"fmt"
    26  
    27  	"github.com/klaytn/klaytn/blockchain"
    28  	"github.com/klaytn/klaytn/blockchain/bloombits"
    29  	"github.com/klaytn/klaytn/blockchain/types"
    30  	"github.com/klaytn/klaytn/common"
    31  	"github.com/klaytn/klaytn/event"
    32  	"github.com/klaytn/klaytn/networks/rpc"
    33  	"github.com/klaytn/klaytn/params"
    34  	"github.com/klaytn/klaytn/storage/database"
    35  )
    36  
    37  func checkCtx(ctx context.Context) error {
    38  	select {
    39  	case <-ctx.Done():
    40  		return ctx.Err()
    41  	default:
    42  		return nil
    43  	}
    44  }
    45  
    46  type filterLocalBackend struct {
    47  	subbridge *SubBridge
    48  }
    49  
    50  func (fb *filterLocalBackend) ChainDB() database.DBManager {
    51  	// TODO-Klaytn consider chain's chainDB instead of bridge's chainDB currently.
    52  	return fb.subbridge.chainDB
    53  }
    54  
    55  func (fb *filterLocalBackend) EventMux() *event.TypeMux {
    56  	// TODO-Klaytn consider chain's eventMux instead of bridge's eventMux currently.
    57  	return fb.subbridge.EventMux()
    58  }
    59  
    60  func (fb *filterLocalBackend) HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error) {
    61  	if header := fb.subbridge.blockchain.GetHeaderByHash(hash); header != nil {
    62  		return header, nil
    63  	}
    64  	return nil, fmt.Errorf("the header does not exist (hash: %d)", hash)
    65  }
    66  
    67  func (fb *filterLocalBackend) HeaderByNumber(ctx context.Context, block rpc.BlockNumber) (*types.Header, error) {
    68  	if err := checkCtx(ctx); err != nil {
    69  		return nil, err
    70  	}
    71  	// TODO-Klaytn consider pendingblock instead of latest block
    72  	if block == rpc.LatestBlockNumber {
    73  		return fb.subbridge.blockchain.CurrentHeader(), nil
    74  	}
    75  	return fb.subbridge.blockchain.GetHeaderByNumber(uint64(block.Int64())), nil
    76  }
    77  
    78  func (fb *filterLocalBackend) GetBlockReceipts(ctx context.Context, hash common.Hash) types.Receipts {
    79  	if err := checkCtx(ctx); err != nil {
    80  		return nil
    81  	}
    82  	return fb.subbridge.blockchain.GetReceiptsByBlockHash(hash)
    83  }
    84  
    85  func (fb *filterLocalBackend) GetLogs(ctx context.Context, hash common.Hash) ([][]*types.Log, error) {
    86  	if err := checkCtx(ctx); err != nil {
    87  		return nil, err
    88  	}
    89  	return fb.subbridge.blockchain.GetLogsByHash(hash), nil
    90  }
    91  
    92  func (fb *filterLocalBackend) SubscribeNewTxsEvent(ch chan<- blockchain.NewTxsEvent) event.Subscription {
    93  	return fb.subbridge.txPool.SubscribeNewTxsEvent(ch)
    94  }
    95  
    96  func (fb *filterLocalBackend) SubscribeChainEvent(ch chan<- blockchain.ChainEvent) event.Subscription {
    97  	return fb.subbridge.blockchain.SubscribeChainEvent(ch)
    98  }
    99  
   100  func (fb *filterLocalBackend) SubscribeRemovedLogsEvent(ch chan<- blockchain.RemovedLogsEvent) event.Subscription {
   101  	return fb.subbridge.blockchain.SubscribeRemovedLogsEvent(ch)
   102  }
   103  
   104  func (fb *filterLocalBackend) SubscribeLogsEvent(ch chan<- []*types.Log) event.Subscription {
   105  	return fb.subbridge.blockchain.SubscribeLogsEvent(ch)
   106  }
   107  
   108  func (fb *filterLocalBackend) BloomStatus() (uint64, uint64) {
   109  	// TODO-Klaytn consider this number of sections.
   110  	// BloomBitsBlocks (const : 4096), the number of processed sections maintained by the chain indexer
   111  	return 4096, 0
   112  }
   113  
   114  func (fb *filterLocalBackend) ServiceFilter(_dummyCtx context.Context, session *bloombits.MatcherSession) {
   115  	// TODO-Klaytn this method should implmentation to support indexed tag in solidity
   116  	//for i := 0; i < bloomFilterThreads; i++ {
   117  	//	go session.Multiplex(bloomRetrievalBatch, bloomRetrievalWait, backend.bloomRequests)
   118  	//}
   119  }
   120  
   121  func (fb *filterLocalBackend) ChainConfig() *params.ChainConfig {
   122  	return fb.subbridge.blockchain.Config()
   123  }