github.com/klaytn/klaytn@v1.10.2/datasync/chaindatafetcher/api.go (about) 1 // Copyright 2020 The klaytn Authors 2 // This file is part of the klaytn library. 3 // 4 // The klaytn library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The klaytn library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the klaytn library. If not, see <http://www.gnu.org/licenses/>. 16 17 package chaindatafetcher 18 19 import ( 20 "errors" 21 "sync/atomic" 22 23 "github.com/klaytn/klaytn/datasync/chaindatafetcher/types" 24 ) 25 26 type PublicChainDataFetcherAPI struct { 27 f *ChainDataFetcher 28 } 29 30 func NewPublicChainDataFetcherAPI(f *ChainDataFetcher) *PublicChainDataFetcherAPI { 31 return &PublicChainDataFetcherAPI{f: f} 32 } 33 34 func (api *PublicChainDataFetcherAPI) StartFetching() error { 35 return api.f.startFetching() 36 } 37 38 func (api *PublicChainDataFetcherAPI) StopFetching() error { 39 return api.f.stopFetching() 40 } 41 42 func (api *PublicChainDataFetcherAPI) StartRangeFetching(start, end uint64, reqType interface{}) error { 43 var t types.RequestType 44 switch reqType { 45 case "all": 46 t = types.RequestTypeGroupAll 47 case "block": 48 t = types.RequestTypeBlockGroup 49 case "trace": 50 t = types.RequestTypeTraceGroup 51 default: 52 ut, ok := reqType.(float64) 53 if !ok { 54 return errors.New("the request type should be 'all', 'block', 'trace', or uint type") 55 } 56 t = types.RequestType(ut) 57 } 58 59 if !t.IsValid() { 60 return errors.New("the request type is not valid") 61 } 62 63 return api.f.startRangeFetching(start, end, t) 64 } 65 66 func (api *PublicChainDataFetcherAPI) StopRangeFetching() error { 67 return api.f.stopRangeFetching() 68 } 69 70 func (api *PublicChainDataFetcherAPI) Status() string { 71 return api.f.status() 72 } 73 74 func (api *PublicChainDataFetcherAPI) ReadCheckpoint() (int64, error) { 75 return api.f.checkpointDB.ReadCheckpoint() 76 } 77 78 func (api *PublicChainDataFetcherAPI) WriteCheckpoint(checkpoint int64) error { 79 isRunning := atomic.LoadUint32(&api.f.fetchingStarted) 80 if isRunning == running { 81 return errors.New("call stopFetching before writing checkpoint manually") 82 } 83 84 api.f.checkpoint = checkpoint 85 return api.f.checkpointDB.WriteCheckpoint(checkpoint) 86 } 87 88 // GetConfig returns the configuration setting of the launched chaindata fetcher. 89 func (api *PublicChainDataFetcherAPI) GetConfig() *ChainDataFetcherConfig { 90 return api.f.config 91 }