Sfoglia il codice sorgente

修改定时器的策略,修改DCR打币接口,刷新列出账户的缓存

476052856 6 anni fa
parent
commit
a2d196524e
4 ha cambiato i file con 109 aggiunte e 80 eliminazioni
  1. BIN
      coinsapi
  2. BIN
      coinsapi.exe
  3. 24 13
      src/bitcoin/bitcoinmain.go
  4. 85 67
      src/dcrcash/dcrcashcoin.go

BIN
coinsapi


BIN
coinsapi.exe


+ 24 - 13
src/bitcoin/bitcoinmain.go

@@ -5,6 +5,7 @@ import (
 	//"errors"
 	"fmt"
 	"strconv"
+	"sync"
 	. "template"
 	"time"
 
@@ -14,9 +15,10 @@ import (
 )
 
 var (
-	Config    config
-	CoinApi   BitCoinApi
-	m_unspent []map[string]interface{}
+	Config       config
+	CoinApi      BitCoinApi
+	m_unspent    []map[string]interface{}
+	m_mutUnspent sync.Mutex
 )
 
 type BitCoin struct {
@@ -42,7 +44,7 @@ func (m BitCoin) InitDriver(parm interface{}) bool {
 	}
 	go m.getwalletinfo()
 	//缓存当前未花费的
-	go listunspentpool()
+	go AsyncRoutine()
 	return true
 }
 
@@ -477,18 +479,27 @@ func (m BitCoin) SumcoinBalance(parm interface{}) ([]byte, error) {
 	return nil, nil
 }
 
-func listunspentpool() {
+func AsyncRoutine() {
+	CachelistunspentTicker := time.NewTicker(120 * time.Second)
+	for {
+		select {
+		case _ = <-CachelistunspentTicker.C:
+			Cachelistunspent()
+		}
+	}
+}
+func Cachelistunspent() {
+	fmt.Println("cachelistunspent time:", time.Now().Format("2006-01-02 15:04:05"))
 	var address []string
 	m_unspent = make([]map[string]interface{}, 0)
-	for range time.Tick(2 * time.Minute) {
-		unspent, err := CoinApi.Listunspent(address)
-		if err != nil {
-			LOG("ERROR", "  get listunspent:%v", err.Error())
-			//return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, LISTUNSPENT_ERR, getErrMsg(LISTUNSPENT_ERR, nil))
-		}
-
-		m_unspent = unspent
+	unspent, err := CoinApi.Listunspent(address)
+	if err != nil {
+		LOG("ERROR", "  get listunspent:%v", err.Error())
+		//return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, LISTUNSPENT_ERR, getErrMsg(LISTUNSPENT_ERR, nil))
 	}
+	m_mutUnspent.Lock()
+	m_unspent = unspent
+	m_mutUnspent.Unlock()
 
 }
 

+ 85 - 67
src/dcrcash/dcrcashcoin.go

@@ -13,13 +13,15 @@ import (
 )
 
 var (
-	Config         config
-	CoinApi        DcrCashApi
-	WalleTxfee     float64
-	m_balanceparam map[string]float64
-	mut            sync.Mutex
-	m_unspent      []map[string]interface{}
-	m_AccValues    map[string]float64
+	Config           config
+	CoinApi          DcrCashApi
+	WalleTxfee       float64
+	m_balanceparam   map[string]float64
+	m_mutblance      sync.Mutex
+	m_unspent        []map[string]interface{}
+	m_mutspent       sync.Mutex
+	m_AccValues      map[string]float64
+	m_mutaccoutValue sync.Mutex
 )
 
 type DcrCash struct {
@@ -38,10 +40,8 @@ func (m DcrCash) InitDriver(param interface{}) bool {
 		return false
 	}
 	go m.getwalletinfo()
-	//列出当期钱包的所有账户
-	go listaccounts()
-	//列出所有账户的balance,和listunspent
-	go SaveGetbalance()
+	//缓存数据
+	go AsyncRoutine()
 	return true
 }
 
@@ -279,7 +279,9 @@ func (m DcrCash) TransferAccounts(parm interface{}) ([]byte, error) {
 		}
 		return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, SENDCOINS_ERR, getErrMsg(SENDCOINS_ERR, nil))
 	}
-
+	go func() {
+		Cachelistaccount()
+	}()
 	if len(transactionid) != 0 {
 		var jbuf []byte
 		resp := make(map[string]interface{})
@@ -469,71 +471,87 @@ func (m DcrCash) ChainHeight(parm interface{}) ([]byte, error) {
 	return []byte(sendstr), nil
 }
 
-func SaveGetbalance() {
-	var address []string
-	m_unspent = make([]map[string]interface{}, 0)
+func CacheGetbalance() {
+	fmt.Println("Cachegetbalance time:", time.Now().Format("2006-01-02 15:04:05"))
 	m_balanceparam = make(map[string]float64)
-	for range time.Tick(2 * time.Minute) {
-		fmt.Println("2 mine times", time.Now().Format("2006-01-02 15:04:05"))
-		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))
+	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
 		}
-		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))
+		if vbalance.Spendable > maxConfirbalance {
+			maxConfirbalance = vbalance.Spendable
 		}
-		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
-			}
-
+		if vbalance.Unconfirmed > maxUnconfirbalance {
+			maxUnconfirbalance = vbalance.Unconfirmed
 		}
-		mut.Lock()
-		m_balanceparam["totalbalance"] = totalbalance
-		m_balanceparam["confirmedbalance"] = maxConfirbalance
-		m_balanceparam["unconfirmed"] = maxUnconfirbalance
-		m_balanceparam["ntime"] = float64(time.Now().Unix())
-		mut.Unlock()
 
-		unspent, err := CoinApi.Listunspent(address)
-		if err != nil {
-			LOG("ERROR", "  get listunspent:%v", err.Error())
-			//return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, LISTUNSPENT_ERR, getErrMsg(LISTUNSPENT_ERR, nil))
-		}
+	}
+	m_mutblance.Lock()
+	m_balanceparam["totalbalance"] = totalbalance
+	m_balanceparam["confirmedbalance"] = maxConfirbalance
+	m_balanceparam["unconfirmed"] = maxUnconfirbalance
+	m_balanceparam["ntime"] = float64(time.Now().Unix())
+	m_mutblance.Unlock()
+}
 
-		m_unspent = unspent
+func Cachelistunspent() {
+	fmt.Println("cachelistunspent time:", time.Now().Format("2006-01-02 15:04:05"))
+	var address []string
+	m_unspent = make([]map[string]interface{}, 0)
+	unspent, err := CoinApi.Listunspent(address)
+	if err != nil {
+		LOG("ERROR", "  get listunspent:%v", err.Error())
+		//return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, LISTUNSPENT_ERR, getErrMsg(LISTUNSPENT_ERR, nil))
 	}
+	m_mutspent.Lock()
+	m_unspent = unspent
+	m_mutspent.Unlock()
 }
 
-func listaccounts() {
-	for range time.Tick(90 * time.Second) {
-		fmt.Println("listaccounts after 90s.....", time.Now().Format("2006-01-02 15:04:05"))
-		//list Accounts and account of balance
-		accountsofValues, err := CoinApi.ListAccounts()
-		if err != nil {
-			LOG("ERROR", "List accounts error:%v", err.Error())
-			//return nil, err
-		}
-		//
-		mut.Lock()
-		if err = json.Unmarshal(accountsofValues, &m_AccValues); err != nil {
-			LOG("ERROR", "Unmarshal error:%v", err.Error())
-			//return nil, err
+func Cachelistaccount() {
+	fmt.Println("Cachelistaccounts time", time.Now().Format("2006-01-02 15:04:05"))
+	//list Accounts and account of balance
+	accountsofValues, err := CoinApi.ListAccounts()
+	if err != nil {
+		LOG("ERROR", "List accounts error:%v", err.Error())
+		//return nil, err
+	}
+	//
+	m_mutaccoutValue.Lock()
+	if err = json.Unmarshal(accountsofValues, &m_AccValues); err != nil {
+		LOG("ERROR", "Unmarshal error:%v", err.Error())
+		//return nil, err
+	}
+	m_mutaccoutValue.Unlock()
+}
+
+func AsyncRoutine() {
+	CacheGetbalanceTicker := time.NewTicker(120 * time.Second)
+	CachelistaccountTicker := time.NewTicker(90 * time.Second)
+	CachelistunspentTicker := time.NewTicker(120 * time.Second)
+	for {
+		select {
+		case _ = <-CacheGetbalanceTicker.C:
+			CacheGetbalance()
+		case _ = <-CachelistaccountTicker.C:
+			Cachelistaccount()
+		case _ = <-CachelistunspentTicker.C:
+			Cachelistunspent()
 		}
-		mut.Unlock()
 	}
 }