cosmos-sdk/app/app.go

222 lines
5.3 KiB
Go
Raw Normal View History

package app
2016-02-05 23:16:33 -08:00
import (
"encoding/hex"
2017-02-24 14:25:48 -08:00
"encoding/json"
"strings"
2017-01-14 20:42:47 -08:00
abci "github.com/tendermint/abci/types"
2017-05-01 07:03:54 -07:00
wire "github.com/tendermint/go-wire"
2016-02-05 23:16:33 -08:00
eyes "github.com/tendermint/merkleeyes/client"
2017-06-21 09:15:11 -07:00
cmn "github.com/tendermint/tmlibs/common"
2017-05-01 07:03:54 -07:00
"github.com/tendermint/tmlibs/log"
sm "github.com/tendermint/basecoin/state"
"github.com/tendermint/basecoin/types"
"github.com/tendermint/basecoin/version"
2016-02-05 23:16:33 -08:00
)
2016-03-27 12:47:50 -07:00
const (
maxTxSize = 10240
2016-04-18 08:09:19 -07:00
PluginNameBase = "base"
2016-03-27 12:47:50 -07:00
)
2016-02-05 23:16:33 -08:00
2016-02-16 12:29:54 -08:00
type Basecoin struct {
2016-05-01 13:52:08 -07:00
eyesCli *eyes.Client
state *sm.State
cacheState *sm.State
plugins *types.Plugins
2017-05-01 07:03:54 -07:00
logger log.Logger
2016-02-05 23:16:33 -08:00
}
2016-02-21 15:09:47 -08:00
func NewBasecoin(eyesCli *eyes.Client) *Basecoin {
2016-05-01 13:52:08 -07:00
state := sm.NewState(eyesCli)
2016-03-27 12:47:50 -07:00
plugins := types.NewPlugins()
2016-02-16 12:29:54 -08:00
return &Basecoin{
2016-05-01 13:52:08 -07:00
eyesCli: eyesCli,
state: state,
cacheState: nil,
plugins: plugins,
2017-05-01 07:03:54 -07:00
logger: log.NewNopLogger(),
2016-02-05 23:16:33 -08:00
}
}
2017-05-01 07:03:54 -07:00
func (app *Basecoin) SetLogger(l log.Logger) {
app.logger = l
app.state.SetLogger(l.With("module", "state"))
2017-05-01 07:03:54 -07:00
}
// XXX For testing, not thread safe!
2017-02-03 13:06:12 -08:00
func (app *Basecoin) GetState() *sm.State {
return app.state.CacheWrap()
}
// ABCI::Info
2017-01-14 20:42:47 -08:00
func (app *Basecoin) Info() abci.ResponseInfo {
2017-06-21 09:15:11 -07:00
resp, err := app.eyesCli.InfoSync()
if err != nil {
cmn.PanicCrisis(err)
}
return abci.ResponseInfo{
Data: cmn.Fmt("Basecoin v%v", version.Version),
LastBlockHeight: resp.LastBlockHeight,
LastBlockAppHash: resp.LastBlockAppHash,
}
2016-02-05 23:16:33 -08:00
}
func (app *Basecoin) RegisterPlugin(plugin types.Plugin) {
app.plugins.RegisterPlugin(plugin)
2017-01-13 01:27:07 -08:00
}
// ABCI::SetOption
2017-02-13 14:04:49 -08:00
func (app *Basecoin) SetOption(key string, value string) string {
pluginName, key := splitKey(key)
if pluginName != PluginNameBase {
// Set option on plugin
2017-02-13 14:04:49 -08:00
plugin := app.plugins.GetByName(pluginName)
if plugin == nil {
2017-02-13 14:04:49 -08:00
return "Invalid plugin name: " + pluginName
2016-02-05 23:16:33 -08:00
}
2017-05-01 07:03:54 -07:00
app.logger.Info("SetOption on plugin", "plugin", pluginName, "key", key, "value", value)
2016-05-01 13:52:08 -07:00
return plugin.SetOption(app.state, key, value)
} else {
// Set option on basecoin
switch key {
2017-03-14 00:11:49 -07:00
case "chain_id":
app.state.SetChainID(value)
return "Success"
case "account":
var acc GenesisAccount
2017-02-24 14:25:48 -08:00
err := json.Unmarshal([]byte(value), &acc)
if err != nil {
2016-03-27 23:04:58 -07:00
return "Error decoding acc message: " + err.Error()
}
acc.Balance.Sort()
addr, err := acc.GetAddr()
if err != nil {
return "Invalid address: " + err.Error()
}
app.state.SetAccount(addr, acc.ToAccount())
app.logger.Info("SetAccount", "addr", hex.EncodeToString(addr), "acc", acc)
2017-03-14 00:11:49 -07:00
return "Success"
2016-02-05 23:16:33 -08:00
}
return "Unrecognized option key " + key
2016-02-05 23:16:33 -08:00
}
}
// ABCI::DeliverTx
2017-01-14 20:42:47 -08:00
func (app *Basecoin) DeliverTx(txBytes []byte) (res abci.Result) {
2016-02-05 23:16:33 -08:00
if len(txBytes) > maxTxSize {
2017-01-14 20:42:47 -08:00
return abci.ErrBaseEncodingError.AppendLog("Tx size exceeds maximum")
2016-02-05 23:16:33 -08:00
}
2017-01-12 12:25:04 -08:00
2016-02-05 23:16:33 -08:00
// Decode tx
var tx types.Tx
err := wire.ReadBinaryBytes(txBytes, &tx)
if err != nil {
2017-01-14 20:42:47 -08:00
return abci.ErrBaseEncodingError.AppendLog("Error decoding tx: " + err.Error())
2016-02-05 23:16:33 -08:00
}
2017-01-12 12:25:04 -08:00
2016-03-22 13:07:03 -07:00
// Validate and exec tx
res = sm.ExecTx(app.state, app.plugins, tx, false, nil)
if res.IsErr() {
2017-01-14 20:42:47 -08:00
return res.PrependLog("Error in DeliverTx")
2016-02-05 23:16:33 -08:00
}
return res
2016-02-05 23:16:33 -08:00
}
// ABCI::CheckTx
2017-01-14 20:42:47 -08:00
func (app *Basecoin) CheckTx(txBytes []byte) (res abci.Result) {
2016-02-05 23:16:33 -08:00
if len(txBytes) > maxTxSize {
2017-01-14 20:42:47 -08:00
return abci.ErrBaseEncodingError.AppendLog("Tx size exceeds maximum")
2016-02-05 23:16:33 -08:00
}
2017-01-12 12:25:04 -08:00
2016-02-05 23:16:33 -08:00
// Decode tx
var tx types.Tx
err := wire.ReadBinaryBytes(txBytes, &tx)
if err != nil {
2017-01-14 20:42:47 -08:00
return abci.ErrBaseEncodingError.AppendLog("Error decoding tx: " + err.Error())
2016-02-05 23:16:33 -08:00
}
2017-01-12 12:25:04 -08:00
2016-02-05 23:16:33 -08:00
// Validate tx
res = sm.ExecTx(app.cacheState, app.plugins, 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
}
2017-01-14 20:42:47 -08:00
return abci.OK
2016-02-05 23:16:33 -08:00
}
// ABCI::Query
2017-01-28 09:29:32 -08:00
func (app *Basecoin) Query(reqQuery abci.RequestQuery) (resQuery abci.ResponseQuery) {
if len(reqQuery.Data) == 0 {
resQuery.Log = "Query cannot be zero length"
resQuery.Code = abci.CodeType_EncodingError
return
2016-03-27 21:51:04 -07:00
}
2017-01-12 12:25:04 -08:00
2017-02-13 07:50:17 -08:00
// handle special path for account info
if reqQuery.Path == "/account" {
reqQuery.Path = "/key"
2017-05-21 15:23:58 -07:00
reqQuery.Data = types.AccountKey(reqQuery.Data)
2017-02-13 07:50:17 -08:00
}
2017-01-28 09:29:32 -08:00
resQuery, err := app.eyesCli.QuerySync(reqQuery)
if err != nil {
resQuery.Log = "Failed to query MerkleEyes: " + err.Error()
resQuery.Code = abci.CodeType_InternalError
return
}
return
2016-02-05 23:16:33 -08:00
}
// ABCI::Commit
2017-01-14 20:42:47 -08:00
func (app *Basecoin) Commit() (res abci.Result) {
2017-01-12 17:49:51 -08:00
// Commit state
res = app.state.Commit()
2017-01-12 12:25:04 -08:00
// Wrap the committed state in cache for CheckTx
app.cacheState = app.state.CacheWrap()
if res.IsErr() {
2017-06-21 09:15:11 -07:00
cmn.PanicSanity("Error getting hash: " + res.Error())
2016-02-05 23:16:33 -08:00
}
return res
2016-02-05 23:16:33 -08:00
}
// ABCI::InitChain
2017-01-14 20:42:47 -08:00
func (app *Basecoin) InitChain(validators []*abci.Validator) {
2016-04-18 08:09:19 -07:00
for _, plugin := range app.plugins.GetList() {
plugin.InitChain(app.state, validators)
2016-04-18 08:09:19 -07:00
}
2016-03-15 15:01:53 -07:00
}
// ABCI::BeginBlock
func (app *Basecoin) BeginBlock(hash []byte, header *abci.Header) {
2016-04-18 08:09:19 -07:00
for _, plugin := range app.plugins.GetList() {
plugin.BeginBlock(app.state, hash, header)
2016-04-18 08:09:19 -07:00
}
2016-03-29 14:25:17 -07:00
}
// ABCI::EndBlock
func (app *Basecoin) EndBlock(height uint64) (res abci.ResponseEndBlock) {
2016-04-18 08:09:19 -07:00
for _, plugin := range app.plugins.GetList() {
pluginRes := plugin.EndBlock(app.state, height)
res.Diffs = append(res.Diffs, pluginRes.Diffs...)
2016-04-18 08:09:19 -07:00
}
return
2016-03-15 15:01:53 -07:00
}
//----------------------------------------
2016-04-17 12:41:26 -07:00
// Splits the string at the first '/'.
// if there are none, the second string is nil.
2016-04-17 12:41:26 -07:00
func splitKey(key string) (prefix string, suffix string) {
2016-03-27 12:47:50 -07:00
if strings.Contains(key, "/") {
keyParts := strings.SplitN(key, "/", 2)
return keyParts[0], keyParts[1]
}
return key, ""
}