// receives on a write channel and writes out on the socket func (wsc *WSConnection) writeRoutine() { defer wsc.baseConn.Close() var n, err = int64(0), error(nil) for { select { case <-wsc.Quit: return case <-wsc.pingTicker.C: err := wsc.baseConn.WriteMessage(websocket.PingMessage, []byte{}) if err != nil { log.Error("Failed to write ping message on websocket", "error", err) wsc.Stop() return } case msg := <-wsc.writeChan: buf := new(bytes.Buffer) wire.WriteJSON(msg, buf, &n, &err) if err != nil { log.Error("Failed to marshal RPCResponse to JSON", "error", err) } else { wsc.baseConn.SetWriteDeadline(time.Now().Add(time.Second * wsWriteTimeoutSeconds)) if err = wsc.baseConn.WriteMessage(websocket.TextMessage, buf.Bytes()); err != nil { log.Warn("Failed to write response on websocket", "error", err) wsc.Stop() return } } } } }
func (tx *PermissionsTx) WriteSignBytes(chainID string, w io.Writer, n *int64, err *error) { wire.WriteTo([]byte(Fmt(`{"chain_id":%s`, jsonEscape(chainID))), w, n, err) wire.WriteTo([]byte(Fmt(`,"tx":[%v,{"args":"`, TxTypePermissions)), w, n, err) wire.WriteJSON(tx.PermArgs, w, n, err) wire.WriteTo([]byte(`","input":`), w, n, err) tx.Input.WriteSignBytes(w, n, err) wire.WriteTo([]byte(`}]}`), w, n, err) }
func WriteRPCResponse(w http.ResponseWriter, res RPCResponse) { buf, n, err := new(bytes.Buffer), int64(0), error(nil) wire.WriteJSON(res, buf, &n, &err) if err != nil { log.Error("Failed to write RPC response", "error", err, "res", res) } w.Header().Set("Content-Type", "application/json") w.WriteHeader(200) w.Write(buf.Bytes()) }
func binaryWriter(args ...interface{}) ([]interface{}, error) { list := []interface{}{} for _, a := range args { buf, n, err := new(bytes.Buffer), new(int64), new(error) wire.WriteJSON(a, buf, n, err) if *err != nil { return nil, *err } list = append(list, buf.Bytes()) } return list, nil }
func argsToJson(args ...interface{}) ([]string, error) { l := len(args) jsons := make([]string, l) n, err := new(int64), new(error) for i, a := range args { buf := new(bytes.Buffer) wire.WriteJSON(a, buf, n, err) if *err != nil { return nil, *err } jsons[i] = string(buf.Bytes()) } return jsons, nil }
// Used to write json files using tendermints wire package. func writeJSON(file string, v interface{}) error { var n int64 var errW error fo, errC := os.Create(file) if errC != nil { return errC } wire.WriteJSON(v, fo, &n, &errW) if errW != nil { return errW } errL := fo.Close() if errL != nil { return errL } return nil }
func NewNode() *Node { // Get BlockStore blockStoreDB := dbm.GetDB("blockstore") blockStore := bc.NewBlockStore(blockStoreDB) // Get State stateDB := dbm.GetDB("state") state := sm.LoadState(stateDB) var genDoc *stypes.GenesisDoc if state == nil { genDoc, state = sm.MakeGenesisStateFromFile(stateDB, config.GetString("genesis_file")) state.Save() // write the gendoc to db buf, n, err := new(bytes.Buffer), new(int64), new(error) wire.WriteJSON(genDoc, buf, n, err) stateDB.Set(stypes.GenDocKey, buf.Bytes()) if *err != nil { Exit(Fmt("Unable to write gendoc to db: %v", err)) } } else { genDocBytes := stateDB.Get(stypes.GenDocKey) err := new(error) wire.ReadJSONPtr(&genDoc, genDocBytes, err) if *err != nil { Exit(Fmt("Unable to read gendoc from db: %v", err)) } } // add the chainid to the global config config.Set("chain_id", state.ChainID) // Get PrivValidator privValidatorFile := config.GetString("priv_validator_file") privValidator := types.LoadOrGenPrivValidator(privValidatorFile) // Generate node PrivKey privKey := acm.GenPrivKeyEd25519() // Make event switch eventSwitch := events.NewEventSwitch() _, err := eventSwitch.Start() if err != nil { Exit(Fmt("Failed to start switch: %v", err)) } // Make PEXReactor book := p2p.NewAddrBook(config.GetString("addrbook_file")) pexReactor := p2p.NewPEXReactor(book) // Make BlockchainReactor bcReactor := bc.NewBlockchainReactor(state.Copy(), blockStore, config.GetBool("fast_sync")) // Make MempoolReactor mempool := mempl.NewMempool(state.Copy()) mempoolReactor := mempl.NewMempoolReactor(mempool) // Make ConsensusReactor consensusState := consensus.NewConsensusState(state.Copy(), blockStore, mempoolReactor) consensusReactor := consensus.NewConsensusReactor(consensusState, blockStore, config.GetBool("fast_sync")) if privValidator != nil { consensusReactor.SetPrivValidator(privValidator) } // Make p2p network switch sw := p2p.NewSwitch() sw.AddReactor("PEX", pexReactor) sw.AddReactor("MEMPOOL", mempoolReactor) sw.AddReactor("BLOCKCHAIN", bcReactor) sw.AddReactor("CONSENSUS", consensusReactor) // add the event switch to all services // they should all satisfy events.Eventable SetFireable(eventSwitch, pexReactor, bcReactor, mempoolReactor, consensusReactor) // run the profile server profileHost := config.GetString("prof_laddr") if profileHost != "" { go func() { log.Warn("Profile server", "error", http.ListenAndServe(profileHost, nil)) }() } // set vm log level vm.SetDebug(config.GetBool("vm_log")) return &Node{ sw: sw, evsw: eventSwitch, book: book, blockStore: blockStore, pexReactor: pexReactor, bcReactor: bcReactor, mempoolReactor: mempoolReactor, consensusState: consensusState, consensusReactor: consensusReactor, privValidator: privValidator, genDoc: genDoc, privKey: privKey, } }