/
ValidatorGroupContract.go
290 lines (282 loc) · 8.81 KB
/
ValidatorGroupContract.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
// 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 vm
import (
"github.com/MatrixAINetwork/go-matrix/common"
"math/big"
"github.com/MatrixAINetwork/go-matrix/crypto"
"github.com/MatrixAINetwork/go-matrix/core/vm/validatorGroup"
"github.com/MatrixAINetwork/go-matrix/params"
"github.com/MatrixAINetwork/go-matrix/accounts/abi"
"github.com/MatrixAINetwork/go-matrix/rlp"
"errors"
)
var (
manBase = big.NewInt(1e18)
rateDecmal = 1.0e9
rateDecmalBig = big.NewInt(1e9)
developContractAddress = common.BytesToAddress([]byte{10})
ValidatorGroupContractAddress = common.BytesToAddress([]byte{20})
depLvel = []*big.Int{big.NewInt(0),
new(big.Int).Mul(big.NewInt(10000),manBase),
new(big.Int).Mul(big.NewInt(100000),manBase)}
errArguments = errors.New("Input Arguments Error")
errExpired = errors.New("Validator Group is Expired")
errNotExist = errors.New("Validator Account is not exist")
errOwnerInsufficient = errors.New("Owner Deposit Amount is insufficient")
errNotOwner = errors.New("Tx sender is not Owner")
errInsufficient = errors.New("insufficient balance for Refund")
)
type MethodInterface interface {
PrecompiledContract
MethodName()string
MethodID()[4]byte
}
type BaseMethod struct {
Name string
Abi *abi.ABI
GasUsed uint64
run func (input []byte, contract *Contract, evm *EVM) ([]byte, error)
}
func (bm* BaseMethod)Inputs()abi.Arguments{
return bm.Abi.Methods[bm.Name].Inputs
}
func (bm* BaseMethod)Outputs()abi.Arguments{
return bm.Abi.Methods[bm.Name].Outputs
}
func (bm* BaseMethod)MethodName()string{
return bm.Name
}
/*
func (bm* BaseMethod)getFuncID(abi *abi.ABI,funcName string)[4]byte{
funId := abi.Methods[funcName].Id()
var ID [4]byte
copy(ID[:],funId[:4])
return ID
}
*/
func (bm* BaseMethod)MethodID()[4]byte{
if len(bm.Name)==0{
return [4]byte{}
}
if method,exist := bm.Abi.Methods[bm.Name];exist{
funId := method.Id()
var ID [4]byte
copy(ID[:],funId[:4])
return ID
}
return [4]byte{}
}
func (bm* BaseMethod)RequiredGas(input []byte)uint64{
return bm.GasUsed
}
func (bm* BaseMethod)Run(input []byte, contract *Contract, evm *EVM) ([]byte, error){
return bm.run(input,contract,evm)
}
type BaseContract struct {
anonymousMethod MethodInterface
// constructMethod MethodInterface
methodMap map[[4]byte]MethodInterface
}
func (bc* BaseContract)AddMethod(md MethodInterface) {
bc.methodMap[md.MethodID()] = md
}
func (bc* BaseContract)GetMethod(input []byte)PrecompiledContract{
if len(input) == 0 {
return bc.anonymousMethod
}
if len(input)<4{
return nil
}
var ID [4]byte
copy(ID[:],input[:4])
return bc.methodMap[ID]
}
func (bc* BaseContract)RequiredGas(input []byte) uint64 {
method := bc.GetMethod(input)
if method!= nil{
return method.RequiredGas(input)
}
return params.TxGasContractCreation * 2
}
func (bc* BaseContract)Run(input []byte, contract *Contract, evm *EVM) ([]byte, error) {
method := bc.GetMethod(input)
if method!= nil{
return method.Run(input,contract,evm)
}
return nil,errExecutionReverted
}
func EmergeKey(address common.Address,surffix string)common.Hash{
hashKey := common.Hash{}
copy(hashKey[:],address[:])
copy(hashKey[20:],[]byte(surffix))
return hashKey
}
type ValidatorContractState struct {
childGroup validatorGroup.AddressSlice
}
func (vc *ValidatorContractState)RemoveEmptyValidatorGroup(address common.Address,state StateDBManager)error{
err := vc.GetState(ValidatorGroupContractAddress,state)
if err != nil{
return err
}
vc.childGroup.Remove(address)
return vc.SetState(ValidatorGroupContractAddress,state)
// return nil
}
func (vc *ValidatorContractState)Find(address common.Address)bool{
_,exist := vc.childGroup.Find(address)
return exist
}
func (vc *ValidatorContractState)Insert(address common.Address){
vc.childGroup.Insert(address)
}
func (vc *ValidatorContractState)SetState(contractAddress common.Address,state StateDBManager)error {
data,err := rlp.EncodeToBytes([]common.Address(vc.childGroup))
if err != nil {
return err
}
state.SetStateByteArray(params.MAN_COIN,contractAddress,EmergeKey(contractAddress,"ValiList"),data)
return nil
}
func (vc *ValidatorContractState)GetState(contractAdress common.Address,state StateDBManager)error {
data := state.GetStateByteArray(params.MAN_COIN,contractAdress,EmergeKey(contractAdress,"ValiList"))
if len(data) == 0 {
vc.childGroup = validatorGroup.AddressSlice{}
return nil
}
err := rlp.DecodeBytes(data,&vc.childGroup)
if err != nil {
return err
}
return nil
}
func (vc *ValidatorContractState)GetValidatorGroupInfo(time uint64,state StateDBManager)(map[common.Address]*ValidatorGroupState,error){
err := vc.GetState(ValidatorGroupContractAddress,state)
if err != nil{
return nil,err
}
validatorGroupMap := make(map[common.Address]*ValidatorGroupState)
for _,addr := range vc.childGroup {
states := NewValidatorGroupState()
err := states.GetState(addr,time,state)
if err != nil{
return nil,err
}
validatorGroupMap[addr] = states
}
return validatorGroupMap,nil
}
//
type ValidatorGroupContract struct {
BaseContract
childs ValidatorContractState
}
func NewTransInterestsInterface()TransInterestsInterface{
return NewValidatorGroupContract()
}
func NewValidatorGroupContract()*ValidatorGroupContract{
vg := &ValidatorGroupContract{}
vg.methodMap = make(map[[4]byte]MethodInterface)
vg.CreateValidatorGroupMethod()
return vg
}
func (vg* ValidatorGroupContract)IsPrecompiledContract(address common.Address) bool {
return vg.childs.Find(address)
}
func (vg* ValidatorGroupContract)CreateValidatorGroupMethod(){
bm := &BaseMethod{
Name:"createValidatorGroup",
Abi:&validatorGroup.ValidatorGroupContractAbi,
GasUsed:params.TxGasContractCreation+params.CallValueTransferGas*2,
}
bm.run = func(input []byte, contract *Contract, evm *EVM) ([]byte, error) {
data,err := bm.Inputs().UnpackValues(input[4:])
if err!=nil{
return nil,err
}
err = vg.childs.GetState(contract.Address(),evm.StateDB)
if err!=nil{
return nil,err
}
nonce := evm.StateDB.GetNonce(evm.Cointyp, contract.CallerAddress)-1
contractAddr := crypto.CreateAddress(contract.CallerAddress, nonce)
if vg.IsPrecompiledContract(contractAddr)|| len(evm.StateDB.GetCode(params.MAN_COIN,contractAddr))!=0 {
return nil,ErrContractAddressCollision
}
vg.childs.Insert(contractAddr)
err = vg.childs.SetState(contract.Address(),evm.StateDB)
if err!=nil{
return nil,err
}
if !evm.StateDB.Exist(evm.Cointyp, contractAddr) {
evm.StateDB.CreateAccount(evm.Cointyp, contractAddr)
}
if !evm.CanTransfer(evm.StateDB, contract.Address(), contract.value, evm.Cointyp){
return nil, errors.New("insufficient balance for createValidatorGroup")
}
evm.Transfer(evm.StateDB, contract.Address(), contractAddr, contract.value, evm.Cointyp)
child := NewValidatorGroup()
err = child.Constructor(contractAddr,
data[0].(common.Address),
contract.CallerAddress,
data[1].(*big.Int),
data[2].(*big.Int),
data[3].(*big.Int),
data[4].([]*big.Int),
contract,evm)
if err!=nil{
return nil,err
}
dType := data[2].(*big.Int)
vg.AddCreateValidatorGroupLog(data[0].(common.Address),contractAddr,dType,contract,evm)
return contractAddr[:],nil
}
vg.AddMethod(bm)
}
func (vg* ValidatorGroupContract)AddCreateValidatorGroupLog(signAccount,newContract common.Address,dType *big.Int,contract *Contract, evm *EVM)error{
topics := []common.Hash{
validatorGroup.ValidatorGroupContractAbi.Events["CreateValidatorGroup"].Id(),
contract.CallerAddress.Hash(),
signAccount.Hash(),
newContract.Hash(),
}
data,err := validatorGroup.ValidatorGroupContractAbi.Events["CreateValidatorGroup"].Inputs.NonIndexed().Pack(contract.value,dType)
if err != nil {
return err
}
AddContractLog(topics,data,contract, evm)
return nil
}
//reward
func (vg* ValidatorGroupContract)TransferRewards(amount *big.Int,time uint64,contractAddress common.Address,state StateDBManager)error{
err := vg.childs.GetState(ValidatorGroupContractAddress,state)
if err != nil{
return err
}
if vg.childs.Find(contractAddress){
return NewValidatorGroup().TransferRewards(amount,time,contractAddress,state)
}
return nil
}
func (vg* ValidatorGroupContract)TransferInterests(amount *big.Int,position uint64,time uint64,address common.Address,state StateDBManager)error{
if position == 0 {
return vg.TransferCurrentInterests(amount,time,address,state)
}
return nil
}
//current Interests
func (vg* ValidatorGroupContract)TransferCurrentInterests(amount *big.Int,time uint64,contractAddress common.Address,state StateDBManager)error{
err := vg.childs.GetState(ValidatorGroupContractAddress,state)
if err != nil{
return err
}
if vg.childs.Find(contractAddress){
return NewValidatorGroup().TransferCurrentInterests(amount,time,contractAddress,state)
}
return nil
}
//view
func (vg* ValidatorGroupContract)ListValidatorGroup(){
}