This repository has been archived by the owner on Mar 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathengine.go
50 lines (42 loc) · 1.62 KB
/
engine.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package main
import (
"context"
"github.com/hyperledger-labs/fabex/db"
"github.com/hyperledger-labs/fabex/helpers"
"github.com/hyperledger-labs/fabex/ledgerclient"
"github.com/hyperledger/fabric-sdk-go/pkg/client/channel"
"github.com/hyperledger/fabric-sdk-go/pkg/client/ledger"
fabctx "github.com/hyperledger/fabric-sdk-go/pkg/common/providers/context"
"github.com/hyperledger/fabric-sdk-go/pkg/fabsdk"
"github.com/pkg/errors"
)
type Engine struct {
db db.Storage
channelClient *channel.Client
ledgerClient *ledgerclient.CustomLedgerClient
channelContext fabctx.ChannelProvider
}
func engineCreator(sdk *fabsdk.FabricSDK, dbInstance db.Storage) func(ch, user, org string) (*Engine, error) {
return func(ch, user, org string) (*Engine, error) {
clientChannelContext := sdk.ChannelContext(ch, fabsdk.WithUser(user), fabsdk.WithOrg(org))
ledgerClient, err := ledger.New(clientChannelContext)
if err != nil {
return nil, errors.WithStack(errors.Wrapf(err, "failed to create ledger client"))
}
channelclient, err := channel.New(clientChannelContext)
if err != nil {
return nil, errors.WithStack(errors.Wrapf(err, "failed to create channel cient"))
}
return &Engine{db: dbInstance, channelClient: channelclient, ledgerClient: &ledgerclient.CustomLedgerClient{Client: ledgerClient}, channelContext: clientChannelContext}, nil
}
}
func (e *Engine) Run(ctx context.Context) error {
ch, err := e.channelContext()
if err != nil {
return errors.WithStack(err)
}
if err := e.db.Init(ch.ChannelID()); err != nil {
return err
}
return helpers.Explore(ctx, e.channelContext, e.db, e.ledgerClient)
}