-
Notifications
You must be signed in to change notification settings - Fork 1
/
chaincode.go
89 lines (76 loc) · 2.38 KB
/
chaincode.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package cc
import (
"encoding/hex"
"fmt"
"math/big"
"reflect"
"runtime/debug"
"github.com/anoideaopen/acl/helpers"
"github.com/anoideaopen/acl/internal/config"
"github.com/anoideaopen/acl/proto"
"github.com/hyperledger/fabric-chaincode-go/shim"
"github.com/hyperledger/fabric-protos-go/peer"
)
type (
ACL struct {
adminSKI []byte
config *proto.ACLConfig
}
ccFunc func(stub shim.ChaincodeStubInterface, args []string) peer.Response
)
func New() *ACL {
return &ACL{}
}
// Init - method for initialize chaincode
// args: adminSKI, validatorsCount, validatorBase58Ed25519PublicKey1, ..., validatorBase58Ed25519PublicKeyN
func (c *ACL) Init(stub shim.ChaincodeStubInterface) peer.Response {
if err := config.SetConfig(stub); err != nil {
return shim.Error(err.Error())
}
return shim.Success(nil)
}
type Account struct {
Address string `json:"address"`
Balance *big.Int `json:"balance"`
}
func (c *ACL) Invoke(stub shim.ChaincodeStubInterface) peer.Response {
defer func() {
if r := recover(); r != nil {
fmt.Println("panic invoke\n" + string(debug.Stack()))
}
}()
fn, args := stub.GetFunctionAndParameters()
// Always read the config so that there is no determinism when executing a transaction
// init config begin
cfg, err := config.GetConfig(stub)
if err != nil {
return shim.Error(err.Error())
}
if cfg == nil {
return shim.Error("ACL chaincode not initialized, please invoke Init with init args first")
}
c.config = cfg
adminSKI, err := hex.DecodeString(cfg.AdminSKIEncoded)
if err != nil {
return shim.Error(fmt.Sprintf(config.ErrInvalidAdminSKI, cfg.AdminSKIEncoded))
}
c.adminSKI = adminSKI
// init config end
methods := make(map[string]ccFunc)
t := reflect.TypeOf(c)
var ok bool
for i := 0; i < t.NumMethod(); i++ {
method := t.Method(i)
if method.Name != "Init" && method.Name != "Invoke" {
name := helpers.ToLowerFirstLetter(method.Name)
if methods[name], ok = reflect.ValueOf(c).MethodByName(method.Name).Interface().(func(shim.ChaincodeStubInterface, []string) peer.Response); !ok {
return shim.Error(fmt.Sprintf("Chaincode initialization failure: cc method %s does not satisfy signature func(stub shim.ChaincodeStubInterface, args []string) peer.Response", method.Name))
}
}
}
ccInvoke, ok := methods[fn]
if !ok {
return shim.Error(fmt.Sprintf("unknown method %s in tx %s", fn, stub.GetTxID()))
}
return ccInvoke(stub, args)
}