Skip to content

Commit

Permalink
Arch refactor alpha (Hyperledger-TWGC#190)
Browse files Browse the repository at this point in the history
* add crpto interface as repare for Hyperledger-TWGC#127

Signed-off-by: Sam Yuan <yy19902439@126.com>

* adding Worker interface for Hyperledger-TWGC#56 and decouple Assembler and Integrator

Signed-off-by: Sam Yuan <yy19902439@126.com>

* refactor for worker interface

Signed-off-by: Sam Yuan <yy19902439@126.com>

* package structure

Signed-off-by: Sam Yuan <yy19902439@126.com>

* fix up

Signed-off-by: Sam Yuan <yy19902439@126.com>

* package refactor

Signed-off-by: Sam Yuan <yy19902439@126.com>

* fix up

Signed-off-by: Sam Yuan <yy19902439@126.com>

* fix up

Signed-off-by: Sam Yuan <yy19902439@126.com>

* remove Envelope from elements

Signed-off-by: Sam Yuan <yy19902439@126.com>

* add memeory free

Signed-off-by: Sam Yuan <yy19902439@126.com>

* remove Proposal from Elements

Signed-off-by: Sam Yuan <yy19902439@126.com>

* fix up

Signed-off-by: Sam Yuan <yy19902439@126.com>

* move start time to ctx

Signed-off-by: Sam Yuan <yy19902439@126.com>
  • Loading branch information
SamYuan1990 committed Aug 24, 2021
1 parent b0fb13e commit a8e4cf2
Show file tree
Hide file tree
Showing 32 changed files with 842 additions and 630 deletions.
6 changes: 3 additions & 3 deletions cmd/tape/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"os"

"tape/pkg/infra"
"tape/pkg/infra/cmdImpl"

"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -40,10 +40,10 @@ func main() {
fullCmd := kingpin.MustParse(app.Parse(os.Args[1:]))
switch fullCmd {
case version.FullCommand():
fmt.Printf(infra.GetVersionInfo())
fmt.Printf(cmdImpl.GetVersionInfo())
case run.FullCommand():
checkArgs(rate, burst, logger)
err = infra.Process(*con, *num, *burst, *rate, logger)
err = cmdImpl.Process(*con, *num, *burst, *rate, logger)
default:
err = errors.Errorf("invalid command: %s", fullCmd)
}
Expand Down
74 changes: 0 additions & 74 deletions pkg/infra/assembler.go

This file was deleted.

13 changes: 13 additions & 0 deletions pkg/infra/basic/basic_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package basic_test

import (
"testing"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

func TestBasic(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Basic Suite")
}
2 changes: 1 addition & 1 deletion pkg/infra/client.go → pkg/infra/basic/client.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package infra
package basic

import (
"context"
Expand Down
12 changes: 6 additions & 6 deletions pkg/infra/client_test.go → pkg/infra/basic/client_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package infra_test
package basic_test

import (
"context"
"tape/pkg/infra"
"tape/pkg/infra/basic"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
Expand All @@ -12,21 +12,21 @@ import (
var _ = Describe("Client", func() {

Context("Client Error handling", func() {
dummy := infra.Node{
dummy := basic.Node{
Addr: "invalid_addr",
}
logger := log.New()

It("captures error from endorser", func() {
_, err := infra.CreateEndorserClient(dummy, logger)
_, err := basic.CreateEndorserClient(dummy, logger)
Expect(err).Should(MatchError(ContainSubstring("error connecting to invalid_addr")))
})
It("captures error from broadcaster", func() {
_, err := infra.CreateBroadcastClient(context.Background(), dummy, logger)
_, err := basic.CreateBroadcastClient(context.Background(), dummy, logger)
Expect(err).Should(MatchError(ContainSubstring("error connecting to invalid_addr")))
})
It("captures error from DeliverFilter", func() {
_, err := infra.CreateDeliverFilteredClient(context.Background(), dummy, logger)
_, err := basic.CreateDeliverFilteredClient(context.Background(), dummy, logger)
Expect(err).Should(MatchError(ContainSubstring("error connecting to invalid_addr")))
})
})
Expand Down
49 changes: 46 additions & 3 deletions pkg/infra/config.go → pkg/infra/basic/config.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
package infra
package basic

import (
"crypto/ecdsa"
"crypto/x509"
"encoding/pem"
"io/ioutil"
"sync"
"tape/internal/fabric/bccsp/utils"

"github.com/gogo/protobuf/proto"
"github.com/hyperledger/fabric-protos-go/msp"
"github.com/hyperledger/fabric-protos-go/peer"
"github.com/pkg/errors"
"gopkg.in/yaml.v2"
)

type Elements struct {
SignedProp *peer.SignedProposal
Responses []*peer.ProposalResponse
Lock sync.Mutex
}

type Config struct {
Endorsers []Node `yaml:"endorsers"`
Committers []Node `yaml:"committers"`
Expand Down Expand Up @@ -65,7 +77,7 @@ func LoadConfig(f string) (Config, error) {
return config, nil
}

func (c Config) LoadCrypto() (*Crypto, error) {
func (c Config) LoadCrypto() (*CryptoImpl, error) {
var allcerts []string
for _, p := range c.Endorsers {
allcerts = append(allcerts, p.TLSCACert)
Expand Down Expand Up @@ -98,7 +110,7 @@ func (c Config) LoadCrypto() (*Crypto, error) {
return nil, errors.Wrapf(err, "error get msp id")
}

return &Crypto{
return &CryptoImpl{
Creator: name,
PrivKey: priv,
SignCert: cert,
Expand Down Expand Up @@ -136,3 +148,34 @@ func (n *Node) loadConfig() error {
n.TLSCARootByte = TLSCARoot
return nil
}

func GetPrivateKey(f string) (*ecdsa.PrivateKey, error) {
in, err := ioutil.ReadFile(f)
if err != nil {
return nil, err
}

k, err := utils.PEMtoPrivateKey(in, []byte{})
if err != nil {
return nil, err
}

key, ok := k.(*ecdsa.PrivateKey)
if !ok {
return nil, errors.Errorf("expecting ecdsa key")
}

return key, nil
}

func GetCertificate(f string) (*x509.Certificate, []byte, error) {
in, err := ioutil.ReadFile(f)
if err != nil {
return nil, nil, err
}

block, _ := pem.Decode(in)

c, err := x509.ParseCertificate(block.Bytes)
return c, in, err
}
18 changes: 9 additions & 9 deletions pkg/infra/config_test.go → pkg/infra/basic/config_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package infra_test
package basic_test

import (
"io/ioutil"
"os"
"text/template"

"tape/pkg/infra"
"tape/pkg/infra/basic"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
Expand Down Expand Up @@ -75,16 +75,16 @@ var _ = Describe("Config", func() {

generateConfigFile(f.Name(), struct{ TlsFile string }{tlsFile.Name()})

c, err := infra.LoadConfig(f.Name())
c, err := basic.LoadConfig(f.Name())
Expect(err).NotTo(HaveOccurred())
Expect(c).To(Equal(infra.Config{
Endorsers: []infra.Node{
Expect(c).To(Equal(basic.Config{
Endorsers: []basic.Node{
{Addr: "peer0.org1.example.com:7051", TLSCACert: tlsFile.Name(), TLSCACertByte: []byte("a")},
{Addr: "peer0.org2.example.com:7051", TLSCACert: tlsFile.Name(), TLSCACertByte: []byte("a")},
},
Committers: []infra.Node{{Addr: "peer0.org2.example.com:7051", TLSCACert: tlsFile.Name(), TLSCACertByte: []byte("a")}},
Committers: []basic.Node{{Addr: "peer0.org2.example.com:7051", TLSCACert: tlsFile.Name(), TLSCACertByte: []byte("a")}},
CommitThreshold: 1,
Orderer: infra.Node{Addr: "orderer.example.com:7050", TLSCACert: tlsFile.Name(), TLSCACertByte: []byte("a")},
Orderer: basic.Node{Addr: "orderer.example.com:7050", TLSCACert: tlsFile.Name(), TLSCACertByte: []byte("a")},
Channel: "mychannel",
Chaincode: "mycc",
Version: "",
Expand All @@ -102,7 +102,7 @@ var _ = Describe("Config", func() {

Context("bad", func() {
It("fails to load missing config file", func() {
_, err := infra.LoadConfig("invalid_file")
_, err := basic.LoadConfig("invalid_file")
Expect(err).Should(MatchError(ContainSubstring("invalid_file")))
})

Expand All @@ -113,7 +113,7 @@ var _ = Describe("Config", func() {

generateConfigFile(f.Name(), struct{ TlsFile string }{"invalid_file"})

_, err := infra.LoadConfig(f.Name())
_, err := basic.LoadConfig(f.Name())
Expect(err).Should(MatchError(ContainSubstring("invalid_file")))
})
})
Expand Down
72 changes: 72 additions & 0 deletions pkg/infra/basic/crypto.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package basic

import (
"crypto/ecdsa"
"crypto/rand"
"crypto/sha256"
"crypto/x509"
"encoding/asn1"
"math/big"

"tape/internal/fabric/bccsp/utils"
"tape/internal/fabric/common/crypto"

"github.com/hyperledger/fabric-protos-go/common"
)

type CryptoConfig struct {
MSPID string
PrivKey string
SignCert string
TLSCACerts []string
}

type ECDSASignature struct {
R, S *big.Int
}

type CryptoImpl struct {
Creator []byte
PrivKey *ecdsa.PrivateKey
SignCert *x509.Certificate
}

func (s *CryptoImpl) Sign(message []byte) ([]byte, error) {
ri, si, err := ecdsa.Sign(rand.Reader, s.PrivKey, digest(message))
if err != nil {
return nil, err
}

si, _, err = utils.ToLowS(&s.PrivKey.PublicKey, si)
if err != nil {
return nil, err
}

return asn1.Marshal(ECDSASignature{ri, si})
}

func (s *CryptoImpl) Serialize() ([]byte, error) {
return s.Creator, nil
}

func (s *CryptoImpl) NewSignatureHeader() (*common.SignatureHeader, error) {
creator, err := s.Serialize()
if err != nil {
return nil, err
}
nonce, err := crypto.GetRandomNonce()
if err != nil {
return nil, err
}

return &common.SignatureHeader{
Creator: creator,
Nonce: nonce,
}, nil
}

func digest(in []byte) []byte {
h := sha256.New()
h.Write(in)
return h.Sum(nil)
}
Loading

0 comments on commit a8e4cf2

Please sign in to comment.