Browse Source

Lots of updates to use new go-crypto / json style

pull/455/head
Ethan Frey 8 years ago
committed by Ethan Buchman
parent
commit
e325ffc681
7 changed files with 41 additions and 31 deletions
  1. +5
    -5
      benchmarks/codec_test.go
  2. +3
    -2
      cmd/tendermint/commands/show_validator.go
  3. +12
    -12
      config/tendermint_test/config.go
  4. +1
    -1
      glide.lock
  5. +1
    -1
      node/node.go
  6. +9
    -5
      types/genesis.go
  7. +10
    -5
      types/priv_validator.go

+ 5
- 5
benchmarks/codec_test.go View File

@ -12,10 +12,10 @@ import (
func BenchmarkEncodeStatusWire(b *testing.B) { func BenchmarkEncodeStatusWire(b *testing.B) {
b.StopTimer() b.StopTimer()
pubKey := crypto.GenPrivKeyEd25519().PubKey().(crypto.PubKeyEd25519)
pubKey := crypto.GenPrivKeyEd25519().PubKey()
status := &ctypes.ResultStatus{ status := &ctypes.ResultStatus{
NodeInfo: &p2p.NodeInfo{ NodeInfo: &p2p.NodeInfo{
PubKey: pubKey,
PubKey: pubKey.Unwrap().(crypto.PubKeyEd25519),
Moniker: "SOMENAME", Moniker: "SOMENAME",
Network: "SOMENAME", Network: "SOMENAME",
RemoteAddr: "SOMEADDR", RemoteAddr: "SOMEADDR",
@ -40,7 +40,7 @@ func BenchmarkEncodeStatusWire(b *testing.B) {
func BenchmarkEncodeNodeInfoWire(b *testing.B) { func BenchmarkEncodeNodeInfoWire(b *testing.B) {
b.StopTimer() b.StopTimer()
pubKey := crypto.GenPrivKeyEd25519().PubKey().(crypto.PubKeyEd25519)
pubKey := crypto.GenPrivKeyEd25519().PubKey().Unwrap().(crypto.PubKeyEd25519)
nodeInfo := &p2p.NodeInfo{ nodeInfo := &p2p.NodeInfo{
PubKey: pubKey, PubKey: pubKey,
Moniker: "SOMENAME", Moniker: "SOMENAME",
@ -61,7 +61,7 @@ func BenchmarkEncodeNodeInfoWire(b *testing.B) {
func BenchmarkEncodeNodeInfoBinary(b *testing.B) { func BenchmarkEncodeNodeInfoBinary(b *testing.B) {
b.StopTimer() b.StopTimer()
pubKey := crypto.GenPrivKeyEd25519().PubKey().(crypto.PubKeyEd25519)
pubKey := crypto.GenPrivKeyEd25519().PubKey().Unwrap().(crypto.PubKeyEd25519)
nodeInfo := &p2p.NodeInfo{ nodeInfo := &p2p.NodeInfo{
PubKey: pubKey, PubKey: pubKey,
Moniker: "SOMENAME", Moniker: "SOMENAME",
@ -83,7 +83,7 @@ func BenchmarkEncodeNodeInfoBinary(b *testing.B) {
func BenchmarkEncodeNodeInfoProto(b *testing.B) { func BenchmarkEncodeNodeInfoProto(b *testing.B) {
b.StopTimer() b.StopTimer()
pubKey := crypto.GenPrivKeyEd25519().PubKey().(crypto.PubKeyEd25519)
pubKey := crypto.GenPrivKeyEd25519().PubKey().Unwrap().(crypto.PubKeyEd25519)
pubKey2 := &proto.PubKey{Ed25519: &proto.PubKeyEd25519{Bytes: pubKey[:]}} pubKey2 := &proto.PubKey{Ed25519: &proto.PubKeyEd25519{Bytes: pubKey[:]}}
nodeInfo := &proto.NodeInfo{ nodeInfo := &proto.NodeInfo{
PubKey: pubKey2, PubKey: pubKey2,


+ 3
- 2
cmd/tendermint/commands/show_validator.go View File

@ -5,7 +5,7 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/tendermint/go-wire"
data "github.com/tendermint/go-data"
"github.com/tendermint/tendermint/types" "github.com/tendermint/tendermint/types"
) )
@ -22,5 +22,6 @@ func init() {
func showValidator(cmd *cobra.Command, args []string) { func showValidator(cmd *cobra.Command, args []string) {
privValidatorFile := config.GetString("priv_validator_file") privValidatorFile := config.GetString("priv_validator_file")
privValidator := types.LoadOrGenPrivValidator(privValidatorFile) privValidator := types.LoadOrGenPrivValidator(privValidatorFile)
fmt.Println(string(wire.JSONBytesPretty(privValidator.PubKey)))
pubKeyJSONBytes, _ := data.ToJSON(privValidator.PubKey)
fmt.Println(string(pubKeyJSONBytes))
} }

+ 12
- 12
config/tendermint_test/config.go View File

@ -137,10 +137,10 @@ var defaultGenesis = `{
"chain_id": "tendermint_test", "chain_id": "tendermint_test",
"validators": [ "validators": [
{ {
"pub_key": [
1,
"3B3069C422E19688B45CBFAE7BB009FC0FA1B1EA86593519318B7214853803C8"
],
"pub_key": {
"type": "ed25519",
"data":"3B3069C422E19688B45CBFAE7BB009FC0FA1B1EA86593519318B7214853803C8"
},
"amount": 10, "amount": 10,
"name": "" "name": ""
} }
@ -150,14 +150,14 @@ var defaultGenesis = `{
var defaultPrivValidator = `{ var defaultPrivValidator = `{
"address": "D028C9981F7A87F3093672BF0D5B0E2A1B3ED456", "address": "D028C9981F7A87F3093672BF0D5B0E2A1B3ED456",
"pub_key": [
1,
"3B3069C422E19688B45CBFAE7BB009FC0FA1B1EA86593519318B7214853803C8"
],
"priv_key": [
1,
"27F82582AEFAE7AB151CFB01C48BB6C1A0DA78F9BDDA979A9F70A84D074EB07D3B3069C422E19688B45CBFAE7BB009FC0FA1B1EA86593519318B7214853803C8"
],
"pub_key": {
"type": "ed25519",
"data": "3B3069C422E19688B45CBFAE7BB009FC0FA1B1EA86593519318B7214853803C8"
},
"priv_key": {
"type": "ed25519",
"data": "27F82582AEFAE7AB151CFB01C48BB6C1A0DA78F9BDDA979A9F70A84D074EB07D3B3069C422E19688B45CBFAE7BB009FC0FA1B1EA86593519318B7214853803C8"
},
"last_height": 0, "last_height": 0,
"last_round": 0, "last_round": 0,
"last_step": 0 "last_step": 0


+ 1
- 1
glide.lock View File

@ -106,7 +106,7 @@ imports:
- name: github.com/tendermint/go-merkle - name: github.com/tendermint/go-merkle
version: 714d4d04557fd068a7c2a1748241ce8428015a96 version: 714d4d04557fd068a7c2a1748241ce8428015a96
- name: github.com/tendermint/go-p2p - name: github.com/tendermint/go-p2p
version: e8f33a47846708269d373f9c8080613d6c4f66b2
version: a163fddbdd2541793d22402afb113e6ccb391774
subpackages: subpackages:
- upnp - upnp
- name: github.com/tendermint/go-rpc - name: github.com/tendermint/go-rpc


+ 1
- 1
node/node.go View File

@ -372,7 +372,7 @@ func (n *Node) makeNodeInfo() *p2p.NodeInfo {
} }
nodeInfo := &p2p.NodeInfo{ nodeInfo := &p2p.NodeInfo{
PubKey: n.privKey.PubKey().(crypto.PubKeyEd25519),
PubKey: n.privKey.PubKey().Unwrap().(crypto.PubKeyEd25519),
Moniker: n.config.GetString("moniker"), Moniker: n.config.GetString("moniker"),
Network: n.config.GetString("chain_id"), Network: n.config.GetString("chain_id"),
Version: version.Version, Version: version.Version,


+ 9
- 5
types/genesis.go View File

@ -1,11 +1,11 @@
package types package types
import ( import (
"encoding/json"
"time" "time"
. "github.com/tendermint/go-common" . "github.com/tendermint/go-common"
"github.com/tendermint/go-crypto" "github.com/tendermint/go-crypto"
"github.com/tendermint/go-wire"
) )
//------------------------------------------------------------ //------------------------------------------------------------
@ -31,14 +31,18 @@ type GenesisDoc struct {
// Utility method for saving GenensisDoc as JSON file. // Utility method for saving GenensisDoc as JSON file.
func (genDoc *GenesisDoc) SaveAs(file string) error { func (genDoc *GenesisDoc) SaveAs(file string) error {
genDocBytes := wire.JSONBytesPretty(genDoc)
genDocBytes, err := json.Marshal(genDoc)
if err != nil {
return err
}
return WriteFile(file, genDocBytes, 0644) return WriteFile(file, genDocBytes, 0644)
} }
//------------------------------------------------------------ //------------------------------------------------------------
// Make genesis state from file // Make genesis state from file
func GenesisDocFromJSON(jsonBlob []byte) (genDoc *GenesisDoc, err error) {
wire.ReadJSONPtr(&genDoc, jsonBlob, &err)
return
func GenesisDocFromJSON(jsonBlob []byte) (*GenesisDoc, error) {
genDoc := GenesisDoc{}
err := json.Unmarshal(jsonBlob, &genDoc)
return &genDoc, err
} }

+ 10
- 5
types/priv_validator.go View File

@ -2,6 +2,7 @@ package types
import ( import (
"bytes" "bytes"
"encoding/json"
"errors" "errors"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
@ -10,7 +11,6 @@ import (
. "github.com/tendermint/go-common" . "github.com/tendermint/go-common"
"github.com/tendermint/go-crypto" "github.com/tendermint/go-crypto"
"github.com/tendermint/go-wire"
) )
const ( const (
@ -96,13 +96,14 @@ func LoadPrivValidator(filePath string) *PrivValidator {
if err != nil { if err != nil {
Exit(err.Error()) Exit(err.Error())
} }
privVal := wire.ReadJSON(&PrivValidator{}, privValJSONBytes, &err).(*PrivValidator)
privVal := PrivValidator{}
err = json.Unmarshal(privValJSONBytes, &privVal)
if err != nil { if err != nil {
Exit(Fmt("Error reading PrivValidator from %v: %v\n", filePath, err)) Exit(Fmt("Error reading PrivValidator from %v: %v\n", filePath, err))
} }
privVal.filePath = filePath privVal.filePath = filePath
privVal.Signer = NewDefaultSigner(privVal.PrivKey) privVal.Signer = NewDefaultSigner(privVal.PrivKey)
return privVal
return &privVal
} }
func LoadOrGenPrivValidator(filePath string) *PrivValidator { func LoadOrGenPrivValidator(filePath string) *PrivValidator {
@ -136,8 +137,12 @@ func (privVal *PrivValidator) save() {
if privVal.filePath == "" { if privVal.filePath == "" {
PanicSanity("Cannot save PrivValidator: filePath not set") PanicSanity("Cannot save PrivValidator: filePath not set")
} }
jsonBytes := wire.JSONBytesPretty(privVal)
err := WriteFileAtomic(privVal.filePath, jsonBytes, 0600)
jsonBytes, err := json.Marshal(privVal)
if err != nil {
// `@; BOOM!!!
PanicCrisis(err)
}
err = WriteFileAtomic(privVal.filePath, jsonBytes, 0600)
if err != nil { if err != nil {
// `@; BOOM!!! // `@; BOOM!!!
PanicCrisis(err) PanicCrisis(err)


Loading…
Cancel
Save