App compiles. Ready to build on it

This commit is contained in:
Jae Kwon 2017-12-26 17:04:48 -08:00
parent ab2cef884d
commit 657820372c
7 changed files with 146 additions and 92 deletions

View File

@ -8,12 +8,13 @@ import (
"github.com/golang/protobuf/proto" "github.com/golang/protobuf/proto"
"github.com/pkg/errors" "github.com/pkg/errors"
abci "github.com/tendermint/abci/types" abci "github.com/tendermint/abci/types"
cmn "github.com/tendermint/tmlibs/common"
"github.com/tendermint/tmlibs/log" "github.com/tendermint/tmlibs/log"
"github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types"
) )
var mainHeaderKey = "header" var mainHeaderKey = []byte("header")
// App - The ABCI application // App - The ABCI application
type App struct { type App struct {
@ -23,16 +24,16 @@ type App struct {
name string name string
// Main (uncached) state // Main (uncached) state
store types.CommitMultiStore ms types.CommitMultiStore
// CheckTx state, a cache-wrap of store. // CheckTx state, a cache-wrap of `.ms`.
storeCheck types.CacheMultiStore msCheck types.CacheMultiStore
// DeliverTx state, a cache-wrap of store. // DeliverTx state, a cache-wrap of `.ms`.
storeDeliver types.CacheMultiStore msDeliver types.CacheMultiStore
// Current block header // Current block header
header *abci.Header header abci.Header
// Handler for CheckTx and DeliverTx. // Handler for CheckTx and DeliverTx.
handler types.Handler handler types.Handler
@ -54,8 +55,8 @@ func (app *App) Name() string {
return app.name return app.name
} }
func (app *App) SetCommitStore(store types.CommitMultiStore) { func (app *App) SetCommitMultiStore(ms types.CommitMultiStore) {
app.store = store app.ms = ms
} }
func (app *App) SetHandler(handler types.Handler) { func (app *App) SetHandler(handler types.Handler) {
@ -63,26 +64,33 @@ func (app *App) SetHandler(handler types.Handler) {
} }
func (app *App) LoadLatestVersion() error { func (app *App) LoadLatestVersion() error {
store := app.store app.ms.LoadLatestVersion()
store.LoadLatestVersion()
return app.initFromStore() return app.initFromStore()
} }
func (app *App) LoadVersion(version int64) error { func (app *App) LoadVersion(version int64) error {
store := app.store app.ms.LoadVersion(version)
store.LoadVersion(version)
return app.initFromStore() return app.initFromStore()
} }
// Initializes the remaining logic from app.store. // The last CommitID of the multistore.
func (app *App) LastCommitID() types.CommitID {
return app.ms.LastCommitID()
}
// The last commited block height.
func (app *App) LastBlockHeight() int64 {
return app.ms.LastCommitID().Version
}
// Initializes the remaining logic from app.ms.
func (app *App) initFromStore() error { func (app *App) initFromStore() error {
store := app.store lastCommitID := app.ms.LastCommitID()
lastCommitID := store.LastCommitID() main := app.ms.GetKVStore("main")
main := store.GetKVStore("main") header := abci.Header{}
header := (*abci.Header)(nil)
// Main store should exist. // Main store should exist.
if store.GetKVStore("main") == nil { if app.ms.GetKVStore("main") == nil {
return errors.New("App expects MultiStore with 'main' KVStore") return errors.New("App expects MultiStore with 'main' KVStore")
} }
@ -93,7 +101,7 @@ func (app *App) initFromStore() error {
errStr := fmt.Sprintf("Version > 0 but missing key %s", mainHeaderKey) errStr := fmt.Sprintf("Version > 0 but missing key %s", mainHeaderKey)
return errors.New(errStr) return errors.New(errStr)
} }
err := proto.Unmarshal(headerBytes, header) err := proto.Unmarshal(headerBytes, &header)
if err != nil { if err != nil {
return errors.Wrap(err, "Failed to parse Header") return errors.Wrap(err, "Failed to parse Header")
} }
@ -106,8 +114,8 @@ func (app *App) initFromStore() error {
// Set App state. // Set App state.
app.header = header app.header = header
app.storeCheck = nil app.msCheck = nil
app.storeDeliver = nil app.msDeliver = nil
app.valUpdates = nil app.valUpdates = nil
return nil return nil
@ -118,7 +126,7 @@ func (app *App) initFromStore() error {
// Implements ABCI // Implements ABCI
func (app *App) Info(req abci.RequestInfo) abci.ResponseInfo { func (app *App) Info(req abci.RequestInfo) abci.ResponseInfo {
lastCommitID := app.store.LastCommitID() lastCommitID := app.ms.LastCommitID()
return abci.ResponseInfo{ return abci.ResponseInfo{
Data: app.name, Data: app.name,
@ -148,8 +156,8 @@ func (app *App) Query(req abci.RequestQuery) (res abci.ResponseQuery) {
// Implements ABCI // Implements ABCI
func (app *App) BeginBlock(req abci.RequestBeginBlock) (res abci.ResponseBeginBlock) { func (app *App) BeginBlock(req abci.RequestBeginBlock) (res abci.ResponseBeginBlock) {
app.header = req.Header app.header = req.Header
app.storeDeliver = app.store.CacheMultiStore() app.msDeliver = app.ms.CacheMultiStore()
app.storeCheck = app.store.CacheMultiStore() app.msCheck = app.ms.CacheMultiStore()
return return
} }
@ -159,20 +167,22 @@ func (app *App) CheckTx(txBytes []byte) (res abci.ResponseCheckTx) {
// Initialize arguments to Handler. // Initialize arguments to Handler.
var isCheckTx = true var isCheckTx = true
var ctx = types.NewContext(app.header, isCheckTx, txBytes) var ctx = types.NewContext(app.header, isCheckTx, txBytes)
var store = app.store var tx types.Tx = nil // nil until a decorator parses one.
var tx Tx = nil // nil until a decorator parses one.
// Run the handler. // Run the handler.
var result = app.handler(ctx, app.store, tx) var result = app.handler(ctx, app.ms, tx)
// Tell the blockchain engine (i.e. Tendermint). // Tell the blockchain engine (i.e. Tendermint).
return abci.ResponseDeliverTx{ return abci.ResponseCheckTx{
Code: result.Code, Code: result.Code,
Data: result.Data, Data: result.Data,
Log: result.Log, Log: result.Log,
Gas: result.Gas, GasWanted: result.GasWanted,
FeeDenom: result.FeeDenom, Fee: cmn.KI64Pair{
FeeAmount: result.FeeAmount, []byte(result.FeeDenom),
result.FeeAmount,
},
Tags: result.Tags,
} }
} }
@ -182,15 +192,14 @@ func (app *App) DeliverTx(txBytes []byte) (res abci.ResponseDeliverTx) {
// Initialize arguments to Handler. // Initialize arguments to Handler.
var isCheckTx = false var isCheckTx = false
var ctx = types.NewContext(app.header, isCheckTx, txBytes) var ctx = types.NewContext(app.header, isCheckTx, txBytes)
var store = app.store var tx types.Tx = nil // nil until a decorator parses one.
var tx Tx = nil // nil until a decorator parses one.
// Run the handler. // Run the handler.
var result = app.handler(ctx, app.store, tx) var result = app.handler(ctx, app.ms, tx)
// After-handler hooks. // After-handler hooks.
if result.Code == abci.CodeType_OK { if result.Code == abci.CodeTypeOK {
app.valUpdates = append(app.valUpdates, result.ValUpdate) app.valUpdates = append(app.valUpdates, result.ValidatorUpdates...)
} else { } else {
// Even though the Code is not OK, there will be some side effects, // Even though the Code is not OK, there will be some side effects,
// like those caused by fee deductions or sequence incrementations. // like those caused by fee deductions or sequence incrementations.
@ -198,10 +207,12 @@ func (app *App) DeliverTx(txBytes []byte) (res abci.ResponseDeliverTx) {
// Tell the blockchain engine (i.e. Tendermint). // Tell the blockchain engine (i.e. Tendermint).
return abci.ResponseDeliverTx{ return abci.ResponseDeliverTx{
Code: result.Code, Code: result.Code,
Data: result.Data, Data: result.Data,
Log: result.Log, Log: result.Log,
Tags: result.Tags, GasWanted: result.GasWanted,
GasUsed: result.GasUsed,
Tags: result.Tags,
} }
} }
@ -214,12 +225,14 @@ func (app *App) EndBlock(req abci.RequestEndBlock) (res abci.ResponseEndBlock) {
// Implements ABCI // Implements ABCI
func (app *App) Commit() (res abci.ResponseCommit) { func (app *App) Commit() (res abci.ResponseCommit) {
app.storeDeliver.Write() app.msDeliver.Write()
commitID := app.store.Commit() commitID := app.ms.Commit()
app.logger.Debug("Commit synced", app.logger.Debug("Commit synced",
"commit", commitID, "commit", commitID,
) )
return abci.NewResultOK(hash, "") return abci.ResponseCommit{
Data: commitID.Hash,
}
} }
//---------------------------------------- //----------------------------------------

View File

@ -7,12 +7,13 @@ import (
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
sdk "github.com/cosmos/cosmos-sdk" "github.com/cosmos/cosmos-sdk/store"
"github.com/cosmos/cosmos-sdk/types"
abci "github.com/tendermint/abci/types" abci "github.com/tendermint/abci/types"
"github.com/tendermint/go-crypto" "github.com/tendermint/go-crypto"
cmn "github.com/tendermint/tmlibs/common" cmn "github.com/tendermint/tmlibs/common"
dbm "github.com/tendermint/tmlibs/db"
) )
func TestBasic(t *testing.T) { func TestBasic(t *testing.T) {
@ -24,31 +25,33 @@ func TestBasic(t *testing.T) {
} }
// Create app. // Create app.
app := sdk.NewApp(t.Name()) app := NewApp(t.Name())
app.SetStore(mockMultiStore()) app.SetCommitMultiStore(newCommitMultiStore())
app.SetHandler(func(ctx Context, store MultiStore, tx Tx) Result { app.SetHandler(func(ctx types.Context, store types.MultiStore, tx types.Tx) types.Result {
// This could be a decorator. // This could be a decorator.
fromJSON(ctx.TxBytes(), &tx) var ttx testTx
fromJSON(ctx.TxBytes(), &ttx)
fmt.Println(">>", tx) // XXX
return types.Result{}
}) })
// Load latest state, which should be empty. // Load latest state, which should be empty.
err := app.LoadLatestVersion() err := app.LoadLatestVersion()
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, app.NextVersion(), 1) assert.Equal(t, app.LastBlockHeight(), int64(0))
// Create the validators // Create the validators
var numVals = 3 var numVals = 3
var valSet = make([]*abci.Validator, numVals) var valSet = make([]abci.Validator, numVals)
for i := 0; i < numVals; i++ { for i := 0; i < numVals; i++ {
valSet[i] = makeVal(secret(i)) valSet[i] = makeVal(secret(i))
} }
// Initialize the chain // Initialize the chain
app.InitChain(abci.RequestInitChain{ app.InitChain(abci.RequestInitChain{
Validators: valset, Validators: valSet,
}) })
// Simulate the start of a block. // Simulate the start of a block.
@ -62,24 +65,26 @@ func TestBasic(t *testing.T) {
} }
txBytes := toJSON(tx) txBytes := toJSON(tx)
res := app.DeliverTx(txBytes) res := app.DeliverTx(txBytes)
require.True(res.IsOK(), "%#v", res) assert.True(t, res.IsOK(), "%#v", res)
} }
// Simulate the end of a block. // Simulate the end of a block.
// Get the summary of validator updates. // Get the summary of validator updates.
res := app.EndBlock(app.height) res := app.EndBlock(abci.RequestEndBlock{})
valUpdates := res.ValidatorUpdates valUpdates := res.ValidatorUpdates
// Assert that validator updates are correct. // Assert that validator updates are correct.
for _, val := range valSet { for _, val := range valSet {
// Sanity
assert.NotEqual(t, len(val.PubKey), 0)
// Find matching update and splice it out. // Find matching update and splice it out.
for j := 0; j < len(valUpdates); { for j := 0; j < len(valUpdates); {
assert.NotEqual(len(valUpdates.PubKey), 0) valUpdate := valUpdates[j]
// Matched. // Matched.
if bytes.Equal(valUpdate.PubKey, val.PubKey) { if bytes.Equal(valUpdate.PubKey, val.PubKey) {
assert.Equal(valUpdate.NewPower, val.Power+1) assert.Equal(t, valUpdate.Power, val.Power+1)
if j < len(valUpdates)-1 { if j < len(valUpdates)-1 {
// Splice it out. // Splice it out.
valUpdates = append(valUpdates[:j], valUpdates[j+1:]...) valUpdates = append(valUpdates[:j], valUpdates[j+1:]...)
@ -100,9 +105,9 @@ func randPower() int64 {
return cmn.RandInt64() return cmn.RandInt64()
} }
func makeVal(secret string) *abci.Validator { func makeVal(secret string) abci.Validator {
return &abci.Validator{ return abci.Validator{
PubKey: makePubKey(string).Bytes(), PubKey: makePubKey(secret).Bytes(),
Power: randPower(), Power: randPower(),
} }
} }
@ -112,29 +117,43 @@ func makePubKey(secret string) crypto.PubKey {
} }
func makePrivKey(secret string) crypto.PrivKey { func makePrivKey(secret string) crypto.PrivKey {
return crypto.GenPrivKeyEd25519FromSecret([]byte(id)) privKey := crypto.GenPrivKeyEd25519FromSecret([]byte(secret))
return privKey.Wrap()
} }
func secret(index int) []byte { func secret(index int) string {
return []byte(fmt.Sprintf("secret%d", index)) return fmt.Sprintf("secret%d", index)
} }
func copyVal(val *abci.Validator) *abci.Validator { func copyVal(val abci.Validator) abci.Validator {
val2 := *val // val2 := *val
return &val2 // return &val2
return val
} }
func toJSON(o interface{}) []byte { func toJSON(o interface{}) []byte {
bytes, err := json.Marshal(o) bz, err := json.Marshal(o)
if err != nil { if err != nil {
panic(err) panic(err)
} }
return bytes // fmt.Println(">> toJSON:", string(bz))
return bz
} }
func fromJSON(bytes []byte, ptr interface{}) { func fromJSON(bz []byte, ptr interface{}) {
err := json.Unmarshal(bytes, ptr) // fmt.Println(">> fromJSON:", string(bz))
err := json.Unmarshal(bz, ptr)
if err != nil { if err != nil {
panic(err) panic(err)
} }
} }
// Creates a sample CommitMultiStore
func newCommitMultiStore() types.CommitMultiStore {
dbMain := dbm.NewMemDB()
dbXtra := dbm.NewMemDB()
ms := store.NewMultiStore(dbMain) // Also store rootMultiStore metadata here (it shouldn't clash)
ms.SetSubstoreLoader("main", store.NewIAVLStoreLoader(dbMain, 0, 0))
ms.SetSubstoreLoader("xtra", store.NewIAVLStoreLoader(dbXtra, 0, 0))
return ms
}

4
glide.lock generated
View File

@ -112,7 +112,7 @@ imports:
- leveldb/table - leveldb/table
- leveldb/util - leveldb/util
- name: github.com/tendermint/abci - name: github.com/tendermint/abci
version: 4274f8eca234325d2fbc9a434f9bc099bd2c40d4 version: 8f87efd7f86c2bae9df69aff69d715593d5d79f7
subpackages: subpackages:
- client - client
- example/dummy - example/dummy
@ -174,7 +174,7 @@ imports:
- types - types
- version - version
- name: github.com/tendermint/tmlibs - name: github.com/tendermint/tmlibs
version: b70ae4919befb6ae3e5cb40ae8174e122e771d08 version: b25df389db3c98f4b964bd39511c199f02d07715
subpackages: subpackages:
- autofile - autofile
- cli - cli

View File

@ -48,6 +48,7 @@ type iavlStore struct {
tree *iavl.VersionedTree tree *iavl.VersionedTree
// How many old versions we hold onto. // How many old versions we hold onto.
// A value of 0 means keep all history.
numHistory int64 numHistory int64
} }
@ -71,7 +72,7 @@ func (st *iavlStore) Commit() CommitID {
} }
// Release an old version of history // Release an old version of history
if st.numHistory < st.tree.Version64() { if st.numHistory > 0 && (st.numHistory < st.tree.Version64()) {
toRelease := version - st.numHistory toRelease := version - st.numHistory
st.tree.DeleteVersion(toRelease) st.tree.DeleteVersion(toRelease)
} }

View File

@ -26,6 +26,8 @@ type rootMultiStore struct {
substores map[string]CommitStore substores map[string]CommitStore
} }
var _ CommitMultiStore = (*rootMultiStore)(nil)
func NewMultiStore(db dbm.DB) *rootMultiStore { func NewMultiStore(db dbm.DB) *rootMultiStore {
return &rootMultiStore{ return &rootMultiStore{
db: db, db: db,
@ -35,6 +37,7 @@ func NewMultiStore(db dbm.DB) *rootMultiStore {
} }
} }
// Implements CommitMultiStore.
func (rs *rootMultiStore) SetSubstoreLoader(name string, loader CommitStoreLoader) { func (rs *rootMultiStore) SetSubstoreLoader(name string, loader CommitStoreLoader) {
if _, ok := rs.storeLoaders[name]; ok { if _, ok := rs.storeLoaders[name]; ok {
panic(fmt.Sprintf("rootMultiStore duplicate substore name " + name)) panic(fmt.Sprintf("rootMultiStore duplicate substore name " + name))
@ -42,17 +45,18 @@ func (rs *rootMultiStore) SetSubstoreLoader(name string, loader CommitStoreLoade
rs.storeLoaders[name] = loader rs.storeLoaders[name] = loader
} }
// Call once after all calls to SetSubstoreLoader are complete. // Implements CommitMultiStore.
func (rs *rootMultiStore) GetSubstore(name string) CommitStore {
return rs.substores[name]
}
// Implements CommitMultiStore.
func (rs *rootMultiStore) LoadLatestVersion() error { func (rs *rootMultiStore) LoadLatestVersion() error {
ver := getLatestVersion(rs.db) ver := getLatestVersion(rs.db)
return rs.LoadVersion(ver) return rs.LoadVersion(ver)
} }
// NOTE: Returns 0 unless LoadVersion() or LoadLatestVersion() is called. // Implements CommitMultiStore.
func (rs *rootMultiStore) NextVersion() int64 {
return rs.nextVersion
}
func (rs *rootMultiStore) LoadVersion(ver int64) error { func (rs *rootMultiStore) LoadVersion(ver int64) error {
// Special logic for version 0 // Special logic for version 0
@ -106,10 +110,13 @@ func (rs *rootMultiStore) LoadVersion(ver int64) error {
return nil return nil
} }
// Implements CommitStore //----------------------------------------
// +CommitStore
// Implements CommitStore.
func (rs *rootMultiStore) Commit() CommitID { func (rs *rootMultiStore) Commit() CommitID {
// Commit substores // Commit substores.
version := rs.nextVersion version := rs.nextVersion
state := commitSubstores(version, rs.substores) state := commitSubstores(version, rs.substores)
@ -129,27 +136,36 @@ func (rs *rootMultiStore) Commit() CommitID {
return commitID return commitID
} }
// Implements CommitStore // Implements CommitStore.
func (rs *rootMultiStore) CacheWrap() CacheWrap { func (rs *rootMultiStore) CacheWrap() CacheWrap {
return rs.CacheMultiStore().(CacheWrap) return rs.CacheMultiStore().(CacheWrap)
} }
// Get the last committed CommitID //----------------------------------------
// +MultiStore
// Implements MultiStore.
func (rs *rootMultiStore) LastCommitID() CommitID { func (rs *rootMultiStore) LastCommitID() CommitID {
return rs.lastCommitID return rs.lastCommitID
} }
// Implements MultiStore // Implements MultiStore.
// NOTE: Returns 0 unless LoadVersion() or LoadLatestVersion() is called.
func (rs *rootMultiStore) NextVersion() int64 {
return rs.nextVersion
}
// Implements MultiStore.
func (rs *rootMultiStore) CacheMultiStore() CacheMultiStore { func (rs *rootMultiStore) CacheMultiStore() CacheMultiStore {
return newCacheMultiStoreFromRMS(rs) return newCacheMultiStoreFromRMS(rs)
} }
// Implements MultiStore // Implements MultiStore.
func (rs *rootMultiStore) GetCommitStore(name string) CommitStore { func (rs *rootMultiStore) GetStore(name string) interface{} {
return rs.substores[name] return rs.substores[name]
} }
// Implements MultiStore // Implements MultiStore.
func (rs *rootMultiStore) GetKVStore(name string) KVStore { func (rs *rootMultiStore) GetKVStore(name string) KVStore {
return rs.substores[name].(KVStore) return rs.substores[name].(KVStore)
} }
@ -208,6 +224,7 @@ func (sc substoreCore) Hash() []byte {
} }
//---------------------------------------- //----------------------------------------
// Misc.
func getLatestVersion(db dbm.DB) int64 { func getLatestVersion(db dbm.DB) int64 {
var latest int64 var latest int64

View File

@ -17,8 +17,8 @@ type Result struct {
// Log is just debug information. NOTE: nondeterministic. // Log is just debug information. NOTE: nondeterministic.
Log string Log string
// GasAllocated is the maximum units of work we allow this tx to perform. // GasWanted is the maximum units of work we allow this tx to perform.
GasAllocated int64 GasWanted int64
// GasUsed is the amount of gas actually consumed. NOTE: not used. // GasUsed is the amount of gas actually consumed. NOTE: not used.
GasUsed int64 GasUsed int64
@ -28,7 +28,7 @@ type Result struct {
FeeDenom string FeeDenom string
// Changes to the validator set. // Changes to the validator set.
ValSetDiff []abci.Validator ValidatorUpdates []abci.Validator
// Tags are used for transaction indexing and pubsub. // Tags are used for transaction indexing and pubsub.
Tags []cmn.KVPair Tags []cmn.KVPair

View File

@ -26,7 +26,7 @@ type MultiStore interface {
// call CacheMultiStore.Write(). // call CacheMultiStore.Write().
CacheMultiStore() CacheMultiStore CacheMultiStore() CacheMultiStore
// Convenience // Convenience for fetching substores.
GetStore(name string) interface{} GetStore(name string) interface{}
GetKVStore(name string) KVStore GetKVStore(name string) KVStore
} }
@ -56,7 +56,11 @@ type CommitMultiStore interface {
// Add a substore loader. // Add a substore loader.
SetSubstoreLoader(name string, loader CommitStoreLoader) SetSubstoreLoader(name string, loader CommitStoreLoader)
// Gets the substore, which is a CommitSubstore.
GetSubstore(name string) CommitStore
// Load the latest persisted version. // Load the latest persisted version.
// Called once after all calls to SetSubstoreLoader are complete.
LoadLatestVersion() error LoadLatestVersion() error
// Load a specific persisted version. When you load an old version, or // Load a specific persisted version. When you load an old version, or