/
depositInfo.go
245 lines (205 loc) · 9.26 KB
/
depositInfo.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
// Copyright (c) 2018 The MATRIX Authors
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php
package depoistInfo
import (
"context"
"math/big"
"reflect"
"github.com/MatrixAINetwork/go-matrix/common"
"github.com/MatrixAINetwork/go-matrix/core/state"
"github.com/MatrixAINetwork/go-matrix/core/types"
"github.com/MatrixAINetwork/go-matrix/core/vm"
"github.com/MatrixAINetwork/go-matrix/params"
"github.com/MatrixAINetwork/go-matrix/rpc"
"github.com/pkg/errors"
)
type manBackend interface {
StateAndHeaderByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*state.StateDBManage, *types.Header, error)
StateAndHeaderByHash(ctx context.Context, hash common.Hash) (*state.StateDBManage, *types.Header, error)
}
type DepositInfo struct {
MatrixDeposit vm.MatrixDeposit
Contract *vm.Contract
manApi manBackend
p vm.PrecompiledContract
}
var depositInfo *DepositInfo
func NewDepositInfo(manApi manBackend) {
pre := vm.PrecompiledContractsByzantium[common.BytesToAddress([]byte{10})]
depositInfo = &DepositInfo{manApi: manApi, p: pre}
//getDepositListTest()
}
func AddOnlineTime(stateDB vm.StateDBManager, address common.Address, ot *big.Int) error {
return depositInfo.MatrixDeposit.AddOnlineTime(depositInfo.Contract, stateDB, address, ot)
}
func GetOnlineTime(stateDB vm.StateDBManager, address common.Address) (*big.Int, error) {
return depositInfo.MatrixDeposit.GetOnlineTime(depositInfo.Contract, stateDB, address), nil
}
func SetOnlineTime(stateDB vm.StateDBManager, address common.Address, ot *big.Int) error {
return depositInfo.MatrixDeposit.SetOnlineTime(depositInfo.Contract, stateDB, address, ot)
}
func GetDepositList(tm *big.Int, getDeposit common.RoleType) ([]vm.DepositDetail, error) {
db, err := getDepositInfo(tm)
if err != nil {
return nil, err
}
contract := vm.NewContract(vm.AccountRef(common.HexToAddress("1337")), vm.AccountRef(common.BytesToAddress([]byte{10})), big.NewInt(0), 60000, params.MAN_COIN)
//var depositList []vm.DepositDetail
depositList := make([]vm.DepositDetail, 0)
if common.RoleValidator == common.RoleValidator&getDeposit {
depositList = append(depositList, depositInfo.MatrixDeposit.GetValidatorDepositList(contract, db)...)
}
if common.RoleMiner == common.RoleMiner&getDeposit {
depositList = append(depositList, depositInfo.MatrixDeposit.GetMinerDepositList(contract, db)...)
}
return depositList, nil
}
func GetDepositAndWithDrawList(tm *big.Int) ([]vm.DepositDetail, error) {
db, err := getDepositInfo(tm)
if err != nil {
return nil, err
}
contract := vm.NewContract(vm.AccountRef(common.HexToAddress("1337")), vm.AccountRef(common.BytesToAddress([]byte{10})), big.NewInt(0), 60000, params.MAN_COIN)
var depositList []vm.DepositDetail
depositList = depositInfo.MatrixDeposit.GetAllDepositList(contract, db, true)
return depositList, nil
}
func GetAllDeposit(tm *big.Int) ([]vm.DepositDetail, error) {
db, err := getDepositInfo(tm)
if err != nil {
return nil, err
}
contract := vm.NewContract(vm.AccountRef(common.HexToAddress("1337")), vm.AccountRef(common.BytesToAddress([]byte{10})), big.NewInt(0), 60000, params.MAN_COIN)
var depositList []vm.DepositDetail
depositList = depositInfo.MatrixDeposit.GetAllDepositList(contract, db, false)
return depositList, nil
}
func GetDepositListByHash(hash common.Hash, getDeposit common.RoleType) ([]vm.DepositDetail, error) {
db, err := getDepositInfoByHash(hash)
if err != nil {
return nil, err
}
contract := vm.NewContract(vm.AccountRef(common.HexToAddress("1337")), vm.AccountRef(common.BytesToAddress([]byte{10})), big.NewInt(0), 60000, params.MAN_COIN)
//var depositList []vm.DepositDetail
depositList := make([]vm.DepositDetail, 0)
if common.RoleValidator == common.RoleValidator&getDeposit {
depositList = append(depositList, depositInfo.MatrixDeposit.GetValidatorDepositList(contract, db)...)
}
if common.RoleMiner == common.RoleMiner&getDeposit {
depositList = append(depositList, depositInfo.MatrixDeposit.GetMinerDepositList(contract, db)...)
}
return depositList, nil
}
func GetDepositAndWithDrawListByHash(hash common.Hash) ([]vm.DepositDetail, error) {
db, err := getDepositInfoByHash(hash)
if err != nil {
return nil, err
}
contract := vm.NewContract(vm.AccountRef(common.HexToAddress("1337")), vm.AccountRef(common.BytesToAddress([]byte{10})), big.NewInt(0), 60000, params.MAN_COIN)
var depositList []vm.DepositDetail
depositList = depositInfo.MatrixDeposit.GetAllDepositList(contract, db, true)
return depositList, nil
}
func GetAllDepositByHash(hash common.Hash) ([]vm.DepositDetail, error) {
db, err := getDepositInfoByHash(hash)
if err != nil {
return nil, err
}
contract := vm.NewContract(vm.AccountRef(common.HexToAddress("1337")), vm.AccountRef(common.BytesToAddress([]byte{10})), big.NewInt(0), 60000, params.MAN_COIN)
var depositList []vm.DepositDetail
depositList = depositInfo.MatrixDeposit.GetAllDepositList(contract, db, false)
return depositList, nil
}
func getDepositInfo(tm *big.Int) (db vm.StateDBManager, err error) {
depositInfo.Contract = vm.NewContract(vm.AccountRef(common.HexToAddress("1337")), vm.AccountRef(common.BytesToAddress([]byte{10})), big.NewInt(0), 0, params.MAN_COIN)
var c context.Context
var h rpc.BlockNumber
h = rpc.BlockNumber(tm.Int64())
//encode := hexutil.EncodeBig(tm)
//err = h.UnmarshalJSON([]byte(encode))
//if err != nil {
// return nil, err
//}
db, _, err = depositInfo.manApi.StateAndHeaderByNumber(c, h)
if err != nil {
return nil, err
}
if db == nil {
return nil, errors.New("db is nil")
}
dbValue := reflect.ValueOf(db)
if dbValue.Kind() == reflect.Ptr && dbValue.IsNil() {
return nil, errors.New("db is nil")
}
return db, nil
}
func getDepositInfoByHash(hash common.Hash) (db vm.StateDBManager, err error) {
depositInfo.Contract = vm.NewContract(vm.AccountRef(common.HexToAddress("1337")), vm.AccountRef(common.BytesToAddress([]byte{10})), big.NewInt(0), 0, params.MAN_COIN)
var c context.Context
db, _, err = depositInfo.manApi.StateAndHeaderByHash(c, hash)
if err != nil {
return nil, err
}
if db == nil {
return nil, errors.New("db is nil")
}
dbValue := reflect.ValueOf(db)
if dbValue.Kind() == reflect.Ptr && dbValue.IsNil() {
return nil, errors.New("db is nil")
}
return db, nil
}
func ResetSlash(stateDB vm.StateDBManager, address common.Address) error {
return depositInfo.MatrixDeposit.ResetSlash(depositInfo.Contract, stateDB, address)
}
func GetSlash(stateDB vm.StateDBManager, address common.Address) (*big.Int, error) {
return depositInfo.MatrixDeposit.GetSlash(depositInfo.Contract, stateDB, address), nil
}
func GetAllSlash(stateDB vm.StateDBManager) map[common.Address]*big.Int {
return depositInfo.MatrixDeposit.GetAllSlash(depositInfo.Contract, stateDB)
}
func AddSlash(stateDB vm.StateDBManager, address common.Address, slash *big.Int) error {
return depositInfo.MatrixDeposit.AddSlash(depositInfo.Contract, stateDB, address, slash)
}
func SetSlash(stateDB vm.StateDBManager, address common.Address, slash *big.Int) error {
return depositInfo.MatrixDeposit.SetSlash(depositInfo.Contract, stateDB, address, slash)
}
func ResetInterest(stateDB vm.StateDBManager, address common.Address) error {
return depositInfo.MatrixDeposit.ResetInterest(depositInfo.Contract, stateDB, address)
}
func GetInterest(stateDB vm.StateDBManager, address common.Address) (*big.Int, error) {
return depositInfo.MatrixDeposit.GetInterest(depositInfo.Contract, stateDB, address), nil
}
func GetAllInterest(stateDB vm.StateDBManager) map[common.Address]*big.Int {
return depositInfo.MatrixDeposit.GetAllInterest(depositInfo.Contract, stateDB)
}
func AddInterest(stateDB vm.StateDBManager, address common.Address, reward *big.Int) error {
return depositInfo.MatrixDeposit.AddInterest(depositInfo.Contract, stateDB, address, reward)
}
func SetInterest(stateDB vm.StateDBManager, address common.Address, reward *big.Int) error {
return depositInfo.MatrixDeposit.SetInterest(depositInfo.Contract, stateDB, address, reward)
}
func GetDeposit(stateDB vm.StateDBManager, address common.Address) *big.Int {
return depositInfo.MatrixDeposit.GetDeposit(depositInfo.Contract, stateDB, address)
}
func SetDeposit(stateDB vm.StateDBManager, address common.Address) error {
return depositInfo.MatrixDeposit.SetDeposit(depositInfo.Contract, stateDB, address)
}
func AddDeposit(stateDB vm.StateDBManager, address common.Address) error {
return depositInfo.MatrixDeposit.AddDeposit(depositInfo.Contract, stateDB, address)
}
// 获取A0账户
func GetDepositAccount(stateDB vm.StateDBManager, authAccount common.Address) common.Address {
if depositInfo.Contract == nil {
depositInfo.Contract = vm.NewContract(vm.AccountRef(common.HexToAddress("1337")), vm.AccountRef(common.BytesToAddress([]byte{10})), big.NewInt(0), 0, params.MAN_COIN)
}
return depositInfo.MatrixDeposit.GetDepositAccount(depositInfo.Contract, stateDB, authAccount)
}
// 获取A1账户
func GetAuthAccount(stateDB vm.StateDBManager, depositAccount common.Address) common.Address {
if depositInfo.Contract == nil {
depositInfo.Contract = vm.NewContract(vm.AccountRef(common.HexToAddress("1337")), vm.AccountRef(common.BytesToAddress([]byte{10})), big.NewInt(0), 0, params.MAN_COIN)
}
return depositInfo.MatrixDeposit.GetAuthAccount(depositInfo.Contract, stateDB, depositAccount)
}