OSDN Git Service

Add is-mining and gas-rate (#124)
authorLiu-Cheng Xu <xuliuchengxlc@gmail.com>
Wed, 22 Nov 2017 10:40:27 +0000 (18:40 +0800)
committerPaladz <yzhu101@uottawa.ca>
Wed, 22 Nov 2017 10:40:27 +0000 (18:40 +0800)
* Add is-mining and gas-rate

* Change gasRate to public

blockchain/reactor.go
cmd/cobra/commands/bytomcli.go
cmd/cobra/commands/mining.go [new file with mode: 0644]
cmd/cobra/commands/transaction.go [new file with mode: 0644]
protocol/validation/validation.go

index a223c5a..0504bbb 100755 (executable)
@@ -27,6 +27,7 @@ import (
        "github.com/bytom/protocol"
        "github.com/bytom/protocol/bc"
        "github.com/bytom/protocol/bc/legacy"
+       "github.com/bytom/protocol/validation"
        "github.com/bytom/types"
 )
 
@@ -174,6 +175,8 @@ func (bcr *BlockchainReactor) BuildHander() {
        m.Handle("/get-block-by-height", jsonHandler(bcr.getBlockByHeight))
        m.Handle("/get-block-transactions-count-by-height", jsonHandler(bcr.getBlockTransactionsCountByHeight))
        m.Handle("/block-height", jsonHandler(bcr.getBlockHeight))
+       m.Handle("/is-mining", jsonHandler(bcr.isMining))
+       m.Handle("/gas-rate", jsonHandler(bcr.gasRate))
 
        latencyHandler := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
                if l := latency(m, req); l != nil {
@@ -526,3 +529,11 @@ func (bcr *BlockchainReactor) getBlockTransactionsCountByHeight(height uint64) (
 func (bcr *BlockchainReactor) getBlockHeight() uint64 {
        return bcr.chain.Height()
 }
+
+func (bcr *BlockchainReactor) isMining() bool {
+       return bcr.mining.IsMining()
+}
+
+func (bcr *BlockchainReactor) gasRate() int64 {
+       return validation.GasRate
+}
index a0d8e16..4f613a8 100644 (file)
@@ -105,11 +105,15 @@ func AddCommands() {
        BytomcliCmd.AddCommand(deleteKeyCmd)
        BytomcliCmd.AddCommand(listKeysCmd)
 
+       BytomcliCmd.AddCommand(isMiningCmd)
+
        BytomcliCmd.AddCommand(netInfoCmd)
        BytomcliCmd.AddCommand(netListeningCmd)
        BytomcliCmd.AddCommand(peerCountCmd)
        BytomcliCmd.AddCommand(netSyncingCmd)
 
+       BytomcliCmd.AddCommand(gasRateCmd)
+
        BytomcliCmd.AddCommand(createAccessTokenCmd)
        BytomcliCmd.AddCommand(listAccessTokenCmd)
        BytomcliCmd.AddCommand(deleteAccessTokenCmd)
diff --git a/cmd/cobra/commands/mining.go b/cmd/cobra/commands/mining.go
new file mode 100644 (file)
index 0000000..289d4aa
--- /dev/null
@@ -0,0 +1,19 @@
+package commands
+
+import (
+       "context"
+
+       "github.com/spf13/cobra"
+       jww "github.com/spf13/jwalterweatherman"
+)
+
+var isMiningCmd = &cobra.Command{
+       Use:   "is-mining",
+       Short: "If client is actively mining new blocks",
+       Run: func(cmd *cobra.Command, args []string) {
+               var response interface{}
+               client := mustRPCClient()
+               client.Call(context.Background(), "/is-mining", nil, &response)
+               jww.FEEDBACK.Printf("is mining: %v\n", response)
+       },
+}
diff --git a/cmd/cobra/commands/transaction.go b/cmd/cobra/commands/transaction.go
new file mode 100644 (file)
index 0000000..e8223e8
--- /dev/null
@@ -0,0 +1,19 @@
+package commands
+
+import (
+       "context"
+
+       "github.com/spf13/cobra"
+       jww "github.com/spf13/jwalterweatherman"
+)
+
+var gasRateCmd = &cobra.Command{
+       Use:   "gas-rate",
+       Short: "Print the current gas rate",
+       Run: func(cmd *cobra.Command, args []string) {
+               var response interface{}
+               client := mustRPCClient()
+               client.Call(context.Background(), "/gas-rate", nil, &response)
+               jww.FEEDBACK.Printf("gas rate: %v\n", response)
+       },
+}
index c48d256..e0c854b 100644 (file)
@@ -15,7 +15,8 @@ import (
 const (
        defaultGasLimit = int64(80000)
        muxGasCost      = int64(10)
-       gasRate         = int64(1000)
+       // GasRate indicates the current gas rate
+       GasRate = int64(1000)
 )
 
 type gasState struct {
@@ -30,7 +31,7 @@ func (g *gasState) setGas(BTMValue int64) error {
        }
        g.BTMValue = BTMValue
 
-       if gasAmount, ok := checked.DivInt64(BTMValue, gasRate); ok {
+       if gasAmount, ok := checked.DivInt64(BTMValue, GasRate); ok {
                if gasAmount == 0 {
                        g.gasLeft = muxGasCost
                } else if gasAmount < defaultGasLimit {