PeernetOfficial / core

Core library. Use this to create a new Peernet application.
MIT License
36 stars 5 forks source link

Blockchain Multi Store #73

Closed Kleissner closed 2 years ago

Kleissner commented 2 years ago

Create a new multi store for storing other peers blockchain. This issue will keep track of unused code.

Storing the count of blockchains value turned out to be unstable. Therefore the "blockchain count" will not be stored in the KV database. Here is the code:

MultiStore struct:
    blockchainCount uint64      // Count of unique blockchains stored.

init:
    multi.blockchainCount, _ = multi.readStat()

func (multi *MultiStore) readStat() (count uint64, valid bool) {
    buffer, found := multi.Database.Get([]byte("stat"))
    if !found || len(buffer) != 8 {
        return 0, false
    }

    return binary.LittleEndian.Uint64(buffer[0:8]), true
}

func (multi *MultiStore) writeStat(count uint64) (err error) {
    buffer := make([]byte, 8)
    binary.LittleEndian.PutUint64(buffer[0:8], count)

    return multi.Database.Set([]byte("stat"), buffer)
}

func (multi *MultiStore) GetBlockchainCount() (count uint64) {
    multi.statMutex.Lock()
    defer multi.statMutex.Unlock()

    return multi.blockchainCount
}

func (multi *MultiStore) AdjustBlockchainCount(delta uint64) {
    multi.statMutex.Lock()
    defer multi.statMutex.Unlock()

    multi.blockchainCount += delta

    multi.writeStat(multi.blockchainCount)
}