Refactor CounterApplication to implement AppContext

This commit is contained in:
Jae Kwon 2015-11-29 14:34:15 -08:00
parent 5d994944c6
commit 6a5b804523
1 changed files with 50 additions and 23 deletions

View File

@ -2,18 +2,16 @@ package example
import ( import (
"encoding/binary" "encoding/binary"
"sync"
. "github.com/tendermint/go-common" . "github.com/tendermint/go-common"
"github.com/tendermint/tmsp/types" "github.com/tendermint/tmsp/types"
) )
type CounterApplication struct { type CounterApplication struct {
hashCount int mtx sync.Mutex
lastHashCount int hashCount int
txCount int txCount int
lastTxCount int
commitCount int commitCount int
} }
@ -21,47 +19,76 @@ func NewCounterApplication() *CounterApplication {
return &CounterApplication{} return &CounterApplication{}
} }
func (dapp *CounterApplication) Echo(message string) string { func (app *CounterApplication) Open() types.AppContext {
return &CounterAppContext{
app: app,
hashCount: app.hashCount,
txCount: app.txCount,
commitCount: app.commitCount,
}
}
//--------------------------------------------------------------------------------
type CounterAppContext struct {
app *CounterApplication
hashCount int
txCount int
commitCount int
}
func (appC *CounterAppContext) Echo(message string) string {
return message return message
} }
func (dapp *CounterApplication) Info() []string { func (appC *CounterAppContext) Info() []string {
return []string{Fmt("hash, tx, commit counts:%d, %d, %d", dapp.hashCount, dapp.txCount, dapp.commitCount)} return []string{Fmt("hash, tx, commit counts:%d, %d, %d", appC.hashCount, appC.txCount, appC.commitCount)}
} }
func (dapp *CounterApplication) SetOption(key string, value string) types.RetCode { func (appC *CounterAppContext) SetOption(key string, value string) types.RetCode {
return 0 return 0
} }
func (dapp *CounterApplication) AppendTx(tx []byte) ([]types.Event, types.RetCode) { func (appC *CounterAppContext) AppendTx(tx []byte) ([]types.Event, types.RetCode) {
dapp.txCount += 1 appC.txCount += 1
return nil, 0 return nil, 0
} }
func (dapp *CounterApplication) GetHash() ([]byte, types.RetCode) { func (appC *CounterAppContext) GetHash() ([]byte, types.RetCode) {
hash := make([]byte, 32) hash := make([]byte, 32)
binary.PutVarint(hash, int64(dapp.hashCount)) binary.PutVarint(hash, int64(appC.hashCount))
dapp.hashCount += 1 appC.hashCount += 1
return hash, 0 return hash, 0
} }
func (dapp *CounterApplication) Commit() types.RetCode { func (appC *CounterAppContext) Commit() types.RetCode {
dapp.lastHashCount = dapp.hashCount appC.commitCount += 1
dapp.lastTxCount = dapp.txCount
dapp.commitCount += 1 appC.app.mtx.Lock()
appC.app.hashCount = appC.hashCount
appC.app.txCount = appC.txCount
appC.app.commitCount = appC.commitCount
appC.app.mtx.Unlock()
return 0 return 0
} }
func (dapp *CounterApplication) Rollback() types.RetCode { func (appC *CounterAppContext) Rollback() types.RetCode {
dapp.hashCount = dapp.lastHashCount appC.app.mtx.Lock()
dapp.txCount = dapp.lastTxCount appC.hashCount = appC.app.hashCount
appC.txCount = appC.app.txCount
appC.commitCount = appC.app.commitCount
appC.app.mtx.Unlock()
return 0 return 0
} }
func (dapp *CounterApplication) AddListener(key string) types.RetCode { func (appC *CounterAppContext) AddListener(key string) types.RetCode {
return 0 return 0
} }
func (dapp *CounterApplication) RemListener(key string) types.RetCode { func (appC *CounterAppContext) RemListener(key string) types.RetCode {
return 0 return 0
} }
func (appC *CounterAppContext) Close() error {
return nil
}