Commit 14b1bbc3 authored by mdj33's avatar mdj33 Committed by vipwzw

add para chain wallet mining process

parent 7c3f8b34
...@@ -156,6 +156,7 @@ func New(cfg *types.Consensus, sub []byte) queue.Module { ...@@ -156,6 +156,7 @@ func New(cfg *types.Consensus, sub []byte) queue.Module {
commitMsgNotify: make(chan int64, 1), commitMsgNotify: make(chan int64, 1),
delMsgNotify: make(chan int64, 1), delMsgNotify: make(chan int64, 1),
mainBlockAdd: make(chan *types.BlockDetail, 1), mainBlockAdd: make(chan *types.BlockDetail, 1),
minerSwitch: make(chan bool, 1),
quit: make(chan struct{}), quit: make(chan struct{}),
} }
c.SetChild(para) c.SetChild(para)
...@@ -818,7 +819,7 @@ func checkMinerTx(current *types.BlockDetail) error { ...@@ -818,7 +819,7 @@ func checkMinerTx(current *types.BlockDetail) error {
func (client *client) Query_CreateNewAccount(acc *types.Account) (types.Message, error) { func (client *client) Query_CreateNewAccount(acc *types.Account) (types.Message, error) {
plog.Info("Query_CreateNewAccount", "acc", acc) plog.Info("Query_CreateNewAccount", "acc", acc)
// 需要para共识这边处理新创建的账户是否是超级节点发送commit共识交易的账户 // 需要para共识这边处理新创建的账户是否是超级节点发送commit共识交易的账户
// 需要实现具体处理 to be。。。。 client.commitMsgClient.onWalletAccount(acc)
return &types.Reply{IsOk: true, Msg: []byte("OK")}, nil return &types.Reply{IsOk: true, Msg: []byte("OK")}, nil
} }
...@@ -826,6 +827,6 @@ func (client *client) Query_CreateNewAccount(acc *types.Account) (types.Message, ...@@ -826,6 +827,6 @@ func (client *client) Query_CreateNewAccount(acc *types.Account) (types.Message,
func (client *client) Query_WalletStatus(walletStatus *types.WalletStatus) (types.Message, error) { func (client *client) Query_WalletStatus(walletStatus *types.WalletStatus) (types.Message, error) {
plog.Info("Query_WalletStatus", "walletStatus", walletStatus) plog.Info("Query_WalletStatus", "walletStatus", walletStatus)
// 需要para共识这边根据walletStatus.IsWalletLock锁的状态开启/关闭发送共识交易 // 需要para共识这边根据walletStatus.IsWalletLock锁的状态开启/关闭发送共识交易
// 需要实现具体处理 to be。。。。 client.commitMsgClient.onWalletStatus(walletStatus)
return &types.Reply{IsOk: true, Msg: []byte("OK")}, nil return &types.Reply{IsOk: true, Msg: []byte("OK")}, nil
} }
...@@ -19,6 +19,7 @@ import ( ...@@ -19,6 +19,7 @@ import (
var ( var (
consensusInterval = 16 //about 1 new block interval consensusInterval = 16 //about 1 new block interval
minerInterval = 2
) )
type commitMsgClient struct { type commitMsgClient struct {
...@@ -27,6 +28,7 @@ type commitMsgClient struct { ...@@ -27,6 +28,7 @@ type commitMsgClient struct {
commitMsgNotify chan int64 commitMsgNotify chan int64
delMsgNotify chan int64 delMsgNotify chan int64
mainBlockAdd chan *types.BlockDetail mainBlockAdd chan *types.BlockDetail
minerSwitch chan bool
currentTx *types.Transaction currentTx *types.Transaction
checkTxCommitTimes int32 checkTxCommitTimes int32
privateKey crypto.PrivKey privateKey crypto.PrivKey
...@@ -41,16 +43,13 @@ func (client *commitMsgClient) handler() { ...@@ -41,16 +43,13 @@ func (client *commitMsgClient) handler() {
var sendingHeight int64 //当前发送的最大高度 var sendingHeight int64 //当前发送的最大高度
var sendingMsgs []*pt.ParacrossNodeStatus var sendingMsgs []*pt.ParacrossNodeStatus
var readTick <-chan time.Time var readTick <-chan time.Time
var ticker *time.Ticker
client.paraClient.wg.Add(1) client.paraClient.wg.Add(1)
consensusCh := make(chan *pt.ParacrossStatus, 1) consensusCh := make(chan *pt.ParacrossStatus, 1)
go client.getConsensusHeight(consensusCh) go client.getConsensusHeight(consensusCh)
client.paraClient.wg.Add(1) client.paraClient.wg.Add(1)
priKeyCh := make(chan crypto.PrivKey, 1)
go client.fetchPrivacyKey(priKeyCh)
client.paraClient.wg.Add(1)
sendMsgCh := make(chan *types.Transaction, 1) sendMsgCh := make(chan *types.Transaction, 1)
go client.sendCommitMsg(sendMsgCh) go client.sendCommitMsg(sendMsgCh)
...@@ -155,7 +154,7 @@ out: ...@@ -155,7 +154,7 @@ out:
case rsp := <-consensusCh: case rsp := <-consensusCh:
consensHeight := rsp.Height consensHeight := rsp.Height
plog.Info("para consensus rcv", "notify", notification, "sending", len(sendingMsgs), plog.Info("para consensus rcv", "notify", notification, "sending", len(sendingMsgs),
"consensHeigt", rsp.Height, "finished", finishHeight, "sync", isSync, "consensBlockHash", common.ToHex(rsp.BlockHash)) "consensHeigt", rsp.Height, "finished", finishHeight, "sync", isSync, "miner", readTick!=nil, "consensBlockHash", common.ToHex(rsp.BlockHash))
if notification == nil || isRollback { if notification == nil || isRollback {
continue continue
...@@ -194,13 +193,23 @@ out: ...@@ -194,13 +193,23 @@ out:
isSync = true isSync = true
} }
case key, ok := <-priKeyCh: case miner := <-client.minerSwitch:
if !ok { plog.Info("para consensus mining","miner",miner)
priKeyCh = nil //停止挖矿
if !miner{
readTick = nil
if ticker != nil{
ticker.Stop()
}
plog.Info("para consensus stop mining")
continue continue
} }
client.privateKey = key //开启挖矿
readTick = time.Tick(time.Second * 2) if client.privateKey != nil && readTick == nil{
ticker = time.NewTicker(time.Second * time.Duration(minerInterval))
readTick = ticker.C
plog.Info("para consensus start mining")
}
case <-client.quit: case <-client.quit:
break out break out
...@@ -575,51 +584,71 @@ func (client *commitMsgClient) getConsensusStatus(block *types.Block) (*pt.Parac ...@@ -575,51 +584,71 @@ func (client *commitMsgClient) getConsensusStatus(block *types.Block) (*pt.Parac
} }
func (client *commitMsgClient) fetchPrivacyKey(ch chan crypto.PrivKey) { func (client *commitMsgClient) onWalletStatus(status *types.WalletStatus) {
defer client.paraClient.wg.Done() if status == nil || client.paraClient.authAccount == ""{
if client.paraClient.authAccount == "" {
close(ch)
return return
} }
if !status.IsWalletLock && client.privateKey == nil{
client.fetchPriKey()
plog.Info("para commit fetchPriKey")
}
select {
case client.minerSwitch <- !status.IsWalletLock:
case <-client.quit:
}
}
func (client *commitMsgClient) onWalletAccount(acc *types.Account) {
if acc == nil || client.paraClient.authAccount == "" || client.paraClient.authAccount != acc.Addr || client.privateKey != nil{
return
}
err := client.fetchPriKey()
if err != nil{
plog.Error("para commit fetchPriKey", "err", err.Error())
return
}
select {
case client.minerSwitch <- true:
case <-client.quit:
}
}
func (client *commitMsgClient) fetchPriKey() error {
req := &types.ReqString{Data: client.paraClient.authAccount} req := &types.ReqString{Data: client.paraClient.authAccount}
out:
for {
select {
case <-client.quit:
break out
case <-time.NewTimer(time.Second * 2).C:
msg := client.paraClient.GetQueueClient().NewMessage("wallet", types.EventDumpPrivkey, req)
err := client.paraClient.GetQueueClient().Send(msg, true)
if err != nil {
plog.Error("para commit send msg", "err", err.Error())
break out
}
resp, err := client.paraClient.GetQueueClient().Wait(msg)
if err != nil {
plog.Error("para commit msg sign to wallet", "err", err.Error())
continue
}
str := resp.GetData().(*types.ReplyString).Data
pk, err := common.FromHex(str)
if err != nil && pk == nil {
panic(err)
}
secp, err := crypto.New(types.GetSignName("", types.SECP256K1)) msg := client.paraClient.GetQueueClient().NewMessage("wallet", types.EventDumpPrivkey, req)
if err != nil { err := client.paraClient.GetQueueClient().Send(msg, true)
panic(err) if err != nil {
} plog.Error("para commit send msg", "err", err.Error())
return err
}
resp, err := client.paraClient.GetQueueClient().Wait(msg)
if err != nil {
plog.Error("para commit msg sign to wallet", "err", err.Error())
return err
}
str := resp.GetData().(*types.ReplyString).Data
pk, err := common.FromHex(str)
if err != nil && pk == nil {
return err
}
priKey, err := secp.PrivKeyFromBytes(pk) secp, err := crypto.New(types.GetSignName("", types.SECP256K1))
if err != nil { if err != nil {
panic(err) return err
} }
ch <- priKey priKey, err := secp.PrivKeyFromBytes(pk)
close(ch) if err != nil {
break out plog.Error("para commit msg get priKey", "err", err.Error())
} return err
} }
client.privateKey = priKey
plog.Info("para commit fetchPriKey success")
return nil
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment