-
Notifications
You must be signed in to change notification settings - Fork 46
/
base.go
92 lines (79 loc) · 2.21 KB
/
base.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
package app
import (
"github.com/iov-one/weave"
"github.com/iov-one/weave/errors"
abci "github.com/tendermint/tendermint/abci/types"
)
// BaseApp adds DeliverTx, CheckTx, and BeginBlock
// handlers to the storage and query functionality of StoreApp
type BaseApp struct {
*StoreApp
decoder weave.TxDecoder
handler weave.Handler
ticker weave.Ticker
debug bool
}
var _ abci.Application = BaseApp{}
// NewBaseApp constructs a basic abci application
func NewBaseApp(
store *StoreApp,
decoder weave.TxDecoder,
handler weave.Handler,
ticker weave.Ticker,
debug bool,
) BaseApp {
return BaseApp{
StoreApp: store,
decoder: decoder,
handler: handler,
ticker: ticker,
debug: debug,
}
}
// DeliverTx - ABCI - dispatches to the handler
func (b BaseApp) DeliverTx(txBytes []byte) abci.ResponseDeliverTx {
tx, err := b.loadTx(txBytes)
if err != nil {
return weave.DeliverTxError(err, b.debug)
}
// ignore error here, allow it to be logged
ctx := weave.WithLogInfo(b.BlockContext(),
"call", "deliver_tx",
"path", weave.GetPath(tx))
res, err := b.handler.Deliver(ctx, b.DeliverStore(), tx)
if err == nil {
b.AddValChange(res.Diff)
}
return weave.DeliverOrError(res, err, b.debug)
}
// CheckTx - ABCI - dispatches to the handler
func (b BaseApp) CheckTx(txBytes []byte) abci.ResponseCheckTx {
tx, err := b.loadTx(txBytes)
if err != nil {
return weave.CheckTxError(err, b.debug)
}
ctx := weave.WithLogInfo(b.BlockContext(),
"call", "check_tx",
"path", weave.GetPath(tx))
res, err := b.handler.Check(ctx, b.CheckStore(), tx)
return weave.CheckOrError(res, err, b.debug)
}
// BeginBlock - ABCI
func (b BaseApp) BeginBlock(req abci.RequestBeginBlock) abci.ResponseBeginBlock {
// default: set the context properly
b.StoreApp.BeginBlock(req)
var response abci.ResponseBeginBlock
if b.ticker != nil {
ctx := weave.WithLogInfo(b.BlockContext(), "call", "begin_block")
tr := b.ticker.Tick(ctx, b.DeliverStore())
response.Tags = append(response.Tags, tr.Tags...)
b.AddValChange(tr.Diff)
}
return response
}
// loadTx calls the decoder, and capture any panics
func (b BaseApp) loadTx(txBytes []byte) (tx weave.Tx, err error) {
defer errors.Recover(&err)
tx, err = b.decoder(txBytes)
return
}