-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
105 lines (85 loc) · 2.52 KB
/
handler.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
package app
import (
"encoding/json"
"strings"
ethTypes "github.com/ethereum/go-ethereum/core/types"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/blockservice/echoin/modules/governance"
"github.com/blockservice/echoin/modules/stake"
"github.com/blockservice/echoin/sdk"
"github.com/blockservice/echoin/sdk/errors"
"github.com/blockservice/echoin/sdk/state"
"github.com/blockservice/echoin/types"
)
func (app BaseApp) checkHandler(ctx types.Context, store state.SimpleDB, tx *ethTypes.Transaction) abci.ResponseCheckTx {
currentState, from, nonce, resp := app.EthApp.basicCheck(tx)
if resp.Code != abci.CodeTypeOK {
return resp
}
ctx.WithSigners(from)
ctx.SetNonce(nonce)
var echoinTx sdk.Tx
if err := json.Unmarshal(tx.Data(), &echoinTx); err != nil {
return errors.CheckResult(err)
}
name, err := lookupRoute(echoinTx)
if err != nil {
return errors.CheckResult(err)
}
var res sdk.CheckResult
if name == "stake" {
res, err = stake.CheckTx(ctx, store, echoinTx)
} else if name == "governance" {
res, err = governance.CheckTx(ctx, store, echoinTx)
}
if err != nil {
return errors.CheckResult(err)
}
currentState.SetNonce(from, nonce+1)
return res.ToABCI()
}
func (app BaseApp) deliverHandler(ctx types.Context, store state.SimpleDB, tx *ethTypes.Transaction) abci.ResponseDeliverTx {
hash := tx.Hash().Bytes()
var echoinTx sdk.Tx
if err := json.Unmarshal(tx.Data(), &echoinTx); err != nil {
return errors.DeliverResult(err)
}
signer := ethTypes.NewEIP155Signer(tx.ChainId())
// Make sure the transaction is signed properly
from, err := ethTypes.Sender(signer, tx)
if err != nil {
return errors.DeliverResult(err)
}
// increase nonce
app.EthApp.DeliverTxState().SetNonce(from, tx.Nonce()+1)
ctx.WithSigners(from)
ctx.SetNonce(tx.Nonce())
name, err := lookupRoute(echoinTx)
if err != nil {
return errors.DeliverResult(err)
}
var res sdk.DeliverResult
switch name {
case "stake":
res, err = stake.DeliverTx(ctx, store, echoinTx, hash)
case "governance":
res, err = governance.DeliverTx(ctx, store, echoinTx, hash)
default:
return errors.DeliverResult(errors.ErrUnknownTxType(echoinTx.Unwrap()))
}
if err != nil {
return errors.DeliverResult(err)
}
// accumulate gasFee
app.StoreApp.TotalUsedGasFee.Add(app.StoreApp.TotalUsedGasFee, res.GasFee)
return res.ToABCI()
}
func lookupRoute(tx sdk.Tx) (string, error) {
kind, err := tx.GetKind()
if err != nil {
return "", err
}
// grab everything before the /
name := strings.SplitN(kind, "/", 2)[0]
return name, nil
}