-
Notifications
You must be signed in to change notification settings - Fork 31
/
restricting_contract.go
115 lines (98 loc) · 3.88 KB
/
restricting_contract.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
// Copyright 2021 The Alaya Network Authors
// This file is part of the Alaya-Go library.
//
// The Alaya-Go library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The Alaya-Go library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the Alaya-Go library. If not, see <http://www.gnu.org/licenses/>.
package vm
import (
"fmt"
"math/big"
"github.com/AlayaNetwork/Alaya-Go/common"
"github.com/AlayaNetwork/Alaya-Go/common/vm"
"github.com/AlayaNetwork/Alaya-Go/log"
"github.com/AlayaNetwork/Alaya-Go/params"
"github.com/AlayaNetwork/Alaya-Go/x/plugin"
"github.com/AlayaNetwork/Alaya-Go/x/restricting"
)
const (
TxCreateRestrictingPlan = 4000
QueryRestrictingInfo = 4100
)
type RestrictingContract struct {
Plugin *plugin.RestrictingPlugin
Contract *Contract
Evm *EVM
}
func (rc *RestrictingContract) RequiredGas(input []byte) uint64 {
if checkInputEmpty(input) {
return 0
}
return params.RestrictingPlanGas
}
func (rc *RestrictingContract) Run(input []byte) ([]byte, error) {
if checkInputEmpty(input) {
return nil, nil
}
return execPlatonContract(input, rc.FnSigns())
}
func (rc *RestrictingContract) FnSigns() map[uint16]interface{} {
return map[uint16]interface{}{
// Set
TxCreateRestrictingPlan: rc.createRestrictingPlan,
// Get
QueryRestrictingInfo: rc.getRestrictingInfo,
}
}
func (rc *RestrictingContract) CheckGasPrice(gasPrice *big.Int, fcode uint16) error {
return nil
}
// createRestrictingPlan is a Alaya precompiled contract function, used for create a restricting plan
func (rc *RestrictingContract) createRestrictingPlan(account common.Address, plans []restricting.RestrictingPlan) ([]byte, error) {
//sender := rc.Contract.Caller()
from := rc.Contract.CallerAddress
txHash := rc.Evm.StateDB.TxHash()
blockNum := rc.Evm.BlockNumber
blockHash := rc.Evm.BlockHash
state := rc.Evm.StateDB
log.Debug("Call createRestrictingPlan of RestrictingContract", "blockNumber", blockNum.Uint64(),
"blockHash", blockHash.TerminalString(), "txHash", txHash.Hex(), "from", from.String(), "account", account.String())
if !rc.Contract.UseGas(params.CreateRestrictingPlanGas) {
return nil, ErrOutOfGas
}
if !rc.Contract.UseGas(params.ReleasePlanGas * uint64(len(plans))) {
return nil, ErrOutOfGas
}
err := rc.Plugin.AddRestrictingRecord(from, account, blockNum.Uint64(), blockHash, plans, state, txHash)
switch err.(type) {
case nil:
return txResultHandler(vm.RestrictingContractAddr, rc.Evm, "",
"", TxCreateRestrictingPlan, common.NoErr)
case *common.BizError:
bizErr := err.(*common.BizError)
return txResultHandler(vm.RestrictingContractAddr, rc.Evm, "createRestrictingPlan",
bizErr.Error(), TxCreateRestrictingPlan, bizErr)
default:
log.Error("Failed to cal addRestrictingRecord on createRestrictingPlan", "blockNumber", blockNum.Uint64(),
"blockHash", blockHash.TerminalString(), "txHash", txHash.Hex(), "error", err)
return nil, err
}
}
// createRestrictingPlan is a Alaya precompiled contract function, used for getting restricting info.
// first output param is a slice of byte of restricting info;
// the secend output param is the result what plugin executed GetRestrictingInfo returns.
func (rc *RestrictingContract) getRestrictingInfo(account common.Address) ([]byte, error) {
state := rc.Evm.StateDB
result, err := rc.Plugin.GetRestrictingInfo(account, state)
return callResultHandler(rc.Evm, fmt.Sprintf("getRestrictingInfo, account: %s", account.String()),
result, err), nil
}