/
ledger.go
86 lines (72 loc) · 2.37 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
79
80
81
82
83
84
85
86
package blockchain
import (
"errors"
. "github.com/elastos/Elastos.ELA/common"
. "github.com/elastos/Elastos.ELA/core/types"
"github.com/elastos/Elastos.ELA/core/types/payload"
"github.com/elastos/Elastos.ELA/dpos/state"
)
var FoundationAddress Uint168
var DefaultLedger *Ledger
// Ledger - the struct for ledger
type Ledger struct {
Blockchain *BlockChain
Store IChainStore
Arbitrators state.Arbitrators
}
//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) (*payload.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
}
//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.GetHeight()
}
//Get blocks and confirms by given height range, if end equals zero will be treat as current highest block height
func (l *Ledger) GetDposBlocks(start, end uint32) ([]*DposBlock, error) {
//todo complete me
return nil, nil
}
//Append blocks and confirms directly
func (l *Ledger) AppendDposBlocks(confirms []*DposBlock) error {
//todo complete me
return nil
}