Browse Source

BeginBlock

pull/1780/head
Ethan Buchman 8 years ago
parent
commit
6aa85b642e
5 changed files with 135 additions and 0 deletions
  1. +2
    -0
      client/socket_client.go
  2. +75
    -0
      example/chain_aware/chain_aware_app.go
  3. +50
    -0
      example/chain_aware/chain_aware_test.go
  4. +1
    -0
      example/dummy/dummy.go
  5. +7
    -0
      server/socket_server.go

+ 2
- 0
client/socket_client.go View File

@ -432,6 +432,8 @@ func resMatchesReq(req *types.Request, res *types.Response) (ok bool) {
_, ok = res.Value.(*types.Response_Query) _, ok = res.Value.(*types.Response_Query)
case *types.Request_InitChain: case *types.Request_InitChain:
_, ok = res.Value.(*types.Response_InitChain) _, ok = res.Value.(*types.Response_InitChain)
case *types.Request_BeginBlock:
_, ok = res.Value.(*types.Response_BeginBlock)
case *types.Request_EndBlock: case *types.Request_EndBlock:
_, ok = res.Value.(*types.Response_EndBlock) _, ok = res.Value.(*types.Response_EndBlock)
} }


+ 75
- 0
example/chain_aware/chain_aware_app.go View File

@ -0,0 +1,75 @@
package main
import (
"flag"
. "github.com/tendermint/go-common"
"github.com/tendermint/tmsp/server"
"github.com/tendermint/tmsp/types"
)
func main() {
addrPtr := flag.String("addr", "tcp://0.0.0.0:46658", "Listen address")
tmspPtr := flag.String("tmsp", "socket", "socket | grpc")
flag.Parse()
// Start the listener
_, err := server.NewServer(*addrPtr, *tmspPtr, NewChainAwareApplication())
if err != nil {
Exit(err.Error())
}
// Wait forever
TrapSignal(func() {
// Cleanup
})
}
type ChainAwareApplication struct {
beginCount int
endCount int
}
func NewChainAwareApplication() *ChainAwareApplication {
return &ChainAwareApplication{}
}
func (app *ChainAwareApplication) Info() string {
return "nil"
}
func (app *ChainAwareApplication) SetOption(key string, value string) (log string) {
return ""
}
func (app *ChainAwareApplication) AppendTx(tx []byte) types.Result {
return types.NewResultOK(nil, "")
}
func (app *ChainAwareApplication) CheckTx(tx []byte) types.Result {
return types.NewResultOK(nil, "")
}
func (app *ChainAwareApplication) Commit() types.Result {
return types.NewResultOK([]byte("nil"), "")
}
func (app *ChainAwareApplication) Query(query []byte) types.Result {
return types.NewResultOK([]byte(Fmt("%d,%d", app.beginCount, app.endCount)), "")
}
func (app *ChainAwareApplication) BeginBlock(height uint64) {
app.beginCount += 1
return
}
func (app *ChainAwareApplication) EndBlock(height uint64) []*types.Validator {
app.endCount += 1
return nil
}
func (app *ChainAwareApplication) InitChain(vals []*types.Validator) {
return
}

+ 50
- 0
example/chain_aware/chain_aware_test.go View File

@ -0,0 +1,50 @@
package main
import (
"strconv"
"strings"
"testing"
. "github.com/tendermint/go-common"
"github.com/tendermint/tmsp/client"
"github.com/tendermint/tmsp/server"
)
func TestChainAware(t *testing.T) {
app := NewChainAwareApplication()
// Start the listener
_, err := server.NewServer("unix://test.sock", "socket", app)
if err != nil {
t.Fatal(err)
}
// Connect to the socket
client, err := tmspcli.NewSocketClient("unix://test.sock", false)
if err != nil {
Exit(Fmt("Error starting socket client: %v", err.Error()))
}
client.Start()
defer client.Stop()
n := uint64(5)
for i := uint64(0); i < n; i++ {
client.BeginBlockSync(i)
client.EndBlockSync(i)
client.CommitSync()
}
r := app.Query(nil)
spl := strings.Split(string(r.Data), ",")
if len(spl) != 2 {
t.Fatal("expected %d,%d ; got %s", n, n, string(r.Data))
}
beginCount, _ := strconv.Atoi(spl[0])
endCount, _ := strconv.Atoi(spl[1])
if uint64(beginCount) != n {
t.Fatalf("expected beginCount of %d, got %d", n, beginCount)
} else if uint64(endCount) != n {
t.Fatalf("expected endCount of %d, got %d", n, endCount)
}
}

+ 1
- 0
example/dummy/dummy.go View File

@ -1,6 +1,7 @@
package dummy package dummy
import ( import (
"fmt"
"strings" "strings"
. "github.com/tendermint/go-common" . "github.com/tendermint/go-common"


+ 7
- 0
server/socket_server.go View File

@ -193,6 +193,13 @@ func (s *SocketServer) handleRequest(req *types.Request, responses chan<- *types
} else { } else {
responses <- types.ToResponseInitChain() responses <- types.ToResponseInitChain()
} }
case *types.Request_BeginBlock:
if app, ok := s.app.(types.BlockchainAware); ok {
app.BeginBlock(r.BeginBlock.Height)
responses <- types.ToResponseBeginBlock()
} else {
responses <- types.ToResponseBeginBlock()
}
case *types.Request_EndBlock: case *types.Request_EndBlock:
if app, ok := s.app.(types.BlockchainAware); ok { if app, ok := s.app.(types.BlockchainAware); ok {
validators := app.EndBlock(r.EndBlock.Height) validators := app.EndBlock(r.EndBlock.Height)


Loading…
Cancel
Save