From 3af877e967ff85aea8b34bee33910d17c3bd64f5 Mon Sep 17 00:00:00 2001 From: shobhitJava Date: Fri, 5 May 2017 14:08:34 +0530 Subject: [PATCH 1/4] Create Test --- Test | 1 + 1 file changed, 1 insertion(+) create mode 100644 Test diff --git a/Test b/Test new file mode 100644 index 000000000..57ddad2ae --- /dev/null +++ b/Test @@ -0,0 +1 @@ +\ From d2d6d68895fd74436754b41a7241c55fc179ca25 Mon Sep 17 00:00:00 2001 From: shobhitJava Date: Fri, 5 May 2017 15:50:03 +0530 Subject: [PATCH 2/4] added policy file --- policy.go | 166 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 166 insertions(+) create mode 100644 policy.go diff --git a/policy.go b/policy.go new file mode 100644 index 000000000..0caebd579 --- /dev/null +++ b/policy.go @@ -0,0 +1,166 @@ +package main + +import ( + + "fmt" + "errors" + "github.com/hyperledger/fabric/core/chaincode/shim" + "encoding/json" +) + +type Policy struct{ + +} +type PolicyDetails struct { + FirstName string + LastName string + VehicleNumber string + Make string + Model string + RegNo string + RegState string + ECC string +} + +func main() { + err := shim.Start(new(Policy)) + if err != nil { + fmt.Printf("Error starting Simple chaincode: %s", err) + } +} + + +func (t *Policy) Init(stub shim.ChaincodeStubInterface, function string, args []string) ([]byte, error) { + var msg string + fmt.Print("inside init method" + function) + if len(args) <= 1 { + return nil, errors.New("Incorrect number of arguments. Expecting more then 1") + } + +u:=PolicyDetails{} +u.FirstName=args[0] +u.LastName=args[1] +u.VehicleNumber=args[2] +u.Make=args[3] +u.Model=args[4] +//u.RegNo=args[5] +//u.RegState=args[6] +//u.ECC=args[7] + + +json_byte, err:=json.Marshal(u); + //hardcoded the key since not using the DB + err = stub.PutState(u.VehicleNumber, json_byte) + if err != nil { + msg="UnSuccesful" + return []byte(msg), err + } + msg="Success" + return []byte(msg), nil +} + +func (t *Policy) Invoke(stub shim.ChaincodeStubInterface, function string, args []string) ([]byte, error) { + fmt.Println("invoke is running " + function) + // Handle different functions + + fmt.Println("invoke did not find func: " + function) + + return nil, errors.New("Received unknown function invocation: " + function) + + +} + +// Query is our entry point for queries +func (t *Policy) Query(stub shim.ChaincodeStubInterface, function string, args []string) ([]byte, error) { + fmt.Println("query is running " + function) + + // Handle different functions + if function == "getPolicy" { + return t.getPolicy(stub, args) + } else if function == "updatePolicy" { + return t.updatePolicy(stub, args) + } + + fmt.Println("query did not find func: " + function) + + return nil, errors.New("Received unknown function query: " + function) +} + + +// read - query function to read key/value pair +func (t *Policy) read(stub shim.ChaincodeStubInterface, args []string) ([]byte, error) { + var key, jsonResp string + var err error + + if len(args) != 1 { + return nil, errors.New("Incorrect number of arguments. Expecting name of the key to query") + } + + key = args[0] + valAsbytes, err := stub.GetState(key) + if err != nil { + jsonResp = "{\"Error\":\"Failed to get state for " + key + "\"}" + return nil, errors.New(jsonResp) + } + return valAsbytes, nil +} + +func (t *Policy) getPolicy(stub shim.ChaincodeStubInterface, args []string) ([]byte, error) { + var key, jsonResp string + var err error + + if len(args) != 1 { + return nil, errors.New("Incorrect number of arguments. Expecting name of the key to query") + } + + key = args[0] + valAsbytes, err := stub.GetState(key) + if err != nil { + jsonResp = "{\"Error\":\"Failed to get state for " + key + "\"}" + return nil, errors.New(jsonResp) + } + + return valAsbytes, nil +} + +func (t *Policy) updatePolicy(stub shim.ChaincodeStubInterface, args []string) ([]byte, error) { + var key,key2, jsonResp string + var err error + var msg string + if len(args) <= 1 { + return nil, errors.New("Incorrect number of arguments. Expecting name of the key to query") + } + + key = args[0] + key2= args[1] + fmt.Println(key2+" is the new key2") + valAsbytes, err := stub.GetState(key) + if err != nil { + jsonResp = "{\"Error\":\"Failed to get state for " + key + "\"}" + return nil, errors.New(jsonResp) + } + src_json:=[]byte(valAsbytes) +u := PolicyDetails{} + json.Unmarshal(src_json, &u) + //ponits are hardcoded as of now can be made dynamic by getting value from args + if key2== "firstName" { + u.FirstName=key2 + + + }else if key2=="lastName" { + u.LastName=key2 + + } + json_byte, err:=json.Marshal(u); +if err != nil { + msg="Some error occured" + panic(err) + } + + err = stub.PutState(key, json_byte) + + + return []byte(msg), nil +} + + From 0b7c5154082c6e419829defed6da5e0d4a37a107 Mon Sep 17 00:00:00 2001 From: shobhitJava Date: Fri, 5 May 2017 16:00:09 +0530 Subject: [PATCH 3/4] deleting it --- assignMent.go | 211 -------------------------------------------------- 1 file changed, 211 deletions(-) delete mode 100644 assignMent.go diff --git a/assignMent.go b/assignMent.go deleted file mode 100644 index 9a1e076ff..000000000 --- a/assignMent.go +++ /dev/null @@ -1,211 +0,0 @@ -package main - -import ( - - "fmt" - "errors" - "github.com/hyperledger/fabric/core/chaincode/shim" - "encoding/json" - "strconv" -) - -type UserBenefit struct{ - -} -type User struct { - FfId string - Title string - Gender string - FirstName string - LastName string - Dob string - Email string - Country string - Address string - City string - Zip string - CreatedBy string - TotalPoint string -} - -func main() { - err := shim.Start(new(UserBenefit)) - if err != nil { - fmt.Printf("Error starting Simple chaincode: %s", err) - } -} - - -func (t *UserBenefit) Init(stub shim.ChaincodeStubInterface, function string, args []string) ([]byte, error) { - var msg string - - if len(args) <= 1 { - return nil, errors.New("Incorrect number of arguments. Expecting more then 1") - } - -u:=User{} -u.FfId=args[0] -u.Title=args[1] -u.Gender=args[2] -u.FirstName=args[3] -u.LastName=args[4] -u.Dob=args[5] -u.Email=args[6] -u.Country=args[7] -u.Address=args[8] -u.City=args[9] -u.Zip=args[10] -u.CreatedBy=args[11] -u.TotalPoint=args[12] - -json_byte, err:=json.Marshal(u); - //hardcoded the key since not using the DB - err = stub.PutState("user", json_byte) - if err != nil { - msg="UnSuccesful" - return []byte(msg), err - } - msg="Success" - return []byte(msg), nil -} - -func (t *UserBenefit) Invoke(stub shim.ChaincodeStubInterface, function string, args []string) ([]byte, error) { - fmt.Println("invoke is running " + function) - // Handle different functions - if function == "addDelete" { - return t.addDeletePoints(stub,args) - } - fmt.Println("invoke did not find func: " + function) - - return nil, errors.New("Received unknown function invocation: " + function) -} - -// Query is our entry point for queries -func (t *UserBenefit) Query(stub shim.ChaincodeStubInterface, function string, args []string) ([]byte, error) { - fmt.Println("query is running " + function) - - // Handle different functions - if function == "read" { - return t.read(stub, args) - } else if function == "getPoints" { - return t.getPoints(stub, args) - } else if function == "getUser"{ - return t.getUser(stub, args) - } - - fmt.Println("query did not find func: " + function) - - return nil, errors.New("Received unknown function query: " + function) -} - - -// read - query function to read key/value pair -func (t *UserBenefit) read(stub shim.ChaincodeStubInterface, args []string) ([]byte, error) { - var key, jsonResp string - var err error - - if len(args) != 1 { - return nil, errors.New("Incorrect number of arguments. Expecting name of the key to query") - } - - key = args[0] - valAsbytes, err := stub.GetState(key) - if err != nil { - jsonResp = "{\"Error\":\"Failed to get state for " + key + "\"}" - return nil, errors.New(jsonResp) - } - src_json:=[]byte(valAsbytes) - u := User{} - json.Unmarshal(src_json, &u) - point:=[]byte(u.TotalPoint) - - - return point, nil -} - -func (t *UserBenefit) getPoints(stub shim.ChaincodeStubInterface, args []string) ([]byte, error) { - var key string - - var err error -fmt.Print("hi in get Points") - if len(args) != 1 { - return nil, errors.New("Incorrect number of arguments. Expecting name of the key to query") - } - - key = args[0] - valAsbytes, err := stub.GetState(key) - if err != nil { - jsonResp := "{\"Error\":\"Failed to get state for " + key + "\"}" - return nil, errors.New(jsonResp) - } - src_json:=[]byte(valAsbytes) - u := User{} - json.Unmarshal(src_json, &u) - jsonResp := []byte("{\"TotalPoints\":\"" + u.TotalPoint +"\"}") - - - return jsonResp, nil -} -func (t *UserBenefit) getUser(stub shim.ChaincodeStubInterface, args []string) ([]byte, error) { - var key, jsonResp string - var err error - - if len(args) != 1 { - return nil, errors.New("Incorrect number of arguments. Expecting name of the key to query") - } - - key = args[0] - valAsbytes, err := stub.GetState("user") - if err != nil { - jsonResp = "{\"Error\":\"Failed to get state for " + key + "\"}" - return nil, errors.New(jsonResp) - } - - return valAsbytes, nil -} - -func (t *UserBenefit) addDeletePoints(stub shim.ChaincodeStubInterface, args []string) ([]byte, error) { - var key,key2, jsonResp string - var err error - var msg string - if len(args) <= 1 { - return nil, errors.New("Incorrect number of arguments. Expecting name of the key to query") - } - - key = args[0] - key2= args[1] - fmt.Println(key2+" is the new key2") - valAsbytes, err := stub.GetState("user") - if err != nil { - jsonResp = "{\"Error\":\"Failed to get state for " + key + "\"}" - return nil, errors.New(jsonResp) - } - src_json:=[]byte(valAsbytes) -u := User{} - json.Unmarshal(src_json, &u) - //ponits are hardcoded as of now can be made dynamic - if key2== "add" { - i, n := strconv.Atoi(u.TotalPoint) - if n != nil { - msg="Points not added" - panic(n) - } - i=i+20 - u.TotalPoint=strconv.Itoa(i) - }else if key2=="delete" { - i, n := strconv.Atoi(u.TotalPoint) - if n != nil { - msg="Point not substracted" - panic(n) - } - i=i-20 - u.TotalPoint=strconv.Itoa(i)} - json_byte, err:=json.Marshal(u); - - fmt.Println(u.TotalPoint+" the total point") - err = stub.PutState("user", json_byte) - return []byte(msg), nil -} - - - From bb209d29ead5528c7bc1eeb9f395bcb00a81f1df Mon Sep 17 00:00:00 2001 From: shobhitJava Date: Sat, 6 May 2017 10:23:10 +0530 Subject: [PATCH 4/4] new policy --- policy.go | 119 ++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 76 insertions(+), 43 deletions(-) diff --git a/policy.go b/policy.go index 0caebd579..d78215950 100644 --- a/policy.go +++ b/policy.go @@ -12,14 +12,20 @@ type Policy struct{ } type PolicyDetails struct { - FirstName string - LastName string + FirstName string + LastName string VehicleNumber string - Make string - Model string + Make string + Model string + ManYear string RegNo string RegState string - ECC string + ECC string + Status string + MetroInurance string + AvonInurance string + BharatiInurance string + } func main() { @@ -32,30 +38,7 @@ func main() { func (t *Policy) Init(stub shim.ChaincodeStubInterface, function string, args []string) ([]byte, error) { var msg string - fmt.Print("inside init method" + function) - if len(args) <= 1 { - return nil, errors.New("Incorrect number of arguments. Expecting more then 1") - } - -u:=PolicyDetails{} -u.FirstName=args[0] -u.LastName=args[1] -u.VehicleNumber=args[2] -u.Make=args[3] -u.Model=args[4] -//u.RegNo=args[5] -//u.RegState=args[6] -//u.ECC=args[7] - - -json_byte, err:=json.Marshal(u); - //hardcoded the key since not using the DB - err = stub.PutState(u.VehicleNumber, json_byte) - if err != nil { - msg="UnSuccesful" - return []byte(msg), err - } - msg="Success" + msg="In side Init" return []byte(msg), nil } @@ -64,7 +47,11 @@ func (t *Policy) Invoke(stub shim.ChaincodeStubInterface, function string, args // Handle different functions fmt.Println("invoke did not find func: " + function) - + if function == "updatePolicy" { + return t.updatePolicy(stub, args) + }else if function=="createPolicy" { + return t.createPolicy(stub, args) + } return nil, errors.New("Received unknown function invocation: " + function) @@ -73,13 +60,12 @@ func (t *Policy) Invoke(stub shim.ChaincodeStubInterface, function string, args // Query is our entry point for queries func (t *Policy) Query(stub shim.ChaincodeStubInterface, function string, args []string) ([]byte, error) { fmt.Println("query is running " + function) + // Handle different functions if function == "getPolicy" { return t.getPolicy(stub, args) - } else if function == "updatePolicy" { - return t.updatePolicy(stub, args) - } + } fmt.Println("query did not find func: " + function) @@ -124,16 +110,16 @@ func (t *Policy) getPolicy(stub shim.ChaincodeStubInterface, args []string) ([]b } func (t *Policy) updatePolicy(stub shim.ChaincodeStubInterface, args []string) ([]byte, error) { - var key,key2, jsonResp string + var key,key2,key3,key4,key5, jsonResp string var err error var msg string - if len(args) <= 1 { - return nil, errors.New("Incorrect number of arguments. Expecting name of the key to query") - } key = args[0] key2= args[1] - fmt.Println(key2+" is the new key2") + key3=args[2] + key4=args[3] + key5=args[4] + valAsbytes, err := stub.GetState(key) if err != nil { jsonResp = "{\"Error\":\"Failed to get state for " + key + "\"}" @@ -143,13 +129,18 @@ func (t *Policy) updatePolicy(stub shim.ChaincodeStubInterface, args []string) ( u := PolicyDetails{} json.Unmarshal(src_json, &u) //ponits are hardcoded as of now can be made dynamic by getting value from args - if key2== "firstName" { - u.FirstName=key2 - + if key2== "status" { + u.Status=key3 + fmt.Println(key2+" is the new key2"+ key+"kjdkfjds"+key3) - }else if key2=="lastName" { - u.LastName=key2 + } + if key4=="MetroInurance" { + u.MetroInurance=key5 + }else if key4=="AvonInurance"{ + u.AvonInurance=key5 + }else if key4=="BharatiInurance"{ + u.BharatiInurance=key5 } json_byte, err:=json.Marshal(u); if err != nil { @@ -158,8 +149,50 @@ if err != nil { } err = stub.PutState(key, json_byte) +if err != nil { + msg="UnSuccesful" + return []byte(msg), err + panic(err) + } + msg="Success" + fmt.Println(key+" is the new key898998 ") + + return []byte(msg), nil +} + +func (t *Policy) createPolicy(stub shim.ChaincodeStubInterface, args []string) ([]byte, error) { + var msg string + if len(args) <= 1 { + return nil, errors.New("Incorrect number of arguments. Expecting more then 1") + } + +u:=PolicyDetails{} +u.FirstName=args[0] +u.LastName=args[1] +u.VehicleNumber=args[2] +u.Make=args[3] +u.Model=args[4] +u.ManYear=args[5] +u.RegNo=args[6] +u.RegState=args[7] +u.ECC=args[8] +u.Status=args[9] +u.MetroInurance=args[10] +u.AvonInurance=args[11] +u.BharatiInurance=args[12] + + +json_byte, err:=json.Marshal(u); + + err = stub.PutState(u.VehicleNumber, json_byte) + if err != nil { + msg="UnSuccesful" + return []byte(msg), err + panic(err) + } + msg="Success" return []byte(msg), nil }