diff --git a/.gitignore b/.gitignore index 1377554e..7a357eac 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ *.swp +.bak diff --git a/accounts/account.go b/accounts/account.go new file mode 100644 index 00000000..ee12ced3 --- /dev/null +++ b/accounts/account.go @@ -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 +} diff --git a/binary/byteslice.go b/binary/byteslice.go index c17625e6..d134ed1a 100644 --- a/binary/byteslice.go +++ b/binary/byteslice.go @@ -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 diff --git a/binary/string.go b/binary/string.go index 2bc1d8cb..9dd37d89 100644 --- a/binary/string.go +++ b/binary/string.go @@ -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 diff --git a/blocks/codec_test.go b/blocks/codec_test.go new file mode 100644 index 00000000..a789522d --- /dev/null +++ b/blocks/codec_test.go @@ -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") + } + } +} diff --git a/config/config.go b/config/config.go index 0350b6fe..01382f4a 100644 --- a/config/config.go +++ b/config/config.go @@ -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) } diff --git a/log.go b/log.go new file mode 100644 index 00000000..560d009f --- /dev/null +++ b/log.go @@ -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 := ` + + + + + + + + +` + + var err error + log, err = seelog.LoggerFromConfigAsBytes([]byte(config)) + if err != nil { + panic(err) + } + + p2p.SetLogger(log) +} diff --git a/main.go b/main.go index 81839430..a6c3761f 100644 --- a/main.go +++ b/main.go @@ -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") } diff --git a/p2p/log.go b/p2p/log.go index 5cb2e230..0a0ab276 100644 --- a/p2p/log.go +++ b/p2p/log.go @@ -6,22 +6,6 @@ import ( var log seelog.LoggerInterface -func init() { - // TODO: replace with configuration file in the ~/.tendermint directory. - config := ` - - - - - - - - -` - - var err error - log, err = seelog.LoggerFromConfigAsBytes([]byte(config)) - if err != nil { - panic(err) - } +func SetLogger(l seelog.LoggerInterface) { + log = l }