storage.cpp 5.2 KB
Newer Older
1 2 3 4
/**
 *  @file
 *  @copyright defined in eos/LICENSE.txt
 */
5 6 7
#include "storage.hpp"

namespace TOKEN_NAME {
8 9 10
   void store_account( account_name name, const account& account_to_store ) {
      if ( account_to_store.is_empty() ) {
         accounts::remove( account_to_store, name );
11
      } else {
12
         accounts::store(account_to_store, name );
13 14 15
      } 
   }

16
   void apply_storage_transfer( const TOKEN_NAME::transfer& transfer_storage ) {
17
      eosio::require_recipient( transfer_storage.to, transfer_storage.from );
P
Pravin 已提交
18
      eosio::require_auth( transfer_storage.from );
19

20 21
      account from = get_account( transfer_storage.from );
      account to   = get_account( transfer_storage.to );
22

23 24
      from.balance -= transfer_storage.quantity; /// token subtraction has underflow assertion
      to.balance   += transfer_storage.quantity; /// token addition has overflow assertion
25

26 27
      store_account( transfer_storage.from, from );
      store_account( transfer_storage.to, to );
28 29 30 31 32 33 34 35 36 37 38 39
   }

   bool validate_ipfspath( const char* ipfspath, uint32_t len ) {
      // To be implemented
      return true;  
   }

   bool validate_eospath( const char* eospath, uint32_t len ) {
      // To be implemented
      return true;
   }

40 41
   uint32_t read_link_from_buffer( const char* buffer, uint32_t bufferlen,
                                TOKEN_NAME::link& link_to_read, uint32_t& eospathlen, uint32_t ipfspathlen ) {
42 43 44 45 46
      // To be implemented
      return 0;
   }
 
   void apply_storage_setlink() {
47
      TOKEN_NAME::link link_to_set;
48 49 50
      uint32_t eospathlen;
      uint32_t ipfspathlen;
      char tmp[4098];
51
      auto bufferlen = read_action(tmp, 4098);
52
      auto linklen = read_link_from_buffer( tmp, bufferlen, link_to_set, eospathlen, ipfspathlen );
53
      eosio::require_recipient( link_to_set.owner );
P
Pravin 已提交
54
      eosio::require_auth( link_to_set.owner );
55 56
      validate_ipfspath( link_to_set.ipfspath, ipfspathlen );
      validate_eospath( link_to_set.eospath, eospathlen );
57
      ::store_str( current_receiver(), N(storage), link_to_set.eospath, eospathlen, (char*)&link_to_set, linklen );
58 59 60 61
   }
   
   void apply_storage_removelink( char* eospath, uint32_t eospathlen ) {
      char tmp[4098];
62
      auto len = ::load_str( current_receiver(), current_receiver(), N(storage), eospath, eospathlen, tmp, 4098 );
63
      TOKEN_NAME::link link_to_remove;
64
      uint32_t ipfspathlen;
65
      len = read_link_from_buffer( tmp, len, link_to_remove, eospathlen, ipfspathlen );
P
Pravin 已提交
66
      eosio::require_auth( link_to_remove.owner );
67
      uint32_t stake = link_to_remove.stake;
68
      ::remove_str( current_receiver(), N(storage), link_to_remove.eospath, eospathlen );
69
      // Reduce Quota usage in account table
70 71 72 73 74
      // How does producer know to free cached file?
   }
   
   void apply_storage_createstore( char* eospath, uint32_t eospathlen ) {
      char tmp[4098];
75
      auto len = ::load_str( current_receiver(), current_receiver(), N(storage), eospath, eospathlen, tmp, 4098 );
76
      TOKEN_NAME::link link_to_create;
77
      uint32_t ipfspathlen;
78
      len = read_link_from_buffer( tmp, len, link_to_create, eospathlen, ipfspathlen );
79
      
P
Pravin 已提交
80
      // eosio::require_auth( producer )
81
      // How do we validate the require_auth() is a producer?
82
      // logic goes here to reduce number of tokens and increase quote used using bancor algorithm
83
      link_to_create.accept = 1;
84
      ::store_str( current_receiver(), N(storage), link_to_create.eospath, eospathlen, (char*)&link_to_create, len );
85 86 87 88
   }
   
   void apply_storage_rejectstore( char* eospath, uint32_t eospathlen ) {
      char tmp[4098];
89
      auto len = ::load_str( current_receiver(), current_receiver(), N(storage), eospath, eospathlen, tmp, 4098 );
90
      TOKEN_NAME::link link_to_reject;
91
      uint32_t ipfspathlen;
92
      len = read_link_from_buffer( tmp, len, link_to_reject, eospathlen, ipfspathlen );
P
Pravin 已提交
93
      // eosio::require_auth( producer )
94
      // How do we validate the require_auth() is a producer?
95
      link_to_reject.accept = 0;
96
      ::store_str( current_receiver(), N(storage), link_to_reject.eospath, eospathlen, (char*)&link_to_reject, len );
97 98 99 100 101 102 103 104 105 106 107
   }
}  // namespace TOKEN_NAME

using namespace TOKEN_NAME;

extern "C" {

    /// The apply method implements the dispatch of events to this contract
    void apply( uint64_t code, uint64_t action ) {
       if( code == N(storage) ) {
          if( action == N(transfer) ) {
108
               TOKEN_NAME::apply_storage_transfer( eosio::current_action< TOKEN_NAME::transfer >() );
109 110 111 112
          } else if (action == N(setlink) ) {
               TOKEN_NAME::apply_storage_setlink(); 
          } else if (action == N(removelink) ) {
               char tmp[1025];
113
               auto len = read_action( tmp, 1025 );
114 115 116
               TOKEN_NAME::apply_storage_removelink( tmp, len );
          } else if (action == N(acceptstore) ) {
               char tmp[1025];
117
               auto len = read_action( tmp, 1025 );
118 119 120
               TOKEN_NAME::apply_storage_createstore( tmp, len );
          } else if (action == N(rejectstore) ) {
               char tmp[1025];
121
               auto len = read_action( tmp, 1025 );
122 123
               TOKEN_NAME::apply_storage_rejectstore( tmp, len );
          } else {
124
               eos_assert(0, "unknown message");
125 126
          }
       } else {
127
           eos_assert(0, "unknown code");
128 129 130
       }
    }
}