github.com/turingchain2020/turingchain@v1.1.21/wallet/walletbizpolicy.go (about)

     1  // Copyright Turing Corp. 2018 All Rights Reserved.
     2  // Use of policy source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package wallet
     6  
     7  import (
     8  	"sync"
     9  
    10  	"github.com/turingchain2020/turingchain/common/crypto"
    11  	"github.com/turingchain2020/turingchain/common/db"
    12  	"github.com/turingchain2020/turingchain/types"
    13  	wcom "github.com/turingchain2020/turingchain/wallet/common"
    14  )
    15  
    16  const (
    17  	walletBizPolicyX = "walletBizPolicy"
    18  )
    19  
    20  func init() {
    21  	wcom.RegisterPolicy(walletBizPolicyX, newPolicy())
    22  }
    23  
    24  func newPolicy() wcom.WalletBizPolicy {
    25  	ret := &walletBizPlicy{
    26  		mtx: &sync.Mutex{},
    27  	}
    28  	return ret
    29  }
    30  
    31  // 默认的钱包业务实现
    32  type walletBizPlicy struct {
    33  	mtx           *sync.Mutex
    34  	walletOperate wcom.WalletOperate
    35  }
    36  
    37  func (policy *walletBizPlicy) Init(walletBiz wcom.WalletOperate, sub []byte) {
    38  	policy.setWalletOperate(walletBiz)
    39  }
    40  
    41  func (policy *walletBizPlicy) setWalletOperate(walletBiz wcom.WalletOperate) {
    42  	policy.mtx.Lock()
    43  	defer policy.mtx.Unlock()
    44  	policy.walletOperate = walletBiz
    45  }
    46  
    47  func (policy *walletBizPlicy) getWalletOperate() wcom.WalletOperate {
    48  	policy.mtx.Lock()
    49  	defer policy.mtx.Unlock()
    50  	return policy.walletOperate
    51  }
    52  
    53  func (policy *walletBizPlicy) OnAddBlockTx(block *types.BlockDetail, tx *types.Transaction, index int32, dbbatch db.Batch) *types.WalletTxDetail {
    54  	return nil
    55  }
    56  
    57  func (policy *walletBizPlicy) OnDeleteBlockTx(block *types.BlockDetail, tx *types.Transaction, index int32, dbbatch db.Batch) *types.WalletTxDetail {
    58  	return nil
    59  }
    60  
    61  func (policy *walletBizPlicy) SignTransaction(key crypto.PrivKey, req *types.ReqSignRawTx) (needSysSign bool, signtx string, err error) {
    62  	needSysSign = true
    63  	return
    64  }
    65  
    66  func (policy *walletBizPlicy) OnCreateNewAccount(acc *types.Account) {
    67  	wg := policy.getWalletOperate().GetWaitGroup()
    68  	wg.Add(1)
    69  	go policy.rescanReqTxDetailByAddr(acc.Addr, wg)
    70  }
    71  
    72  func (policy *walletBizPlicy) OnImportPrivateKey(acc *types.Account) {
    73  	wg := policy.getWalletOperate().GetWaitGroup()
    74  	wg.Add(1)
    75  	go policy.rescanReqTxDetailByAddr(acc.Addr, wg)
    76  }
    77  
    78  func (policy *walletBizPlicy) OnWalletLocked() {
    79  }
    80  
    81  func (policy *walletBizPlicy) OnWalletUnlocked(WalletUnLock *types.WalletUnLock) {
    82  }
    83  
    84  func (policy *walletBizPlicy) OnAddBlockFinish(block *types.BlockDetail) {
    85  }
    86  
    87  func (policy *walletBizPlicy) OnDeleteBlockFinish(block *types.BlockDetail) {
    88  }
    89  
    90  func (policy *walletBizPlicy) OnClose() {
    91  }
    92  
    93  func (policy *walletBizPlicy) OnSetQueueClient() {
    94  }
    95  
    96  func (policy *walletBizPlicy) Call(funName string, in types.Message) (ret types.Message, err error) {
    97  	err = types.ErrNotSupport
    98  	return
    99  }
   100  
   101  //从blockchain模块同步addr参与的所有交易详细信息
   102  func (policy *walletBizPlicy) rescanReqTxDetailByAddr(addr string, wg *sync.WaitGroup) {
   103  	defer wg.Done()
   104  	policy.reqTxDetailByAddr(addr)
   105  }
   106  
   107  //从blockchain模块同步addr参与的所有交易详细信息
   108  func (policy *walletBizPlicy) reqTxDetailByAddr(addr string) {
   109  	if len(addr) == 0 {
   110  		walletlog.Error("reqTxDetailByAddr input addr is nil!")
   111  		return
   112  	}
   113  	var txInfo types.ReplyTxInfo
   114  
   115  	i := 0
   116  	operater := policy.getWalletOperate()
   117  	for {
   118  		//首先从blockchain模块获取地址对应的所有交易hashs列表,从最新的交易开始获取
   119  		var ReqAddr types.ReqAddr
   120  		ReqAddr.Addr = addr
   121  		ReqAddr.Flag = 0
   122  		ReqAddr.Direction = 0
   123  		ReqAddr.Count = int32(MaxTxHashsPerTime)
   124  		if i == 0 {
   125  			ReqAddr.Height = -1
   126  			ReqAddr.Index = 0
   127  		} else {
   128  			ReqAddr.Height = txInfo.GetHeight()
   129  			ReqAddr.Index = txInfo.GetIndex()
   130  		}
   131  		i++
   132  		ReplyTxInfos, err := operater.GetAPI().GetTransactionByAddr(&ReqAddr)
   133  		if err != nil {
   134  			walletlog.Error("reqTxDetailByAddr", "GetTransactionByAddr error", err, "addr", addr)
   135  			return
   136  		}
   137  		if ReplyTxInfos == nil {
   138  			walletlog.Info("reqTxDetailByAddr ReplyTxInfos is nil")
   139  			return
   140  		}
   141  		txcount := len(ReplyTxInfos.TxInfos)
   142  
   143  		var ReqHashes types.ReqHashes
   144  		ReqHashes.Hashes = make([][]byte, len(ReplyTxInfos.TxInfos))
   145  		for index, ReplyTxInfo := range ReplyTxInfos.TxInfos {
   146  			ReqHashes.Hashes[index] = ReplyTxInfo.GetHash()
   147  			txInfo.Hash = ReplyTxInfo.GetHash()
   148  			txInfo.Height = ReplyTxInfo.GetHeight()
   149  			txInfo.Index = ReplyTxInfo.GetIndex()
   150  		}
   151  		operater.GetTxDetailByHashs(&ReqHashes)
   152  		if txcount < int(MaxTxHashsPerTime) {
   153  			return
   154  		}
   155  	}
   156  }