OSDN Git Service

Miner - proof of work (#311)
authorGuanghua Guo <1536310027@qq.com>
Wed, 24 Jan 2018 01:37:10 +0000 (09:37 +0800)
committerGitHub <noreply@github.com>
Wed, 24 Jan 2018 01:37:10 +0000 (09:37 +0800)
* Add cmd/miner

* Add marshal function

* WorkResp mashal and unmashal ok

* miner getwork and do work ok

* Delete dead rpc code

* Delete WorkResp Unmarshal and Marshal function

blockchain/miner.go
blockchain/rpc/info.go [deleted file]
blockchain/rpc/net.go
blockchain/rpc_reactor.go
cmd/miner/main.go [new file with mode: 0644]
util/util.go

index 959041c..92c000c 100644 (file)
@@ -6,23 +6,24 @@ import (
 )
 
 // Get the parameters of mining
-func (bcr *BlockchainReactor) getWork() Response {
-       var resp workResp
+func (bcr *BlockchainReactor) getWork() *WorkResp {
+       var resp WorkResp
        if block, err := mining.NewBlockTemplate(bcr.chain, bcr.txPool, bcr.accounts); err != nil {
-               return NewErrorResponse(err)
+               return nil
        } else {
                resp.Header = block.BlockHeader
        }
        seedCaches := bcr.chain.SeedCaches()
        if seedCache, err := seedCaches.Get(&resp.Header.Seed); err != nil {
-               return NewErrorResponse(err)
+               return nil
        } else {
-               resp.cache = seedCache
+               resp.Cache = seedCache
        }
-       return NewSuccessResponse(resp)
+
+       return &resp
 }
 
-type workResp struct {
-       Header legacy.BlockHeader `json:"header"`
-       cache  []uint32       `json:"cache"`
+type WorkResp struct {
+       Header legacy.BlockHeader
+       Cache  []uint32
 }
diff --git a/blockchain/rpc/info.go b/blockchain/rpc/info.go
deleted file mode 100644 (file)
index afc46a6..0000000
+++ /dev/null
@@ -1,11 +0,0 @@
-package rpc
-
-/*import (
-       "github.com/bytom/blockchain/txdb"
-
-       ctypes "github.com/bytom/blockchain/rpc/types"
-)
-
-func BlockHeight(blockStore *txdb.Store) (*ctypes.ResultBlockchainInfo, error) {
-       return &ctypes.ResultBlockchainInfo{LastHeight: blockStore.Height()}, nil
-}*/
index 4891805..dbb6a88 100644 (file)
@@ -27,20 +27,3 @@ func NetInfo(p2pSwitch *p2p.Switch) (*ctypes.ResultNetInfo, error) {
                Peers:     peers,
        }, nil
 }
-
-//-----------------------------------------------------------------------------
-
-// Dial given list of seeds
-/*func UnsafeDialSeeds(seeds []string) (*ctypes.ResultDialSeeds, error) {
-
-       if len(seeds) == 0 {
-               return &ctypes.ResultDialSeeds{}, fmt.Errorf("No seeds provided")
-       }
-       // starts go routines to dial each seed after random delays
-       logger.Info("DialSeeds", "addrBook", addrBook, "seeds", seeds)
-       err := p2pSwitch.DialSeeds(addrBook, seeds)
-       if err != nil {
-               return &ctypes.ResultDialSeeds{}, err
-       }
-       return &ctypes.ResultDialSeeds{"Dialing seeds in progress. See /net_info for details"}, nil
-}*/
index 5b0439e..542b002 100644 (file)
@@ -110,6 +110,7 @@ func (bcr *BlockchainReactor) BuildHandler() {
 
        m.Handle("/is-mining", jsonHandler(bcr.isMining))
        m.Handle("/gas-rate", jsonHandler(bcr.gasRate))
+       m.Handle("/get-work", jsonHandler(bcr.getWork))
 
        latencyHandler := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
                if l := latency(m, req); l != nil {
diff --git a/cmd/miner/main.go b/cmd/miner/main.go
new file mode 100644 (file)
index 0000000..8839592
--- /dev/null
@@ -0,0 +1,45 @@
+package main
+
+import (
+       "context"
+       "fmt"
+
+       "github.com/bytom/blockchain"
+       "github.com/bytom/consensus/algorithm"
+       "github.com/bytom/consensus/difficulty"
+       "github.com/bytom/util"
+)
+
+const (
+       maxNonce = ^uint64(0) // 2^32 - 1
+)
+
+// do proof of work
+func doWork(work *blockchain.WorkResp) {
+       fmt.Printf("work:%v\n", work)
+       for i := uint64(0); i <= maxNonce; i++ {
+               work.Header.Nonce = i
+               headerHash := work.Header.Hash()
+               proofHash, err := algorithm.AIHash(work.Header.Height, &headerHash, work.Cache)
+               if err != nil {
+                       fmt.Printf("Mining: failed on AIHash: %v\n", err)
+                       return
+               }
+
+               if difficulty.CheckProofOfWork(proofHash, work.Header.Bits) {
+                       // to do: submitWork
+                       fmt.Printf("Mining: successful-----proof hash:%v\n", proofHash)
+                       return
+               }
+       }
+}
+
+func main() {
+       var work blockchain.WorkResp
+       client := util.MustRPCClient()
+       if err := client.Call(context.Background(), "/get-work", nil, &work); err == nil {
+               doWork(&work)
+       } else {
+               fmt.Printf("---err:%v\n", err)
+       }
+}
index ec9c499..b15bcae 100644 (file)
@@ -34,7 +34,7 @@ var (
 )
 
 // Wraper rpc's client
-func mustRPCClient() *rpc.Client {
+func MustRPCClient() *rpc.Client {
        // TODO(kr): refactor some of this cert-loading logic into bytom/blockchain
        // and use it from cored as well.
        // Note that this function, unlike maybeUseTLS in cored,
@@ -83,7 +83,7 @@ func ClientCall(path string, req ...interface{}) (interface{}, int) {
                request = req[0]
        }
 
-       client := mustRPCClient()
+       client := MustRPCClient()
        client.Call(context.Background(), path, request, response)
 
        switch response.Status {