新注册的用户请输入邮箱并保存,随后登录邮箱激活账号。后续可直接使用邮箱登录!

Commit a54d1bf3 authored by taifu's avatar taifu

Merge branch 'v2.2.0_alpha_alias' into 'v2.2.0_alpha_qc' (merge request !62)

v2.2.0_alpha_alias
parents f8f86205 5ba3de12
......@@ -123,17 +123,20 @@ func (r *CertManageRuntime) UpdateAlias(txSimContext protocol.TxSimContext, para
return nil, err
}
if txSimContext.GetSender().OrgId != certificate.Subject.Organization[0] {
return nil, fmt.Errorf("update alias fail, "+
"you can't change other organization cert. "+
"your org is %s cert org is %s",
txSimContext.GetSender().OrgId, certificate.Subject.Organization[0])
if !containOrgId(txSimContext, certificate.Subject.Organization[0]) {
err = fmt.Errorf("update alias fail, "+
"you can't change to other organization[%s] cert. ",
certificate.Subject.Organization[0])
r.log.Warn(err)
return nil, err
}
if txSimContext.GetSender().OrgId != nowCertificate.Subject.Organization[0] {
return nil, fmt.Errorf("update alias fail, "+
"you can't change other organization cert. "+
"your org is %s cert org is %s",
txSimContext.GetSender().OrgId, nowCertificate.Subject.Organization[0])
if !containOrgId(txSimContext, nowCertificate.Subject.Organization[0]) {
err = fmt.Errorf("update alias fail, "+
"you can't change other organization[%s] cert. ",
certificate.Subject.Organization[0])
r.log.Warn(err)
return nil, err
}
nowCert := &commonPb.AliasCertInfo{
......@@ -177,7 +180,7 @@ func (r *CertManageRuntime) DeleteAlias(txSimContext protocol.TxSimContext, para
names := string(params[paramNameAliases])
names = strings.TrimSpace(names)
if utils.IsAnyBlank(names) {
err = fmt.Errorf("update alias failed, alias name is nil")
err = fmt.Errorf("delete alias failed, alias name is nil")
r.log.Warn(err)
return nil, err
}
......@@ -198,16 +201,16 @@ func (r *CertManageRuntime) DeleteAlias(txSimContext protocol.TxSimContext, para
// verify org
certificate, err := utils.ParseCert(certAliasInfo.NowCert.Cert)
if err != nil || certificate == nil || certificate.Subject.Organization == nil {
err = fmt.Errorf("update alias fail, params[%s] format error, err:%s", paramNameCert, err)
err = fmt.Errorf("delete alias fail, params[%s] format error, err:%s", paramNameCert, err)
r.log.Warn(err)
return nil, err
}
if txSimContext.GetSender().OrgId != certificate.Subject.Organization[0] {
return nil, fmt.Errorf("delete alias fail, "+
"you can't change other organization cert. "+
"your org is %s cert org is %s",
txSimContext.GetSender().OrgId, certificate.Subject.Organization[0])
if !containOrgId(txSimContext, certificate.Subject.Organization[0]) {
err = fmt.Errorf("delete alias fail, "+
"you can't delete other organization[%s] cert. ",
certificate.Subject.Organization[0])
r.log.Warn(err)
return nil, err
}
blankCert := &commonPb.AliasCertInfo{BlockHeight: txSimContext.GetBlockHeight()}
......@@ -271,10 +274,10 @@ func (r *CertManageRuntime) addAliasCore(txSimContext protocol.TxSimContext, ali
if certAliasInfo == nil {
certAliasInfo = &commonPb.AliasInfo{Alias: aliasName}
}
//err = r.saveAllAlias(txSimContext, aliasName)
//if err != nil {
// return err
//}
err = r.saveAllAlias(txSimContext, aliasName)
if err != nil {
return err
}
certAliasInfo.NowCert = nowCert
certAliasInfo.HisCerts = append(certAliasInfo.HisCerts, nowCert)
err = r.setAliasToDb(txSimContext, certAliasInfo)
......@@ -285,20 +288,20 @@ func (r *CertManageRuntime) addAliasCore(txSimContext protocol.TxSimContext, ali
return err
}
//func (r *CertManageRuntime) saveAllAlias(txSimContext protocol.TxSimContext, aliasStr string) error {
// allAliasBytes, err := txSimContext.Get(certManageContractName, []byte(certAliasKey+"all"))
// if err != nil {
// r.log.Warnf("save all alias failed, err: ", err.Error())
// return err
// }
// allAlias := string(allAliasBytes)
// if len(allAlias) > 0 {
// allAlias += ","
// }
// allAlias += aliasStr
// _ = txSimContext.Put(certManageContractName, []byte(certAliasKey+"all"), []byte(allAlias))
// return nil
//}
func (r *CertManageRuntime) saveAllAlias(txSimContext protocol.TxSimContext, aliasStr string) error {
allAliasBytes, err := txSimContext.Get(certManageContractName, []byte(certAliasKey+"all"))
if err != nil {
r.log.Warnf("save all alias failed, err: ", err.Error())
return err
}
allAlias := string(allAliasBytes)
if len(allAlias) > 0 {
allAlias += ","
}
allAlias += aliasStr
_ = txSimContext.Put(certManageContractName, []byte(certAliasKey+"all"), []byte(allAlias))
return nil
}
func (r *CertManageRuntime) setAliasToDb(txSimContext protocol.TxSimContext, certAliasInfo *commonPb.AliasInfo) error {
certAliasBytes, err := certAliasInfo.Marshal()
......@@ -359,6 +362,17 @@ func (r *CertManageRuntime) setCertHash(txSimContext protocol.TxSimContext, nowC
return nil
}
func containOrgId(ctx protocol.TxSimContext, orgId string) bool {
endorses := ctx.GetTx().Endorsers
if len(endorses) == 0 {
return ctx.GetSender().OrgId == orgId
}
for _, endorse := range endorses {
return endorse.Signer.OrgId == orgId
}
return false
}
// Stay new function
//
//func (r *CertManageRuntime) FreezeAlias(txSimContext protocol.TxSimContext, params map[string][]byte) (
......
......@@ -125,7 +125,7 @@ func Test_DeleteAlias(t *testing.T) {
// no alias add
params := make(map[string][]byte)
params[paramNameAliases] = []byte("alias03")
result, err = mgrRuntime.DeleteAlias(txSimContext, nil)
result, err = mgrRuntime.DeleteAlias(txSimContext, params)
assert.NotNil(t, err)
assert.Nil(t, result)
......@@ -152,7 +152,7 @@ func Test_DeleteAlias(t *testing.T) {
assert.Nil(t, err)
assert.Equal(t, "ok", string(result2))
// dep delete
// repeat
params[paramNameAliases] = []byte("alias03")
params[paramNameCert] = getOrg1Client1Signer().MemberInfo
result, err = mgrRuntime.DeleteAlias(txSimContext, params)
......
......@@ -12,8 +12,9 @@ import (
"sync"
"testing"
"chainmaker.org/chainmaker/pb-go/v2/accesscontrol"
commonPb "chainmaker.org/chainmaker/pb-go/v2/common"
"chainmaker.org/chainmaker/pb-go/v2/accesscontrol"
"chainmaker.org/chainmaker/protocol/v2/test"
"github.com/stretchr/testify/assert"
......@@ -64,6 +65,20 @@ var _ protocol.TxSimContext = (*mock.MockTxSimContext)(nil)
func initEnv(t *testing.T) (*CertManageRuntime, *mock.MockTxSimContext, func()) {
r, m, f := initEnvCore(t)
m.EXPECT().GetSender().Return(getOrg1Client1Signer()).AnyTimes()
m.EXPECT().GetTx().DoAndReturn(
func() *commonPb.Transaction {
return &commonPb.Transaction{
Sender: &commonPb.EndorsementEntry{
Signer: getOrg1Client1Signer(),
},
Endorsers: []*commonPb.EndorsementEntry{
{
Signer: getOrg1Client1Signer(),
Signature: nil,
},
},
}
}).AnyTimes()
return r, m, f
}
......@@ -72,6 +87,20 @@ var cache = NewCacheMock()
func initEnvSender2(t *testing.T) (*CertManageRuntime, *mock.MockTxSimContext, func()) {
r, m, f := initEnvCore(t)
m.EXPECT().GetSender().Return(getOrg2Client1Signer()).AnyTimes()
m.EXPECT().GetTx().DoAndReturn(
func() *commonPb.Transaction {
return &commonPb.Transaction{
Sender: &commonPb.EndorsementEntry{
Signer: getOrg1Client1Signer(),
},
Endorsers: []*commonPb.EndorsementEntry{
{
Signer: getOrg2Client1Signer(),
Signature: nil,
},
},
}
}).AnyTimes()
return r, m, f
}
......@@ -97,14 +126,6 @@ func initEnvCore(t *testing.T) (*CertManageRuntime, *mock.MockTxSimContext, func
func(name string, key []byte) error {
return cache.Del(name, string(key))
}).AnyTimes()
txSimContext.EXPECT().GetTx().DoAndReturn(
func() *commonPb.Transaction {
return &commonPb.Transaction{
Sender: &commonPb.EndorsementEntry{
Signer: getOrg1Client1Signer(),
},
}
}).AnyTimes()
txSimContext.EXPECT().GetBlockHeight().Return(uint64(1)).AnyTimes()
return certMgrRuntime, txSimContext, func() { ctrl.Finish() }
}
......
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