Jelajahi Sumber

增加dcr——balance的获取数据慢的,进行2分钟同步一次,兼容以前的

476052856 6 tahun lalu
induk
melakukan
4ff9d61903
5 mengubah file dengan 88 tambahan dan 43 penghapusan
  1. TEMPAT SAMPAH
      coinsapi
  2. 6 6
      src/dcrcash/dcrcash.go
  3. 3 3
      src/dcrcash/dcrcash_test.go
  4. 78 33
      src/dcrcash/dcrcashcoin.go
  5. 1 1
      src/dcrcash/type.go

TEMPAT SAMPAH
coinsapi


+ 6 - 6
src/dcrcash/dcrcash.go

@@ -156,7 +156,7 @@ func (m DcrCashApi) WalleGetBlockCount() (uint64, error) {
 //get the server's total available balance
 func (m DcrCashApi) WalleGetBalance(account string) ([]byte, error) {
 
-	var liteBalance BalanceResult
+	var acountresult AcountResult
 
 	// getbalance all
 	result, err := dcrcashRpc.RunCall("getbalance", account)
@@ -165,20 +165,20 @@ func (m DcrCashApi) WalleGetBalance(account string) ([]byte, error) {
 		return nil, err
 	}
 
-	if err := json.Unmarshal([]byte(result), &liteBalance); err != nil {
+	if err := json.Unmarshal([]byte(result), &acountresult); err != nil {
 		fmt.Println("Unmarshal err :", err.Error())
 		return nil, err
 	}
-	fmt.Println("WalleGetBalance:", len(liteBalance.Result.Balances))
+	fmt.Println("WalleGetBalance:", len(acountresult.Result.Balances))
 	// success
-	if len(liteBalance.Error.Message) == 0 {
+	if len(acountresult.Error.Message) == 0 {
 		var jbuf []byte
-		if jbuf, err = json.Marshal(liteBalance.Result.Balances); err != nil {
+		if jbuf, err = json.Marshal(acountresult.Result.Balances); err != nil {
 			return nil, err
 		}
 		return jbuf, nil
 	}
-	err = errors.New(liteBalance.Error.Message)
+	err = errors.New(acountresult.Error.Message)
 	return nil, err
 }
 

+ 3 - 3
src/dcrcash/dcrcash_test.go

@@ -46,13 +46,13 @@ import (
 
 func Test_Balance(t *testing.T) {
 	client := http.DefaultClient
-	geturl := fmt.Sprintf(`http://118.31.213.53:8803/coinproxy/coinbalance`)
-	//geturl := fmt.Sprintf(`http://localhost:15741/coinproxy/coinbalance`)
+	//geturl := fmt.Sprintf(`http://118.31.213.53:8803/coinproxy/coinbalance`)
+	geturl := fmt.Sprintf(`http://localhost:15741/coinproxy/coinbalance`)
 	timestamp := fmt.Sprintf("%v", time.Now().Unix())
 	t.Logf("post url:%v", geturl)
 
 	//var b []byte
-	postdata := fmt.Sprintf(`{"cointype":"DCR","timestamp":"%v"}`, timestamp)
+	postdata := fmt.Sprintf(`{"cointype":"DCR","Sync":false,"timestamp":"%v"}`, timestamp)
 	request, err := http.NewRequest("POST", geturl, strings.NewReader(postdata))
 	if err != nil {
 		t.Errorf("err:%s", err.Error())

+ 78 - 33
src/dcrcash/dcrcashcoin.go

@@ -4,6 +4,7 @@ import (
 	"encoding/json"
 	"fmt"
 	"strconv"
+	"sync"
 	. "template"
 	"time"
 
@@ -12,9 +13,11 @@ import (
 )
 
 var (
-	Config     config
-	CoinApi    DcrCashApi
-	WalleTxfee float64
+	Config       config
+	CoinApi      DcrCashApi
+	WalleTxfee   float64
+	balanceparam map[string]float64
+	mut          sync.Mutex
 )
 
 type DcrCash struct {
@@ -58,7 +61,6 @@ func (m DcrCash) initconfig() bool {
 		LOG("ERROR", "init json error:%v", err.Error())
 		return false
 	}
-
 	fee := float64(f)
 	CoinApi.WalletSetTxFee(fee) //
 	return true
@@ -86,40 +88,43 @@ func (m DcrCash) getwalletinfo() bool {
 }
 
 func (m DcrCash) CoinBalance(parm interface{}) ([]byte, error) {
-	confirmedBalance, err := CoinApi.WalleGetBalance("*")
-	if err != nil {
-		LOG("ERROR", " GetBalance err:%v", err.Error())
-		return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, GETBALANCE_ERR, getErrMsg(GETBALANCE_ERR, nil))
-	}
-	var balance []account
-	if err = json.Unmarshal(confirmedBalance, &balance); err != nil {
-		LOG("ERROR", " inner json error:%v", err.Error())
-		return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, INNER_ERR, getErrMsg(INNER_ERR, nil))
-	}
-	var totalbalance float64
-	//var totalUnconfirbalance float64
-	var maxConfirbalance float64
-	var maxUnconfirbalance float64
-	for _, vbalance := range balance {
-		/*if vbalance.Spendable != 0 {
-			totalConfirbalance += vbalance.Spendable
-		}
-		if vbalance.Unconfirmed != 0 {
-			totalUnconfirbalance += vbalance.Unconfirmed
-		}*/
-		if vbalance.Total != 0 {
-			totalbalance += vbalance.Total
-		}
-		if vbalance.Spendable > maxConfirbalance {
-			maxConfirbalance = vbalance.Spendable
+	var totalbalance, maxConfirbalance, maxUnconfirbalance float64
+	pjs := parm.(*simplejson.Json)
+	input, err := pjs.Get("Sync").Bool()
+	if !input {
+		fmt.Println("the balance is not sync!!")
+		totalbalance = balanceparam["totalbalance"]
+		maxConfirbalance = balanceparam["confirmedbalance"]
+		maxUnconfirbalance = balanceparam["unconfirmed"]
+	} else {
+		acountBalace, err := CoinApi.WalleGetBalance("*")
+		if err != nil {
+			LOG("ERROR", " GetBalance err:%v", err.Error())
+			return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, GETBALANCE_ERR, getErrMsg(GETBALANCE_ERR, nil))
 		}
-		if vbalance.Unconfirmed > maxUnconfirbalance {
-			maxUnconfirbalance = vbalance.Unconfirmed
+		var accounts []account
+		if err = json.Unmarshal(acountBalace, &accounts); err != nil {
+			LOG("ERROR", " inner json error:%v", err.Error())
+			return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, INNER_ERR, getErrMsg(INNER_ERR, nil))
 		}
 
+		for _, vbalance := range accounts {
+			if vbalance.Total != 0 {
+				totalbalance += vbalance.Total
+			}
+			if vbalance.Spendable > maxConfirbalance {
+				maxConfirbalance = vbalance.Spendable
+			}
+			if vbalance.Unconfirmed > maxUnconfirbalance {
+				maxUnconfirbalance = vbalance.Unconfirmed
+			}
+
+		}
 	}
+
 	var jbuf []byte
 	resp := make(map[string]interface{})
+
 	resp["confirmedbalance"] = maxConfirbalance
 	resp["unconfirmed"] = maxUnconfirbalance
 	resp["totalbalance"] = totalbalance
@@ -462,7 +467,47 @@ func (m DcrCash) ChainHeight(parm interface{}) ([]byte, error) {
 	return []byte(sendstr), nil
 }
 
-func init() {
+func SaveGetbalance() {
+	balanceparam = make(map[string]float64)
+	for range time.Tick(2 * time.Minute) {
+		//DcrCashApi.WalleGetBalance()
+		acountBalace, err := CoinApi.WalleGetBalance("*")
+		if err != nil {
+			LOG("ERROR", " GetBalance err:%v", err.Error())
+			//return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, GETBALANCE_ERR, getErrMsg(GETBALANCE_ERR, nil))
+		}
+		var accounts []account
+		if err = json.Unmarshal(acountBalace, &accounts); err != nil {
+			LOG("ERROR", " inner json error:%v", err.Error())
+			//return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, INNER_ERR, getErrMsg(INNER_ERR, nil))
+		}
+		var totalbalance float64
+		//var totalUnconfirbalance float64
+		var maxConfirbalance float64
+		var maxUnconfirbalance float64
+		for _, vbalance := range accounts {
+			if vbalance.Total != 0 {
+				totalbalance += vbalance.Total
+			}
+			if vbalance.Spendable > maxConfirbalance {
+				maxConfirbalance = vbalance.Spendable
+			}
+			if vbalance.Unconfirmed > maxUnconfirbalance {
+				maxUnconfirbalance = vbalance.Unconfirmed
+			}
+
+		}
+		mut.Lock()
+		balanceparam["totalbalance"] = totalbalance
+		balanceparam["confirmedbalance"] = maxConfirbalance
+		balanceparam["unconfirmed"] = maxUnconfirbalance
+		mut.Unlock()
+		fmt.Println("2 mine times", time.Now().Format("2006-01-02 15:04:05"))
+	}
+}
 
+func init() {
 	Register(NameCoin, &DcrCash{})
+	go SaveGetbalance()
+
 }

+ 1 - 1
src/dcrcash/type.go

@@ -62,7 +62,7 @@ type Accounts struct {
 }
 
 //get Balance result struct
-type BalanceResult struct {
+type AcountResult struct {
 	Result Accounts `json:"result"`
 	Error  RpcError `json:"error"`
 	Id     int      `json:"id"`