From 8e4a39562e9f723db38a6c2805afd20e7bba7952 Mon Sep 17 00:00:00 2001
From: Ethan Buchman <ethan@coinculture.info>
Date: Tue, 12 Apr 2016 00:10:26 -0400
Subject: [PATCH] some cleanup, notes about locks

---
 tmsp/erisdb.go | 34 ++++++++++++++++------------------
 1 file changed, 16 insertions(+), 18 deletions(-)

diff --git a/tmsp/erisdb.go b/tmsp/erisdb.go
index f649a488..12e0cf65 100644
--- a/tmsp/erisdb.go
+++ b/tmsp/erisdb.go
@@ -43,18 +43,13 @@ func (app *ErisDBApp) GetState() *sm.State {
 	return app.state.Copy()
 }
 
+// TODO: this is used for call/callcode and to get nonces during mempool.
+// the former should work on last committed state only and the later should
+// be handled by the client, or a separate wallet-like nonce tracker thats not part of the app
 func (app *ErisDBApp) GetCheckCache() *sm.BlockCache {
-	app.mtx.Lock()
-	defer app.mtx.Unlock()
 	return app.checkCache
 }
 
-func (app *ErisDBApp) ResetCheckCache() {
-	app.mtx.Lock()
-	defer app.mtx.Unlock()
-	app.checkCache = sm.NewBlockCache(app.state)
-}
-
 func (app *ErisDBApp) SetHostAddress(host string) {
 	app.host = host
 	app.client = client.NewClientURI(host) //fmt.Sprintf("http://%s", host))
@@ -120,10 +115,6 @@ func (app ErisDBApp) AppendTx(txBytes []byte) (res tmsp.Result) {
 
 // Implements tmsp.Application
 func (app ErisDBApp) CheckTx(txBytes []byte) (res tmsp.Result) {
-	log.Info("Check Tx", "tx", txBytes)
-	defer func() {
-		log.Info("Check Tx", "res", res)
-	}()
 	var n int
 	var err error
 	tx := new(types.Tx)
@@ -133,13 +124,12 @@ func (app ErisDBApp) CheckTx(txBytes []byte) (res tmsp.Result) {
 		return tmsp.NewError(tmsp.CodeType_EncodingError, fmt.Sprintf("Encoding error: %v", err))
 	}
 
-	// we need the lock because CheckTx can run concurrently with Commit,
-	// and Commit refreshes the checkCache
-	app.mtx.Lock()
-	defer app.mtx.Unlock()
+	log.Info("CheckTx", "tx", tx)
+
+	// TODO: make errors tmsp aware
 	err = sm.ExecTx(app.checkCache, *tx, false, nil)
 	if err != nil {
-		return tmsp.NewError(tmsp.CodeType_InternalError, fmt.Sprintf("Encoding error: %v", err))
+		return tmsp.NewError(tmsp.CodeType_InternalError, fmt.Sprintf("Internal error: %v", err))
 	}
 
 	return tmsp.NewResultOK(nil, "Success")
@@ -147,12 +137,20 @@ func (app ErisDBApp) CheckTx(txBytes []byte) (res tmsp.Result) {
 
 // Implements tmsp.Application
 // Commit the state (called at end of block)
+// NOTE: CheckTx/AppendTx must not run concurrently with Commit -
+//	the mempool should run during AppendTxs, but lock for Commit and Update
 func (app *ErisDBApp) Commit() (res tmsp.Result) {
+	app.mtx.Lock() // the lock protects app.state
+	defer app.mtx.Unlock()
+
+	app.state.LastBlockHeight += 1
+	log.Info("Commit", "block", app.state.LastBlockHeight)
+
 	// sync the AppendTx cache
 	app.cache.Sync()
 
 	// reset the check cache to the new height
-	app.ResetCheckCache()
+	app.checkCache = sm.NewBlockCache(app.state)
 
 	// save state to disk
 	app.state.Save()
-- 
GitLab