Skip to content

improvements #690

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/fabric/fabricdev/core/fabricdev/vault/vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (p *populator) Populate(rws *vault.ReadWriteSet, rwsetBytes []byte, namespa
// marshaller is the custom marshaller for FabricDEV
type marshaller struct{}

func (m *marshaller) Marshal(rws *vault.ReadWriteSet) ([]byte, error) {
func (m *marshaller) Marshal(txID string, rws *vault.ReadWriteSet) ([]byte, error) {
panic("implement me")
}

Expand Down
14 changes: 7 additions & 7 deletions platform/common/core/generic/vault/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ func TTestShardLikeCommit(t *testing.T, ddb VersionedPersistence, vp artifactsPr
rwsb.WriteSet.Add(ns, k1, []byte("k1FromTxidInvalid"))
rwsb.WriteSet.Add(ns, k2, []byte("k2FromTxidInvalid"))
marshaller := vp.NewMarshaller()
rwsBytes, err := marshaller.Marshal(rwsb)
rwsBytes, err := marshaller.Marshal("pineapple", rwsb)
assert.NoError(t, err)

// give it to the kvs and check whether it's valid - it won't be
Expand Down Expand Up @@ -425,7 +425,7 @@ func TTestShardLikeCommit(t *testing.T, ddb VersionedPersistence, vp artifactsPr
rwsb.ReadSet.Add(ns, k2, versionBlockTxNumToBytes(37, 3))
rwsb.WriteSet.Add(ns, k1, []byte("k1FromTxidValid"))
rwsb.WriteSet.Add(ns, k2, []byte("k2FromTxidValid"))
rwsBytes, err = marshaller.Marshal(rwsb)
rwsBytes, err = marshaller.Marshal("pineapple", rwsb)
assert.NoError(t, err)

// give it to the kvs and check whether it's valid - it will be
Expand Down Expand Up @@ -482,7 +482,7 @@ func TTestVaultErr(t *testing.T, ddb VersionedPersistence, vp artifactsProvider)
}
rws.ReadSet.Add("pineapple", "key", versionBlockTxNumToBytes(35, 1))
m := vp.NewMarshaller()
rwsBytes, err := m.Marshal(rws)
rwsBytes, err := m.Marshal("pineapple", rws)
assert.NoError(t, err)

ncrwset, err := vault1.NewRWSet("not-closed")
Expand Down Expand Up @@ -563,7 +563,7 @@ func TTestMerge(t *testing.T, ddb VersionedPersistence, vp artifactsProvider) {
rwsb.WriteSet.Add(ns, k1, []byte("newv1"))
rwsb.MetaWriteSet.Add(ns, k1, map[string][]byte{"k1": []byte("v1")})
m := vp.NewMarshaller()
rwsBytes, err := m.Marshal(rwsb)
rwsBytes, err := m.Marshal("pineapple", rwsb)
assert.NoError(t, err)

err = rws.AppendRWSet(rwsBytes)
Expand Down Expand Up @@ -593,7 +593,7 @@ func TTestMerge(t *testing.T, ddb VersionedPersistence, vp artifactsProvider) {
},
}
rwsb.ReadSet.Add(ns, k1, versionBlockTxNumToBytes(36, 1))
rwsBytes, err = m.Marshal(rwsb)
rwsBytes, err = m.Marshal("pineapple", rwsb)
assert.NoError(t, err)

err = rws.AppendRWSet(rwsBytes)
Expand All @@ -606,7 +606,7 @@ func TTestMerge(t *testing.T, ddb VersionedPersistence, vp artifactsProvider) {
},
}
rwsb.WriteSet.Add(ns, k2, []byte("v2"))
rwsBytes, err = m.Marshal(rwsb)
rwsBytes, err = m.Marshal("pineapple", rwsb)
assert.NoError(t, err)

err = rws.AppendRWSet(rwsBytes)
Expand All @@ -632,7 +632,7 @@ func TTestMerge(t *testing.T, ddb VersionedPersistence, vp artifactsProvider) {
},
}
rwsb.MetaWriteSet.Add(ns, k3, map[string][]byte{"k": []byte("v")})
rwsBytes, err = m.Marshal(rwsb)
rwsBytes, err = m.Marshal("pineapple", rwsb)
assert.NoError(t, err)

err = rws.AppendRWSet(rwsBytes)
Expand Down
2 changes: 1 addition & 1 deletion platform/common/core/generic/vault/interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ func (i *Interceptor[V]) AppendRWSet(raw []byte, nss ...string) error {
}

func (i *Interceptor[V]) Bytes() ([]byte, error) {
return i.Marshaller.Marshal(&i.Rws)
return i.Marshaller.Marshal(i.TxID, &i.Rws)
}

func (i *Interceptor[V]) Equals(other interface{}, nss ...string) error {
Expand Down
9 changes: 5 additions & 4 deletions platform/common/core/generic/vault/vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ type Populator interface {
}

type Marshaller interface {
Marshal(rws *ReadWriteSet) ([]byte, error)
Marshal(txID string, rws *ReadWriteSet) ([]byte, error)
Append(destination *ReadWriteSet, raw []byte, nss ...string) error
}

Expand Down Expand Up @@ -303,9 +303,10 @@ func (db *Vault[V]) commitRWs(inputs ...commitInput) error {
return err
}

db.logger.Debugf("parse writes")
db.logger.Debugf("parse writes from [%d] inputs", len(inputs))
writes := make(map[driver.Namespace]map[driver.PKey]VersionedValue)
for _, input := range inputs {
for i, input := range inputs {
db.logger.Debugf("input [%d] has [%d] writes", i, len(input.rws.Writes))
for ns, ws := range input.rws.Writes {
vals, err := db.versionBuilder.VersionedValues(input.rws, ns, ws, input.block, input.indexInBloc)
if err != nil {
Expand All @@ -320,7 +321,7 @@ func (db *Vault[V]) commitRWs(inputs ...commitInput) error {
}

if errs := db.storeAllWrites(writes); len(errs) == 0 {
db.logger.Debugf("Successfully stored writes for %d namespaces", len(writes))
db.logger.Debugf("Successfully stored writes [%v] for [%d] namespaces", writes, len(writes))
} else if discarded, err := db.discard("", 0, 0, errs); err != nil {
return errors.Wrapf(err, "failed storing writes")
} else if discarded {
Expand Down
2 changes: 1 addition & 1 deletion platform/common/core/generic/vault/vault_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (p *populator) Populate(rws *ReadWriteSet, rwsetBytes []byte, namespaces ..

type marshaller struct{}

func (m *marshaller) Marshal(rws *ReadWriteSet) ([]byte, error) {
func (m *marshaller) Marshal(txID string, rws *ReadWriteSet) ([]byte, error) {
return json.Marshal(rws)
}

Expand Down
15 changes: 13 additions & 2 deletions platform/fabric/core/generic/config/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@ import (
"time"

driver2 "github.com/hyperledger-labs/fabric-smart-client/platform/common/driver"
"github.com/pkg/errors"

"github.com/hyperledger-labs/fabric-smart-client/platform/fabric/driver"
"github.com/hyperledger-labs/fabric-smart-client/platform/view/services/flogging"
"github.com/hyperledger-labs/fabric-smart-client/platform/view/services/grpc"
"github.com/pkg/errors"
)

const (
Expand All @@ -29,6 +28,8 @@ const (
defaultNumRetries = 3
defaultRetrySleep = 1 * time.Second
defaultCacheSize = 100

GenericDriver = "generic"
)

var logger = flogging.MustGetLogger("fabric-sdk.core.generic.config")
Expand All @@ -44,6 +45,7 @@ var funcTypeMap = map[string]driver.PeerFunctionType{
type Service struct {
driver.Configuration
name string
driver string
prefix string

configuredOrderers int
Expand All @@ -61,6 +63,10 @@ func NewService(configService driver.Configuration, name string, defaultConfig b
if len(prefix) == 0 && !defaultConfig {
return nil, errors.Errorf("configuration for [%s] not found", name)
}
driver := configService.GetString(fmt.Sprintf("fabric.%sdriver", prefix))
if len(driver) == 0 {
driver = GenericDriver
}

tlsEnabled := configService.GetBool(fmt.Sprintf("fabric.%stls.enabled", prefix))
orderers, err := readItems[*grpc.ConnectionConfig](configService, prefix, "orderers")
Expand Down Expand Up @@ -88,6 +94,7 @@ func NewService(configService driver.Configuration, name string, defaultConfig b
return &Service{
Configuration: configService,
name: name,
driver: driver,
prefix: prefix,
configuredOrderers: len(orderers),
orderers: orderers,
Expand Down Expand Up @@ -138,6 +145,10 @@ func (s *Service) NetworkName() string {
return s.name
}

func (s *Service) DriverName() string {
return s.driver
}

func (s *Service) TLSEnabled() bool {
return s.GetBool("tls.enabled")
}
Expand Down
65 changes: 65 additions & 0 deletions platform/fabric/core/generic/msp/driver/mock/config_provider.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion platform/fabric/core/generic/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ var logger = flogging.MustGetLogger("fabric-sdk.core")
type NewChannelFunc = func(network driver.FabricNetworkService, name string, quiet bool) (driver.Channel, error)

type Network struct {
name string
name string
driver string

configService driver.ConfigService
localMembership driver.LocalMembership
Expand Down Expand Up @@ -55,6 +56,7 @@ func NewNetwork(
) (*Network, error) {
return &Network{
name: name,
driver: config.DriverName(),
configService: config,
ChannelMap: map[string]driver.Channel{},
localMembership: localMembership,
Expand All @@ -69,6 +71,10 @@ func (f *Network) Name() string {
return f.name
}

func (f *Network) Driver() string {
return f.driver
}

func (f *Network) Channel(name string) (driver.Channel, error) {
logger.Debugf("Getting Channel [%s]", name)

Expand Down
45 changes: 10 additions & 35 deletions platform/fabric/core/generic/ordering/ordering.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"context"
"sync"

"github.com/hyperledger-labs/fabric-smart-client/platform/fabric/core/generic/fabricutils"
"github.com/hyperledger-labs/fabric-smart-client/pkg/utils/proto"
"github.com/hyperledger-labs/fabric-smart-client/platform/fabric/core/generic/metrics"
"github.com/hyperledger-labs/fabric-smart-client/platform/fabric/driver"
"github.com/hyperledger-labs/fabric-smart-client/platform/view/services/flogging"
Expand Down Expand Up @@ -38,6 +38,7 @@ type Transaction interface {
Proposal() driver.Proposal
ProposalResponses() []driver.ProposalResponse
Bytes() ([]byte, error)
Envelope() (driver.Envelope, error)
}

type TransactionWithEnvelope interface {
Expand Down Expand Up @@ -127,43 +128,17 @@ func (o *Service) SetConsensusType(consensusType ConsensusType) error {
}

func (o *Service) createFabricEndorseTransactionEnvelope(tx Transaction) (*common2.Envelope, error) {
ets, err := o.GetEndorserTransactionService(tx.Channel())
env, err := tx.Envelope()
if err != nil {
return nil, errors.Wrapf(err, "failed getting channel [%s]", tx.Channel())
return nil, errors.Wrapf(err, "failed creating envelope for transaction [%s]", tx.ID())
}
txRaw, err := tx.Bytes()
raw, err := env.Bytes()
if err != nil {
return nil, errors.Wrapf(err, "failed marshalling tx [%s]", tx.ID())
return nil, errors.Wrapf(err, "failed marshalling envelope for transaction [%s]", tx.ID())
}
err = ets.StoreTransaction(tx.ID(), txRaw)
if err != nil {
return nil, errors.Wrap(err, "failed storing tx")
}

// tx contains the proposal and the endorsements, assemble them in a fabric transaction
signerID := tx.Creator()
signer, err := o.SigService.GetSigner(signerID)
if err != nil {
logger.Errorf("signer not found for %s while creating tx envelope for ordering [%s]", signerID.UniqueID(), err)
return nil, errors.Wrapf(err, "signer not found for %s while creating tx envelope for ordering", signerID.UniqueID())
commonEnv := &common2.Envelope{}
if err := proto.Unmarshal(raw, commonEnv); err != nil {
return nil, errors.Wrapf(err, "failed unmarshalling envelope for transaction [%s]", tx.ID())
}
env, err := fabricutils.CreateEndorserSignedTX(&signerWrapper{signerID, signer}, tx.Proposal(), tx.ProposalResponses()...)
if err != nil {
return nil, errors.WithMessage(err, "could not assemble transaction")
}

return env, nil
}

type signerWrapper struct {
creator view.Identity
signer driver.Signer
}

func (s *signerWrapper) Sign(message []byte) ([]byte, error) {
return s.signer.Sign(message)
}

func (s *signerWrapper) Serialize() ([]byte, error) {
return s.creator, nil
return commonEnv, nil
}
11 changes: 11 additions & 0 deletions platform/fabric/core/generic/transaction/pr.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ package transaction

import (
"github.com/hyperledger-labs/fabric-smart-client/pkg/utils/proto"
"github.com/hyperledger-labs/fabric-smart-client/platform/fabric/driver"
"github.com/hyperledger-labs/fabric-smart-client/platform/view/view"
"github.com/hyperledger/fabric-protos-go/peer"
"github.com/hyperledger/fabric/protoutil"
"github.com/pkg/errors"
Expand Down Expand Up @@ -83,3 +85,12 @@ func (p *ProposalResponse) Bytes() ([]byte, error) {
}
return raw, nil
}

func (p *ProposalResponse) VerifyEndorsement(provider driver.VerifierProvider) error {
endorser := view.Identity(p.pr.Endorsement.Endorser)
v, err := provider.GetVerifier(endorser)
if err != nil {
return errors.Wrapf(err, "failed getting verifier for [%s]", endorser)
}
return v.Verify(append(p.Payload(), endorser...), p.EndorserSignature())
}
2 changes: 1 addition & 1 deletion platform/fabric/core/generic/vault/vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ type marshaller struct {
versionMarshaller vault.BlockTxIndexVersionMarshaller
}

func (m *marshaller) Marshal(rws *vault.ReadWriteSet) ([]byte, error) {
func (m *marshaller) Marshal(txID string, rws *vault.ReadWriteSet) ([]byte, error) {
rwsb := rwsetutil.NewRWSetBuilder()

for ns, keyMap := range rws.Reads {
Expand Down
Loading