Commit d84308aa authored by madengji's avatar madengji Committed by 33cn

fix ut

parent 6ef4b91d
......@@ -56,7 +56,7 @@ grpcLogFile="grpc33.log"
[p2p.sub.dht]
isSeed=true
DHTDataPath="paradatadir/p2pstore"
[rpc]
# 避免与主链配置冲突
......@@ -112,6 +112,7 @@ mainForkParacrossCommitTx=2270000
mainLoopCheckCommitTxDoneForkHeight=4320000
#无平行链交易的主链区块间隔,平行链产生一个空块,从高度0开始,配置[blockHeight:interval],比如["0:50","1000:100"]
emptyBlockInterval=["0:50"]
blsSign=true
[store]
......
......@@ -6,13 +6,14 @@ package para
import (
"bytes"
"math/big"
"sort"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/33cn/chain33/util"
"github.com/33cn/chain33/common"
"github.com/33cn/chain33/common/crypto"
"github.com/33cn/chain33/types"
......@@ -36,7 +37,6 @@ type blsClient struct {
cryptoCli crypto.Crypto
blsPriKey crypto.PrivKey
blsPubKey crypto.PubKey
peers map[string]bool
peersBlsPubKey map[string]crypto.PubKey
commitsPool map[int64]*pt.ParaBlsSignSumDetails
rcvCommitTxCh chan []*pt.ParacrossCommitAction
......@@ -50,7 +50,6 @@ type blsClient struct {
func newBlsClient(para *client, cfg *subConfig) *blsClient {
b := &blsClient{paraClient: para}
b.selfID = cfg.AuthAccount
b.peers = make(map[string]bool)
cli, err := crypto.New("bls")
if err != nil {
panic("new bls crypto fail")
......@@ -231,7 +230,6 @@ func (b *blsClient) sendAggregateTx(nodes []string) error {
func (b *blsClient) rcvCommitTx(tx *types.Transaction) error {
if !b.isValidNodes(tx.From()) {
b.updatePeers(tx.From(), false)
plog.Error("rcvCommitTx is not valid node", "addr", tx.From())
return pt.ErrParaNodeAddrNotExisted
}
......@@ -251,7 +249,7 @@ func (b *blsClient) rcvCommitTx(tx *types.Transaction) error {
plog.Error("rcvCommitTx checkCommitTx ", "err", err)
return errors.Wrap(err, "checkCommitTx")
}
b.updatePeers(tx.From(), true)
if len(commits) > 0 {
plog.Debug("rcvCommitTx tx", "addr", tx.From(), "height", commits[0].Status.Height)
}
......@@ -328,8 +326,8 @@ func isMostCommitDone(peers int, txsBuff map[int64]*pt.ParaBlsSignSumDetails) bo
}
for i, v := range txsBuff {
most, _ := getMostCommit(v.Msgs)
if isCommitDone(peers, most) {
most, _ := util.GetMostCommit(v.Msgs)
if util.IsCommitDone(peers, most) {
plog.Info("blssign.isMostCommitDone", "height", i, "most", most, "peers", peers)
return true
}
......@@ -341,8 +339,8 @@ func isMostCommitDone(peers int, txsBuff map[int64]*pt.ParaBlsSignSumDetails) bo
func filterDoneCommits(peers int, pool map[int64]*pt.ParaBlsSignSumDetails) []*pt.ParaBlsSignSumDetails {
var seq []int64
for i, v := range pool {
most, hash := getMostCommit(v.Msgs)
if !isCommitDone(peers, most) {
most, hash := util.GetMostCommit(v.Msgs)
if !util.IsCommitDone(peers, most) {
plog.Debug("blssign.filterDoneCommits not commit done", "height", i)
continue
}
......@@ -393,7 +391,7 @@ func (b *blsClient) aggregateCommit2Action(nodes []string, commits []*pt.ParaBls
return nil, errors.Wrapf(err, "bls aggregate=%s", v.Addrs)
}
a.Bls.Sign = sign.Bytes()
bits, remains := setAddrsBitMap(nodes, v.Addrs)
bits, remains := util.SetAddrsBitMap(nodes, v.Addrs)
plog.Debug("AggregateCommit2Action", "nodes", nodes, "addr", v.Addrs, "bits", common.ToHex(bits), "height", v.Height)
if len(remains) > 0 {
plog.Info("bls.signDoneCommits", "remains", remains)
......@@ -421,22 +419,6 @@ func (b *blsClient) aggregateSigns(signs [][]byte) (crypto.Signature, error) {
return agg.Aggregate(signatures)
}
func (b *blsClient) updatePeers(id string, add bool) {
b.mutex.Lock()
defer b.mutex.Unlock()
if _, ok := b.peers[id]; ok {
if !add {
delete(b.peers, id)
}
return
}
if add {
b.peers[id] = true
}
}
func (b *blsClient) setBlsPriKey(secpPrkKey []byte) {
b.blsPriKey = b.getBlsPriKey(secpPrkKey)
b.blsPubKey = b.blsPriKey.PubKey()
......@@ -448,9 +430,9 @@ func (b *blsClient) getBlsPriKey(key []byte) crypto.PrivKey {
var newKey [common.Sha256Len]byte
copy(newKey[:], key)
for {
plog.Info("para commit getBlsPriKey try", "key", common.ToHex(newKey[:]))
pri, err := b.cryptoCli.PrivKeyFromBytes(newKey[:])
if nil != err {
plog.Debug("para commit getBlsPriKey try", "key", common.ToHex(newKey[:]))
copy(newKey[:], common.Sha256(newKey[:]))
continue
}
......@@ -482,53 +464,11 @@ func (b *blsClient) blsSign(commits []*pt.ParacrossCommitAction) error {
return errors.Wrapf(types.ErrInvalidParam, "addr=%s,height=%d", b.selfID, cmt.Status.Height)
}
cmt.Bls.Sign = sign
plog.Debug("blsign msg", "data", common.ToHex(data), "height", cmt.Status.Height, "sign", len(cmt.Bls.Sign), "src", len(sign))
plog.Info("bls sign msg", "data", common.ToHex(data), "height", cmt.Status.Height, "sign", len(cmt.Bls.Sign), "src", len(sign))
}
return nil
}
//设置nodes范围内的bitmap,如果addrs在node不存在,也不设置,返回未命中的addrs
func setAddrsBitMap(nodes, addrs []string) ([]byte, map[string]bool) {
rst := big.NewInt(0)
addrsMap := make(map[string]bool)
for _, n := range addrs {
addrsMap[n] = true
}
for i, a := range nodes {
if _, exist := addrsMap[a]; exist {
rst.SetBit(rst, i, 1)
delete(addrsMap, a)
}
}
return rst.Bytes(), addrsMap
}
func getMostCommit(commits [][]byte) (int, string) {
stats := make(map[string]int)
n := len(commits)
for i := 0; i < n; i++ {
if _, ok := stats[string(commits[i])]; ok {
stats[string(commits[i])]++
} else {
stats[string(commits[i])] = 1
}
}
most := -1
var hash string
for k, v := range stats {
if v > most {
most = v
hash = k
}
}
return most, hash
}
func isCommitDone(nodes, mostSame int) bool {
return 3*mostSame > 2*nodes
}
func (b *blsClient) getBlsPubKey(addr string) (crypto.PubKey, error) {
//先从缓存中获取
if v, ok := b.peersBlsPubKey[addr]; ok {
......
......@@ -11,33 +11,11 @@ import (
"github.com/33cn/chain33/types"
"github.com/33cn/chain33/common/crypto"
_ "github.com/33cn/plugin/plugin/crypto/bls"
pt "github.com/33cn/plugin/plugin/dapp/paracross/types"
"github.com/stretchr/testify/assert"
)
func TestSetAddrsBitMap(t *testing.T) {
nodes := []string{"aa", "bb", "cc", "dd"}
addrs := []string{}
rst, rem := setAddrsBitMap(nodes, addrs)
assert.Equal(t, len(rst), 0)
assert.Equal(t, len(rem), 0)
addrs = []string{"aa"}
rst, rem = setAddrsBitMap(nodes, addrs)
assert.Equal(t, rst, []byte{0x1})
assert.Equal(t, len(rem), 0)
addrs = []string{"aa", "cc"}
rst, rem = setAddrsBitMap(nodes, addrs)
assert.Equal(t, rst, []byte{0x5})
assert.Equal(t, len(rem), 0)
addrs = []string{"aa", "cc", "dd"}
rst, rem = setAddrsBitMap(nodes, addrs)
assert.Equal(t, rst, []byte{0xd})
assert.Equal(t, len(rem), 0)
}
func TestIntegrateCommits(t *testing.T) {
pool := make(map[int64]*pt.ParaBlsSignSumDetails)
var commits []*pt.ParacrossCommitAction
......@@ -135,12 +113,17 @@ func testVerifyBlsSign(t *testing.T, cryptCli crypto.Crypto) {
commit := &pt.ParacrossCommitAction{}
blsInfo := &pt.ParacrossCommitBlsInfo{}
signData := "0x82753675393576758571cbbaefada498614b4a0a967ca2dd5724eb46ecfd1c89f1e49792ebbe1866c1d6d6ceaf3054c7189751477a5b7312218eb77dcab1bfb6287c6fbf2e1c6cf8fe2ade7c17596b081dc98be785a34db5b45a5cca08e7e744"
//bls-cgo sign data
//signData := "0x82753675393576758571cbbaefada498614b4a0a967ca2dd5724eb46ecfd1c89f1e49792ebbe1866c1d6d6ceaf3054c7189751477a5b7312218eb77dcab1bfb6287c6fbf2e1c6cf8fe2ade7c17596b081dc98be785a34db5b45a5cca08e7e744"
//g1pubs' sign data
signData := "0x90b4510399b16e6b3c3129593b29f88bfbc6bb1ab3fe44f682f7ff32a9b8e7086c07d28a25efc29b460fb40ea2674c7910d63293f0d57670276b7baabe4c95c92143063296371b8ba2a0e540f7956d569740bc08553a0dc6bf2fff4f4241c082"
blsInfo.Sign, err = common.FromHex(signData)
assert.NoError(t, err)
status := &pt.ParacrossNodeStatus{}
data := "0x1a0c757365722e702e706172612e322097162f9d4a888121fdba2fb1ab402596acdbcb602121bd12284adb739d85f225"
//data := "0x1a0c757365722e702e706172612e322097162f9d4a888121fdba2fb1ab402596acdbcb602121bd12284adb739d85f225"
data := "0x1a0c757365722e702e706172612e"
msg, err := common.FromHex(data)
assert.NoError(t, err)
types.Decode(msg, status)
......
......@@ -927,11 +927,13 @@ func (client *commitMsgClient) getNodeGroupAddrs() (string, error) {
func (client *commitMsgClient) onWalletStatus(status *types.WalletStatus) {
if status == nil || client.authAccount == "" {
plog.Info("para onWalletStatus", "status", status == nil, "auth", client.authAccount == "")
return
}
if !status.IsWalletLock && client.privateKey == nil {
plog.Info("para commit fetchPriKey try")
client.fetchPriKey()
plog.Info("para commit fetchPriKey")
plog.Info("para commit fetchPriKey ok")
}
if client.privateKey == nil {
......@@ -951,9 +953,10 @@ func (client *commitMsgClient) onWalletAccount(acc *types.Account) {
if acc == nil || client.authAccount == "" || client.authAccount != acc.Addr || client.privateKey != nil {
return
}
plog.Error("para onWalletAccount try fetch prikey")
err := client.fetchPriKey()
if err != nil {
plog.Error("para commit fetchPriKey", "err", err.Error())
plog.Error("para onWalletAccount", "err", err.Error())
return
}
......
......@@ -44,7 +44,7 @@ func (d Driver) PrivKeyFromBytes(b []byte) (privKey crypto.PrivKey, err error) {
privKeyBytes := new([BLSPrivateKeyLength]byte)
copy(privKeyBytes[:], b[:BLSPrivateKeyLength])
priv := g1pubs.DeserializeSecretKey(*privKeyBytes)
if priv == nil {
if priv.GetFRElement() == nil {
return nil, errors.New("invalid bls privkey")
}
privBytes := priv.Serialize()
......
......@@ -3,7 +3,6 @@ FROM ubuntu:16.04
WORKDIR /root
COPY chain33 chain33
COPY chain33-cli chain33-cli
COPY chain33-para-cli chain33-para-cli
COPY chain33.toml chain33*.toml ./
COPY entrypoint.sh entrypoint.sh
......
......@@ -415,7 +415,7 @@ func getValidAddrs(nodes map[string]struct{}, addrs []string) []string {
//bls签名共识交易验证 大约平均耗时30ms (20~40ms)
func (a *action) procBlsSign(nodesArry []string, commit *pt.ParacrossCommitAction) ([]string, error) {
signAddrs := getAddrsByBitMap(nodesArry, commit.Bls.AddrsMap)
signAddrs := util.GetAddrsByBitMap(nodesArry, commit.Bls.AddrsMap)
var pubs []string
for _, addr := range signAddrs {
pub, err := getAddrBlsPubKey(a.db, commit.Status.Title, addr)
......@@ -468,7 +468,7 @@ func verifyBlsSign(cryptoCli crypto.Crypto, pubs []string, commit *pt.ParacrossC
"addrsMap", common.ToHex(commit.Bls.AddrsMap), "sign", common.ToHex(commit.Bls.Sign), "data", common.ToHex(msg))
return pt.ErrBlsSignVerify
}
clog.Info("paracross procBlsSign success", "title", commit.Status.Title, "height", commit.Status.Height, "time", types.Since(t1))
clog.Debug("paracross procBlsSign success", "title", commit.Status.Title, "height", commit.Status.Height, "time", types.Since(t1))
return nil
}
......
......@@ -18,6 +18,7 @@ import (
"github.com/33cn/chain33/common/log"
mty "github.com/33cn/chain33/system/dapp/manage/types"
"github.com/33cn/chain33/types"
_ "github.com/33cn/plugin/plugin/crypto/bls"
"github.com/33cn/plugin/plugin/dapp/paracross/testnode"
pt "github.com/33cn/plugin/plugin/dapp/paracross/types"
"github.com/stretchr/testify/assert"
......
......@@ -9,8 +9,6 @@ import (
"strconv"
"math/big"
"github.com/33cn/chain33/common"
dbm "github.com/33cn/chain33/common/db"
"github.com/33cn/chain33/system/dapp"
......@@ -210,19 +208,6 @@ func makeParaNodeGroupReceipt(title string, prev, current *types.ConfigItem) *ty
}
}
//获取nodes范围内的bitmap,如果bitmap超出了nodes范围,也不处理,防止越界
func getAddrsByBitMap(nodes []string, bitmap []byte) []string {
rst := big.NewInt(0).SetBytes(bitmap)
addrs := make([]string, 0)
for i, a := range nodes {
if rst.Bit(i) == uint(0x1) {
addrs = append(addrs, a)
}
}
return addrs
}
//get secp256 addr's bls pubkey
func getAddrBlsPubKey(db dbm.KV, title, addr string) (string, error) {
addrStat, err := getNodeAddr(db, title, addr)
......
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