Last active
October 15, 2018 13:58
-
-
Save tbfleming/253e84d14e13e713917aac68e737d48f to your computer and use it in GitHub Desktop.
cib eos demo: self-destruct
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"structs": [{ | |
"name": "selfdestruct", | |
"base": "", | |
"fields": [ | |
{"name":"user", "type":"account_name"} | |
] | |
},{ | |
"name": "countdown", | |
"base": "", | |
"fields": [ | |
{"name":"count", "type":"uint32"} | |
] | |
}], | |
"actions": [{ | |
"name": "selfdestruct", | |
"type": "selfdestruct" | |
},{ | |
"name": "countdown", | |
"type": "countdown" | |
}] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <eosiolib/eosio.hpp> | |
#include <eosiolib/transaction.hpp> | |
uint8_t deadcode[] = { | |
0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x0a, 0x02, 0x60, 0x03, 0x7e, 0x7e, 0x7e, | |
0x00, 0x60, 0x00, 0x00, 0x02, 0x0d, 0x01, 0x03, 0x65, 0x6e, 0x76, 0x05, 0x61, 0x62, 0x6f, 0x72, | |
0x74, 0x00, 0x01, 0x03, 0x02, 0x01, 0x00, 0x04, 0x04, 0x01, 0x70, 0x00, 0x01, 0x05, 0x03, 0x01, | |
0x00, 0x01, 0x07, 0x09, 0x01, 0x05, 0x61, 0x70, 0x70, 0x6c, 0x79, 0x00, 0x01, 0x09, 0x06, 0x01, | |
0x00, 0x41, 0x01, 0x0b, 0x00, 0x0a, 0x07, 0x01, 0x05, 0x00, 0x10, 0x00, 0x00, 0x0b, 0x00, 0x15, | |
0x04, 0x6e, 0x61, 0x6d, 0x65, 0x01, 0x0e, 0x02, 0x00, 0x08, 0x69, 0x6d, 0x70, 0x6f, 0x72, 0x74, | |
0x24, 0x30, 0x01, 0x01, 0x31, | |
}; | |
struct setcode { | |
uint64_t account_name; | |
uint8_t vmtype{0}; | |
uint8_t vmversion{0}; | |
eosio::bytes code{std::begin(deadcode), std::end(deadcode)}; | |
}; | |
struct countdown_s { | |
int count; | |
}; | |
template<typename Action> | |
void send(uint64_t self, uint64_t sender_id, uint64_t account, uint64_t name, Action action) { | |
eosio::transaction t; | |
t.expiration = ::now() + 10; | |
t.region = 0; | |
t.ref_block_num = ::tapos_block_num(); | |
t.ref_block_prefix = ::tapos_block_prefix(); | |
t.actions.push_back( | |
eosio::action{ | |
eosio::permission_level{self, N(active)}, | |
account, name, std::move(action)}); | |
t.send(sender_id, self); | |
} | |
struct destruct : eosio::contract { | |
destruct(account_name self) : contract{self} {} | |
void selfdestruct(account_name user) { | |
require_auth(user); | |
std::string s(60, '5'); | |
eosio::print(s.c_str(), "\n"); | |
eosio::print("5 User ", eosio::name{user}, " is someone.\n"); | |
eosio::print("5 Therefore user ", eosio::name{user}, " is authorized.\n"); | |
eosio::print("5 This contract will self-destruct in 5 blocks.\n"); | |
eosio::print(s.c_str()); | |
send(_self, 4, _self, N(countdown), countdown_s{4}); | |
} | |
void countdown(int count) { | |
require_auth(_self); | |
std::string s(60, '0' + count); | |
eosio::print(s.c_str(), "\n"); | |
eosio::print(s.c_str(), "\n"); | |
eosio::print(s.c_str(), "\n"); | |
if(count > 1) | |
send(_self, count - 1, _self, N(countdown), countdown_s{count - 1}); | |
else | |
send(_self, count - 1, N(eosio), N(setcode), setcode{_self}); | |
} | |
}; | |
EOSIO_ABI(destruct, (selfdestruct)(countdown)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"actions": [{ | |
"account": "test", | |
"name": "selfdestruct", | |
"authorization": [{ | |
"actor": "test", | |
"permission": "active" | |
}], | |
"data": { | |
"user": "test" | |
} | |
}] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment