Skip to content

Commit

Permalink
Add a command line tool nodectl
Browse files Browse the repository at this point in the history
1. provide a command 'nodectl'
2. provide a local RPC server

Signed-off-by: Ziyan Zhou <[email protected]>
  • Loading branch information
zhouziyan authored and dreamfly281 committed Mar 23, 2017
1 parent a4f6aa0 commit 1f4e0b7
Show file tree
Hide file tree
Showing 21 changed files with 838 additions and 455 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ BUILD_PAR = -ldflags "-X main.Version=$(VERSION)"

all:
$(GC) $(BUILD_PAR) main.go
$(GC) $(BUILD_PAR) nodectl.go

format:
$(GOFMT) -w main.go

Expand Down
5 changes: 3 additions & 2 deletions common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"golang.org/x/crypto/ripemd160"
"crypto/sha256"
. "GoOnchain/errors"
"GoOnchain/common/log"
//"GoOnchain/common/log"
"errors"
"io"
)
Expand All @@ -32,7 +32,8 @@ func GetNonce() uint64 {
Trace()
// Fixme replace with the real random number generator
nonce := uint64(rand.Uint32())<<32 + uint64(rand.Uint32())
log.Debug(fmt.Sprintf("The new nonce is: 0x%x", nonce))
Trace()
fmt.Println(fmt.Sprintf("The new nonce is: 0x%x", nonce))
return nonce
}

Expand Down
19 changes: 9 additions & 10 deletions common/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,24 @@ package common
import (
"bytes"
"fmt"
"path/filepath"
"runtime"
"strconv"
"time"
"path/filepath"
)

func getGID() uint64 {
b := make([]byte, 64)
b = b[:runtime.Stack(b, false)]
b = bytes.TrimPrefix(b, []byte("goroutine "))
b = b[:bytes.IndexByte(b, ' ')]
n, _ := strconv.ParseUint(string(b), 10, 64)
return n
func GetGID() uint64 {
b := make([]byte, 64)
b = b[:runtime.Stack(b, false)]
b = bytes.TrimPrefix(b, []byte("goroutine "))
b = b[:bytes.IndexByte(b, ' ')]
n, _ := strconv.ParseUint(string(b), 10, 64)
return n
}


func Trace() {
t := time.Now().Format("15:04:05.000000")
id := getGID()
id := GetGID()
pc := make([]uintptr, 10)
runtime.Callers(2, pc)
f := runtime.FuncForPC(pc[0])
Expand Down
22 changes: 15 additions & 7 deletions common/log/log.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package log

import (
"GoOnchain/common"
"GoOnchain/config"
"bytes"
"fmt"
"io"
Expand All @@ -27,11 +29,11 @@ const (

var (
levels = map[int]string{
debugLog: "DEBUG",
infoLog: "INFO",
warnLog: "WARN",
errorLog: "ERROR",
fatalLog: "FATAL",
debugLog: "DEBUG",
infoLog: "INFO",
warnLog: "WARN",
errorLog: "ERROR",
fatalLog: "FATAL",
}
)

Expand Down Expand Up @@ -84,7 +86,13 @@ func New(out io.Writer, prefix string, flag, level int) *Logger {
}

func (l *Logger) output(level int, s string) error {
return l.logger.Output(callDepth, AddBracket(LevelName(level))+" "+s)
if (level == 0) || (level == 3) {
gid := common.GetGID()
gidStr := strconv.FormatUint(gid, 10)
return l.logger.Output(callDepth, AddBracket(LevelName(level))+" "+"GID"+" "+gidStr+", "+s)
} else {
return l.logger.Output(callDepth, AddBracket(LevelName(level))+" "+s)
}
}

func (l *Logger) Output(level int, a ...interface{}) error {
Expand Down Expand Up @@ -175,7 +183,7 @@ func CreatePrintLog(path string) {
if err != nil {
fmt.Printf("%s\n", err.Error)
}
var printlevel int = PRINTLEVEL
var printlevel int = config.Parameters.PrintLevel
writers := []io.Writer{
logfile,
os.Stdout,
Expand Down
10 changes: 6 additions & 4 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ type ProtocolConfiguration struct {
CoinVersion int `json:"CoinVersion"`
StandbyMiners []string `json:"StandbyMiners"`
SeedList []string `json:"SeedList"`
HttpJsonPort int `json:"HttpJsonPort"`
NodePort int `json:"NodePort"`
WebSocketPort int `json:"WebSocketPort"`
MinerName string `json:"MinerName"`
HttpJsonPort int `json:"HttpJsonPort"`
HttpLocalPort int `json:"HttpLocalPort"`
NodePort int `json:"NodePort"`
WebSocketPort int `json:"WebSocketPort"`
MinerName string `json:"MinerName"`
PrintLevel int `json:"PrintLevel"`
}

type ProtocolFile struct {
Expand Down
4 changes: 3 additions & 1 deletion config/protocol.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
"192.168.33.11:20338"
],
"HttpJsonPort": 20337,
"HttpLocalPort": 20336,
"NodePort": 20338,
"MinerName": "c4"
"MinerName": "c4",
"PrintLevel": 0
}
}
10 changes: 6 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func main() {
fmt.Println("Can't get local client.")
os.Exit(1)
}

issuer, err := localclient.GetDefaultAccount()
if err != nil {
fmt.Println(err)
Expand All @@ -81,22 +82,23 @@ func main() {
ledger.DefaultLedger.Blockchain = &sampleBlockchain

time.Sleep(2 * time.Second)
neter := net.StartProtocol()


neter, noder := net.StartProtocol()
httpjsonrpc.RegistRpcNode(noder)
time.Sleep(20 * time.Second)

fmt.Println("//**************************************************************************")
fmt.Println("//*** 5. Start DBFT Services ***")
fmt.Println("//**************************************************************************")
dbftServices := dbft.NewDbftService(localclient, "logdbft", neter)
httpjsonrpc.RegistDbftService(dbftServices)
go dbftServices.Start()
time.Sleep(5 * time.Second)
fmt.Println("DBFT Services start completed.")
fmt.Println("//**************************************************************************")
fmt.Println("//*** Init Complete ***")
fmt.Println("//**************************************************************************")
go httpjsonrpc.StartServer()
go httpjsonrpc.StartRPCServer()
go httpjsonrpc.StartLocalServer()

time.Sleep(2 * time.Second)
// if config.Parameters.MinerName == "c4" {
Expand Down
28 changes: 28 additions & 0 deletions net/httpjsonrpc/RPCserver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package httpjsonrpc

import (
. "GoOnchain/common"
. "GoOnchain/config"
"log"
"net/http"
"strconv"
)

func StartRPCServer() {
Trace()
http.HandleFunc("/", Handle)

HandleFunc("getbestblockhash", getBestBlockHash)
HandleFunc("getblock", getBlock)
HandleFunc("getblockcount", getBlockCount)
HandleFunc("getblockhash", getBlockHash)
HandleFunc("getconnectioncount", getConnectionCount)
HandleFunc("getrawmempool", getRawMemPool)
HandleFunc("getrawtransaction", getRawTransaction)
HandleFunc("submitblock", submitBlock)

err := http.ListenAndServe(":"+strconv.Itoa(Parameters.HttpJsonPort), nil)
if err != nil {
log.Fatal("ListenAndServe: ", err.Error())
}
}
64 changes: 0 additions & 64 deletions net/httpjsonrpc/client.go

This file was deleted.

Loading

0 comments on commit 1f4e0b7

Please sign in to comment.