-
Notifications
You must be signed in to change notification settings - Fork 2
/
blockchain.go
67 lines (53 loc) · 1.24 KB
/
blockchain.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
package consensus
import (
"sync"
"time"
"github.com/korthochain/korthochain/pkg/block"
"github.com/korthochain/korthochain/pkg/blockchain"
)
type Hash [HashSize]byte
const HashSize int = 32
// Blockchain synchronization state management, including adding and
// removing blocks. The longest blockchain selection, as well as update
type BlockChain struct {
BlockHeader *block.Block
Bc blockchain.Blockchains
//blockchain db
//accounts db
Oranphs map[Hash]*OrphanBlock
PrevOrphans map[Hash][]*OrphanBlock
oldestOrphan *OrphanBlock
orphanLock sync.RWMutex
}
func New(bc *blockchain.Blockchain) *BlockChain {
return &BlockChain{
Bc: bc,
Oranphs: make(map[Hash]*OrphanBlock),
PrevOrphans: make(map[Hash][]*OrphanBlock),
}
}
//orphan Block data structure
type OrphanBlock struct {
Block *block.Block
Expiration time.Time
}
func (b *BlockChain) OrphanBlockIsExist(hash []byte) (*block.Block, bool) {
b.orphanLock.Lock()
defer b.orphanLock.Unlock()
h := BytesToHash(hash)
oranph, ok := b.Oranphs[h]
if ok {
return oranph.Block, ok
}
return nil, false
}
func BytesToHash(in []byte) Hash {
var tmp Hash
if len(in) != HashSize {
return tmp
}
for i, b := range in {
tmp[i] = b
}
return tmp
}