-
Notifications
You must be signed in to change notification settings - Fork 31
/
slashing_contract.go
131 lines (109 loc) · 4.17 KB
/
slashing_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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// 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/p2p/discover"
"github.com/AlayaNetwork/Alaya-Go/common/consensus"
"github.com/AlayaNetwork/Alaya-Go/common/vm"
"github.com/AlayaNetwork/Alaya-Go/common/hexutil"
"github.com/AlayaNetwork/Alaya-Go/log"
"github.com/AlayaNetwork/Alaya-Go/params"
"github.com/AlayaNetwork/Alaya-Go/common"
"github.com/AlayaNetwork/Alaya-Go/x/plugin"
)
const (
TxReportDuplicateSign = 3000
CheckDuplicateSign = 3001
)
type SlashingContract struct {
Plugin *plugin.SlashingPlugin
Contract *Contract
Evm *EVM
}
func (sc *SlashingContract) RequiredGas(input []byte) uint64 {
if checkInputEmpty(input) {
return 0
}
return params.SlashingGas
}
func (sc *SlashingContract) Run(input []byte) ([]byte, error) {
if checkInputEmpty(input) {
return nil, nil
}
return execPlatonContract(input, sc.FnSigns())
}
func (sc *SlashingContract) FnSigns() map[uint16]interface{} {
return map[uint16]interface{}{
// Set
TxReportDuplicateSign: sc.reportDuplicateSign,
// Get
CheckDuplicateSign: sc.checkDuplicateSign,
}
}
func (sc *SlashingContract) CheckGasPrice(gasPrice *big.Int, fcode uint16) error {
return nil
}
// Report the double signing behavior of the node
func (sc *SlashingContract) reportDuplicateSign(dupType uint8, data string) ([]byte, error) {
txHash := sc.Evm.StateDB.TxHash()
blockNumber := sc.Evm.BlockNumber
blockHash := sc.Evm.BlockHash
from := sc.Contract.CallerAddress
if !sc.Contract.UseGas(params.ReportDuplicateSignGas) {
return nil, ErrOutOfGas
}
if !sc.Contract.UseGas(params.DuplicateEvidencesGas) {
return nil, ErrOutOfGas
}
log.Debug("Call reportDuplicateSign", "blockNumber", blockNumber, "blockHash", blockHash.Hex(),
"TxHash", txHash.Hex(), "from", from.String())
evidence, err := sc.Plugin.DecodeEvidence(consensus.EvidenceType(dupType), data)
if nil != err {
return txResultHandler(vm.SlashingContractAddr, sc.Evm, "reportDuplicateSign",
common.InvalidParameter.Wrap(err.Error()).Error(),
TxReportDuplicateSign, common.InvalidParameter)
}
if txHash == common.ZeroHash {
return nil, nil
}
if err := sc.Plugin.Slash(evidence, blockHash, blockNumber.Uint64(), sc.Evm.StateDB, from); nil != err {
if bizErr, ok := err.(*common.BizError); ok {
return txResultHandler(vm.SlashingContractAddr, sc.Evm, "reportDuplicateSign",
bizErr.Error(), TxReportDuplicateSign, bizErr)
} else {
return nil, err
}
}
return txResultHandler(vm.SlashingContractAddr, sc.Evm, "",
"", TxReportDuplicateSign, common.NoErr)
}
// Check if the node has double sign behavior at a certain block height
func (sc *SlashingContract) checkDuplicateSign(dupType uint8, nodeId discover.NodeID, blockNumber uint64) ([]byte, error) {
log.Info("checkDuplicateSign exist", "blockNumber", blockNumber, "nodeId", nodeId.TerminalString(), "dupType", dupType)
txHash, err := sc.Plugin.CheckDuplicateSign(nodeId, blockNumber, consensus.EvidenceType(dupType), sc.Evm.StateDB)
var data string
if nil != err {
return callResultHandler(sc.Evm, fmt.Sprintf("checkDuplicateSign, duplicateSignBlockNum: %d, nodeId: %s, dupType: %d",
blockNumber, nodeId, dupType), data, common.InternalError.Wrap(err.Error())), nil
}
if len(txHash) > 0 {
data = hexutil.Encode(txHash)
}
return callResultHandler(sc.Evm, fmt.Sprintf("checkDuplicateSign, duplicateSignBlockNum: %d, nodeId: %s, dupType: %d",
blockNumber, nodeId, dupType), data, nil), nil
}