/
ledger.go
executable file
·78 lines (63 loc) · 2.03 KB
/
ledger.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
package blockchain
import (
"errors"
. "github.com/ioeXNetwork/ioeX.MainChain/core"
. "github.com/ioeXNetwork/ioeX.Utility/common"
)
var FoundationAddress Uint168
var DefaultLedger *Ledger
// Ledger - the struct for ledger
type Ledger struct {
Blockchain *Blockchain
Store IChainStore
}
//check weather the transaction contains the doubleSpend.
func (l *Ledger) IsDoubleSpend(Tx *Transaction) bool {
return DefaultLedger.Store.IsDoubleSpend(Tx)
}
//Get the DefaultLedger.
//Note: the later version will support the mutiLedger.So this func mybe expired later.
//Get the Asset from store.
func (l *Ledger) GetAsset(assetID Uint256) (*Asset, error) {
asset, err := l.Store.GetAsset(assetID)
if err != nil {
return nil, errors.New("[Ledger],GetAsset failed with assetID =" + assetID.String())
}
return asset, nil
}
//Get Block With Height.
func (l *Ledger) GetBlockWithHeight(height uint32) (*Block, error) {
temp, err := l.Store.GetBlockHash(height)
if err != nil {
return nil, errors.New("[Ledger],GetBlockWithHeight failed with height=" + string(height))
}
bk, err := DefaultLedger.Store.GetBlock(temp)
if err != nil {
return nil, errors.New("[Ledger],GetBlockWithHeight failed with hash=" + temp.String())
}
return bk, nil
}
//Get block with block hash.
func (l *Ledger) GetBlockWithHash(hash Uint256) (*Block, error) {
bk, err := l.Store.GetBlock(hash)
if err != nil {
return nil, errors.New("[Ledger],GetBlockWithHeight failed with hash=" + hash.String())
}
return bk, nil
}
//BlockInLedger checks if the block existed in ledger
func (l *Ledger) BlockInLedger(hash Uint256) bool {
return l.Store.IsBlockInStore(hash)
}
//Get transaction with hash.
func (l *Ledger) GetTransactionWithHash(hash Uint256) (*Transaction, error) {
tx, _, err := l.Store.GetTransaction(hash)
if err != nil {
return nil, errors.New("[Ledger],GetTransactionWithHash failed with hash=" + hash.String())
}
return tx, nil
}
//Get local block chain height.
func (l *Ledger) GetLocalBlockChainHeight() uint32 {
return l.Blockchain.GetBestHeight()
}