Commit 6704bc83 authored by QM's avatar QM

fix verify bls sign

parent c903ef42
...@@ -12,13 +12,11 @@ import ( ...@@ -12,13 +12,11 @@ import (
"sync/atomic" "sync/atomic"
"time" "time"
"github.com/33cn/chain33/util"
"github.com/33cn/chain33/common" "github.com/33cn/chain33/common"
"github.com/33cn/chain33/common/crypto" "github.com/33cn/chain33/common/crypto"
"github.com/33cn/chain33/types" "github.com/33cn/chain33/types"
"github.com/33cn/chain33/util"
pt "github.com/33cn/plugin/plugin/dapp/paracross/types" pt "github.com/33cn/plugin/plugin/dapp/paracross/types"
"github.com/pkg/errors" "github.com/pkg/errors"
) )
...@@ -45,6 +43,7 @@ type blsClient struct { ...@@ -45,6 +43,7 @@ type blsClient struct {
feedDog uint32 feedDog uint32
quit chan struct{} quit chan struct{}
mutex sync.Mutex mutex sync.Mutex
typeNode uint32
} }
func newBlsClient(para *client, cfg *subConfig) *blsClient { func newBlsClient(para *client, cfg *subConfig) *blsClient {
...@@ -60,6 +59,7 @@ func newBlsClient(para *client, cfg *subConfig) *blsClient { ...@@ -60,6 +59,7 @@ func newBlsClient(para *client, cfg *subConfig) *blsClient {
b.rcvCommitTxCh = make(chan []*pt.ParacrossCommitAction, maxRcvTxCount) b.rcvCommitTxCh = make(chan []*pt.ParacrossCommitAction, maxRcvTxCount)
b.quit = make(chan struct{}) b.quit = make(chan struct{})
b.leaderSwitchInt = defLeaderSwitchInt b.leaderSwitchInt = defLeaderSwitchInt
b.typeNode = pt.ParaCommitNode
if cfg.BlsLeaderSwitchIntval > 0 { if cfg.BlsLeaderSwitchIntval > 0 {
b.leaderSwitchInt = cfg.BlsLeaderSwitchIntval b.leaderSwitchInt = cfg.BlsLeaderSwitchIntval
} }
...@@ -172,23 +172,53 @@ func (b *blsClient) getLeaderInfo() ([]string, int32, int32, int32, bool) { ...@@ -172,23 +172,53 @@ func (b *blsClient) getLeaderInfo() ([]string, int32, int32, int32, bool) {
return nodes, leaderIdx, baseIdx, offIdx, nodes[leaderIdx] == b.selfID return nodes, leaderIdx, baseIdx, offIdx, nodes[leaderIdx] == b.selfID
} }
func (b *blsClient) getSuperNodes() ([]string, string) { func (b *blsClient) getSuperGroupNodes() ([]string, string) {
// 获取授权节点 // 获取授权节点
nodeStr, err := b.paraClient.commitMsgClient.getNodeGroupAddrs() nodeStr, err := b.paraClient.commitMsgClient.getNodeGroupAddrs()
if err != nil {
return nil, ""
}
if strings.Contains(nodeStr, b.selfID) { if strings.Contains(nodeStr, b.selfID) {
if err != nil { b.typeNode = pt.ParaCommitSuperNode
return nil, ""
}
return strings.Split(nodeStr, ","), nodeStr return strings.Split(nodeStr, ","), nodeStr
} else {
b.typeNode = pt.ParaCommitNode
} }
return nil, ""
}
func (b *blsClient) getSupervisionGroupNodes() ([]string, string) {
// 获取监督节点 // 获取监督节点
nodeStr, err = b.paraClient.commitMsgClient.getSupervisionNodeGroupAddrs() nodeStr, err := b.paraClient.commitMsgClient.getSupervisionNodeGroupAddrs()
if err != nil {
return nil, ""
}
if strings.Contains(nodeStr, b.selfID) { if strings.Contains(nodeStr, b.selfID) {
if err != nil { b.typeNode = pt.ParaCommitSupervisionNode
return nil, ""
}
return strings.Split(nodeStr, ","), nodeStr return strings.Split(nodeStr, ","), nodeStr
} else {
b.typeNode = pt.ParaCommitNode
}
return nil, ""
}
func (b *blsClient) getSuperNodes() ([]string, string) {
if b.typeNode == pt.ParaCommitNode {
// 获取授权节点
nodes, nodeStr := b.getSuperGroupNodes()
if len(nodes) > 0 {
return nodes, nodeStr
} else {
return b.getSupervisionGroupNodes()
}
} else if b.typeNode == pt.ParaCommitSuperNode {
return b.getSuperGroupNodes()
} else if b.typeNode == pt.ParaCommitSupervisionNode {
return b.getSupervisionGroupNodes()
} }
return nil, "" return nil, ""
...@@ -278,7 +308,6 @@ func (b *blsClient) rcvCommitTx(tx *types.Transaction) error { ...@@ -278,7 +308,6 @@ func (b *blsClient) rcvCommitTx(tx *types.Transaction) error {
b.rcvCommitTxCh <- commits b.rcvCommitTxCh <- commits
return nil return nil
} }
func (b *blsClient) checkCommitTx(txs []*types.Transaction) ([]*pt.ParacrossCommitAction, error) { func (b *blsClient) checkCommitTx(txs []*types.Transaction) ([]*pt.ParacrossCommitAction, error) {
...@@ -304,7 +333,7 @@ func (b *blsClient) checkCommitTx(txs []*types.Transaction) ([]*pt.ParacrossComm ...@@ -304,7 +333,7 @@ func (b *blsClient) checkCommitTx(txs []*types.Transaction) ([]*pt.ParacrossComm
//验证bls 签名 //验证bls 签名
err = b.verifyBlsSign(tx.From(), commit) err = b.verifyBlsSign(tx.From(), commit)
if err != nil { if err != nil {
return nil, errors.Wrapf(pt.ErrBlsSignVerify, "from=%s", tx.From()) return nil, errors.Wrapf(err, "from=%s", tx.From())
} }
commits = append(commits, commit) commits = append(commits, commit)
} }
...@@ -405,7 +434,7 @@ func (b *blsClient) aggregateCommit2Action(nodes []string, commits []*pt.ParaBls ...@@ -405,7 +434,7 @@ func (b *blsClient) aggregateCommit2Action(nodes []string, commits []*pt.ParaBls
for _, v := range commits { for _, v := range commits {
a := &pt.ParacrossCommitAction{Bls: &pt.ParacrossCommitBlsInfo{}} a := &pt.ParacrossCommitAction{Bls: &pt.ParacrossCommitBlsInfo{}}
s := &pt.ParacrossNodeStatus{} s := &pt.ParacrossNodeStatus{}
types.Decode(v.Msgs[0], s) _ = types.Decode(v.Msgs[0], s)
a.Status = s a.Status = s
sign, err := b.aggregateSigns(v.Signs) sign, err := b.aggregateSigns(v.Signs)
...@@ -492,42 +521,54 @@ func (b *blsClient) blsSign(commits []*pt.ParacrossCommitAction) error { ...@@ -492,42 +521,54 @@ func (b *blsClient) blsSign(commits []*pt.ParacrossCommitAction) error {
} }
func (b *blsClient) getBlsPubKey(addr string) (crypto.PubKey, error) { func (b *blsClient) getBlsPubKey(addr string) (crypto.PubKey, error) {
//先从缓存中获取 if b.typeNode == pt.ParaCommitSuperNode || b.typeNode == pt.ParaCommitSupervisionNode {
if v, ok := b.peersBlsPubKey[addr]; ok { var funcName string
return v, nil if b.typeNode == pt.ParaCommitSuperNode {
} funcName = "GetNodeAddrInfo"
} else {
//缓存没有,则从statedb获取 funcName = "GetSupervisionNodeAddrInfo"
cfg := b.paraClient.GetAPI().GetConfig() }
ret, err := b.paraClient.GetAPI().QueryChain(&types.ChainExecutor{
Driver: "paracross", //先从缓存中获取
FuncName: "GetNodeAddrInfo", if v, ok := b.peersBlsPubKey[addr]; ok {
Param: types.Encode(&pt.ReqParacrossNodeInfo{Title: cfg.GetTitle(), Addr: addr}), return v, nil
}) }
if err != nil {
plog.Error("commitmsg.GetNodeAddrInfo ", "err", err.Error()) //缓存没有,则从statedb获取
return nil, err cfg := b.paraClient.GetAPI().GetConfig()
} ret, err := b.paraClient.GetAPI().QueryChain(&types.ChainExecutor{
resp, ok := ret.(*pt.ParaNodeAddrIdStatus) Driver: "paracross",
if !ok { FuncName: funcName,
plog.Error("commitmsg.getNodeGroupAddrs rsp nok") Param: types.Encode(&pt.ReqParacrossNodeInfo{Title: cfg.GetTitle(), Addr: addr}),
return nil, err })
} if err != nil {
plog.Error("commitmsg.GetNodeAddrInfo ", "funcName", funcName, "err", err.Error())
return nil, err
}
resp, ok := ret.(*pt.ParaNodeAddrIdStatus)
if !ok {
plog.Error("commitmsg.getNodeGroupAddrs rsp nok", "funcName", funcName)
return nil, err
}
s, err := common.FromHex(resp.BlsPubKey)
if err != nil {
plog.Error("commitmsg.getNode pubkey nok", "pubkey", resp.BlsPubKey, "funcName", funcName)
return nil, err
}
pubKey, err := b.cryptoCli.PubKeyFromBytes(s)
if err != nil {
plog.Error("verifyBlsSign.DeserializePublicKey", "key", addr)
return nil, err
}
plog.Info("getBlsPubKey", "addr", addr, "pub", resp.BlsPubKey, "serial", common.ToHex(pubKey.Bytes()))
b.peersBlsPubKey[addr] = pubKey
return pubKey, nil
s, err := common.FromHex(resp.BlsPubKey)
if err != nil {
plog.Error("commitmsg.getNode pubkey nok", "pubkey", resp.BlsPubKey)
return nil, err
}
pubKey, err := b.cryptoCli.PubKeyFromBytes(s)
if err != nil {
plog.Error("verifyBlsSign.DeserializePublicKey", "key", addr)
return nil, err
} }
plog.Info("getBlsPubKey", "addr", addr, "pub", resp.BlsPubKey, "serial", pubKey.Bytes())
b.peersBlsPubKey[addr] = pubKey
return pubKey, nil return nil, errors.New("b.typeNode = pt.ParaCommitNode")
} }
func (b *blsClient) verifyBlsSign(addr string, commit *pt.ParacrossCommitAction) error { func (b *blsClient) verifyBlsSign(addr string, commit *pt.ParacrossCommitAction) error {
......
...@@ -154,6 +154,29 @@ func (p *Paracross) Query_GetNodeAddrInfo(in *pt.ReqParacrossNodeInfo) (types.Me ...@@ -154,6 +154,29 @@ func (p *Paracross) Query_GetNodeAddrInfo(in *pt.ReqParacrossNodeInfo) (types.Me
return stat, nil return stat, nil
} }
//Query_GetSupervisionNodeAddrInfo get specific node addr info
func (p *Paracross) Query_GetSupervisionNodeAddrInfo(in *pt.ReqParacrossNodeInfo) (types.Message, error) {
if in == nil || in.Addr == "" {
return nil, types.ErrInvalidParam
}
cfg := p.GetAPI().GetConfig()
if cfg.IsPara() {
in.Title = cfg.GetTitle()
} else if in.Title == "" {
return nil, types.ErrInvalidParam
}
stat, err := getSupervisionNodeAddr(p.GetStateDB(), in.Title, in.Addr)
if err != nil {
return nil, err
}
stat.QuitId = getParaNodeIDSuffix(stat.QuitId)
stat.ProposalId = getParaNodeIDSuffix(stat.ProposalId)
return stat, nil
}
func (p *Paracross) getMainHeight() (int64, error) { func (p *Paracross) getMainHeight() (int64, error) {
mainHeight := p.GetMainHeight() mainHeight := p.GetMainHeight()
cfg := p.GetAPI().GetConfig() cfg := p.GetAPI().GetConfig()
...@@ -573,9 +596,12 @@ func (p *Paracross) Query_GetHeight(req *types.ReqString) (*pt.ParacrossConsensu ...@@ -573,9 +596,12 @@ func (p *Paracross) Query_GetHeight(req *types.ReqString) (*pt.ParacrossConsensu
return nil, errors.Wrap(types.ErrInvalidParam, "req invalid") return nil, errors.Wrap(types.ErrInvalidParam, "req invalid")
} }
} }
reqTitle := req.Data
var reqTitle string
if cfg.IsPara() { if cfg.IsPara() {
reqTitle = cfg.GetTitle() reqTitle = cfg.GetTitle()
} else if req != nil {
reqTitle = req.Data
} }
res, err := p.paracrossGetHeight(reqTitle) res, err := p.paracrossGetHeight(reqTitle)
if err != nil { if err != 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