|
| 1 | +#include <string> |
| 2 | +#include "logging.h" |
| 3 | +#include "shim.h" |
| 4 | + |
| 5 | +const std::string SEP = "."; |
| 6 | +const std::string PREFIX = SEP + "somePrefix" + SEP; |
| 7 | + |
| 8 | +std::string store(std::string auction_name, std::string bidder_name, int value, shim_ctx_ptr_t ctx) |
| 9 | +{ |
| 10 | + std::string new_key(PREFIX + auction_name + SEP + bidder_name + SEP); |
| 11 | + put_public_state(new_key.c_str(), (uint8_t*)&value, sizeof(int), ctx); |
| 12 | + return "OK"; |
| 13 | +} |
| 14 | + |
| 15 | +std::string retrieve(std::string auction_name, shim_ctx_ptr_t ctx) |
| 16 | +{ |
| 17 | + std::map<std::string, std::string> values; |
| 18 | + std::string bid_composite_key = PREFIX + auction_name + SEP; |
| 19 | + get_public_state_by_partial_composite_key(bid_composite_key.c_str(), values, ctx); |
| 20 | + return "STILL_ALIVE"; |
| 21 | +} |
| 22 | + |
| 23 | +int invoke( |
| 24 | + uint8_t* response, uint32_t max_response_len, uint32_t* actual_response_len, shim_ctx_ptr_t ctx) |
| 25 | +{ |
| 26 | + LOG_DEBUG("[+] +++ Executing chaincode invocation +++"); |
| 27 | + |
| 28 | + std::string function_name; |
| 29 | + std::vector<std::string> params; |
| 30 | + get_func_and_params(function_name, params, ctx); |
| 31 | + std::string result; |
| 32 | + |
| 33 | + if (function_name == "store") |
| 34 | + result = store(params[0], params[1], std::stoi(params[2]), ctx); |
| 35 | + else if (function_name == "retrieve") |
| 36 | + result = retrieve(params[0], ctx); |
| 37 | + |
| 38 | + int neededSize = result.size(); |
| 39 | + if (max_response_len < neededSize) |
| 40 | + { |
| 41 | + LOG_DEBUG("[+] Response buffer too small"); |
| 42 | + *actual_response_len = 0; |
| 43 | + return -1; |
| 44 | + } |
| 45 | + |
| 46 | + memcpy(response, result.c_str(), neededSize); |
| 47 | + *actual_response_len = neededSize; |
| 48 | + LOG_DEBUG("[+] Response: %s", result.c_str()); |
| 49 | + LOG_DEBUG("[+] +++ Executing done +++"); |
| 50 | + return 0; |
| 51 | +} |
0 commit comments