This repository was archived by the owner on May 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathblockstore.go
78 lines (63 loc) · 1.66 KB
/
blockstore.go
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Copyright 2021 ChainSafe Systems
// SPDX-License-Identifier: LGPL-3.0-only
package store
import (
"bytes"
"errors"
"fmt"
"math/big"
"github.com/syndtr/goleveldb/leveldb"
)
type BlockStore struct {
db KeyValueReaderWriter
}
func NewBlockStore(db KeyValueReaderWriter) *BlockStore {
return &BlockStore{
db: db,
}
}
// StoreBlock stores block number per domainID into blockstore
func (bs *BlockStore) StoreBlock(block *big.Int, domainID uint8) error {
key := bytes.Buffer{}
keyS := fmt.Sprintf("chain:%d:block", domainID)
key.WriteString(keyS)
err := bs.db.SetByKey(key.Bytes(), block.Bytes())
if err != nil {
return err
}
return nil
}
// GetLastStoredBlock queries the blockstore and returns latest known block
func (bs *BlockStore) GetLastStoredBlock(domainID uint8) (*big.Int, error) {
key := bytes.Buffer{}
keyS := fmt.Sprintf("chain:%d:block", domainID)
key.WriteString(keyS)
v, err := bs.db.GetByKey(key.Bytes())
if err != nil {
if errors.Is(err, leveldb.ErrNotFound) {
return big.NewInt(0), nil
}
return nil, err
}
block := big.NewInt(0).SetBytes(v)
return block, nil
}
// GetStartBlock queries the blockstore for the latest known block. If the latest block is
// greater than configured startBlock, then startBlock is replaced with the latest known block.
func (bs *BlockStore) GetStartBlock(domainID uint8, startBlock *big.Int, latest bool, fresh bool) (*big.Int, error) {
if latest {
return nil, nil
}
if fresh {
return startBlock, nil
}
latestBlock, err := bs.GetLastStoredBlock(domainID)
if err != nil {
return nil, err
}
if latestBlock.Cmp(startBlock) == 1 {
return latestBlock, nil
} else {
return startBlock, nil
}
}