2017-10-16 10:21:43 -07:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
2017-12-01 09:10:17 -08:00
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/golang/protobuf/proto"
|
|
|
|
"github.com/pkg/errors"
|
2017-10-16 10:21:43 -07:00
|
|
|
abci "github.com/tendermint/abci/types"
|
2017-12-01 09:10:17 -08:00
|
|
|
cmn "github.com/tendermint/tmlibs/common"
|
|
|
|
"github.com/tendermint/tmlibs/log"
|
2017-10-16 10:21:43 -07:00
|
|
|
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk"
|
|
|
|
)
|
|
|
|
|
2017-12-01 09:10:17 -08:00
|
|
|
const mainKeyHeader = "header"
|
|
|
|
|
2017-12-19 21:04:40 -08:00
|
|
|
// App - The ABCI application
|
|
|
|
type App struct {
|
2017-12-01 09:10:17 -08:00
|
|
|
logger log.Logger
|
|
|
|
|
|
|
|
// App name from abci.Info
|
|
|
|
name string
|
|
|
|
|
|
|
|
// DeliverTx (main) state
|
2017-12-14 06:28:37 -08:00
|
|
|
store MultiStore
|
2017-12-01 09:10:17 -08:00
|
|
|
|
|
|
|
// CheckTx state
|
2017-12-14 06:28:37 -08:00
|
|
|
storeCheck CacheMultiStore
|
2017-12-01 09:10:17 -08:00
|
|
|
|
2017-12-03 21:25:37 -08:00
|
|
|
// Current block header
|
|
|
|
header *abci.Header
|
2017-12-01 09:10:17 -08:00
|
|
|
|
|
|
|
// Handler for CheckTx and DeliverTx.
|
2017-10-16 10:21:43 -07:00
|
|
|
handler sdk.Handler
|
2017-12-03 21:25:37 -08:00
|
|
|
|
|
|
|
// Cached validator changes from DeliverTx
|
2017-12-20 17:34:51 -08:00
|
|
|
valUpdates []abci.Validator
|
2017-10-16 10:21:43 -07:00
|
|
|
}
|
|
|
|
|
2017-12-19 21:04:40 -08:00
|
|
|
var _ abci.Application = &App{}
|
2017-10-16 10:21:43 -07:00
|
|
|
|
2017-12-19 21:04:40 -08:00
|
|
|
func NewApp(name string) *App {
|
|
|
|
return &App{
|
|
|
|
name: name,
|
|
|
|
logger: makeDefaultLogger(),
|
2017-12-01 09:10:17 -08:00
|
|
|
}
|
2017-12-19 21:04:40 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (app *App) SetStore(store MultiStore) {
|
|
|
|
app.store = store
|
|
|
|
}
|
|
|
|
|
|
|
|
func (app *App) SetHandler(handler Handler) {
|
|
|
|
app.handler = handler
|
|
|
|
}
|
2017-12-01 09:10:17 -08:00
|
|
|
|
2017-12-19 21:04:40 -08:00
|
|
|
func (app *App) LoadLatestVersion() error {
|
2017-12-20 17:34:51 -08:00
|
|
|
store := app.store
|
|
|
|
store.LoadLastVersion()
|
|
|
|
return app.initFromStore()
|
2017-12-19 21:04:40 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (app *App) LoadVersion(version int64) error {
|
2017-12-20 17:34:51 -08:00
|
|
|
store := app.store
|
|
|
|
store.LoadVersion(version)
|
|
|
|
return app.initFromStore()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initializes the remaining logic from app.store.
|
|
|
|
func (app *App) initFromStore() error {
|
2017-12-19 21:04:40 -08:00
|
|
|
store := app.store
|
2017-12-14 06:28:37 -08:00
|
|
|
lastCommitID := store.LastCommitID()
|
|
|
|
main := store.GetKVStore("main")
|
2017-12-01 09:10:17 -08:00
|
|
|
header := (*abci.Header)(nil)
|
2017-12-14 06:28:37 -08:00
|
|
|
storeCheck := store.CacheMultiStore()
|
2017-12-01 09:10:17 -08:00
|
|
|
|
2017-12-19 21:04:40 -08:00
|
|
|
// Main store should exist.
|
|
|
|
if store.GetKVStore("main") == nil {
|
|
|
|
return errors.New("App expects MultiStore with 'main' KVStore")
|
|
|
|
}
|
|
|
|
|
2017-12-20 17:34:51 -08:00
|
|
|
// If we've committed before, we expect main://<mainKeyHeader>.
|
2017-12-01 09:10:17 -08:00
|
|
|
if !lastCommitID.IsZero() {
|
|
|
|
headerBytes, ok := main.Get(mainKeyHeader)
|
|
|
|
if !ok {
|
2017-12-19 21:04:40 -08:00
|
|
|
errStr := fmt.Sprintf("Version > 0 but missing key %s", mainKeyHeader)
|
|
|
|
return errors.New(errStr)
|
2017-12-01 09:10:17 -08:00
|
|
|
}
|
|
|
|
err = proto.Unmarshal(headerBytes, header)
|
|
|
|
if err != nil {
|
2017-12-19 21:04:40 -08:00
|
|
|
return errors.Wrap(err, "Failed to parse Header")
|
2017-12-01 09:10:17 -08:00
|
|
|
}
|
2017-12-20 17:34:51 -08:00
|
|
|
if header.Height != lastCommitID.Version {
|
|
|
|
errStr := fmt.Sprintf("Expected main://%s.Height %v but got %v", mainKeyHeader, version, headerHeight)
|
2017-12-19 21:04:40 -08:00
|
|
|
return errors.New(errStr)
|
2017-12-01 09:10:17 -08:00
|
|
|
}
|
|
|
|
}
|
2017-12-20 17:34:51 -08:00
|
|
|
|
|
|
|
// Set App state.
|
|
|
|
app.header = header
|
|
|
|
app.storeCheck = app.store.CacheMultiStore()
|
|
|
|
app.valUpdates = nil
|
|
|
|
|
|
|
|
return nil
|
2017-12-01 09:10:17 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------
|
|
|
|
|
2017-10-16 10:21:43 -07:00
|
|
|
// DeliverTx - ABCI - dispatches to the handler
|
2017-12-19 21:04:40 -08:00
|
|
|
func (app *App) DeliverTx(txBytes []byte) abci.ResponseDeliverTx {
|
2017-10-16 10:21:43 -07:00
|
|
|
|
2017-12-14 06:28:37 -08:00
|
|
|
// Initialize arguments to Handler.
|
|
|
|
var isCheckTx = false
|
|
|
|
var ctx = sdk.NewContext(app.header, isCheckTx, txBytes)
|
|
|
|
var store = app.store
|
|
|
|
var tx Tx = nil // nil until a decorator parses one.
|
|
|
|
|
|
|
|
// Run the handler.
|
|
|
|
var result = app.handler(ctx, app.store, tx)
|
|
|
|
|
|
|
|
// After-handler hooks.
|
2017-12-03 21:25:37 -08:00
|
|
|
if result.Code == abci.CodeType_OK {
|
2017-12-20 17:34:51 -08:00
|
|
|
app.valUpdates = append(app.valUpdates, result.ValUpdate)
|
2017-12-03 21:25:37 -08:00
|
|
|
} else {
|
|
|
|
// Even though the Code is not OK, there will be some side effects,
|
|
|
|
// like those caused by fee deductions or sequence incrementations.
|
2017-10-16 10:21:43 -07:00
|
|
|
}
|
2017-12-14 06:28:37 -08:00
|
|
|
|
|
|
|
// Tell the blockchain engine (i.e. Tendermint).
|
2017-12-01 09:10:17 -08:00
|
|
|
return abci.ResponseDeliverTx{
|
2017-12-03 21:25:37 -08:00
|
|
|
Code: result.Code,
|
|
|
|
Data: result.Data,
|
|
|
|
Log: result.Log,
|
|
|
|
Tags: result.Tags,
|
2017-12-01 09:10:17 -08:00
|
|
|
}
|
2017-10-16 10:21:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// CheckTx - ABCI - dispatches to the handler
|
2017-12-19 21:04:40 -08:00
|
|
|
func (app *App) CheckTx(txBytes []byte) abci.ResponseCheckTx {
|
2017-12-01 09:10:17 -08:00
|
|
|
|
2017-12-14 06:28:37 -08:00
|
|
|
// Initialize arguments to Handler.
|
|
|
|
var isCheckTx = true
|
|
|
|
var ctx = sdk.NewContext(app.header, isCheckTx, txBytes)
|
|
|
|
var store = app.store
|
|
|
|
var tx Tx = nil // nil until a decorator parses one.
|
|
|
|
|
|
|
|
// Run the handler.
|
|
|
|
var result = app.handler(ctx, app.store, tx)
|
|
|
|
|
|
|
|
// Tell the blockchain engine (i.e. Tendermint).
|
|
|
|
return abci.ResponseDeliverTx{
|
2017-12-03 21:25:37 -08:00
|
|
|
Code: result.Code,
|
|
|
|
Data: result.Data,
|
|
|
|
Log: result.Log,
|
|
|
|
Gas: result.Gas,
|
|
|
|
FeeDenom: result.FeeDenom,
|
|
|
|
FeeAmount: result.FeeAmount,
|
2017-10-16 10:21:43 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-01 09:10:17 -08:00
|
|
|
// Info - ABCI
|
2017-12-19 21:04:40 -08:00
|
|
|
func (app *App) Info(req abci.RequestInfo) abci.ResponseInfo {
|
2017-12-01 09:10:17 -08:00
|
|
|
|
2017-12-14 06:28:37 -08:00
|
|
|
lastCommitID := app.store.LastCommitID()
|
2017-12-01 09:10:17 -08:00
|
|
|
|
|
|
|
return abci.ResponseInfo{
|
|
|
|
Data: app.Name,
|
|
|
|
LastBlockHeight: lastCommitID.Version,
|
|
|
|
LastBlockAppHash: lastCommitID.Hash,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetOption - ABCI
|
2017-12-19 21:04:40 -08:00
|
|
|
func (app *App) SetOption(key string, value string) string {
|
2017-12-01 09:10:17 -08:00
|
|
|
return "Not Implemented"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Query - ABCI
|
2017-12-19 21:04:40 -08:00
|
|
|
func (app *App) Query(reqQuery abci.RequestQuery) (resQuery abci.ResponseQuery) {
|
2017-12-14 06:28:37 -08:00
|
|
|
/*
|
|
|
|
XXX Make this work with MultiStore.
|
|
|
|
XXX It will require some interfaces updates in store/types.go.
|
2017-10-16 10:21:43 -07:00
|
|
|
|
2017-12-14 06:28:37 -08:00
|
|
|
if len(reqQuery.Data) == 0 {
|
|
|
|
resQuery.Log = "Query cannot be zero length"
|
|
|
|
resQuery.Code = abci.CodeType_EncodingError
|
|
|
|
return
|
2017-12-01 09:10:17 -08:00
|
|
|
}
|
2017-12-14 06:28:37 -08:00
|
|
|
|
|
|
|
// set the query response height to current
|
|
|
|
tree := app.state.Committed()
|
|
|
|
|
|
|
|
height := reqQuery.Height
|
|
|
|
if height == 0 {
|
|
|
|
// TODO: once the rpc actually passes in non-zero
|
|
|
|
// heights we can use to query right after a tx
|
|
|
|
// we must retrun most recent, even if apphash
|
|
|
|
// is not yet in the blockchain
|
|
|
|
|
|
|
|
withProof := app.CommittedHeight() - 1
|
|
|
|
if tree.Tree.VersionExists(withProof) {
|
|
|
|
height = withProof
|
|
|
|
} else {
|
|
|
|
height = app.CommittedHeight()
|
2017-12-01 09:10:17 -08:00
|
|
|
}
|
|
|
|
}
|
2017-12-14 06:28:37 -08:00
|
|
|
resQuery.Height = height
|
|
|
|
|
|
|
|
switch reqQuery.Path {
|
|
|
|
case "/store", "/key": // Get by key
|
|
|
|
key := reqQuery.Data // Data holds the key bytes
|
|
|
|
resQuery.Key = key
|
|
|
|
if reqQuery.Prove {
|
|
|
|
value, proof, err := tree.GetVersionedWithProof(key, height)
|
|
|
|
if err != nil {
|
|
|
|
resQuery.Log = err.Error()
|
|
|
|
break
|
|
|
|
}
|
|
|
|
resQuery.Value = value
|
|
|
|
resQuery.Proof = proof.Bytes()
|
|
|
|
} else {
|
|
|
|
value := tree.Get(key)
|
|
|
|
resQuery.Value = value
|
|
|
|
}
|
2017-12-01 09:10:17 -08:00
|
|
|
|
2017-12-14 06:28:37 -08:00
|
|
|
default:
|
|
|
|
resQuery.Code = abci.CodeType_UnknownRequest
|
|
|
|
resQuery.Log = cmn.Fmt("Unexpected Query path: %v", reqQuery.Path)
|
|
|
|
}
|
|
|
|
return
|
2017-12-01 09:10:17 -08:00
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
|
|
|
// Commit implements abci.Application
|
2017-12-19 21:04:40 -08:00
|
|
|
func (app *App) Commit() (res abci.Result) {
|
2017-12-14 08:18:18 -08:00
|
|
|
commitID := app.store.Commit()
|
|
|
|
app.logger.Debug("Commit synced",
|
|
|
|
"commit", commitID,
|
|
|
|
)
|
|
|
|
return abci.NewResultOK(hash, "")
|
2017-12-01 09:10:17 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// InitChain - ABCI
|
2017-12-19 21:04:40 -08:00
|
|
|
func (app *App) InitChain(req abci.RequestInitChain) {}
|
2017-12-01 09:10:17 -08:00
|
|
|
|
|
|
|
// BeginBlock - ABCI
|
2017-12-19 21:04:40 -08:00
|
|
|
func (app *App) BeginBlock(req abci.RequestBeginBlock) {
|
2017-12-03 21:25:37 -08:00
|
|
|
app.header = req.Header
|
2017-12-01 09:10:17 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// EndBlock - ABCI
|
|
|
|
// Returns a list of all validator changes made in this block
|
2017-12-19 21:04:40 -08:00
|
|
|
func (app *App) EndBlock(height uint64) (res abci.ResponseEndBlock) {
|
2017-12-14 08:18:18 -08:00
|
|
|
// XXX Update to res.Updates.
|
2017-12-20 17:34:51 -08:00
|
|
|
res.Diffs = app.valUpdates
|
|
|
|
app.valUpdates = nil
|
2017-12-01 09:10:17 -08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-12-03 21:25:37 -08:00
|
|
|
// Return index of list with validator of same PubKey, or -1 if no match
|
2017-12-01 09:10:17 -08:00
|
|
|
func pubKeyIndex(val *abci.Validator, list []*abci.Validator) int {
|
|
|
|
for i, v := range list {
|
|
|
|
if bytes.Equal(val.PubKey, v.PubKey) {
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make a simple default logger
|
|
|
|
// TODO: Make log capturable for each transaction, and return it in
|
|
|
|
// ResponseDeliverTx.Log and ResponseCheckTx.Log.
|
|
|
|
func makeDefaultLogger() log.Logger {
|
|
|
|
return log.NewTMLogger(log.NewSyncWriter(os.Stdout)).With("module", "sdk/app")
|
|
|
|
}
|
2017-12-19 21:04:40 -08:00
|
|
|
|
|
|
|
// InitState - used to setup state (was SetOption)
|
|
|
|
// to be call from setting up the genesis file
|
|
|
|
func (app *InitApp) InitState(module, key, value string) error {
|
|
|
|
state := app.Append()
|
|
|
|
logger := app.Logger().With("module", module, "key", key)
|
|
|
|
|
|
|
|
if module == sdk.ModuleNameBase {
|
|
|
|
if key == sdk.ChainKey {
|
|
|
|
app.info.SetChainID(state, value)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
logger.Error("Invalid genesis option")
|
|
|
|
return fmt.Errorf("Unknown base option: %s", key)
|
|
|
|
}
|
|
|
|
|
|
|
|
log, err := app.initState.InitState(logger, state, module, key, value)
|
|
|
|
if err != nil {
|
|
|
|
logger.Error("Invalid genesis option", "err", err)
|
|
|
|
} else {
|
|
|
|
logger.Info(log)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// InitChain - ABCI - sets the initial validators
|
|
|
|
func (app *InitApp) InitChain(req abci.RequestInitChain) {
|
|
|
|
// return early if no InitValidator registered
|
|
|
|
if app.initVals == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
logger, store := app.Logger(), app.Append()
|
|
|
|
app.initVals.InitValidators(logger, store, req.Validators)
|
|
|
|
}
|