Skip to content
This repository has been archived by the owner on Mar 6, 2023. It is now read-only.

Stub of binary stratum implementation #33

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions build_pb.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash
CURDIR=$(pwd)

cd $CURDIR/protob

for i in stratum ; do
protoc --python_out=../stratum/ -I/usr/include -I. $i.proto
done
2 changes: 1 addition & 1 deletion client2.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
s = time.time()

for x in range(n):
r = urllib2.Request('http://california.stratum.bitcoin.cz:8000', data, headers)
r = urllib2.Request('http://stratum.bitcoin.cz:3333', data, headers)
# r = urllib2.Request('http://localhost:8000', data, headers)
resp = urllib2.urlopen(r)

Expand Down
71 changes: 71 additions & 0 deletions protob/stratum.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Sugar for easier handling in Java
option java_package = "com.satoshilabs.stratum.protobuf";
option java_outer_classname = "StratumProtobuf";

import "google/protobuf/descriptor.proto";

extend google.protobuf.EnumValueOptions {
optional string json = 50002;
}

/**
* Mapping between Stratum wire identifier (uint) and a protobuf message
*/
enum MessageType {
MessageType_Subscribe = 10 [(json)='mining.subscribe'];
MessageType_Authorize = 11 [(json)='mining.authorize'];
MessageType_Notify = 12 [(json)='mining.notify'];
MessageType_Submit = 13 [(json)='mining.submit'];
MessageType_SetDifficulty = 20 [(json)='mining.set_difficulty'];
MessageType_Reconnect = 21 [(json)='client.reconnect'];
MessageType_GetVersion = 22 [(json)='client.get_version'];
MessageType_Version = 23;
}

message Subscribe {
required uint32 id = 1;
}

message Authorize {
required uint32 id = 1;
required bytes worker_name = 2;
optional bytes password = 3;
}

message Notify {
optional bytes job_id = 1;
required bytes prevhash = 2;
required bytes coinbase1 = 3;
optional bytes coinbase2 = 4;
repeated bytes merkle_branch = 5;
required bytes version = 6;
required bytes nbits = 7;
required bytes ntime = 8;
optional bool clean_jobs = 9;
}

message Submit {
required uint32 id = 1;
optional bytes worker_name = 2;
optional bytes job_id = 3;
required bytes extranonce2 = 4;
required bytes ntime = 5;
required bytes nonce = 6;
}

message SetDifficulty {
required uint64 difficulty = 1;
}

message Reconnect {
optional bytes hostname = 1;
optional int32 port = 2;
optional int32 wait = 3;
}

message GetVersion {
required uint32 id = 1;
}

message Version {}

46 changes: 46 additions & 0 deletions stratum/protobuf_mapping.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import stratum_pb2 as proto

map_type_to_class = {}
map_class_to_type = {}

map_class_to_json = {}
map_json_to_class = {}

def build_map():
for msg_type, i in proto.MessageType.items():
msg_name = msg_type.replace('MessageType_', '')
msg_class = getattr(proto, msg_name)

map_type_to_class[i] = msg_class
map_class_to_type[msg_class] = i

json_method = proto.MessageType.DESCRIPTOR.values_by_number[i].GetOptions().Extensions[proto.json]

map_class_to_json[msg_class] = json_method
map_json_to_class[json_method] = msg_class

def get_type(msg):
return map_class_to_type[msg.__class__]

def get_class(t):
return map_type_to_class[t]

def get_class_by_json(j):
return map_json_to_class[j]

def get_json_by_class(msg):
return map_class_to_json[msg.__class__]

def check_missing():
from google.protobuf import reflection

types = [getattr(proto, item) for item in dir(proto)
if issubclass(getattr(proto, item).__class__, reflection.GeneratedProtocolMessageType)]

missing = list(set(types) - set(map_type_to_class.values()))

if len(missing):
raise Exception("Following protobuf messages are not defined in mapping: %s" % missing)

build_map()
check_missing()
Loading