github.com/sixexorg/magnetic-ring@v0.0.0-20191119090307-31705a21e419/store/mainchain/storages/ledger_partial_header.go (about) 1 package storages 2 3 import ( 4 "fmt" 5 "sort" 6 7 "github.com/sixexorg/magnetic-ring/common" 8 "github.com/sixexorg/magnetic-ring/core/mainchain/types" 9 ) 10 11 //GetCurrentHeaderHeight return the current header height. 12 //In block sync states, Header height is usually higher than block height that is has already committed to storage 13 func (this *LedgerStoreImp) GetCurrentHeaderHeightbak() uint64 { 14 this.lock.RLock() 15 defer this.lock.RUnlock() 16 size := len(this.headerIndex) 17 if size == 0 { 18 return 0 19 } 20 return uint64(size) 21 } 22 23 func (this *LedgerStoreImp) GetCurrentHeaderHeight() uint64 { 24 this.lock.RLock() 25 defer this.lock.RUnlock() 26 return this.currBlockHeight 27 } 28 29 //GetCurrentHeaderHash return the current header hash. The current header means the latest header. 30 func (this *LedgerStoreImp) GetCurrentHeaderHash() common.Hash { 31 this.lock.RLock() 32 defer this.lock.RUnlock() 33 size := len(this.headerIndex) 34 if size == 0 { 35 return common.Hash{} 36 } 37 return this.headerIndex[uint64(size)] 38 } 39 40 func (this *LedgerStoreImp) addHeaderCache(header *types.Header) { 41 this.lock.Lock() 42 defer this.lock.Unlock() 43 this.headerCache[header.Hash()] = header 44 } 45 46 func (this *LedgerStoreImp) delHeaderCache(blockHash common.Hash) { 47 this.lock.Lock() 48 defer this.lock.Unlock() 49 delete(this.headerCache, blockHash) 50 } 51 52 func (this *LedgerStoreImp) getHeaderCache(blockHash common.Hash) *types.Header { 53 this.lock.RLock() 54 defer this.lock.RUnlock() 55 header, ok := this.headerCache[blockHash] 56 if !ok { 57 return nil 58 } 59 return header 60 } 61 62 //GetHeaderByHash return the block header by block hash 63 func (this *LedgerStoreImp) GetHeaderByHash(blockHash common.Hash) (*types.Header, error) { 64 header := this.getHeaderCache(blockHash) 65 if header != nil { 66 return header, nil 67 } 68 return this.blockStore.GetHeader(blockHash) 69 } 70 func (this *LedgerStoreImp) setHeaderIndex(height uint64, blockHash common.Hash) { 71 this.lock.Lock() 72 defer this.lock.Unlock() 73 this.headerIndex[height] = blockHash 74 } 75 76 func (this *LedgerStoreImp) getHeaderIndex(height uint64) common.Hash { 77 this.lock.RLock() 78 defer this.lock.RUnlock() 79 blockHash, ok := this.headerIndex[height] 80 if !ok { 81 return common.Hash{} 82 } 83 return blockHash 84 } 85 func (this *LedgerStoreImp) AddHeader(header *types.Header) error { 86 nextHeaderHeight := this.GetCurrentHeaderHeight() + 1 87 if header.Height != nextHeaderHeight { 88 return fmt.Errorf("header height %d not equal next header height %d", header.Height, nextHeaderHeight) 89 } 90 err := this.verifyHeader(header) 91 if err != nil { 92 return fmt.Errorf("verifyHeader error %s", err) 93 } 94 this.addHeaderCache(header) 95 this.setHeaderIndex(header.Height, header.Hash()) 96 return nil 97 } 98 99 func (this *LedgerStoreImp) AddHeaders(headers []*types.Header) error { 100 sort.Slice(headers, func(i, j int) bool { 101 return headers[i].Height < headers[j].Height 102 }) 103 var err error 104 for _, header := range headers { 105 err = this.AddHeader(header) 106 if err != nil { 107 return err 108 } 109 } 110 return nil 111 } 112 //func (this *LedgerStoreImp) saveHeaderIndexList() error { 113 // this.lock.RLock() 114 // storeCount := this.storedIndexCount 115 // currHeight := this.currBlockHeight 116 // if currHeight-storeCount < HEADER_INDEX_BATCH_SIZE { 117 // this.lock.RUnlock() 118 // return nil 119 // } 120 // 121 // headerList := make([]common.Hash, HEADER_INDEX_BATCH_SIZE) 122 // for i := uint64(0); i < HEADER_INDEX_BATCH_SIZE; i++ { 123 // height := storeCount + i 124 // headerList[i] = this.headerIndex[height] 125 // } 126 // this.lock.RUnlock() 127 // err := this.blockStore.SaveHeaderIndexList(storeCount, headerList) 128 // if err != nil { 129 // return fmt.Errorf("SaveHeaderIndexList start %d error %s", storeCount, err) 130 // } 131 // 132 // this.lock.Lock() 133 // this.storedIndexCount += HEADER_INDEX_BATCH_SIZE 134 // this.lock.Unlock() 135 // return nil 136 //}