-
Notifications
You must be signed in to change notification settings - Fork 42
/
tx_validate.go
159 lines (136 loc) · 5.03 KB
/
tx_validate.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
// Copyright (c) 2019 Perlin
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package wavelet
import (
"github.com/perlin-network/wavelet/avl"
"github.com/perlin-network/wavelet/sys"
"github.com/pkg/errors"
)
var ErrContractAlreadyExists = errors.New("contract: already exists")
// ValidateTransaction validates signature, and state to make sure that the transaction is acceptable.
func ValidateTransaction(snapshot *avl.Tree, tx Transaction) error {
return validateTransaction(snapshot, tx, true)
}
func validateTransaction(snapshot *avl.Tree, tx Transaction, verifySignature bool) error {
if verifySignature && !tx.VerifySignature() {
return ErrTxInvalidSignature
}
switch tx.Tag {
case sys.TagTransfer:
return validateTransferTransaction(snapshot, tx)
case sys.TagStake:
return validateStakeTransaction(snapshot, tx)
case sys.TagContract:
return validateContractTransaction(snapshot, tx)
case sys.TagBatch:
return validateBatchTransaction(snapshot, tx)
}
return nil
}
func validateTransferTransaction(snapshot *avl.Tree, tx Transaction) error {
payload, err := ParseTransfer(tx.Payload)
if err != nil {
return errors.Wrap(err, "could not parse transfer payload")
}
_, codeAvailable := ReadAccountContractCode(snapshot, payload.Recipient)
if !codeAvailable && (payload.GasLimit > 0 || len(payload.FuncName) > 0 || len(payload.FuncParams) > 0) {
return errors.New(
"transfer: transactions to non-contract accounts should not specify gas limit or function names or params",
)
}
if bal, exist := ReadAccountBalance(snapshot, tx.Sender); !exist {
return errors.New("sender does not exist")
} else if bal < tx.Fee()+payload.Amount+payload.GasLimit+payload.GasDeposit {
return errors.Errorf("sender current balance %d is not enough", bal)
}
return nil
}
func validateStakeTransaction(snapshot *avl.Tree, tx Transaction) error {
payload, err := ParseStake(tx.Payload)
if err != nil {
return err
}
balance, _ := ReadAccountBalance(snapshot, tx.Sender)
stake, _ := ReadAccountStake(snapshot, tx.Sender)
reward, _ := ReadAccountReward(snapshot, tx.Sender)
switch payload.Opcode {
case sys.PlaceStake:
if balance < payload.Amount {
return errors.Errorf(
"stake: %x attempt to place a stake of %d PERLs, but only has %d PERLs",
tx.Sender, payload.Amount, balance,
)
}
case sys.WithdrawStake:
if stake < payload.Amount {
return errors.Errorf(
"stake: %x attempt to withdraw a stake of %d PERLs, but only has staked %d PERLs",
tx.Sender, payload.Amount, stake,
)
}
case sys.WithdrawReward:
if payload.Amount < sys.MinimumRewardWithdraw {
return errors.Errorf(
"stake: %x attempt to withdraw rewards amounting to %d PERLs, but system requires the minimum "+
"amount to withdraw to be %d PERLs",
tx.Sender, payload.Amount, sys.MinimumRewardWithdraw,
)
}
if reward < payload.Amount {
return errors.Errorf(
"stake: %x attempt to withdraw rewards amounting to %d PERLs, but only has rewards amounting "+
"to %d PERLs",
tx.Sender, payload.Amount, reward,
)
}
}
return nil
}
func validateContractTransaction(snapshot *avl.Tree, tx Transaction) error {
payload, err := ParseContract(tx.Payload)
if err != nil {
return err
}
if _, exists := ReadAccountContractCode(snapshot, tx.ID); exists {
return ErrContractAlreadyExists
}
if bal, _ := ReadAccountBalance(snapshot, tx.Sender); bal < tx.Fee()+payload.GasDeposit+payload.GasLimit {
return errors.Errorf("sender current balance %d is not enough", bal)
}
return nil
}
func validateBatchTransaction(snapshot *avl.Tree, tx Transaction) error {
payload, err := ParseBatch(tx.Payload)
if err != nil {
return err
}
for i := uint8(0); i < payload.Size; i++ {
entry := Transaction{
ID: tx.ID,
Sender: tx.Sender,
Nonce: tx.Nonce,
Tag: sys.Tag(payload.Tags[i]),
Payload: payload.Payloads[i],
}
if err := validateTransaction(snapshot, entry, false); err != nil {
return errors.Wrapf(err, "Error while processing %d/%d transaction in a batch.", i+1, payload.Size)
}
}
return nil
}