/
deletePrune.go
69 lines (56 loc) · 1.54 KB
/
deletePrune.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
Copyright 2020 IBM All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package functions
import (
"fmt"
"os"
"path/filepath"
"github.com/hyperledger/fabric-sdk-go/pkg/core/config"
"github.com/hyperledger/fabric-sdk-go/pkg/gateway"
)
// DeletePrune deletes or prunes a variable
func DeletePrune(function, variableName string) ([]byte, error) {
err := os.Setenv("DISCOVERY_AS_LOCALHOST", "true")
if err != nil {
return nil, fmt.Errorf("error setting DISCOVERY_AS_LOCALHOST environemnt variable: %v", err)
}
wallet, err := gateway.NewFileSystemWallet("wallet")
if err != nil {
return nil, fmt.Errorf("failed to create wallet: %v", err)
}
if !wallet.Exists("appUser") {
err := populateWallet(wallet)
if err != nil {
return nil, fmt.Errorf("failed to populate wallet contents: %v", err)
}
}
ccpPath := filepath.Join(
"..",
"..",
"test-network",
"organizations",
"peerOrganizations",
"org1.example.com",
"connection-org1.yaml",
)
gw, err := gateway.Connect(
gateway.WithConfig(config.FromFile(filepath.Clean(ccpPath))),
gateway.WithIdentity(wallet, "appUser"),
)
if err != nil {
return nil, fmt.Errorf("failed to connect to gateway: %v", err)
}
defer gw.Close()
network, err := gw.GetNetwork("mychannel")
if err != nil {
return nil, fmt.Errorf("failed to get network: %v", err)
}
contract := network.GetContract("bigdatacc")
result, err := contract.SubmitTransaction(function, variableName)
if err != nil {
return result, fmt.Errorf("failed to Submit transaction: %v", err)
}
return result, err
}