Remove TMSP Commit/Rollback; Add CheckTx

This commit is contained in:
Jae Kwon
2016-01-08 17:07:19 -08:00
parent aa3e87450a
commit f15476b157
17 changed files with 227 additions and 376 deletions
+31 -65
View File
@@ -2,111 +2,77 @@ package example
import (
"encoding/binary"
"sync"
. "github.com/tendermint/go-common"
"github.com/tendermint/tmsp/types"
)
type CounterApplication struct {
mtx sync.Mutex
hashCount int
txCount int
commitCount int
serial bool
hashCount int
txCount int
serial bool
}
func NewCounterApplication(serial bool) *CounterApplication {
return &CounterApplication{serial: serial}
}
func (app *CounterApplication) Open() types.AppContext {
return &CounterAppContext{
app: app,
hashCount: app.hashCount,
txCount: app.txCount,
commitCount: app.commitCount,
serial: app.serial,
}
}
//--------------------------------------------------------------------------------
type CounterAppContext struct {
app *CounterApplication
hashCount int
txCount int
commitCount int
serial bool
}
func (appC *CounterAppContext) Echo(message string) string {
func (app *CounterApplication) Echo(message string) string {
return message
}
func (appC *CounterAppContext) Info() []string {
return []string{Fmt("hash, tx, commit counts:%d, %d, %d", appC.hashCount, appC.txCount, appC.commitCount)}
func (app *CounterApplication) Info() []string {
return []string{Fmt("hashes:%v, txs:%v", app.hashCount, app.txCount)}
}
func (appC *CounterAppContext) SetOption(key string, value string) types.RetCode {
func (app *CounterApplication) SetOption(key string, value string) types.RetCode {
if key == "serial" && value == "on" {
appC.serial = true
app.serial = true
}
return 0
}
func (appC *CounterAppContext) AppendTx(tx []byte) ([]types.Event, types.RetCode) {
if appC.serial {
func (app *CounterApplication) AppendTx(tx []byte) ([]types.Event, types.RetCode) {
if app.serial {
tx8 := make([]byte, 8)
copy(tx8, tx)
txValue := binary.LittleEndian.Uint64(tx8)
if txValue != uint64(appC.txCount) {
if txValue != uint64(app.txCount) {
return nil, types.RetCodeInternalError
}
}
appC.txCount += 1
app.txCount += 1
return nil, 0
}
func (appC *CounterAppContext) GetHash() ([]byte, types.RetCode) {
appC.hashCount += 1
if appC.txCount == 0 {
func (app *CounterApplication) CheckTx(tx []byte) types.RetCode {
if app.serial {
tx8 := make([]byte, 8)
copy(tx8, tx)
txValue := binary.LittleEndian.Uint64(tx8)
if txValue < uint64(app.txCount) {
return types.RetCodeInternalError
}
}
return 0
}
func (app *CounterApplication) GetHash() ([]byte, types.RetCode) {
app.hashCount += 1
if app.txCount == 0 {
return nil, 0
} else {
hash := make([]byte, 32)
binary.LittleEndian.PutUint64(hash, uint64(appC.txCount))
binary.LittleEndian.PutUint64(hash, uint64(app.txCount))
return hash, 0
}
}
func (appC *CounterAppContext) Commit() types.RetCode {
appC.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()
func (app *CounterApplication) AddListener(key string) types.RetCode {
return 0
}
func (appC *CounterAppContext) Rollback() types.RetCode {
appC.app.mtx.Lock()
appC.hashCount = appC.app.hashCount
appC.txCount = appC.app.txCount
appC.commitCount = appC.app.commitCount
appC.app.mtx.Unlock()
func (app *CounterApplication) RemListener(key string) types.RetCode {
return 0
}
func (appC *CounterAppContext) AddListener(key string) types.RetCode {
return 0
}
func (appC *CounterAppContext) RemListener(key string) types.RetCode {
return 0
}
func (appC *CounterAppContext) Close() error {
return nil
}
+14 -55
View File
@@ -1,8 +1,6 @@
package example
import (
"sync"
. "github.com/tendermint/go-common"
"github.com/tendermint/go-merkle"
"github.com/tendermint/go-wire"
@@ -10,7 +8,6 @@ import (
)
type DummyApplication struct {
mtx sync.Mutex
state merkle.Tree
}
@@ -24,74 +21,36 @@ func NewDummyApplication() *DummyApplication {
return &DummyApplication{state: state}
}
func (dapp *DummyApplication) Open() types.AppContext {
dapp.mtx.Lock()
defer dapp.mtx.Unlock()
return &DummyAppContext{
app: dapp,
state: dapp.state.Copy(),
}
}
func (dapp *DummyApplication) commitState(state merkle.Tree) {
dapp.mtx.Lock()
defer dapp.mtx.Unlock()
dapp.state = state.Copy()
}
func (dapp *DummyApplication) getState() merkle.Tree {
dapp.mtx.Lock()
defer dapp.mtx.Unlock()
return dapp.state.Copy()
}
//--------------------------------------------------------------------------------
type DummyAppContext struct {
app *DummyApplication
state merkle.Tree
}
func (dac *DummyAppContext) Echo(message string) string {
func (app *DummyApplication) Echo(message string) string {
return message
}
func (dac *DummyAppContext) Info() []string {
return []string{Fmt("size:%v", dac.state.Size())}
func (app *DummyApplication) Info() []string {
return []string{Fmt("size:%v", app.state.Size())}
}
func (dac *DummyAppContext) SetOption(key string, value string) types.RetCode {
func (app *DummyApplication) SetOption(key string, value string) types.RetCode {
return 0
}
func (dac *DummyAppContext) AppendTx(tx []byte) ([]types.Event, types.RetCode) {
dac.state.Set(tx, tx)
func (app *DummyApplication) AppendTx(tx []byte) ([]types.Event, types.RetCode) {
app.state.Set(tx, tx)
return nil, 0
}
func (dac *DummyAppContext) GetHash() ([]byte, types.RetCode) {
hash := dac.state.Hash()
func (app *DummyApplication) CheckTx(tx []byte) types.RetCode {
return 0 // all txs are valid
}
func (app *DummyApplication) GetHash() ([]byte, types.RetCode) {
hash := app.state.Hash()
return hash, 0
}
func (dac *DummyAppContext) Commit() types.RetCode {
dac.app.commitState(dac.state)
func (app *DummyApplication) AddListener(key string) types.RetCode {
return 0
}
func (dac *DummyAppContext) Rollback() types.RetCode {
dac.state = dac.app.getState()
func (app *DummyApplication) RemListener(key string) types.RetCode {
return 0
}
func (dac *DummyAppContext) AddListener(key string) types.RetCode {
return 0
}
func (dac *DummyAppContext) RemListener(key string) types.RetCode {
return 0
}
func (dac *DummyAppContext) Close() error {
return nil
}
+25 -30
View File
@@ -5,36 +5,25 @@ util = require("util")
function CounterApp(){
this.hashCount = 0;
this.txCount = 0;
this.commitCount = 0;
this.serial = false;
};
CounterApp.prototype.open = function(){
return new CounterAppContext(this);
}
function CounterAppContext(app) {
this.hashCount = app.hashCount;
this.txCount = app.txCount;
this.commitCount = app.commitCount;
this.serial = false;
}
CounterAppContext.prototype.echo = function(msg){
CounterApp.prototype.echo = function(msg){
return {"response": msg, "ret_code":0}
}
CounterAppContext.prototype.info = function(){
return {"response": [util.format("hash, tx, commit counts: %d, %d, %d", this.hashCount, this.txCount, this.commitCount)]}
CounterApp.prototype.info = function(){
return {"response": [util.format("hashes:%d, txs:%d", this.hashCount, this.txCount)]}
}
CounterAppContext.prototype.set_option = function(key, value){
CounterApp.prototype.set_option = function(key, value){
if (key == "serial" && value == "on"){
this.serial = true;
}
return {"ret_code":0}
}
CounterAppContext.prototype.append_tx = function(txBytes){
CounterApp.prototype.append_tx = function(txBytes){
if (this.serial) {
txByteArray = new Buffer(txBytes)
if (txBytes.length >= 2 && txBytes.slice(0, 2) == "0x") {
@@ -50,7 +39,22 @@ CounterAppContext.prototype.append_tx = function(txBytes){
return {"ret_code":0} // TODO: return events
}
CounterAppContext.prototype.get_hash = function(){
CounterApp.prototype.check_tx = function(txBytes){
if (this.serial) {
txByteArray = new Buffer(txBytes)
if (txBytes.length >= 2 && txBytes.slice(0, 2) == "0x") {
txByteArray = wire.hex2bytes(txBytes.slice(2));
}
r = new msg.buffer(txByteArray)
txValue = wire.decode_big_endian(r, txBytes.length)
if (txValue < this.txCount){
return {"ret_code":1}
}
}
return {"ret_code":0}
}
CounterApp.prototype.get_hash = function(){
this.hashCount += 1;
if (this.txCount == 0){
return {"response": "", "ret_code":0}
@@ -60,24 +64,15 @@ CounterAppContext.prototype.get_hash = function(){
return {"response": h.toString(), "ret_code":0}
}
CounterAppContext.prototype.commit = function(){
this.commitCount += 1;
CounterApp.prototype.add_listener = function(){
return {"ret_code":0}
}
CounterAppContext.prototype.rollback = function(){
CounterApp.prototype.rm_listener = function(){
return {"ret_code":0}
}
CounterAppContext.prototype.add_listener = function(){
return {"ret_code":0}
}
CounterAppContext.prototype.rm_listener = function(){
return {"ret_code":0}
}
CounterAppContext.prototype.event = function(){
CounterApp.prototype.event = function(){
}
console.log("Counter app in Javascript")
+5 -10
View File
@@ -7,17 +7,13 @@ module.exports = {
0x03 : "info",
0x04 : "set_option",
0x21 : "append_tx",
0x22 : "get_hash",
0x23 : "commit",
0x24 : "rollback",
0x25 : "add_listener",
0x26 : "rm_listener",
0x22 : "check_tx",
0x23 : "get_hash",
0x24 : "add_listener",
0x25 : "rm_listener",
},
decoder : RequestDecoder,
buffer: BytesBuffer
}
function RequestDecoder(buf){
@@ -32,9 +28,8 @@ RequestDecoder.prototype.flush = function(){};
RequestDecoder.prototype.info = function(){};
RequestDecoder.prototype.set_option = function(){ return [decode_string(this.buf), decode_string(this.buf)] };
RequestDecoder.prototype.append_tx = function(){ return decode_string(this.buf)};
RequestDecoder.prototype.check_tx = function(){ return decode_string(this.buf)};
RequestDecoder.prototype.get_hash = function(){ };
RequestDecoder.prototype.commit = function(){ };
RequestDecoder.prototype.rollback = function(){ };
RequestDecoder.prototype.add_listener = function(){ }; // TODO
RequestDecoder.prototype.rm_listener = function(){ }; // TODO
+3 -5
View File
@@ -27,8 +27,6 @@ AppServer.prototype.createServer = function(){
socket.name = socket.remoteAddress + ":" + socket.remotePort
console.log("new connection from", socket.name)
appCtx = app.open()
var conn = {
recBuf: new msg.buffer(new Buffer(0)),
resBuf: new msg.buffer(new Buffer(0)),
@@ -90,11 +88,11 @@ AppServer.prototype.createServer = function(){
var res = function(){
if (args == null){
return appCtx[reqType]();
return app[reqType]();
} else if (Array.isArray(args)){
return appCtx[reqType].apply(appCtx, args);
return app[reqType].apply(app, args);
} else {
return appCtx[reqType](args)
return app[reqType](args)
}
}()
+12 -23
View File
@@ -10,28 +10,13 @@ class CounterApplication():
def __init__(self):
self.hashCount = 0
self.txCount = 0
self.commitCount = 0
def open(self):
return CounterAppContext(self)
class CounterAppContext():
def __init__(self, app):
self.app = app
self.hashCount = app.hashCount
self.txCount = app.txCount
self.commitCount = app.commitCount
self.serial = False
def echo(self, msg):
return msg, 0
def info(self):
return ["hash, tx, commit counts:%d, %d, %d" % (self.hashCount,
self.txCount,
self.commitCount)], 0
return ["hashes:%d, txs:%d" % (self.hashCount, self.txCount)], 0
def set_option(self, key, value):
if key == "serial" and value == "on":
@@ -50,6 +35,17 @@ class CounterAppContext():
self.txCount += 1
return None, 0
def check_tx(self, txBytes):
if self.serial:
txByteArray = bytearray(txBytes)
if len(txBytes) >= 2 and txBytes[:2] == "0x":
txByteArray = hex2bytes(txBytes[2:])
txValue = decode_big_endian(
BytesBuffer(txByteArray), len(txBytes))
if txValue < self.txCount:
return 1
return 0
def get_hash(self):
self.hashCount += 1
if self.txCount == 0:
@@ -58,13 +54,6 @@ class CounterAppContext():
h.reverse()
return str(h), 0
def commit(self):
self.commitCount += 1
return 0
def rollback(self):
return 0
def add_listener(self):
return 0
+7 -12
View File
@@ -7,16 +7,14 @@ message_types = {
0x03: "info",
0x04: "set_option",
0x21: "append_tx",
0x22: "get_hash",
0x23: "commit",
0x24: "rollback",
0x25: "add_listener",
0x26: "rm_listener",
0x22: "check_tx",
0x23: "get_hash",
0x24: "add_listener",
0x25: "rm_listener",
}
# return the decoded arguments of tmsp messages
class RequestDecoder():
def __init__(self, reader):
@@ -37,15 +35,12 @@ class RequestDecoder():
def append_tx(self):
return decode_string(self.reader)
def check_tx(self):
return decode_string(self.reader)
def get_hash(self):
return
def commit(self):
return
def rollback(self):
return
def add_listener(self):
# TODO
return
+6 -10
View File
@@ -2,7 +2,6 @@ import socket
import select
import sys
from wire import decode_varint, encode
from reader import BytesBuffer
from msg import RequestDecoder, message_types
@@ -10,12 +9,11 @@ from msg import RequestDecoder, message_types
# hold the asyncronous state of a connection
# ie. we may not get enough bytes on one read to decode the message
class Connection():
def __init__(self, fd, appCtx):
def __init__(self, fd, app):
self.fd = fd
self.appCtx = appCtx
self.app = app
self.recBuf = BytesBuffer(bytearray())
self.resBuf = BytesBuffer(bytearray())
self.msgLength = 0
@@ -30,12 +28,11 @@ class Connection():
# TMSP server responds to messges by calling methods on the app
class TMSPServer():
def __init__(self, app, port=5410):
self.app = app
# map conn file descriptors to (appContext, reqBuf, resBuf, msgDecoder)
# map conn file descriptors to (app, reqBuf, resBuf, msgDecoder)
self.appMap = {}
self.port = port
@@ -60,8 +57,7 @@ class TMSPServer():
self.write_list.append(new_fd)
print 'new connection to', new_addr
appContext = self.app.open()
self.appMap[new_fd] = Connection(new_fd, appContext)
self.appMap[new_fd] = Connection(new_fd, self.app)
def handle_conn_closed(self, r):
self.read_list.remove(r)
@@ -70,7 +66,7 @@ class TMSPServer():
print "connection closed"
def handle_recv(self, r):
# appCtx, recBuf, resBuf, conn
# app, recBuf, resBuf, conn
conn = self.appMap[r]
while True:
try:
@@ -127,7 +123,7 @@ class TMSPServer():
conn.msgLength = 0
conn.inProgress = False
req_f = getattr(conn.appCtx, req_type)
req_f = getattr(conn.app, req_type)
if req_args is None:
res = req_f()
elif isinstance(req_args, tuple):
+12 -23
View File
@@ -10,28 +10,13 @@ class CounterApplication():
def __init__(self):
self.hashCount = 0
self.txCount = 0
self.commitCount = 0
def open(self):
return CounterAppContext(self)
class CounterAppContext():
def __init__(self, app):
self.app = app
self.hashCount = app.hashCount
self.txCount = app.txCount
self.commitCount = app.commitCount
self.serial = False
def echo(self, msg):
return msg, 0
def info(self):
return ["hash, tx, commit counts:%d, %d, %d" % (self.hashCount,
self.txCount,
self.commitCount)], 0
return ["hashes:%d, txs:%d" % (self.hashCount, self.txCount)], 0
def set_option(self, key, value):
if key == "serial" and value == "on":
@@ -50,6 +35,17 @@ class CounterAppContext():
self.txCount += 1
return None, 0
def check_tx(self, txBytes):
if self.serial:
txByteArray = bytearray(txBytes)
if len(txBytes) >= 2 and txBytes[:2] == "0x":
txByteArray = hex2bytes(txBytes[2:])
txValue = decode_big_endian(
BytesBuffer(txByteArray), len(txBytes))
if txValue < self.txCount:
return 1
return 0
def get_hash(self):
self.hashCount += 1
if self.txCount == 0:
@@ -58,13 +54,6 @@ class CounterAppContext():
h.reverse()
return h.decode(), 0
def commit(self):
self.commitCount += 1
return 0
def rollback(self):
return 0
def add_listener(self):
return 0
+7 -12
View File
@@ -7,16 +7,14 @@ message_types = {
0x03: "info",
0x04: "set_option",
0x21: "append_tx",
0x22: "get_hash",
0x23: "commit",
0x24: "rollback",
0x25: "add_listener",
0x26: "rm_listener",
0x22: "check_tx",
0x23: "get_hash",
0x24: "add_listener",
0x25: "rm_listener",
}
# return the decoded arguments of tmsp messages
class RequestDecoder():
def __init__(self, reader):
@@ -37,15 +35,12 @@ class RequestDecoder():
def append_tx(self):
return decode_string(self.reader)
def check_tx(self):
return decode_string(self.reader)
def get_hash(self):
return
def commit(self):
return
def rollback(self):
return
def add_listener(self):
# TODO
return
+6 -9
View File
@@ -12,12 +12,11 @@ from .msg import RequestDecoder, message_types
logger = logging.getLogger(__name__)
class Connection():
def __init__(self, fd, appCtx):
def __init__(self, fd, app):
self.fd = fd
self.appCtx = appCtx
self.app = app
self.recBuf = BytesBuffer(bytearray())
self.resBuf = BytesBuffer(bytearray())
self.msgLength = 0
@@ -32,12 +31,11 @@ class Connection():
# TMSP server responds to messges by calling methods on the app
class TMSPServer():
def __init__(self, app, port=5410):
self.app = app
# map conn file descriptors to (appContext, reqBuf, resBuf, msgDecoder)
# map conn file descriptors to (app, reqBuf, resBuf, msgDecoder)
self.appMap = {}
self.port = port
@@ -62,8 +60,7 @@ class TMSPServer():
self.write_list.append(new_fd)
print('new connection to', new_addr)
appContext = self.app.open()
self.appMap[new_fd] = Connection(new_fd, appContext)
self.appMap[new_fd] = Connection(new_fd, self.app)
def handle_conn_closed(self, r):
self.read_list.remove(r)
@@ -72,7 +69,7 @@ class TMSPServer():
print("connection closed")
def handle_recv(self, r):
# appCtx, recBuf, resBuf, conn
# app, recBuf, resBuf, conn
conn = self.appMap[r]
while True:
try:
@@ -129,7 +126,7 @@ class TMSPServer():
conn.msgLength = 0
conn.inProgress = False
req_f = getattr(conn.appCtx, req_type)
req_f = getattr(conn.app, req_type)
if req_args is None:
res = req_f()
elif isinstance(req_args, tuple):