/
application.go
83 lines (71 loc) · 2.27 KB
/
application.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
package signedpost
import (
"github.com/ethanfrey/signedpost/redux"
"github.com/ethanfrey/tenderize/sign"
merkle "github.com/tendermint/go-merkle"
tmsp "github.com/tendermint/tmsp/types"
)
// Application is the TMSP application for modifying state
type Application struct {
commited *redux.Service
check *redux.Service
}
// NewApp creates a new tmsp application
func NewApp(tree merkle.Tree) *Application {
a := Application{
commited: redux.New(tree, 0),
}
a.check = a.commited.Copy()
return &a
}
// Info is a placeholder
func (app *Application) Info() string {
return app.commited.Info()
}
// SetOption is ignored for now
func (app *Application) SetOption(key, value string) string {
return "ignored"
}
// AppendTx actually does something
func (app *Application) AppendTx(tx []byte) tmsp.Result {
action, err := sign.Receive(tx)
if err != nil {
return tmsp.NewError(tmsp.CodeType_BaseInvalidInput, err.Error())
}
return app.commited.Apply(action)
}
// CheckTx validates a tx for the mempool
func (app *Application) CheckTx(tx []byte) tmsp.Result {
action, err := sign.Receive(tx)
if err != nil {
return tmsp.NewError(tmsp.CodeType_BaseInvalidInput, err.Error())
}
return app.check.Apply(action)
}
// Query returns contents behind given key
func (app *Application) Query(query []byte) tmsp.Result {
_, val, exists := app.commited.GetDB().Get(query)
if !exists {
return tmsp.NewError(tmsp.CodeType_BaseUnknownAddress, "")
}
return tmsp.NewResultOK(val, "")
}
// Commit returns the application Merkle root hash
func (app *Application) Commit() tmsp.Result {
app.check = app.commited.Copy()
hash := app.commited.Hash()
return tmsp.NewResultOK(hash, "")
}
// InitChain make it blockchain aware
// (but we ignore all but BeginBlock)
func (app *Application) InitChain(validators []*tmsp.Validator) {}
// BeginBlock signals the beginning of a block, update service so we tag posts properly
func (app *Application) BeginBlock(height uint64) {
// TODO: this is never called in the current code, so we make do with EndBlock, implying a begin block
}
// EndBlock signals the end of a block, ignored now
// diffs: changed validators from app to TendermintCore
func (app *Application) EndBlock(height uint64) (diffs []*tmsp.Validator) {
app.commited.SetHeight(height + 1)
return nil
}