Skip to content

Instantly share code, notes, and snippets.

@kernelogic
Created October 17, 2021 03:27
Show Gist options
  • Save kernelogic/b87bc83cca77c116a9ddde46cd803c35 to your computer and use it in GitHub Desktop.
Save kernelogic/b87bc83cca77c116a9ddde46cd803c35 to your computer and use it in GitHub Desktop.
whitelist + ratelimit dealfilter
#!/usr/bin/env php
<?php
const RATE_LIMIT = 7200; // in seconds
const JSON_FILE_PATH = "/home/dev/dealfilter.json";
function exitDealReject($reason) {
echo $reason."\n";
exit(1);
}
function exitDealAccept() {
exit(0);
}
function saveJsonToFile($data){
$jsonstring = json_encode($data);
file_put_contents(JSON_FILE_PATH, $jsonstring);
}
$address_allowlist = [
// Estuary
'f3vnq2cmwig3qjisnx5hobxvsd4drn4f54xfxnv4tciw6vnjdsf5xipgafreprh5riwmgtcirpcdmi3urbg36a',
// Dealbot
'f1wdxdpqh3hirrhp353i4o6ld7bsw6evh3v7i5jtq',
// Bidbot
'f144zep4gitj73rrujd3jw6iprljicx6vl4wbeavi',
// 3000rice
// 'f3spci2tzjzhedgvxsoxg4hprrkyszdce5dacaxg4ndkvpshpuma2uf6c4fmk2lvvrobnz5bvr6j5hfeccpupq'
];
$address_nolimitlist = [
// 3000rice
// 'f3spci2tzjzhedgvxsoxg4hprrkyszdce5dacaxg4ndkvpshpuma2uf6c4fmk2lvvrobnz5bvr6j5hfeccpupq'
];
$stdin = fopen('php://stdin', 'r');
stream_set_blocking($stdin, FALSE);
$json_input = stream_get_contents($stdin);
if (!$json_input) {
// No input
exitDealReject("001 Miner can't accept deal right now, please try again later.");
}
$input = json_decode($json_input);
if (!$input) {
// JSON failed to parse
exitDealReject("002 Miner can't accept deal right now, please try again later.");
}
// Reject deals if they are on the allowlist
if (!in_array($input->Proposal->Client, $address_allowlist)) {
exitDealReject("003 Please contact to whitelist: ".$input->Proposal->Client);
}
if (!in_array($input->Proposal->Client, $address_nolimitlist)) {
// Check last time got the deal
$jsonstring = file_get_contents(JSON_FILE_PATH);
$ts_obj = json_decode($jsonstring, true);
$last_ts = $ts_obj[$input->Proposal->Client] | 0;
$current_ts = microtime(true);
$diff = $current_ts - $last_ts;
if($diff <= RATE_LIMIT && !in_array($input->Proposal->Client, $address_nolimitlist)) {
saveJsonToFile($ts_obj);
exitDealReject("004 Rate limit reached. Last deal was received $diff seconds ago.");
} else {
$ts_obj[$input->Proposal->Client] = $current_ts;
}
saveJsonToFile($ts_obj);
}
exitDealAccept();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment