This repository has been archived by the owner on Jul 6, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
/
execution.go
310 lines (276 loc) · 8.84 KB
/
execution.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package state
import (
abci "github.com/tendermint/abci/types"
cmn "github.com/tendermint/tmlibs/common"
"github.com/tendermint/tmlibs/events"
"github.com/tendermint/basecoin/plugins/ibc"
"github.com/tendermint/basecoin/types"
)
// If the tx is invalid, a TMSP error will be returned.
func ExecTx(state *State, pgz *types.Plugins, tx types.Tx, isCheckTx bool, evc events.Fireable) abci.Result {
chainID := state.GetChainID()
// Exec tx
switch tx := tx.(type) {
case *types.SendTx:
// Validate inputs and outputs, basic
res := validateInputsBasic(tx.Inputs)
if res.IsErr() {
return res.PrependLog("in validateInputsBasic()")
}
res = validateOutputsBasic(tx.Outputs)
if res.IsErr() {
return res.PrependLog("in validateOutputsBasic()")
}
// Get inputs
accounts, res := getInputs(state, tx.Inputs)
if res.IsErr() {
return res.PrependLog("in getInputs()")
}
// Get or make outputs.
accounts, res = getOrMakeOutputs(state, accounts, tx.Outputs)
if res.IsErr() {
return res.PrependLog("in getOrMakeOutputs()")
}
// Validate inputs and outputs, advanced
signBytes := tx.SignBytes(chainID)
inTotal, res := validateInputsAdvanced(accounts, signBytes, tx.Inputs)
if res.IsErr() {
return res.PrependLog("in validateInputsAdvanced()")
}
outTotal := sumOutputs(tx.Outputs)
outPlusFees := outTotal
fees := types.Coins{tx.Fee}
if fees.IsValid() { // TODO: fix coins.Plus()
outPlusFees = outTotal.Plus(fees)
}
if !inTotal.IsEqual(outPlusFees) {
return abci.ErrBaseInvalidOutput.AppendLog(cmn.Fmt("Input total (%v) != output total + fees (%v)", inTotal, outPlusFees))
}
// TODO: Fee validation for SendTx
// Good! Adjust accounts
adjustByInputs(state, accounts, tx.Inputs)
adjustByOutputs(state, accounts, tx.Outputs, isCheckTx)
/*
// Fire events
if !isCheckTx {
if evc != nil {
for _, i := range tx.Inputs {
evc.FireEvent(types.EventStringAccInput(i.Address), types.EventDataTx{tx, nil, ""})
}
for _, o := range tx.Outputs {
evc.FireEvent(types.EventStringAccOutput(o.Address), types.EventDataTx{tx, nil, ""})
}
}
}
*/
return abci.NewResultOK(types.TxID(chainID, tx), "")
case *types.AppTx:
// Validate input, basic
res := tx.Input.ValidateBasic()
if res.IsErr() {
return res
}
// Get input account
inAcc := state.GetAccount(tx.Input.Address)
if inAcc == nil {
return abci.ErrBaseUnknownAddress
}
if !tx.Input.PubKey.Empty() {
inAcc.PubKey = tx.Input.PubKey
}
// Validate input, advanced
signBytes := tx.SignBytes(chainID)
res = validateInputAdvanced(inAcc, signBytes, tx.Input)
if res.IsErr() {
state.logger.Info(cmn.Fmt("validateInputAdvanced failed on %X: %v", tx.Input.Address, res))
return res.PrependLog("in validateInputAdvanced()")
}
if !tx.Input.Coins.IsGTE(types.Coins{tx.Fee}) {
state.logger.Info(cmn.Fmt("Sender did not send enough to cover the fee %X", tx.Input.Address))
return abci.ErrBaseInsufficientFunds.AppendLog(cmn.Fmt("input coins is %v, but fee is %v", tx.Input.Coins, types.Coins{tx.Fee}))
}
// Validate call address
plugin := pgz.GetByName(tx.Name)
if plugin == nil {
return abci.ErrBaseUnknownAddress.AppendLog(
cmn.Fmt("Unrecognized plugin name%v", tx.Name))
}
// Good!
coins := tx.Input.Coins.Minus(types.Coins{tx.Fee})
inAcc.Sequence += 1
inAcc.Balance = inAcc.Balance.Minus(tx.Input.Coins)
// If this is a CheckTx, stop now.
if isCheckTx {
state.SetAccount(tx.Input.Address, inAcc)
return abci.OK
}
// Create inAcc checkpoint
inAccCopy := inAcc.Copy()
// Run the tx.
cache := state.CacheWrap()
cache.SetAccount(tx.Input.Address, inAcc)
ctx := types.NewCallContext(tx.Input.Address, inAcc, coins)
res = plugin.RunTx(cache, ctx, tx.Data)
if res.IsOK() {
cache.CacheSync()
state.logger.Info("Successful execution")
// Fire events
/*
if evc != nil {
exception := ""
if res.IsErr() {
exception = res.Error()
}
evc.FireEvent(types.EventStringAccInput(tx.Input.Address), types.EventDataTx{tx, ret, exception})
evc.FireEvent(types.EventStringAccOutput(tx.Address), types.EventDataTx{tx, ret, exception})
}
*/
} else {
state.logger.Info("AppTx failed", "error", res)
// Just return the coins and return.
inAccCopy.Balance = inAccCopy.Balance.Plus(coins)
// But take the gas
// TODO
state.SetAccount(tx.Input.Address, inAccCopy)
}
return res
default:
return abci.ErrBaseEncodingError.SetLog("Unknown tx type")
}
}
//--------------------------------------------------------------------------------
// The accounts from the TxInputs must either already have
// crypto.PubKey.(type) != nil, (it must be known),
// or it must be specified in the TxInput.
func getInputs(state types.AccountGetter, ins []types.TxInput) (map[string]*types.Account, abci.Result) {
accounts := map[string]*types.Account{}
for _, in := range ins {
// Account shouldn't be duplicated
if _, ok := accounts[string(in.Address)]; ok {
return nil, abci.ErrBaseDuplicateAddress
}
acc := state.GetAccount(in.Address)
if acc == nil {
return nil, abci.ErrBaseUnknownAddress
}
if !in.PubKey.Empty() {
acc.PubKey = in.PubKey
}
accounts[string(in.Address)] = acc
}
return accounts, abci.OK
}
func getOrMakeOutputs(state types.AccountGetter, accounts map[string]*types.Account, outs []types.TxOutput) (map[string]*types.Account, abci.Result) {
if accounts == nil {
accounts = make(map[string]*types.Account)
}
for _, out := range outs {
chain, outAddress, _ := out.ChainAndAddress() // already validated
if chain != nil {
// we dont need an account for the other chain.
// we'll just create an outgoing ibc packet
continue
}
// Account shouldn't be duplicated
if _, ok := accounts[string(outAddress)]; ok {
return nil, abci.ErrBaseDuplicateAddress
}
acc := state.GetAccount(outAddress)
// output account may be nil (new)
if acc == nil {
// zero value is valid, empty account
acc = &types.Account{}
}
accounts[string(outAddress)] = acc
}
return accounts, abci.OK
}
// Validate inputs basic structure
func validateInputsBasic(ins []types.TxInput) (res abci.Result) {
for _, in := range ins {
// Check TxInput basic
if res := in.ValidateBasic(); res.IsErr() {
return res
}
}
return abci.OK
}
// Validate inputs and compute total amount of coins
func validateInputsAdvanced(accounts map[string]*types.Account, signBytes []byte, ins []types.TxInput) (total types.Coins, res abci.Result) {
for _, in := range ins {
acc := accounts[string(in.Address)]
if acc == nil {
cmn.PanicSanity("validateInputsAdvanced() expects account in accounts")
}
res = validateInputAdvanced(acc, signBytes, in)
if res.IsErr() {
return
}
// Good. Add amount to total
total = total.Plus(in.Coins)
}
return total, abci.OK
}
func validateInputAdvanced(acc *types.Account, signBytes []byte, in types.TxInput) (res abci.Result) {
// Check sequence/coins
seq, balance := acc.Sequence, acc.Balance
if seq+1 != in.Sequence {
return abci.ErrBaseInvalidSequence.AppendLog(cmn.Fmt("Got %v, expected %v. (acc.seq=%v)", in.Sequence, seq+1, acc.Sequence))
}
// Check amount
if !balance.IsGTE(in.Coins) {
return abci.ErrBaseInsufficientFunds.AppendLog(cmn.Fmt("balance is %v, tried to send %v", balance, in.Coins))
}
// Check signatures
if !acc.PubKey.VerifyBytes(signBytes, in.Signature) {
return abci.ErrBaseInvalidSignature.AppendLog(cmn.Fmt("SignBytes: %X", signBytes))
}
return abci.OK
}
func validateOutputsBasic(outs []types.TxOutput) (res abci.Result) {
for _, out := range outs {
// Check TxOutput basic
if res := out.ValidateBasic(); res.IsErr() {
return res
}
}
return abci.OK
}
func sumOutputs(outs []types.TxOutput) (total types.Coins) {
for _, out := range outs {
total = total.Plus(out.Coins)
}
return total
}
func adjustByInputs(state types.AccountSetter, accounts map[string]*types.Account, ins []types.TxInput) {
for _, in := range ins {
acc := accounts[string(in.Address)]
if acc == nil {
cmn.PanicSanity("adjustByInputs() expects account in accounts")
}
if !acc.Balance.IsGTE(in.Coins) {
cmn.PanicSanity("adjustByInputs() expects sufficient funds")
}
acc.Balance = acc.Balance.Minus(in.Coins)
acc.Sequence += 1
state.SetAccount(in.Address, acc)
}
}
func adjustByOutputs(state *State, accounts map[string]*types.Account, outs []types.TxOutput, isCheckTx bool) {
for _, out := range outs {
destChain, outAddress, _ := out.ChainAndAddress() // already validated
if destChain != nil {
payload := ibc.CoinsPayload{outAddress, out.Coins}
ibc.SaveNewIBCPacket(state, state.GetChainID(), string(destChain), payload)
continue
}
acc := accounts[string(outAddress)]
if acc == nil {
cmn.PanicSanity("adjustByOutputs() expects account in accounts")
}
acc.Balance = acc.Balance.Plus(out.Coins)
if !isCheckTx {
state.SetAccount(outAddress, acc)
}
}
}