forked from nknorg/nkn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ledger.go
109 lines (96 loc) · 3.33 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package ledger
import (
"errors"
"github.com/nknorg/nkn/common"
. "github.com/nknorg/nkn/common"
"github.com/nknorg/nkn/core/asset"
"github.com/nknorg/nkn/core/contract"
tx "github.com/nknorg/nkn/core/transaction"
"github.com/nknorg/nkn/crypto"
. "github.com/nknorg/nkn/errors"
)
var DefaultLedger *Ledger
var StandbyBookKeepers []*crypto.PubKey
type Ledger struct {
Blockchain *Blockchain
Store ILedgerStore
}
// double spend checking for transaction
func (l *Ledger) IsDoubleSpend(Tx *tx.Transaction) bool {
return DefaultLedger.Store.IsDoubleSpend(Tx)
}
// get the default ledger
func GetDefaultLedger() (*Ledger, error) {
if DefaultLedger == nil {
return nil, NewDetailErr(errors.New("[Ledger], GetDefaultLedger failed, DefaultLedger not Exist."), ErrNoCode, "")
}
return DefaultLedger, nil
}
//Calc the BookKeepers address by bookKeepers pubkey.
func GetBookKeeperAddress(bookKeepers []*crypto.PubKey) (Uint160, error) {
if len(bookKeepers) < 1 {
return Uint160{}, NewDetailErr(errors.New("[Ledger] , GetBookKeeperAddress with no bookKeeper"), ErrNoCode, "")
}
var temp []byte
var err error
if len(bookKeepers) > 1 {
temp, err = contract.CreateMultiSigRedeemScript(len(bookKeepers)-(len(bookKeepers)-1)/3, bookKeepers)
if err != nil {
return Uint160{}, NewDetailErr(err, ErrNoCode, "[Ledger],GetBookKeeperAddress failed with CreateMultiSigRedeemScript.")
}
} else {
temp, err = contract.CreateSignatureRedeemScript(bookKeepers[0])
if err != nil {
return Uint160{}, NewDetailErr(err, ErrNoCode, "[Ledger],GetBookKeeperAddress failed with CreateMultiSigRedeemScript.")
}
}
codehash, err := common.ToCodeHash(temp)
if err != nil {
return Uint160{}, NewDetailErr(err, ErrNoCode, "[Ledger],GetBookKeeperAddress failed with ToCodeHash.")
}
return codehash, nil
}
//Get the Asset from store.
func (l *Ledger) GetAsset(assetId Uint256) (*asset.Asset, error) {
asset, err := l.Store.GetAsset(assetId)
if err != nil {
return nil, NewDetailErr(err, ErrNoCode, "[Ledger],GetAsset failed with assetId ="+assetId.ToString())
}
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, NewDetailErr(err, ErrNoCode, "[Ledger],GetBlockWithHeight failed with height="+string(height))
}
bk, err := DefaultLedger.Store.GetBlock(temp)
if err != nil {
return nil, NewDetailErr(err, ErrNoCode, "[Ledger],GetBlockWithHeight failed with hash="+temp.ToString())
}
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, NewDetailErr(err, ErrNoCode, "[Ledger],GetBlockWithHeight failed with hash="+hash.ToString())
}
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) (*tx.Transaction, error) {
tx, err := l.Store.GetTransaction(hash)
if err != nil {
return nil, NewDetailErr(err, ErrNoCode, "[Ledger],GetTransactionWithHash failed with hash="+hash.ToString())
}
return tx, nil
}
//Get local block chain height.
func (l *Ledger) GetLocalBlockChainHeight() uint32 {
return l.Blockchain.BlockHeight
}