bug fixes in binary

This commit is contained in:
Jae Kwon 2014-07-08 15:33:26 -07:00
parent 04eb07c26f
commit dca79ab5c1
9 changed files with 288 additions and 39 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
*.swp
.bak

23
accounts/account.go Normal file
View File

@ -0,0 +1,23 @@
package accounts
import (
. "github.com/tendermint/tendermint/binary"
)
type Account struct {
Name String
PubKey ByteSlice
}
func (self *Account) Verify(msg ByteSlice, sig ByteSlice) bool {
return false
}
type MyAccount struct {
Account
PrivKey ByteSlice
}
func (self *MyAccount) Sign(msg ByteSlice) ByteSlice {
return nil
}

View File

@ -50,7 +50,7 @@ func ReadByteSliceSafe(r io.Reader) (ByteSlice, error) {
func ReadByteSlice(r io.Reader) ByteSlice {
bytes, err := ReadByteSliceSafe(r)
if r != nil {
if err != nil {
panic(err)
}
return bytes

View File

@ -47,7 +47,7 @@ func ReadStringSafe(r io.Reader) (String, error) {
func ReadString(r io.Reader) String {
str, err := ReadStringSafe(r)
if r != nil {
if err != nil {
panic(err)
}
return str

167
blocks/codec_test.go Normal file
View File

@ -0,0 +1,167 @@
package blocks
import (
"bytes"
"encoding/gob"
"encoding/json"
"testing"
. "github.com/tendermint/tendermint/binary"
"github.com/ugorji/go/codec"
"github.com/vmihailenco/msgpack"
)
func BenchmarkTestCustom(b *testing.B) {
b.StopTimer()
h := &Header{
Name: "Header",
Height: 123,
Fees: 123,
Time: 123,
PrevHash: ByteSlice("prevhash"),
ValidationHash: ByteSlice("validationhash"),
DataHash: ByteSlice("datahash"),
}
buf := bytes.NewBuffer(nil)
b.StartTimer()
for i := 0; i < b.N; i++ {
buf.Reset()
h.WriteTo(buf)
h2 := ReadHeader(buf)
if h2.Name != "Header" {
b.Fatalf("wrong name")
}
}
}
type HHeader struct {
Name string `json:"N"`
Height uint64 `json:"H"`
Fees uint64 `json:"F"`
Time uint64 `json:"T"`
PrevHash []byte `json:"PH"`
ValidationHash []byte `json:"VH"`
DataHash []byte `json:"DH"`
}
func BenchmarkTestJSON(b *testing.B) {
b.StopTimer()
h := &HHeader{
Name: "Header",
Height: 123,
Fees: 123,
Time: 123,
PrevHash: []byte("prevhash"),
ValidationHash: []byte("validationhash"),
DataHash: []byte("datahash"),
}
h2 := &HHeader{}
buf := bytes.NewBuffer(nil)
enc := json.NewEncoder(buf)
dec := json.NewDecoder(buf)
b.StartTimer()
for i := 0; i < b.N; i++ {
buf.Reset()
enc.Encode(h)
dec.Decode(h2)
if h2.Name != "Header" {
b.Fatalf("wrong name")
}
}
}
func BenchmarkTestGob(b *testing.B) {
b.StopTimer()
h := &Header{
Name: "Header",
Height: 123,
Fees: 123,
Time: 123,
PrevHash: []byte("prevhash"),
ValidationHash: []byte("validationhash"),
DataHash: []byte("datahash"),
}
h2 := &Header{}
buf := bytes.NewBuffer(nil)
enc := gob.NewEncoder(buf)
dec := gob.NewDecoder(buf)
b.StartTimer()
for i := 0; i < b.N; i++ {
buf.Reset()
enc.Encode(h)
dec.Decode(h2)
if h2.Name != "Header" {
b.Fatalf("wrong name")
}
}
}
func BenchmarkTestMsgPack(b *testing.B) {
b.StopTimer()
var mh codec.MsgpackHandle
handle := &mh
h := &Header{
Name: "Header",
Height: 123,
Fees: 123,
Time: 123,
PrevHash: []byte("prevhash"),
ValidationHash: []byte("validationhash"),
DataHash: []byte("datahash"),
}
h2 := &Header{}
buf := bytes.NewBuffer(nil)
enc := codec.NewEncoder(buf, handle)
dec := codec.NewDecoder(buf, handle)
b.StartTimer()
for i := 0; i < b.N; i++ {
buf.Reset()
enc.Encode(h)
dec.Decode(h2)
if h2.Name != "Header" {
b.Fatalf("wrong name")
}
}
}
func BenchmarkTestMsgPack2(b *testing.B) {
b.StopTimer()
h := &Header{
Name: "Header",
Height: 123,
Fees: 123,
Time: 123,
PrevHash: []byte("prevhash"),
ValidationHash: []byte("validationhash"),
DataHash: []byte("datahash"),
}
h2 := &Header{}
buf := bytes.NewBuffer(nil)
enc := msgpack.NewEncoder(buf)
dec := msgpack.NewDecoder(buf)
b.StartTimer()
for i := 0; i < b.N; i++ {
buf.Reset()
enc.Encode(h)
dec.Decode(h2)
if h2.Name != "Header" {
b.Fatalf("wrong name")
}
}
}

View File

@ -13,15 +13,14 @@ import (
//"encoding/hex"
)
var APP_DIR = os.Getenv("HOME") + "/.tendermint"
/* Global & initialization */
var AppDir = os.Getenv("HOME") + "/.tendermint"
var Config Config_
func init() {
configFile := APP_DIR + "/config.json"
configFile := AppDir + "/config.json"
// try to read configuration. if missing, write default
configBytes, err := ioutil.ReadFile(configFile)
@ -51,7 +50,7 @@ var defaultConfig = Config_{
Port: 8770,
Db: DbConfig{
Type: "level",
Dir: APP_DIR + "/data",
Dir: AppDir + "/data",
},
Twilio: TwilioConfig{},
}
@ -92,7 +91,7 @@ func (cfg *Config_) validate() error {
}
func (cfg *Config_) bytes() []byte {
configBytes, err := json.Marshal(cfg)
configBytes, err := json.MarshalIndent(cfg, "", "\t")
if err != nil {
panic(err)
}

30
log.go Normal file
View File

@ -0,0 +1,30 @@
package main
import (
"github.com/cihub/seelog"
"github.com/tendermint/tendermint/p2p"
)
var log seelog.LoggerInterface
func init() {
// TODO: replace with configuration file in the ~/.tendermint directory.
config := `
<seelog type="asyncloop" minlevel="debug">
<outputs formatid="colored">
<console/>
</outputs>
<formats>
<format id="main" format="%Date/%Time [%LEV] %Msg%n"/>
<format id="colored" format="%EscM(46)%Level%EscM(49) %EscM(36)%File%EscM(39) %Msg%n%EscM(0)"/>
</formats>
</seelog>`
var err error
log, err = seelog.LoggerFromConfigAsBytes([]byte(config))
if err != nil {
panic(err)
}
p2p.SetLogger(log)
}

73
main.go
View File

@ -1,33 +1,35 @@
package main
import (
"fmt"
"os"
"os/signal"
"syscall"
"github.com/tendermint/tendermint/config"
"github.com/tendermint/tendermint/p2p"
)
func initPeer(peer *p2p.Peer) {
//
}
func main() {
// Define channels for our app
chDescs := []ChannelDescriptor{
ChannelDescriptor{
chDescs := []p2p.ChannelDescriptor{
p2p.ChannelDescriptor{
Name: "PEX",
SendBufferSize: 2,
RecvBuffersize: 2,
RecvBufferSize: 2,
},
ChannelDescriptor{
p2p.ChannelDescriptor{
Name: "block",
SendBufferSize: 10,
RecvBufferSize: 10,
},
ChannelDescriptor{
p2p.ChannelDescriptor{
Name: "mempool",
SendBufferSize: 100,
RecvBufferSize: 100,
},
ChannelDescriptor{
p2p.ChannelDescriptor{
Name: "consensus",
SendBufferSize: 1000,
RecvBufferSize: 1000,
@ -35,19 +37,62 @@ func main() {
}
// Create the switch
sw := NewSwitch(chDescs)
sw := p2p.NewSwitch(chDescs)
// Create a listener for incoming connections
l := NewDefaultListener("tcp", ":8001")
l := p2p.NewDefaultListener("tcp", ":8001")
go func() {
for {
inConn, ok := <-l.Connections()
if !ok {
break
}
sw.AddPeerWithConnection(inConn, false)
peer, err := sw.AddPeerWithConnection(inConn, false)
if err != nil {
log.Infof("Ignoring error from incoming connection: %v\n%v",
peer, err)
continue
}
initPeer(peer)
}
}()
// TODO
// Open our address book
book := p2p.NewAddrBook(config.AppDir + "/addrbook.json")
// Start PEX
go p2p.PexHandler(sw, book)
// Sleep forever
go _trapSignal()
select {}
}
func initPeer(peer *p2p.Peer) {
// TODO: ask for more peers if we need them.
}
func trapSignal() {
ch := make(chan os.Signal)
signal.Notify(ch, syscall.SIGINT)
sig := <-ch
fmt.Println("???", sig)
os.Exit(0)
}
func _trapSignal() {
// capture ctrl+c and stop CPU profiler
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
//signal.Notify(c, syscall.SIGINT)
go func() {
fmt.Println("inside")
for sig := range c {
fmt.Println("signal!>>", sig)
log.Infof("captured %v, stopping profiler and exiting..", sig)
os.Exit(1)
}
fmt.Println("inside done")
}()
fmt.Println("ok")
}

View File

@ -6,22 +6,6 @@ import (
var log seelog.LoggerInterface
func init() {
// TODO: replace with configuration file in the ~/.tendermint directory.
config := `
<seelog type="asyncloop" minlevel="debug">
<outputs formatid="colored">
<console/>
</outputs>
<formats>
<format id="main" format="%Date/%Time [%LEV] %Msg%n"/>
<format id="colored" format="%EscM(46)%Level%EscM(49) %EscM(36)%File%EscM(39) %Msg%n%EscM(0)"/>
</formats>
</seelog>`
var err error
log, err = seelog.LoggerFromConfigAsBytes([]byte(config))
if err != nil {
panic(err)
}
func SetLogger(l seelog.LoggerInterface) {
log = l
}