forked from steem-witnesses/steem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reputation_plugin.cpp
223 lines (173 loc) · 7.21 KB
/
reputation_plugin.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#include <steem/chain/steem_fwd.hpp>
#include <steem/plugins/reputation/reputation_plugin.hpp>
#include <steem/plugins/reputation/reputation_objects.hpp>
#include <steem/chain/util/impacted.hpp>
#include <steem/protocol/config.hpp>
#include <steem/chain/database.hpp>
#include <steem/chain/index.hpp>
#include <steem/chain/account_object.hpp>
#include <steem/chain/comment_object.hpp>
#include <fc/smart_ref_impl.hpp>
#include <fc/thread/thread.hpp>
#include <memory>
namespace steem { namespace plugins { namespace reputation {
using namespace steem::protocol;
namespace detail {
class reputation_plugin_impl
{
public:
reputation_plugin_impl( reputation_plugin& _plugin ) :
_db( appbase::app().get_plugin< steem::plugins::chain::chain_plugin >().db() ),
_self( _plugin ) {}
~reputation_plugin_impl() {}
void pre_operation( const operation_notification& op_obj );
void post_operation( const operation_notification& op_obj );
chain::database& _db;
reputation_plugin& _self;
boost::signals2::connection _pre_apply_operation_conn;
boost::signals2::connection _post_apply_operation_conn;
};
struct pre_operation_visitor
{
reputation_plugin_impl& _plugin;
pre_operation_visitor( reputation_plugin_impl& plugin )
: _plugin( plugin ) {}
typedef void result_type;
template< typename T >
void operator()( const T& )const {}
void operator()( const vote_operation& op )const
{
try
{
auto& db = _plugin._db;
const auto& c = db.get_comment( op.author, op.permlink );
if( db.calculate_discussion_payout_time( c ) == fc::time_point_sec::maximum() ) return;
const auto& cv_idx = db.get_index< comment_vote_index >().indices().get< by_comment_voter >();
auto cv = cv_idx.find( boost::make_tuple( c.id, db.get_account( op.voter ).id ) );
if( cv != cv_idx.end() )
{
auto rep_delta = ( cv->rshares >> 6 );
const auto& rep_idx = db.get_index< reputation_index >().indices().get< by_account >();
auto voter_rep = rep_idx.find( op.voter );
auto author_rep = rep_idx.find( op.author );
if( author_rep != rep_idx.end() )
{
// Rule #1: Must have non-negative reputation to effect another user's reputation
if( voter_rep != rep_idx.end() && voter_rep->reputation < 0 ) return;
// Rule #2: If you are down voting another user, you must have more reputation than them to impact their reputation
if( cv->rshares < 0 && !( voter_rep != rep_idx.end() && voter_rep->reputation > author_rep->reputation - rep_delta ) ) return;
if( rep_delta == author_rep->reputation )
{
db.remove( *author_rep );
}
else
{
db.modify( *author_rep, [&]( reputation_object& r )
{
r.reputation -= ( cv->rshares >> 6 ); // Shift away precision from vests. It is noise
});
}
}
}
}
catch( const fc::exception& e ) {}
}
};
struct post_operation_visitor
{
reputation_plugin_impl& _plugin;
post_operation_visitor( reputation_plugin_impl& plugin )
: _plugin( plugin ) {}
typedef void result_type;
template< typename T >
void operator()( const T& )const {}
void operator()( const vote_operation& op )const
{
try
{
auto& db = _plugin._db;
const auto& comment = db.get_comment( op.author, op.permlink );
if( db.calculate_discussion_payout_time( comment ) == fc::time_point_sec::maximum() )
return;
const auto& cv_idx = db.get_index< comment_vote_index >().indices().get< by_comment_voter >();
auto cv = cv_idx.find( boost::make_tuple( comment.id, db.get_account( op.voter ).id ) );
const auto& rep_idx = db.get_index< reputation_index >().indices().get< by_account >();
auto voter_rep = rep_idx.find( op.voter );
auto author_rep = rep_idx.find( op.author );
// Rules are a plugin, do not effect consensus, and are subject to change.
// Rule #1: Must have non-negative reputation to effect another user's reputation
if( voter_rep != rep_idx.end() && voter_rep->reputation < 0 ) return;
if( author_rep == rep_idx.end() )
{
// Rule #2: If you are down voting another user, you must have more reputation than them to impact their reputation
// User rep is 0, so requires voter having positive rep
if( cv->rshares < 0 && !( voter_rep != rep_idx.end() && voter_rep->reputation > 0 )) return;
db.create< reputation_object >( [&]( reputation_object& r )
{
r.account = op.author;
r.reputation = ( cv->rshares >> 6 ); // Shift away precision from vests. It is noise
});
}
else
{
// Rule #2: If you are down voting another user, you must have more reputation than them to impact their reputation
if( cv->rshares < 0 && !( voter_rep != rep_idx.end() && voter_rep->reputation > author_rep->reputation ) ) return;
db.modify( *author_rep, [&]( reputation_object& r )
{
r.reputation += ( cv->rshares >> 6 ); // Shift away precision from vests. It is noise
});
}
}
FC_CAPTURE_AND_RETHROW()
}
};
void reputation_plugin_impl::pre_operation( const operation_notification& note )
{
try
{
note.op.visit( pre_operation_visitor( *this ) );
}
catch( const fc::assert_exception& )
{
if( _db.is_producing() ) throw;
}
}
void reputation_plugin_impl::post_operation( const operation_notification& note )
{
try
{
note.op.visit( post_operation_visitor( *this ) );
}
catch( const fc::assert_exception& )
{
if( _db.is_producing() ) throw;
}
}
} // detail
reputation_plugin::reputation_plugin() {}
reputation_plugin::~reputation_plugin() {}
void reputation_plugin::set_program_options(
boost::program_options::options_description& cli,
boost::program_options::options_description& cfg
)
{}
void reputation_plugin::plugin_initialize( const boost::program_options::variables_map& options )
{
try
{
ilog("Intializing reputation plugin" );
my = std::make_unique< detail::reputation_plugin_impl >( *this );
my->_pre_apply_operation_conn = my->_db.add_pre_apply_operation_handler( [&]( const operation_notification& note ){ my->pre_operation( note ); }, *this, 0 );
my->_post_apply_operation_conn = my->_db.add_post_apply_operation_handler( [&]( const operation_notification& note ){ my->post_operation( note ); }, *this, 0 );
STEEM_ADD_PLUGIN_INDEX(my->_db, reputation_index);
appbase::app().get_plugin< chain::chain_plugin >().report_state_options( name(), fc::variant_object() );
}
FC_CAPTURE_AND_RETHROW()
}
void reputation_plugin::plugin_startup() {}
void reputation_plugin::plugin_shutdown()
{
chain::util::disconnect_signal( my->_pre_apply_operation_conn );
chain::util::disconnect_signal( my->_post_apply_operation_conn );
}
} } } // steem::plugins::reputation