|
| 1 | +/* |
| 2 | +Copyright IBM Corp. 2016 All Rights Reserved. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package example |
| 18 | + |
| 19 | +import ( |
| 20 | + "encoding/json" |
| 21 | + "errors" |
| 22 | + "strconv" |
| 23 | + "strings" |
| 24 | + |
| 25 | + ledger "github.com/hyperledger/fabric/core/ledger" |
| 26 | + "github.com/hyperledger/fabric/protos" |
| 27 | + logging "github.com/op/go-logging" |
| 28 | +) |
| 29 | + |
| 30 | +var logger = logging.MustGetLogger("example") |
| 31 | + |
| 32 | +// App - a sample fund transfer app |
| 33 | +type MarbleApp struct { |
| 34 | + name string |
| 35 | + ledger ledger.ValidatedLedger |
| 36 | +} |
| 37 | + |
| 38 | +// ConstructAppInstance constructs an instance of an app |
| 39 | +func ConstructMarbleAppInstance(ledger ledger.ValidatedLedger) *MarbleApp { |
| 40 | + return &MarbleApp{"marbles_app", ledger} |
| 41 | +} |
| 42 | + |
| 43 | +var marbleIndexStr = "_marbleindex" //name for the key/value that will store a list of all known marbles |
| 44 | +var openTradesStr = "_opentrades" //name for the key/value that will store all open trades |
| 45 | + |
| 46 | +type Marble struct { |
| 47 | + Name string `json:"asset_name"` //the fieldtags are needed to keep case from bouncing around |
| 48 | + Color string `json:"color"` |
| 49 | + Size int `json:"size"` |
| 50 | + User string `json:"owner"` |
| 51 | + Rev string `json:"_rev"` |
| 52 | + Txid string `json:"txid"` |
| 53 | +} |
| 54 | + |
| 55 | +// CreateMarble simulates init transaction |
| 56 | +func (marbleApp *MarbleApp) CreateMarble(args []string) (*protos.Transaction2, error) { |
| 57 | + // 0 1 2 3 |
| 58 | + // "asdf", "blue", "35", "bob" |
| 59 | + logger.Debugf("===COUCHDB=== Entering ----------CreateMarble()----------") |
| 60 | + marbleName := args[0] |
| 61 | + marbleJsonBytes, err := init_marble(args) |
| 62 | + if err != nil { |
| 63 | + return nil, err |
| 64 | + } |
| 65 | + |
| 66 | + var txSimulator ledger.TxSimulator |
| 67 | + if txSimulator, err = marbleApp.ledger.NewTxSimulator(); err != nil { |
| 68 | + return nil, err |
| 69 | + } |
| 70 | + defer txSimulator.Done() |
| 71 | + |
| 72 | + txSimulator.SetState(marbleApp.name, marbleName, marbleJsonBytes) |
| 73 | + |
| 74 | + var txSimulationResults []byte |
| 75 | + if txSimulationResults, err = txSimulator.GetTxSimulationResults(); err != nil { |
| 76 | + return nil, err |
| 77 | + } |
| 78 | + logger.Debugf("===COUCHDB=== CreateMarble() simulation done, packaging into a transaction...") |
| 79 | + tx := constructTransaction(txSimulationResults) |
| 80 | + logger.Debugf("===COUCHDB=== Exiting CreateMarble()") |
| 81 | + return tx, nil |
| 82 | +} |
| 83 | + |
| 84 | +// ============================================================================================================================ |
| 85 | +// Init Marble - create a new marble, store into chaincode state |
| 86 | +// ============================================================================================================================ |
| 87 | +func init_marble(args []string) ([]byte, error) { |
| 88 | + var err error |
| 89 | + |
| 90 | + // 0 1 2 3 |
| 91 | + // "asdf", "blue", "35", "bob" |
| 92 | + if len(args) != 4 { |
| 93 | + return nil, errors.New("Incorrect number of arguments. Expecting 4") |
| 94 | + } |
| 95 | + |
| 96 | + logger.Debugf("===COUCHDB=== Entering init marble") |
| 97 | + if len(args[0]) <= 0 { |
| 98 | + return nil, errors.New("1st argument must be a non-empty string") |
| 99 | + } |
| 100 | + if len(args[1]) <= 0 { |
| 101 | + return nil, errors.New("2nd argument must be a non-empty string") |
| 102 | + } |
| 103 | + if len(args[2]) <= 0 { |
| 104 | + return nil, errors.New("3rd argument must be a non-empty string") |
| 105 | + } |
| 106 | + if len(args[3]) <= 0 { |
| 107 | + return nil, errors.New("4th argument must be a non-empty string") |
| 108 | + } |
| 109 | + |
| 110 | + size, err := strconv.Atoi(args[2]) |
| 111 | + if err != nil { |
| 112 | + return nil, errors.New("3rd argument must be a numeric string") |
| 113 | + } |
| 114 | + |
| 115 | + color := strings.ToLower(args[1]) |
| 116 | + user := strings.ToLower(args[3]) |
| 117 | + |
| 118 | + tx := "tx000000000000001" // COUCHDB hardcode a txid for now for demo purpose |
| 119 | + marbleJson := `{"txid": "` + tx + `", "asset_name": "` + args[0] + `", "color": "` + color + `", "size": ` + strconv.Itoa(size) + `, "owner": "` + user + `"}` |
| 120 | + marbleBytes := []byte(marbleJson) |
| 121 | + |
| 122 | + logger.Debugf("===COUCHDB=== Exiting init marble") |
| 123 | + return marbleBytes, nil |
| 124 | +} |
| 125 | + |
| 126 | +// TransferMarble simulates transfer transaction |
| 127 | +func (marbleApp *MarbleApp) TransferMarble(args []string) (*protos.Transaction2, error) { |
| 128 | + |
| 129 | + // 0 1 |
| 130 | + // "name", "bob" |
| 131 | + if len(args) < 2 { |
| 132 | + return nil, errors.New("Incorrect number of arguments. Expecting 2") |
| 133 | + } |
| 134 | + marbleName := args[0] |
| 135 | + marbleNewOwner := args[1] |
| 136 | + |
| 137 | + logger.Debugf("===COUCHDB=== Entering ----------TransferMarble----------") |
| 138 | + var txSimulator ledger.TxSimulator |
| 139 | + var err error |
| 140 | + if txSimulator, err = marbleApp.ledger.NewTxSimulator(); err != nil { |
| 141 | + return nil, err |
| 142 | + } |
| 143 | + defer txSimulator.Done() |
| 144 | + |
| 145 | + marbleBytes, err := txSimulator.GetState(marbleApp.name, marbleName) |
| 146 | + logger.Debugf("===COUCHDB=== marbleBytes is: %v", marbleBytes) |
| 147 | + if marbleBytes != nil { |
| 148 | + jsonString := string(marbleBytes[:]) |
| 149 | + logger.Debugf("===COUCHDB=== TransferMarble() Retrieved jsonString: \n %s", jsonString) |
| 150 | + } |
| 151 | + |
| 152 | + theMarble := Marble{} |
| 153 | + json.Unmarshal(marbleBytes, &theMarble) //Unmarshal JSON bytes into a Marble struct |
| 154 | + |
| 155 | + logger.Debugf("===COUCHDB=== theMarble after unmarshal: %v", theMarble) |
| 156 | + |
| 157 | + logger.Debugf("===COUCHDB=== Setting the owner to: %s", marbleNewOwner) |
| 158 | + theMarble.User = marbleNewOwner //change the user |
| 159 | + theMarble.Txid = "tx000000000000002" // COUCHDB hardcode a txid for now for demo purpose |
| 160 | + |
| 161 | + updatedMarbleBytes, _ := json.Marshal(theMarble) |
| 162 | + if updatedMarbleBytes != nil { |
| 163 | + updatedJsonString := string(updatedMarbleBytes[:]) |
| 164 | + logger.Debugf("===COUCHDB=== updatedJsonString:\n %s", updatedJsonString) |
| 165 | + } |
| 166 | + err = txSimulator.SetState(marbleApp.name, marbleName, updatedMarbleBytes) |
| 167 | + if err != nil { |
| 168 | + return nil, err |
| 169 | + } |
| 170 | + |
| 171 | + var txSimulationResults []byte |
| 172 | + if txSimulationResults, err = txSimulator.GetTxSimulationResults(); err != nil { |
| 173 | + return nil, err |
| 174 | + } |
| 175 | + logger.Debugf("===COUCHDB=== TransferMarble() simulation done, packaging into a transaction...") |
| 176 | + tx := constructTransaction(txSimulationResults) |
| 177 | + return tx, nil |
| 178 | +} |
0 commit comments