cosmos-sdk/app/app.go

127 lines
3.0 KiB
Go
Raw Normal View History

package app
2016-02-05 23:16:33 -08:00
import (
2016-03-22 13:07:03 -07:00
"github.com/tendermint/basecoin/state"
2016-02-16 12:29:54 -08:00
"github.com/tendermint/basecoin/types"
2016-03-15 15:01:53 -07:00
. "github.com/tendermint/go-common"
2016-02-05 23:16:33 -08:00
"github.com/tendermint/go-wire"
2016-03-22 13:07:03 -07:00
"github.com/tendermint/governmint/gov"
2016-02-05 23:16:33 -08:00
eyes "github.com/tendermint/merkleeyes/client"
tmsp "github.com/tendermint/tmsp/types"
)
const version = "0.1"
const maxTxSize = 10240
2016-02-16 12:29:54 -08:00
type Basecoin struct {
2016-02-21 15:09:47 -08:00
eyesCli *eyes.Client
2016-03-15 15:01:53 -07:00
govMint *gov.Governmint
2016-03-22 13:07:03 -07:00
state *state.State
2016-02-05 23:16:33 -08:00
}
2016-02-21 15:09:47 -08:00
func NewBasecoin(eyesCli *eyes.Client) *Basecoin {
2016-03-24 12:17:26 -07:00
state_ := state.NewState(eyesCli)
2016-03-22 13:07:03 -07:00
govMint := gov.NewGovernmint(eyesCli)
2016-03-24 12:17:26 -07:00
state_.RegisterPlugin([]byte("gov"), govMint)
2016-02-16 12:29:54 -08:00
return &Basecoin{
2016-02-05 23:16:33 -08:00
eyesCli: eyesCli,
2016-03-22 13:07:03 -07:00
govMint: govMint,
2016-03-24 12:17:26 -07:00
state: state_,
2016-02-05 23:16:33 -08:00
}
}
2016-02-08 18:08:55 -08:00
// TMSP::Info
2016-02-16 12:29:54 -08:00
func (app *Basecoin) Info() string {
2016-03-22 13:07:03 -07:00
return Fmt("Basecoin v%v", version)
2016-02-05 23:16:33 -08:00
}
2016-02-08 18:08:55 -08:00
// TMSP::SetOption
2016-02-16 12:29:54 -08:00
func (app *Basecoin) SetOption(key string, value string) (log string) {
2016-03-22 13:07:03 -07:00
switch key {
case "chainID":
app.state.SetChainID(value)
2016-03-22 15:13:31 -07:00
return "Success"
2016-03-22 13:07:03 -07:00
case "account":
2016-02-05 23:16:33 -08:00
var err error
2016-03-20 03:00:43 -07:00
var setAccount types.Account
2016-02-05 23:16:33 -08:00
wire.ReadJSONPtr(&setAccount, []byte(value), &err)
if err != nil {
return "Error decoding setAccount message: " + err.Error()
2016-02-05 23:16:33 -08:00
}
2016-03-20 03:00:43 -07:00
accBytes := wire.BinaryBytes(setAccount)
2016-03-24 11:27:44 -07:00
res := app.eyesCli.SetSync(setAccount.PubKey.Address(), accBytes)
if res.IsErr() {
return "Error saving account: " + res.Error()
2016-02-05 23:16:33 -08:00
}
return "Success"
}
return "Unrecognized option key " + key
}
2016-02-08 18:08:55 -08:00
// TMSP::AppendTx
2016-03-20 03:00:43 -07:00
func (app *Basecoin) AppendTx(txBytes []byte) (res tmsp.Result) {
2016-02-05 23:16:33 -08:00
if len(txBytes) > maxTxSize {
return tmsp.ErrBaseEncodingError.AppendLog("Tx size exceeds maximum")
2016-02-05 23:16:33 -08:00
}
// Decode tx
var tx types.Tx
err := wire.ReadBinaryBytes(txBytes, &tx)
if err != nil {
return tmsp.ErrBaseEncodingError.AppendLog("Error decoding tx: " + err.Error())
2016-02-05 23:16:33 -08:00
}
2016-03-22 13:07:03 -07:00
// Validate and exec tx
res = state.ExecTx(app.state, tx, false, nil)
if res.IsErr() {
2016-03-22 13:07:03 -07:00
return res.PrependLog("Error in AppendTx")
2016-02-05 23:16:33 -08:00
}
return tmsp.OK
2016-02-05 23:16:33 -08:00
}
2016-02-08 18:08:55 -08:00
// TMSP::CheckTx
2016-03-20 03:00:43 -07:00
func (app *Basecoin) CheckTx(txBytes []byte) (res tmsp.Result) {
2016-02-05 23:16:33 -08:00
if len(txBytes) > maxTxSize {
return tmsp.ErrBaseEncodingError.AppendLog("Tx size exceeds maximum")
2016-02-05 23:16:33 -08:00
}
// Decode tx
var tx types.Tx
err := wire.ReadBinaryBytes(txBytes, &tx)
if err != nil {
return tmsp.ErrBaseEncodingError.AppendLog("Error decoding tx: " + err.Error())
2016-02-05 23:16:33 -08:00
}
// Validate tx
2016-03-22 13:07:03 -07:00
res = state.ExecTx(app.state, tx, true, nil)
if res.IsErr() {
2016-03-22 13:07:03 -07:00
return res.PrependLog("Error in CheckTx")
2016-02-05 23:16:33 -08:00
}
return tmsp.OK
2016-02-05 23:16:33 -08:00
}
2016-02-08 18:08:55 -08:00
// TMSP::Query
2016-03-20 03:00:43 -07:00
func (app *Basecoin) Query(query []byte) (res tmsp.Result) {
res = app.eyesCli.GetSync(query)
if res.IsErr() {
return res.PrependLog("Error querying eyesCli")
2016-02-05 23:16:33 -08:00
}
return res
2016-02-05 23:16:33 -08:00
}
2016-02-14 13:12:38 -08:00
// TMSP::Commit
func (app *Basecoin) Commit() (res tmsp.Result) {
res = app.eyesCli.CommitSync()
if res.IsErr() {
panic("Error getting hash: " + res.Error())
2016-02-05 23:16:33 -08:00
}
return res
2016-02-05 23:16:33 -08:00
}
2016-03-15 15:01:53 -07:00
// TMSP::InitChain
func (app *Basecoin) InitChain(validators []*tmsp.Validator) {
app.govMint.InitChain(validators)
}
// TMSP::EndBlock
func (app *Basecoin) EndBlock(height uint64) []*tmsp.Validator {
2016-03-22 15:13:31 -07:00
app.state.ResetCacheState()
2016-03-15 15:01:53 -07:00
return app.govMint.EndBlock(height)
}