mirror of
https://github.com/fluencelabs/tendermint
synced 2025-06-16 22:51:22 +00:00
more fixes from review
This commit is contained in:
@ -32,8 +32,10 @@ func (evc *EventCache) FireEvent(event string, msg interface{}) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Fire events by running evsw.FireEvent on all cached events. Blocks.
|
// Fire events by running evsw.FireEvent on all cached events. Blocks.
|
||||||
|
// Clears cached events
|
||||||
func (evc *EventCache) Flush() {
|
func (evc *EventCache) Flush() {
|
||||||
for _, ei := range evc.events {
|
for _, ei := range evc.events {
|
||||||
evc.evsw.FireEvent(ei.event, ei.msg)
|
evc.evsw.FireEvent(ei.event, ei.msg)
|
||||||
}
|
}
|
||||||
|
evc.events = make([]eventInfo, eventsBufferSize)
|
||||||
}
|
}
|
||||||
|
@ -161,6 +161,10 @@ func (n *Node) MempoolReactor() *mempl.MempoolReactor {
|
|||||||
return n.mempoolReactor
|
return n.mempoolReactor
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (n *Node) EventSwitch() *events.EventSwitch {
|
||||||
|
return n.evsw
|
||||||
|
}
|
||||||
|
|
||||||
//------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------
|
||||||
|
|
||||||
func RunNode() {
|
func RunNode() {
|
||||||
|
@ -212,8 +212,9 @@ type WSResponse struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// a single websocket connection
|
// a single websocket connection
|
||||||
// contains the listeners id
|
// contains listener id, underlying ws connection,
|
||||||
type Connection struct {
|
// and the event switch for subscribing to events
|
||||||
|
type WSConnection struct {
|
||||||
id string
|
id string
|
||||||
wsConn *websocket.Conn
|
wsConn *websocket.Conn
|
||||||
writeChan chan WSResponse
|
writeChan chan WSResponse
|
||||||
@ -225,8 +226,8 @@ type Connection struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// new websocket connection wrapper
|
// new websocket connection wrapper
|
||||||
func NewConnection(wsConn *websocket.Conn) *Connection {
|
func NewWSConnection(wsConn *websocket.Conn) *WSConnection {
|
||||||
return &Connection{
|
return &WSConnection{
|
||||||
id: wsConn.RemoteAddr().String(),
|
id: wsConn.RemoteAddr().String(),
|
||||||
wsConn: wsConn,
|
wsConn: wsConn,
|
||||||
writeChan: make(chan WSResponse, WriteChanBufferSize), // buffered. we keep track when its full
|
writeChan: make(chan WSResponse, WriteChanBufferSize), // buffered. we keep track when its full
|
||||||
@ -234,7 +235,7 @@ func NewConnection(wsConn *websocket.Conn) *Connection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// start the connection and hand her the event switch
|
// start the connection and hand her the event switch
|
||||||
func (con *Connection) Start(evsw *events.EventSwitch) {
|
func (con *WSConnection) Start(evsw *events.EventSwitch) {
|
||||||
if atomic.CompareAndSwapUint32(&con.started, 0, 1) {
|
if atomic.CompareAndSwapUint32(&con.started, 0, 1) {
|
||||||
con.evsw = evsw
|
con.evsw = evsw
|
||||||
|
|
||||||
@ -246,15 +247,29 @@ func (con *Connection) Start(evsw *events.EventSwitch) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// close the connection
|
// close the connection
|
||||||
func (con *Connection) Stop() {
|
func (con *WSConnection) Stop() {
|
||||||
if atomic.CompareAndSwapUint32(&con.stopped, 0, 1) {
|
if atomic.CompareAndSwapUint32(&con.stopped, 0, 1) {
|
||||||
con.wsConn.Close()
|
con.wsConn.Close()
|
||||||
close(con.writeChan)
|
close(con.writeChan)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// attempt to write response to writeChan and record failures
|
||||||
|
func (con *WSConnection) safeWrite(resp WSResponse) {
|
||||||
|
select {
|
||||||
|
case con.writeChan <- resp:
|
||||||
|
// yay
|
||||||
|
con.failedSends = 0
|
||||||
|
default:
|
||||||
|
// channel is full
|
||||||
|
// if this happens too many times in a row,
|
||||||
|
// close connection
|
||||||
|
con.failedSends += 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// read from the socket and subscribe to or unsubscribe from events
|
// read from the socket and subscribe to or unsubscribe from events
|
||||||
func (con *Connection) read() {
|
func (con *WSConnection) read() {
|
||||||
reaper := time.Tick(time.Second * WSConnectionReaperSeconds)
|
reaper := time.Tick(time.Second * WSConnectionReaperSeconds)
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
@ -278,7 +293,7 @@ func (con *Connection) read() {
|
|||||||
err = json.Unmarshal(in, &req)
|
err = json.Unmarshal(in, &req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errStr := fmt.Sprintf("Error unmarshaling data: %s", err.Error())
|
errStr := fmt.Sprintf("Error unmarshaling data: %s", err.Error())
|
||||||
con.writeChan <- WSResponse{Error: errStr}
|
con.safeWrite(WSResponse{Error: errStr})
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
switch req.Type {
|
switch req.Type {
|
||||||
@ -289,16 +304,7 @@ func (con *Connection) read() {
|
|||||||
Event: req.Event,
|
Event: req.Event,
|
||||||
Data: msg,
|
Data: msg,
|
||||||
}
|
}
|
||||||
select {
|
con.safeWrite(resp)
|
||||||
case con.writeChan <- resp:
|
|
||||||
// yay
|
|
||||||
con.failedSends = 0
|
|
||||||
default:
|
|
||||||
// channel is full
|
|
||||||
// if this happens too many times,
|
|
||||||
// close connection
|
|
||||||
con.failedSends += 1
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
case "unsubscribe":
|
case "unsubscribe":
|
||||||
if req.Event != "" {
|
if req.Event != "" {
|
||||||
@ -307,7 +313,7 @@ func (con *Connection) read() {
|
|||||||
con.evsw.RemoveListener(con.id)
|
con.evsw.RemoveListener(con.id)
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
con.writeChan <- WSResponse{Error: "Unknown request type: " + req.Type}
|
con.safeWrite(WSResponse{Error: "Unknown request type: " + req.Type})
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -315,7 +321,7 @@ func (con *Connection) read() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// receives on a write channel and writes out on the socket
|
// receives on a write channel and writes out on the socket
|
||||||
func (con *Connection) write() {
|
func (con *WSConnection) write() {
|
||||||
n, err := new(int64), new(error)
|
n, err := new(int64), new(error)
|
||||||
for {
|
for {
|
||||||
msg, more := <-con.writeChan
|
msg, more := <-con.writeChan
|
||||||
@ -369,7 +375,7 @@ func (wm *WebsocketManager) websocketHandler(w http.ResponseWriter, r *http.Requ
|
|||||||
}
|
}
|
||||||
|
|
||||||
// register connection
|
// register connection
|
||||||
con := NewConnection(wsConn)
|
con := NewWSConnection(wsConn)
|
||||||
log.Info("New websocket connection", "origin", con.id)
|
log.Info("New websocket connection", "origin", con.id)
|
||||||
con.Start(wm.evsw)
|
con.Start(wm.evsw)
|
||||||
}
|
}
|
||||||
|
@ -101,6 +101,7 @@ func (s *State) Copy() *State {
|
|||||||
UnbondingValidators: s.UnbondingValidators.Copy(), // copy the valSet lazily.
|
UnbondingValidators: s.UnbondingValidators.Copy(), // copy the valSet lazily.
|
||||||
accounts: s.accounts.Copy(),
|
accounts: s.accounts.Copy(),
|
||||||
validatorInfos: s.validatorInfos.Copy(),
|
validatorInfos: s.validatorInfos.Copy(),
|
||||||
|
evc: nil,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -12,7 +12,7 @@ import (
|
|||||||
|
|
||||||
func execTxWithState(state *State, tx types.Tx, runCall bool) error {
|
func execTxWithState(state *State, tx types.Tx, runCall bool) error {
|
||||||
cache := NewBlockCache(state)
|
cache := NewBlockCache(state)
|
||||||
err := ExecTx(cache, tx, runCall, false)
|
err := ExecTx(cache, tx, runCall, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
} else {
|
} else {
|
||||||
|
7
vm/vm.go
7
vm/vm.go
@ -307,7 +307,6 @@ func (vm *VM) call(caller, callee *Account, code, input []byte, value uint64, ga
|
|||||||
x, y := stack.Pop64(), stack.Pop64()
|
x, y := stack.Pop64(), stack.Pop64()
|
||||||
stack.Push64(x & y)
|
stack.Push64(x & y)
|
||||||
dbg.Printf(" %v & %v = %v\n", x, y, x&y)
|
dbg.Printf(" %v & %v = %v\n", x, y, x&y)
|
||||||
|
|
||||||
case OR: // 0x17
|
case OR: // 0x17
|
||||||
x, y := stack.Pop64(), stack.Pop64()
|
x, y := stack.Pop64(), stack.Pop64()
|
||||||
stack.Push64(x | y)
|
stack.Push64(x | y)
|
||||||
@ -381,7 +380,7 @@ func (vm *VM) call(caller, callee *Account, code, input []byte, value uint64, ga
|
|||||||
return nil, firstErr(err, ErrInputOutOfBounds)
|
return nil, firstErr(err, ErrInputOutOfBounds)
|
||||||
}
|
}
|
||||||
stack.Push(RightPadWord256(data))
|
stack.Push(RightPadWord256(data))
|
||||||
dbg.Printf(" => 0x%X\n", data)
|
dbg.Printf(" => 0x%X\n", RightPadWord256(data))
|
||||||
|
|
||||||
case CALLDATASIZE: // 0x36
|
case CALLDATASIZE: // 0x36
|
||||||
stack.Push64(uint64(len(input)))
|
stack.Push64(uint64(len(input)))
|
||||||
@ -721,10 +720,12 @@ func subslice(data []byte, offset, length uint64, flip_ bool) (ret []byte, ok bo
|
|||||||
if size < offset {
|
if size < offset {
|
||||||
return nil, false
|
return nil, false
|
||||||
} else if size < offset+length {
|
} else if size < offset+length {
|
||||||
ret, ok = data[offset:], false
|
ret, ok = data[offset:], true
|
||||||
|
ret = RightPadBytes(ret, 32)
|
||||||
} else {
|
} else {
|
||||||
ret, ok = data[offset:offset+length], true
|
ret, ok = data[offset:offset+length], true
|
||||||
}
|
}
|
||||||
|
|
||||||
if flip_ {
|
if flip_ {
|
||||||
ret = flip(ret)
|
ret = flip(ret)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user