-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstore.go
50 lines (44 loc) · 1.11 KB
/
store.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
package main
import (
"encoding/hex"
"errors"
"sync"
)
type Store struct {
headers map[uint64]*BlockHeader
txs map[string]*Transaction
lastBlockHeight uint64
lock sync.RWMutex
}
func NewStore() *Store {
return &Store{
headers: make(map[uint64]*BlockHeader),
txs: make(map[string]*Transaction),
lastBlockHeight: 0,
}
}
//SaveBlock 保存一个区块到账本中
func (s *Store) SaveBlock(block *Block) error {
s.lock.Lock()
defer s.lock.Unlock()
if block.Header.BlockHeight != s.lastBlockHeight+1 {
return errors.New("invalid block height")
}
s.headers[block.Header.BlockHeight] = block.Header
for _, tx := range block.Txs {
s.txs[hex.EncodeToString(tx.TxHash)] = tx
}
s.lastBlockHeight = block.Header.BlockHeight
return nil
}
//GetLastBlockHeight 获得存储的最新区块高度
func (s *Store) GetLastBlockHeight() uint64 {
return s.lastBlockHeight
}
//TxExist 判断一个Tx是否已经存在于账本中
func (s *Store) TxExist(txHash []byte) bool {
s.lock.RLock()
defer s.lock.RUnlock()
_, ok := s.txs[hex.EncodeToString(txHash)]
return ok
}