-
Notifications
You must be signed in to change notification settings - Fork 0
/
tx_slash.go
201 lines (162 loc) · 6.49 KB
/
tx_slash.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
package execution
import (
"math/big"
"github.com/dnerochain/dnero/common"
"github.com/dnerochain/dnero/common/result"
"github.com/dnerochain/dnero/core"
st "github.com/dnerochain/dnero/ledger/state"
"github.com/dnerochain/dnero/ledger/types"
)
var _ TxExecutor = (*SlashTxExecutor)(nil)
// ------------------------------- Slash Transaction -----------------------------------
type SlashTxExecutor struct {
consensus core.ConsensusEngine
valMgr core.ValidatorManager
}
// NewSlashTxExecutor creates a new instance of SlashTxExecutor
func NewSlashTxExecutor(consensus core.ConsensusEngine, valMgr core.ValidatorManager) *SlashTxExecutor {
return &SlashTxExecutor{
consensus: consensus,
valMgr: valMgr,
}
}
func (exec *SlashTxExecutor) sanityCheck(chainID string, view *st.StoreView, transaction types.Tx) result.Result {
tx := transaction.(*types.SlashTx)
validatorSet := getValidatorSet(exec.consensus.GetLedger(), exec.valMgr)
validatorAddresses := getValidatorAddresses(validatorSet)
// Validate proposer, basic
res := tx.Proposer.ValidateBasic()
if res.IsError() {
return res
}
// verify the proposer is one of the validators
res = isAValidator(tx.Proposer.Address, validatorAddresses)
if res.IsError() {
return res
}
proposerAccount, res := getInput(view, tx.Proposer)
if res.IsError() {
return res
}
// verify the proposer's signature
signBytes := tx.SignBytes(chainID)
if !tx.Proposer.Signature.Verify(signBytes, proposerAccount.Address) {
return result.Error("SignBytes: %X", signBytes)
}
slashedAddress := tx.SlashedAddress
slashedAccount := view.GetAccount(slashedAddress)
if slashedAccount == nil {
return result.Error("Account %v does not exist!", slashedAddress)
}
reservedFundFound := false
for _, reservedFund := range slashedAccount.ReservedFunds {
if reservedFund.ReserveSequence == tx.ReserveSequence {
reservedFundFound = true
break
}
}
if !reservedFundFound {
return result.Error("Reserved fund not found for %v", tx.ReserveSequence)
}
validatorAddress := tx.Proposer.Address
validatorAccount := view.GetAccount(validatorAddress)
if validatorAccount == nil {
return result.Error("Validator %v does not exist!", validatorAddress)
}
overspendingProofBytes := tx.SlashProof
slashProofVerified := exec.verifySlashProof(chainID, slashedAccount, overspendingProofBytes)
if !slashProofVerified {
return result.Error("Invalid slash proof: %v", overspendingProofBytes)
}
return result.OK
}
func (exec *SlashTxExecutor) process(chainID string, view *st.StoreView, transaction types.Tx) (common.Hash, result.Result) {
tx := transaction.(*types.SlashTx)
slashedAddress := tx.SlashedAddress
slashedAccount := view.GetAccount(slashedAddress)
var reservedFundIdx int
var reservedFund types.ReservedFund
reservedFundFound := false
for reservedFundIdx, reservedFund = range slashedAccount.ReservedFunds {
if reservedFund.ReserveSequence == tx.ReserveSequence {
reservedFundFound = true
break
}
}
if !reservedFundFound {
return common.Hash{}, result.Error("Reserved fund not found for %v", tx.ReserveSequence)
}
proposerAddress := tx.Proposer.Address
proposerAccount := view.GetAccount(proposerAddress)
if proposerAccount == nil {
return common.Hash{}, result.Error("Proposer %v does not exist!", proposerAddress)
}
// TODO: We should transfer the collateral to a special address, e.g. 0x0 instead of
// transferring to the proposer, so the proposer gain no extra benefit if it colludes with
// the address that overspent
// Slash: transfer the collateral and remainding deposit to the validator that identified the overspending
remainingFund := reservedFund.InitialFund.Minus(reservedFund.UsedFund)
if !remainingFund.IsNonnegative() {
remainingFund = types.NewCoins(0, 0) // Should NOT happen, just to be on the safe side
}
slashedAmount := reservedFund.Collateral.Plus(remainingFund)
proposerAccount.Balance = proposerAccount.Balance.Plus(slashedAmount)
slashedAccount.ReservedFunds = append(slashedAccount.ReservedFunds[:reservedFundIdx],
slashedAccount.ReservedFunds[reservedFundIdx+1:]...)
view.SetAccount(proposerAddress, proposerAccount)
view.SetAccount(slashedAddress, slashedAccount)
txHash := types.TxID(chainID, tx)
return txHash, result.OK
}
func (exec *SlashTxExecutor) verifySlashProof(chainID string, slashedAccount *types.Account, overspendingProofBytes []byte) bool {
var overspendingProof types.OverspendingProof
err := types.FromBytes(overspendingProofBytes, &overspendingProof)
if err != nil {
// TODO: need proper logging and error handling here.
//panic(fmt.Sprintf("Failed to parse overspending proof: %v\n", err))
logger.Errorf("Failed to parse overspending proof: %v", err)
return false
}
slashedAddress := slashedAccount.Address
reserveSequence := overspendingProof.ReserveSequence
for _, reservedFund := range slashedAccount.ReservedFunds {
if reservedFund.ReserveSequence != reserveSequence {
continue
}
settledPaymentLookup := make(map[string]bool)
fundIntendedToSpend := types.NewCoins(0, 0)
for _, servicePaymentTx := range overspendingProof.ServicePayments {
if slashedAddress != servicePaymentTx.Source.Address {
return false // servicePaymentTx does not come from the slashed account
}
if servicePaymentTx.ReserveSequence != overspendingProof.ReserveSequence {
return false // servicePaymentTx does not belong to claimed reserved fund
}
sourceSignedBytes := servicePaymentTx.SourceSignBytes(chainID)
if !servicePaymentTx.Source.Signature.Verify(sourceSignedBytes, slashedAccount.Address) {
return false // servicePaymentTx not signed by the slashed account
}
paymentKey := string(servicePaymentTx.Target.Address[:]) + "." + string(servicePaymentTx.PaymentSequence)
_, targetExists := settledPaymentLookup[paymentKey]
if targetExists {
return false // to prevent using partial payments as proof
}
settledPaymentLookup[paymentKey] = true
fundIntendedToSpend = fundIntendedToSpend.Plus(servicePaymentTx.Source.Coins)
}
fundOverspent := !reservedFund.InitialFund.IsGTE(fundIntendedToSpend)
return fundOverspent
}
return false
}
func (exec *SlashTxExecutor) getTxInfo(transaction types.Tx) *core.TxInfo {
tx := transaction.(*types.SlashTx)
return &core.TxInfo{
Address: tx.Proposer.Address,
Sequence: tx.Proposer.Sequence,
EffectiveGasPrice: exec.calculateEffectiveGasPrice(transaction),
}
}
func (exec *SlashTxExecutor) calculateEffectiveGasPrice(transaction types.Tx) *big.Int {
return new(big.Int).SetUint64(0)
}