)
// 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
}
+++ /dev/null
-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
-}*/
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
-}*/
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 {
--- /dev/null
+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)
+ }
+}
)
// 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,
request = req[0]
}
- client := mustRPCClient()
+ client := MustRPCClient()
client.Call(context.Background(), path, request, response)
switch response.Status {