-
Notifications
You must be signed in to change notification settings - Fork 47
/
ledger.go
161 lines (132 loc) · 4.53 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package agent
import (
"encoding/json"
"fmt"
"github.com/xuperchain/xupercore/bcs/ledger/xledger/state"
"github.com/xuperchain/xupercore/bcs/ledger/xledger/ledger"
consdef "github.com/xuperchain/xupercore/kernel/consensus/def"
"github.com/xuperchain/xupercore/kernel/engines/xuperos/common"
kledger "github.com/xuperchain/xupercore/kernel/ledger"
"github.com/xuperchain/xupercore/lib/logs"
)
type LedgerAgent struct {
log logs.Logger
chainCtx *common.ChainCtx
}
func NewLedgerAgent(chainCtx *common.ChainCtx) *LedgerAgent {
return &LedgerAgent{
log: chainCtx.GetLog(),
chainCtx: chainCtx,
}
}
// 从创世块获取创建合约账户消耗gas
func (t *LedgerAgent) GetNewAccountGas() (int64, error) {
amount := t.chainCtx.Ledger.GenesisBlock.GetConfig().GetNewAccountResourceAmount()
return amount, nil
}
// 从创世块获取治理代币消耗gas
func (t *LedgerAgent) GetNewGovGas() (int64, error) {
// todo 还未实现
amount := t.chainCtx.Ledger.GenesisBlock.GetConfig().GetNewAccountResourceAmount()
return amount, nil
}
// 从创世块获取治理代币消耗gas
func (t *LedgerAgent) GetGenesisPreDistribution() ([]ledger.Predistribution, error) {
preDistribution := t.chainCtx.Ledger.GenesisBlock.GetConfig().GetPredistribution()
return preDistribution, nil
}
// 从创世块获取加密算法类型
func (t *LedgerAgent) GetCryptoType() (string, error) {
cryptoType := t.chainCtx.Ledger.GenesisBlock.GetConfig().GetCryptoType()
return cryptoType, nil
}
// 从创世块获取共识配置
func (t *LedgerAgent) GetConsensusConf() ([]byte, error) {
consensusConf := t.chainCtx.Ledger.GenesisBlock.GetConfig().GenesisConsensus
if _, ok := consensusConf["name"]; !ok {
return nil, fmt.Errorf("consensus config set error,unset name")
}
if _, ok := consensusConf["config"]; !ok {
return nil, fmt.Errorf("consensus config set error,unset config")
}
confStr, err := json.Marshal(consensusConf["config"])
if err != nil {
return nil, fmt.Errorf("json marshal consensus config failed.error:%s", err)
}
if _, ok := consensusConf["name"].(string); !ok {
return nil, fmt.Errorf("consensus name set error")
}
conf := consdef.ConsensusConfig{
ConsensusName: consensusConf["name"].(string),
Config: string(confStr),
}
data, err := json.Marshal(conf)
if err != nil {
return nil, fmt.Errorf("marshal consensus conf failed.error:%s", err)
}
return data, nil
}
// 查询区块
func (t *LedgerAgent) QueryBlock(blkId []byte) (kledger.BlockHandle, error) {
block, err := t.chainCtx.Ledger.QueryBlock(blkId)
if err != nil {
return nil, err
}
return state.NewBlockAgent(block), nil
}
func (t *LedgerAgent) QueryBlockByHeight(height int64) (kledger.BlockHandle, error) {
block, err := t.chainCtx.Ledger.QueryBlockByHeight(height)
if err != nil {
return nil, err
}
return state.NewBlockAgent(block), nil
}
func (t *LedgerAgent) QueryTipBlockHeight() (int64, error) {
meta := t.chainCtx.Ledger.GetMeta()
bh, err := t.QueryBlockHeader(meta.TipBlockid)
if err != nil {
return 0, err
}
return bh.GetHeight(), nil
}
func (t *LedgerAgent) QueryTipBlockHeader() kledger.BlockHandle {
meta := t.chainCtx.Ledger.GetMeta()
blkAgent, _ := t.QueryBlockHeader(meta.TipBlockid)
return blkAgent
}
// 仅查询区块头
func (t *LedgerAgent) QueryBlockHeader(blkId []byte) (kledger.BlockHandle, error) {
block, err := t.chainCtx.Ledger.QueryBlockHeader(blkId)
if err != nil {
return nil, err
}
return state.NewBlockAgent(block), nil
}
func (t *LedgerAgent) QueryBlockHeaderByHeight(height int64) (kledger.BlockHandle, error) {
block, err := t.chainCtx.Ledger.QueryBlockHeaderByHeight(height)
if err != nil {
return nil, err
}
return state.NewBlockAgent(block), nil
}
func (t *LedgerAgent) GetTipBlock() kledger.BlockHandle {
meta := t.chainCtx.Ledger.GetMeta()
blkAgent, _ := t.QueryBlock(meta.TipBlockid)
return blkAgent
}
// 获取状态机最新确认高度快照(只有Get方法,直接返回[]byte)
func (t *LedgerAgent) GetTipXMSnapshotReader() (kledger.XMSnapshotReader, error) {
return t.chainCtx.State.GetTipXMSnapshotReader()
}
// 根据指定blockid创建快照(Select方法不可用)
func (t *LedgerAgent) CreateSnapshot(blkId []byte) (kledger.XMReader, error) {
return t.chainCtx.State.CreateSnapshot(blkId)
}
// 获取最新确认高度快照(Select方法不可用)
func (t *LedgerAgent) GetTipSnapshot() (kledger.XMReader, error) {
return t.chainCtx.State.GetTipSnapshot()
}
// 获取最新状态数据
func (t *LedgerAgent) CreateXMReader() kledger.XMReader {
return t.chainCtx.State.CreateXMReader()
}