72 lines
2.2 KiB
C++
72 lines
2.2 KiB
C++
|
|
#include <stdlib.h>
|
||
|
|
#include <stdio.h>
|
||
|
|
#include <string.h>
|
||
|
|
|
||
|
|
#include "cJSON.h"
|
||
|
|
#include "stellar/stellar.h"
|
||
|
|
#include "stellar/session.h"
|
||
|
|
#include "stellar/stellar_mq.h"
|
||
|
|
#include "stellar/stellar_exdata.h"
|
||
|
|
|
||
|
|
#include "stratum_decoder.h"
|
||
|
|
|
||
|
|
#define unused(x) ((void)(x))
|
||
|
|
extern "C" int commit_test_result_json(cJSON *node, const char *name);
|
||
|
|
|
||
|
|
int g_test_stratum_decoder_plugin_id = 0;
|
||
|
|
int g_stratum_message_topic_id = 0;
|
||
|
|
|
||
|
|
void *ctx_new(struct session *session, void *plugin_env)
|
||
|
|
{
|
||
|
|
unused(plugin_env);
|
||
|
|
unused(session);
|
||
|
|
|
||
|
|
cJSON *root = cJSON_CreateObject();
|
||
|
|
return (void *)root;
|
||
|
|
}
|
||
|
|
|
||
|
|
void ctx_free(struct session *sess, void *session_ctx, void *plugin_env)
|
||
|
|
{
|
||
|
|
unused(sess);
|
||
|
|
unused(plugin_env);
|
||
|
|
|
||
|
|
commit_test_result_json((cJSON *)session_ctx, "socks_decoder_test");
|
||
|
|
}
|
||
|
|
|
||
|
|
void test_stratum_decoder_on_message(struct session *session, int topic_id, const void *msg, void *per_session_ctx, void *plugin_env)
|
||
|
|
{
|
||
|
|
unused(plugin_env);
|
||
|
|
unused(topic_id);
|
||
|
|
|
||
|
|
struct stratum_field *stratum = (struct stratum_field *)msg;
|
||
|
|
cJSON *root = (cJSON *)per_session_ctx;
|
||
|
|
|
||
|
|
cJSON *stratum_json = cJSON_CreateObject();
|
||
|
|
cJSON_AddItemToObject(root, "stratum", stratum_json);
|
||
|
|
|
||
|
|
cJSON_AddStringToObject(stratum_json, "Tuple4", session_get0_readable_addr(session));
|
||
|
|
cJSON_AddStringToObject(stratum_json, "type", stratum->type == ETH ? "ETH" : "OTHER");
|
||
|
|
cJSON_AddStringToObject(stratum_json, "mining_pools", (char *)stratum->mining_pools.iov_base);
|
||
|
|
cJSON_AddStringToObject(stratum_json, "mining_program", (char *)stratum->mining_program.iov_base);
|
||
|
|
cJSON_AddStringToObject(stratum_json, "mining_subscribe", (char *)stratum->mining_subscribe.iov_base);
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
extern "C" void *STRATUM_DECODER_TEST_PLUG_INIT(struct stellar *st)
|
||
|
|
{
|
||
|
|
g_test_stratum_decoder_plugin_id = stellar_session_plugin_register(st, ctx_new, ctx_free, NULL);
|
||
|
|
g_stratum_message_topic_id = stellar_mq_get_topic_id(st, STRATUM_MESSAGE_TOPIC);
|
||
|
|
|
||
|
|
if (stellar_session_mq_subscribe(st, g_stratum_message_topic_id, test_stratum_decoder_on_message, g_test_stratum_decoder_plugin_id) < 0)
|
||
|
|
{
|
||
|
|
printf("subscribe topic %s failed\n", STRATUM_MESSAGE_TOPIC);
|
||
|
|
return NULL;
|
||
|
|
}
|
||
|
|
|
||
|
|
return NULL;
|
||
|
|
}
|
||
|
|
|
||
|
|
extern "C" void STRATUM_DECODER_TEST_PLUG_DESTROY(void * plugin_env)
|
||
|
|
{
|
||
|
|
unused(plugin_env);
|
||
|
|
}
|