diff --git a/benchmarks/codec_test.go b/benchmarks/codec_test.go index b34417992..83e25e212 100644 --- a/benchmarks/codec_test.go +++ b/benchmarks/codec_test.go @@ -4,6 +4,7 @@ import ( "testing" "time" + "github.com/tendermint/go-amino" "github.com/tendermint/go-crypto" proto "github.com/tendermint/tendermint/benchmarks/proto" @@ -13,6 +14,8 @@ import ( func BenchmarkEncodeStatusWire(b *testing.B) { b.StopTimer() + cdc := amino.NewCodec() + ctypes.RegisterAmino(cdc) pubKey := crypto.GenPrivKeyEd25519().PubKey() status := &ctypes.ResultStatus{ NodeInfo: p2p.NodeInfo{ @@ -43,6 +46,8 @@ func BenchmarkEncodeStatusWire(b *testing.B) { func BenchmarkEncodeNodeInfoWire(b *testing.B) { b.StopTimer() + cdc := amino.NewCodec() + ctypes.RegisterAmino(cdc) pubKey := crypto.GenPrivKeyEd25519().PubKey() nodeInfo := p2p.NodeInfo{ PubKey: pubKey, @@ -66,6 +71,8 @@ func BenchmarkEncodeNodeInfoWire(b *testing.B) { func BenchmarkEncodeNodeInfoBinary(b *testing.B) { b.StopTimer() + cdc := amino.NewCodec() + ctypes.RegisterAmino(cdc) pubKey := crypto.GenPrivKeyEd25519().PubKey() nodeInfo := p2p.NodeInfo{ PubKey: pubKey, diff --git a/cmd/priv_val_server/main.go b/cmd/priv_val_server/main.go index 9f3ec73ca..6b10b8178 100644 --- a/cmd/priv_val_server/main.go +++ b/cmd/priv_val_server/main.go @@ -30,7 +30,7 @@ func main() { "privPath", *privValPath, ) - privVal := priv_val.LoadPrivValidatorJSON(*privValPath) + privVal := priv_val.LoadFilePV(*privValPath) rs := priv_val.NewRemoteSigner( logger, diff --git a/cmd/tendermint/commands/gen_validator.go b/cmd/tendermint/commands/gen_validator.go index 59fe30128..ff0d97d76 100644 --- a/cmd/tendermint/commands/gen_validator.go +++ b/cmd/tendermint/commands/gen_validator.go @@ -1,12 +1,11 @@ package commands import ( - "encoding/json" "fmt" "github.com/spf13/cobra" - "github.com/tendermint/tendermint/types" + pvm "github.com/tendermint/tendermint/types/priv_validator" ) // GenValidatorCmd allows the generation of a keypair for a @@ -18,11 +17,11 @@ var GenValidatorCmd = &cobra.Command{ } func genValidator(cmd *cobra.Command, args []string) { - privValidator := types.GenPrivValidatorFS("") - privValidatorJSONBytes, err := json.MarshalIndent(privValidator, "", "\t") + pv := pvm.GenFilePV("") + jsbz, err := cdc.MarshalJSON(pv) if err != nil { panic(err) } fmt.Printf(`%v -`, string(privValidatorJSONBytes)) +`, string(jsbz)) } diff --git a/cmd/tendermint/commands/init.go b/cmd/tendermint/commands/init.go index 70648f8ff..224452646 100644 --- a/cmd/tendermint/commands/init.go +++ b/cmd/tendermint/commands/init.go @@ -4,6 +4,7 @@ import ( "github.com/spf13/cobra" "github.com/tendermint/tendermint/types" + pvm "github.com/tendermint/tendermint/types/priv_validator" cmn "github.com/tendermint/tmlibs/common" ) @@ -17,13 +18,13 @@ var InitFilesCmd = &cobra.Command{ func initFiles(cmd *cobra.Command, args []string) { // private validator privValFile := config.PrivValidatorFile() - var privValidator *types.PrivValidatorFS + var pv *pvm.FilePV if cmn.FileExists(privValFile) { - privValidator = types.LoadPrivValidatorFS(privValFile) + pv = pvm.LoadFilePV(privValFile) logger.Info("Found private validator", "path", privValFile) } else { - privValidator = types.GenPrivValidatorFS(privValFile) - privValidator.Save() + pv = pvm.GenFilePV(privValFile) + pv.Save() logger.Info("Generated private validator", "path", privValFile) } @@ -36,7 +37,7 @@ func initFiles(cmd *cobra.Command, args []string) { ChainID: cmn.Fmt("test-chain-%v", cmn.RandStr(6)), } genDoc.Validators = []types.GenesisValidator{{ - PubKey: privValidator.GetPubKey(), + PubKey: pv.GetPubKey(), Power: 10, }} diff --git a/cmd/tendermint/commands/reset_priv_validator.go b/cmd/tendermint/commands/reset_priv_validator.go index 513365237..78db87ded 100644 --- a/cmd/tendermint/commands/reset_priv_validator.go +++ b/cmd/tendermint/commands/reset_priv_validator.go @@ -5,7 +5,7 @@ import ( "github.com/spf13/cobra" - "github.com/tendermint/tendermint/types" + pvm "github.com/tendermint/tendermint/types/priv_validator" "github.com/tendermint/tmlibs/log" ) @@ -27,7 +27,7 @@ var ResetPrivValidatorCmd = &cobra.Command{ // ResetAll removes the privValidator files. // Exported so other CLI tools can use it. func ResetAll(dbDir, privValFile string, logger log.Logger) { - resetPrivValidatorFS(privValFile, logger) + resetFilePV(privValFile, logger) if err := os.RemoveAll(dbDir); err != nil { logger.Error("Error removing directory", "err", err) return @@ -44,18 +44,18 @@ func resetAll(cmd *cobra.Command, args []string) { // XXX: this is totally unsafe. // it's only suitable for testnets. func resetPrivValidator(cmd *cobra.Command, args []string) { - resetPrivValidatorFS(config.PrivValidatorFile(), logger) + resetFilePV(config.PrivValidatorFile(), logger) } -func resetPrivValidatorFS(privValFile string, logger log.Logger) { +func resetFilePV(privValFile string, logger log.Logger) { // Get PrivValidator if _, err := os.Stat(privValFile); err == nil { - privValidator := types.LoadPrivValidatorFS(privValFile) - privValidator.Reset() + pv := pvm.LoadFilePV(privValFile) + pv.Reset() logger.Info("Reset PrivValidator", "file", privValFile) } else { - privValidator := types.GenPrivValidatorFS(privValFile) - privValidator.Save() + pv := pvm.GenFilePV(privValFile) + pv.Save() logger.Info("Generated PrivValidator", "file", privValFile) } } diff --git a/cmd/tendermint/commands/testnet.go b/cmd/tendermint/commands/testnet.go index 3864e7ad5..eb86e4f8d 100644 --- a/cmd/tendermint/commands/testnet.go +++ b/cmd/tendermint/commands/testnet.go @@ -9,6 +9,7 @@ import ( cfg "github.com/tendermint/tendermint/config" "github.com/tendermint/tendermint/types" + pvm "github.com/tendermint/tendermint/types/priv_validator" cmn "github.com/tendermint/tmlibs/common" ) @@ -46,10 +47,10 @@ func testnetFiles(cmd *cobra.Command, args []string) { cmn.Exit(err.Error()) } // Read priv_validator.json to populate vals - privValFile := filepath.Join(dataDir, mach, defaultConfig.PrivValidator) - privVal := types.LoadPrivValidatorFS(privValFile) + pvFile := filepath.Join(dataDir, mach, defaultConfig.PrivValidator) + pv := pvm.LoadFilePV(pvFile) genVals[i] = types.GenesisValidator{ - PubKey: privVal.GetPubKey(), + PubKey: pv.GetPubKey(), Power: 1, Name: mach, } @@ -78,13 +79,13 @@ func initMachCoreDirectory(base, mach string) error { // Create priv_validator.json file if not present defaultConfig := cfg.DefaultBaseConfig() dir := filepath.Join(base, mach) - privValPath := filepath.Join(dir, defaultConfig.PrivValidator) - dir = filepath.Dir(privValPath) + pvPath := filepath.Join(dir, defaultConfig.PrivValidator) + dir = filepath.Dir(pvPath) err := cmn.EnsureDir(dir, 0700) if err != nil { return err } - ensurePrivValidator(privValPath) + ensurePrivValidator(pvPath) return nil } @@ -93,6 +94,6 @@ func ensurePrivValidator(file string) { if cmn.FileExists(file) { return } - privValidator := types.GenPrivValidatorFS(file) - privValidator.Save() + pv := pvm.GenFilePV(file) + pv.Save() } diff --git a/lite/files/commit.go b/lite/files/commit.go index 33f5bb677..8a7e4721e 100644 --- a/lite/files/commit.go +++ b/lite/files/commit.go @@ -1,13 +1,11 @@ package files import ( - "encoding/json" + "io/ioutil" "os" "github.com/pkg/errors" - wire "github.com/tendermint/go-wire" - "github.com/tendermint/tendermint/lite" liteErr "github.com/tendermint/tendermint/lite/errors" ) @@ -19,7 +17,7 @@ const ( MaxFullCommitSize = 1024 * 1024 ) -// SaveFullCommit exports the seed in binary / go-wire style +// SaveFullCommit exports the seed in binary / go-amino style func SaveFullCommit(fc lite.FullCommit, path string) error { f, err := os.Create(path) if err != nil { @@ -27,9 +25,11 @@ func SaveFullCommit(fc lite.FullCommit, path string) error { } defer f.Close() - var n int - wire.WriteBinary(fc, f, &n, &err) - return errors.WithStack(err) + _, err = cdc.MarshalBinaryWriter(f, fc) + if err != nil { + return errors.WithStack(err) + } + return nil } // SaveFullCommitJSON exports the seed in a json format @@ -39,9 +39,15 @@ func SaveFullCommitJSON(fc lite.FullCommit, path string) error { return errors.WithStack(err) } defer f.Close() - stream := json.NewEncoder(f) - err = stream.Encode(fc) - return errors.WithStack(err) + bz, err := cdc.MarshalJSON(fc) + if err != nil { + return errors.WithStack(err) + } + _, err = f.Write(bz) + if err != nil { + return errors.WithStack(err) + } + return nil } // LoadFullCommit loads the full commit from the file system. @@ -56,9 +62,11 @@ func LoadFullCommit(path string) (lite.FullCommit, error) { } defer f.Close() - var n int - wire.ReadBinaryPtr(&fc, f, MaxFullCommitSize, &n, &err) - return fc, errors.WithStack(err) + _, err = cdc.UnmarshalBinaryReader(f, &fc, 0) + if err != nil { + return fc, errors.WithStack(err) + } + return fc, nil } // LoadFullCommitJSON loads the commit from the file system in JSON format. @@ -73,7 +81,13 @@ func LoadFullCommitJSON(path string) (lite.FullCommit, error) { } defer f.Close() - stream := json.NewDecoder(f) - err = stream.Decode(&fc) - return fc, errors.WithStack(err) + bz, err := ioutil.ReadAll(f) + if err != nil { + return fc, errors.WithStack(err) + } + err = cdc.UnmarshalJSON(bz, &fc) + if err != nil { + return fc, errors.WithStack(err) + } + return fc, nil } diff --git a/lite/files/wire.go b/lite/files/wire.go new file mode 100644 index 000000000..99f98931e --- /dev/null +++ b/lite/files/wire.go @@ -0,0 +1,12 @@ +package files + +import ( + "github.com/tendermint/go-amino" + "github.com/tendermint/go-crypto" +) + +var cdc = amino.NewCodec() + +func init() { + crypto.RegisterAmino(cdc) +} diff --git a/lite/helpers.go b/lite/helpers.go index 7df77027c..714675afb 100644 --- a/lite/helpers.go +++ b/lite/helpers.go @@ -23,7 +23,7 @@ type ValKeys []crypto.PrivKey func GenValKeys(n int) ValKeys { res := make(ValKeys, n) for i := range res { - res[i] = crypto.GenPrivKeyEd25519().Wrap() + res[i] = crypto.GenPrivKeyEd25519() } return res } @@ -32,7 +32,7 @@ func GenValKeys(n int) ValKeys { func (v ValKeys) Change(i int) ValKeys { res := make(ValKeys, len(v)) copy(res, v) - res[i] = crypto.GenPrivKeyEd25519().Wrap() + res[i] = crypto.GenPrivKeyEd25519() return res } @@ -46,7 +46,7 @@ func (v ValKeys) Extend(n int) ValKeys { func GenSecpValKeys(n int) ValKeys { res := make(ValKeys, n) for i := range res { - res[i] = crypto.GenPrivKeySecp256k1().Wrap() + res[i] = crypto.GenPrivKeySecp256k1() } return res } diff --git a/lite/proxy/proxy.go b/lite/proxy/proxy.go index 34aa99fa0..fe10399dc 100644 --- a/lite/proxy/proxy.go +++ b/lite/proxy/proxy.go @@ -3,10 +3,12 @@ package proxy import ( "net/http" + "github.com/tendermint/go-amino" "github.com/tendermint/tmlibs/log" rpcclient "github.com/tendermint/tendermint/rpc/client" "github.com/tendermint/tendermint/rpc/core" + ctypes "github.com/tendermint/tendermint/rpc/core/types" rpc "github.com/tendermint/tendermint/rpc/lib/server" ) @@ -23,13 +25,15 @@ func StartProxy(c rpcclient.Client, listenAddr string, logger log.Logger) error return err } + cdc := amino.NewCodec() + ctypes.RegisterAmino(cdc) r := RPCRoutes(c) // build the handler... mux := http.NewServeMux() - rpc.RegisterRPCFuncs(mux, r, logger) + rpc.RegisterRPCFuncs(mux, r, cdc, logger) - wm := rpc.NewWebsocketManager(r, rpc.EventSubscriber(c)) + wm := rpc.NewWebsocketManager(r, cdc, rpc.EventSubscriber(c)) wm.SetLogger(logger) core.SetLogger(logger) mux.HandleFunc(wsEndpoint, wm.WebsocketHandler) diff --git a/types/event_bus_test.go b/types/event_bus_test.go index 9002b531c..40aa0746a 100644 --- a/types/event_bus_test.go +++ b/types/event_bus_test.go @@ -73,7 +73,7 @@ func benchmarkEventBus(numClients int, randQueries bool, randEvents bool, b *tes eventType = randEvent() } - eventBus.Publish(eventType, "Gamora") + eventBus.Publish(eventType, EventDataString("Gamora")) } } diff --git a/types/events.go b/types/events.go index cdffc0ee5..342d4bc20 100644 --- a/types/events.go +++ b/types/events.go @@ -47,15 +47,17 @@ func (_ EventDataTx) AssertIsTMEventData() {} func (_ EventDataRoundState) AssertIsTMEventData() {} func (_ EventDataVote) AssertIsTMEventData() {} func (_ EventDataProposalHeartbeat) AssertIsTMEventData() {} +func (_ EventDataString) AssertIsTMEventData() {} func RegisterEventDatas(cdc *amino.Codec) { cdc.RegisterInterface((*TMEventData)(nil), nil) - cdc.RegisterConcrete(EventDataNewBlock{}, "tendermint/EventDataNameNewBlock", nil) - cdc.RegisterConcrete(EventDataNewBlockHeader{}, "tendermint/EventDataNameNewBlockHeader", nil) - cdc.RegisterConcrete(EventDataTx{}, "tendermint/EventDataNameTx", nil) - cdc.RegisterConcrete(EventDataRoundState{}, "tendermint/EventDataNameRoundState", nil) - cdc.RegisterConcrete(EventDataVote{}, "tendermint/EventDataNameVote", nil) - cdc.RegisterConcrete(EventDataProposalHeartbeat{}, "tendermint/EventDataNameProposalHeartbeat", nil) + cdc.RegisterConcrete(EventDataNewBlock{}, "tendermint/event/NewBlock", nil) + cdc.RegisterConcrete(EventDataNewBlockHeader{}, "tendermint/event/NewBlockHeader", nil) + cdc.RegisterConcrete(EventDataTx{}, "tendermint/event/Tx", nil) + cdc.RegisterConcrete(EventDataRoundState{}, "tendermint/event/RoundState", nil) + cdc.RegisterConcrete(EventDataVote{}, "tendermint/event/Vote", nil) + cdc.RegisterConcrete(EventDataProposalHeartbeat{}, "tendermint/event/ProposalHeartbeat", nil) + cdc.RegisterConcrete(EventDataString(""), "tendermint/event/ProposalString", nil) } // Most event messages are basic types (a block, a transaction) @@ -93,6 +95,8 @@ type EventDataVote struct { Vote *Vote } +type EventDataString string + /////////////////////////////////////////////////////////////////////////////// // PUBSUB ///////////////////////////////////////////////////////////////////////////////