-
Notifications
You must be signed in to change notification settings - Fork 48
/
contract_info.go
143 lines (116 loc) · 3.9 KB
/
contract_info.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
package reader
import (
"errors"
xctx "github.com/xuperchain/xupercore/kernel/common/xcontext"
"github.com/xuperchain/xupercore/kernel/engines/xuperos/common"
"github.com/xuperchain/xupercore/lib/logs"
"github.com/xuperchain/xupercore/protos"
)
type ContractReader interface {
// 查询该链合约统计数据
QueryContractStatData() (*protos.ContractStatData, error)
// 查询账户下合约状态
GetAccountContracts(account string) ([]*protos.ContractStatus, error)
// 查询地址下合约状态
GetAddressContracts(addr string, needContent bool) (map[string][]*protos.ContractStatus, error)
// 查询地址下账户
GetAccountByAK(addr string) ([]string, error)
// 查询合约账户ACL
QueryAccountACL(account string) (*protos.Acl, error)
// 查询合约方法ACL
QueryContractMethodACL(contract, method string) (*protos.Acl, error)
// 查询账户治理代币余额
QueryAccountGovernTokenBalance(account string) (*protos.GovernTokenBalance, error)
//
GetContractDesc(name string) (*protos.WasmCodeDesc, error)
}
type contractReader struct {
chainCtx *common.ChainCtx
baseCtx xctx.XContext
log logs.Logger
}
func NewContractReader(chainCtx *common.ChainCtx, baseCtx xctx.XContext) ContractReader {
if chainCtx == nil || baseCtx == nil {
return nil
}
reader := &contractReader{
chainCtx: chainCtx,
baseCtx: baseCtx,
log: baseCtx.GetLog(),
}
return reader
}
func (t *contractReader) QueryContractStatData() (*protos.ContractStatData, error) {
contractStatData, err := t.chainCtx.State.QueryContractStatData()
if err != nil {
return nil, common.CastError(err)
}
return contractStatData, nil
}
func (t *contractReader) GetAccountContracts(account string) ([]*protos.ContractStatus, error) {
contracts, err := t.chainCtx.State.GetAccountContracts(account)
if err != nil {
return nil, common.CastError(err)
}
contractStatusList := make([]*protos.ContractStatus, 0, len(contracts))
for _, contractName := range contracts {
status, err := t.chainCtx.State.GetContractStatus(contractName)
if err != nil {
t.log.Warn("get contract status error", "err", err)
return nil, common.CastError(err)
}
contractStatusList = append(contractStatusList, status)
}
return contractStatusList, nil
}
func (t *contractReader) GetAddressContracts(address string,
needContent bool) (map[string][]*protos.ContractStatus, error) {
accounts, err := t.GetAccountByAK(address)
if err != nil {
return nil, common.CastError(err)
}
out := make(map[string][]*protos.ContractStatus, len(accounts))
for _, account := range accounts {
contractStatusList, err := t.GetAccountContracts(account)
if err != nil {
t.log.Warn("get account contracts error", "err", err, "account", account)
continue
}
out[account] = contractStatusList
}
return out, nil
}
func (t *contractReader) GetAccountByAK(address string) ([]string, error) {
accounts, err := t.chainCtx.State.QueryAccountContainAK(address)
if err != nil {
return nil, common.CastError(err)
}
return accounts, nil
}
func (t *contractReader) QueryAccountACL(account string) (*protos.Acl, error) {
acl, err := t.chainCtx.State.QueryAccountACL(account)
if err != nil {
return nil, common.CastError(err)
}
return acl, nil
}
func (t *contractReader) QueryContractMethodACL(contract, method string) (*protos.Acl, error) {
acl, err := t.chainCtx.State.QueryContractMethodACL(contract, method)
if err != nil {
return nil, common.CastError(err)
}
return acl, nil
}
func (t *contractReader) QueryAccountGovernTokenBalance(account string) (*protos.GovernTokenBalance, error) {
amount, err := t.chainCtx.State.QueryAccountGovernTokenBalance(account)
if err != nil {
return nil, common.CastError(err)
}
return amount, nil
}
func (t *contractReader) GetContractDesc(name string) (*protos.WasmCodeDesc, error) {
if name == "" {
return nil, errors.New("contract name can not be empty")
}
return t.chainCtx.State.GetContractDesc(name)
}