cosmos-sdk/app/app.go

127 lines
3.2 KiB
Go
Raw Normal View History

package app
2016-02-05 23:16:33 -08:00
import (
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-15 15:01:53 -07:00
gov "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-02-05 23:16:33 -08:00
}
2016-02-21 15:09:47 -08:00
func NewBasecoin(eyesCli *eyes.Client) *Basecoin {
2016-02-16 12:29:54 -08:00
return &Basecoin{
2016-02-05 23:16:33 -08:00
eyesCli: eyesCli,
2016-03-15 15:01:53 -07:00
govMint: gov.NewGovernmint(eyesCli),
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-15 15:01:53 -07:00
return Fmt("Basecoin v%v\n - %v", version, app.govMint.Info())
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-02-05 23:16:33 -08:00
if key == "setAccount" {
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)
err = app.eyesCli.SetSync(setAccount.PubKey.Address(), accBytes)
2016-02-05 23:16:33 -08:00
if err != nil {
return "Error saving account: " + err.Error()
}
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 {
2016-03-20 03:00:43 -07:00
return types.ErrEncodingError.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 {
2016-03-20 03:00:43 -07:00
return types.ErrEncodingError.AppendLog("Error decoding tx: " + err.Error())
2016-02-05 23:16:33 -08:00
}
// Validate tx
2016-03-20 03:00:43 -07:00
res = validateTx(tx)
if !res.IsOK() {
return res.PrependLog("Error validating tx")
2016-02-05 23:16:33 -08:00
}
// Execute tx
2016-03-20 03:00:43 -07:00
// TODO: get or make state with app.eeysCli, pass it to
// state.execution.go ExecTx
// Synchronize the txCache.
//storeAccounts(app.eyesCli, accs)
return types.ResultOK
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 {
2016-03-20 03:00:43 -07:00
return types.ErrEncodingError.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 {
2016-03-20 03:00:43 -07:00
return types.ErrEncodingError.AppendLog("Error decoding tx: " + err.Error())
2016-02-05 23:16:33 -08:00
}
// Validate tx
2016-03-20 03:00:43 -07:00
res = validateTx(tx)
if !res.IsOK() {
return res.PrependLog("Error validating tx")
2016-02-05 23:16:33 -08:00
}
// Execute tx
2016-03-20 03:00:43 -07:00
// TODO: get or make state with app.eeysCli, pass it to
// state.execution.go ExecTx
// Synchronize the txCache.
//storeAccounts(app.eyesCli, accs)
return types.ResultOK.SetLog("Success")
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) {
return types.ResultOK
2016-02-05 23:16:33 -08:00
value, err := app.eyesCli.GetSync(query)
if err != nil {
panic("Error making query: " + err.Error())
}
2016-03-20 03:00:43 -07:00
return types.ResultOK.SetData(value).SetLog("Success")
2016-02-05 23:16:33 -08:00
}
2016-02-14 13:12:38 -08:00
// TMSP::Commit
2016-02-16 12:29:54 -08:00
func (app *Basecoin) Commit() (hash []byte, log string) {
2016-02-14 13:12:38 -08:00
hash, log, err := app.eyesCli.CommitSync()
2016-02-05 23:16:33 -08:00
if err != nil {
panic("Error getting hash: " + err.Error())
}
return hash, "Success"
}
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 {
return app.govMint.EndBlock(height)
}