aboutsummaryrefslogtreecommitdiffstats
path: root/core/rawdb/accessors_core_block.go
blob: 5fa5c8f86e40ab0c17e278d09630002c1c5f9f09 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package rawdb

import (
    "bytes"

    coreTypes "github.com/dexon-foundation/dexon-consensus/core/types"

    "github.com/dexon-foundation/dexon/common"
    "github.com/dexon-foundation/dexon/log"
    "github.com/dexon-foundation/dexon/rlp"
)

func ReadCoreBlockRLP(db DatabaseReader, hash common.Hash) rlp.RawValue {
    data, _ := db.Get(coreBlockKey(hash))
    return data
}

func WriteCoreBlockRLP(db DatabaseWriter, hash common.Hash, rlp rlp.RawValue) {
    if err := db.Put(coreBlockKey(hash), rlp); err != nil {
        log.Crit("Failed to store core block", "err", err)
    }
}

func HasCoreBlock(db DatabaseReader, hash common.Hash) bool {
    if has, err := db.Has(coreBlockKey(hash)); !has || err != nil {
        return false
    }
    return true
}

func ReadCoreBlock(db DatabaseReader, hash common.Hash) *coreTypes.Block {
    data := ReadCoreBlockRLP(db, hash)
    if len(data) == 0 {
        return nil
    }

    block := new(coreTypes.Block)
    if err := rlp.Decode(bytes.NewReader(data), block); err != nil {
        log.Error("Invalid core block RLP", "hash", hash, "err", err)
        return nil
    }
    return block
}

func WriteCoreBlock(db DatabaseWriter, hash common.Hash, block *coreTypes.Block) {
    data, err := rlp.EncodeToBytes(block)
    if err != nil {
        log.Crit("Failed to RLP encode core block", "err", err)
    }
    WriteCoreBlockRLP(db, hash, data)
}