Skip to content

Commit 7f5cad9

Browse files
committed
modify tests, fix create nonce bytes
1 parent 6dd920a commit 7f5cad9

File tree

7 files changed

+37
-25
lines changed

7 files changed

+37
-25
lines changed

__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@
1616
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
1717
# DEALINGS IN THE SOFTWARE.
1818

19-
__version__ = "1.1.0"
19+
__version__ = "1.1.1"

cubit.pyx

+9-9
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ cdef extern from "kernels/main.hh":
3636
void reset_cuda_c();
3737
int runTestSealMeetsDifficulty(unsigned char* seal, unsigned long* limit);
3838
int runTestLessThan(uint256 a, uint256 b);
39-
void runTestCreatePreSeal(unsigned char* pre_seal, uint64 nonce, unsigned char* block_bytes);
40-
void runTestCreateNonceBytes(unsigned long long nonce, unsigned char* nonce_bytes);
41-
void runTestSealHash(unsigned char* seal, unsigned char* block_hash, uint64 nonce);
39+
void runTestCreatePreSeal(unsigned char* pre_seal, uint64 nonce, unsigned char* block_bytes, int dev_id);
40+
void runTestCreateNonceBytes(unsigned long long nonce, unsigned char* nonce_bytes, int dev_id);
41+
void runTestSealHash(unsigned char* seal, unsigned char* block_hash, uint64 nonce, int dev_id);
4242
void runTestPreSealHash(unsigned char* seal, unsigned char* preseal_bytes);
4343
void runTest(unsigned char* data, unsigned long size, unsigned char* digest);
4444
void runTestKeccak(unsigned char* data, unsigned long size, unsigned char* digest);
@@ -74,12 +74,12 @@ cpdef bytes run_test_keccak(unsigned char* data, unsigned int length):
7474
finally:
7575
PyMem_Free(digest_)
7676

77-
cpdef bytes run_test_seal_hash(unsigned char* block_bytes, uint64 nonce):
77+
cpdef bytes run_test_seal_hash(unsigned char* block_bytes, uint64 nonce, int dev_id):
7878
cdef unsigned char* digest_ = <unsigned char*> PyMem_Malloc(
7979
64 * sizeof(unsigned char))
8080

8181
try:
82-
runTestSealHash(digest_, block_bytes, nonce)
82+
runTestSealHash(digest_, block_bytes, nonce, dev_id)
8383

8484
return digest_[:32]
8585
finally:
@@ -96,13 +96,13 @@ cpdef bytes run_test_preseal_hash(unsigned char* preseal_bytes):
9696
finally:
9797
PyMem_Free(digest_)
9898

99-
cpdef bytes run_test_create_nonce_bytes(uint64 nonce):
99+
cpdef bytes run_test_create_nonce_bytes(uint64 nonce, int dev_id):
100100
cdef unsigned char* nonce_bytes = <unsigned char*> PyMem_Malloc(
101101
8 * sizeof(unsigned char))
102102
cdef int i
103103

104104
try:
105-
runTestCreateNonceBytes(nonce, nonce_bytes)
105+
runTestCreateNonceBytes(nonce, nonce_bytes, dev_id)
106106

107107
# Convert digest to python string
108108
nonce_bytes_str = nonce_bytes
@@ -153,13 +153,13 @@ cpdef int run_test_seal_meets_difficulty(const unsigned char[:] seal, const unsi
153153
PyMem_Free(seal_)
154154
PyMem_Free(limit_char)
155155

156-
cpdef bytearray run_test_create_pre_seal(uint64 nonce, unsigned char* block_bytes):
156+
cpdef bytearray run_test_create_pre_seal(uint64 nonce, unsigned char* block_bytes, int dev_id):
157157
cdef unsigned char* preseal_bytes = <unsigned char*> PyMem_Malloc(
158158
40 * sizeof(unsigned char))
159159
cdef int i
160160

161161
try:
162-
runTestCreatePreSeal(preseal_bytes, nonce, block_bytes)
162+
runTestCreatePreSeal(preseal_bytes, nonce, block_bytes, dev_id)
163163

164164
return bytearray(preseal_bytes[:40])
165165
finally:

kernels/main.cu

+16-4
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ __device__ bool seal_meets_difficulty(BYTE* seal, uint256 limit) {
8585

8686
__device__ void create_nonce_bytes(uint64 nonce, BYTE* nonce_bytes) {
8787
// Convert nonce to bytes (little endian) and store at start of pre_seal;
88-
for (int i = 0; i < 5; i++) {
88+
for (int i = 0; i < 8; i++) {
8989
nonce_bytes[i] = (nonce >> (i * 8)) & 0xFF;
9090
}
9191
}
@@ -204,6 +204,12 @@ __global__ void test_keccak256(BYTE* data, int size, BYTE* digest) {
204204

205205
__global__ void test_seal_hash(BYTE* seal, BYTE* block_hash, uint64 nonce) {
206206
create_seal_hash(seal, block_hash, nonce);
207+
// print out seal
208+
printf("seal: ");
209+
for (int i = 0; i < 64; i++) {
210+
printf("%02x", seal[i]);
211+
}
212+
printf("\n");
207213
}
208214

209215
__global__ void test_preseal_hash(BYTE* seal, BYTE* preseal_bytes) {
@@ -242,7 +248,9 @@ bool runTestSealMeetsDifficulty(BYTE* seal, uint256 limit) {
242248
return result;
243249
}
244250

245-
void runTestCreateNonceBytes(uint64 nonce, BYTE* nonce_bytes) {
251+
void runTestCreateNonceBytes(uint64 nonce, BYTE* nonce_bytes, int dev_id) {
252+
checkCudaErrors(cudaSetDevice(dev_id));
253+
246254
BYTE* dev_nonce_bytes = NULL;
247255
checkCudaErrors(cudaMallocManaged(&dev_nonce_bytes, sizeof(BYTE) * 8));
248256

@@ -254,7 +262,9 @@ void runTestCreateNonceBytes(uint64 nonce, BYTE* nonce_bytes) {
254262
checkCudaErrors(cudaDeviceSynchronize());
255263
}
256264

257-
void runTestCreatePreSeal(unsigned char* pre_seal, uint64 nonce, unsigned char* block_bytes) {
265+
void runTestCreatePreSeal(unsigned char* pre_seal, uint64 nonce, unsigned char* block_bytes, int dev_id) {
266+
checkCudaErrors(cudaSetDevice(dev_id));
267+
258268
// Test sha256
259269
BYTE* dev_pre_seal = NULL;
260270
checkCudaErrors(cudaMallocManaged(&dev_pre_seal, sizeof(BYTE) * 40));
@@ -306,9 +316,11 @@ void runTestKeccak(BYTE* data, unsigned long size, BYTE* digest) {
306316
checkCudaErrors(cudaDeviceSynchronize());
307317
}
308318

309-
void runTestSealHash(BYTE* seal, BYTE* block_hash, uint64 nonce) {
319+
void runTestSealHash(BYTE* seal, BYTE* block_hash, uint64 nonce, int dev_id) {
310320
BYTE* dev_seal = NULL;
311321
BYTE* dev_block_hash = NULL;
322+
checkCudaErrors(cudaSetDevice(dev_id));
323+
312324
checkCudaErrors(cudaMallocManaged(&dev_seal, 64));
313325
checkCudaErrors(cudaMallocManaged(&dev_block_hash, 64));
314326
// Copy data to device

kernels/main.hh

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@
2424
extern void reset_cuda_c();
2525
extern int runTestLessThan(unsigned long* a, unsigned long* b);
2626
extern bool runTestSealMeetsDifficulty(unsigned char* seal, unsigned long* limit);
27-
extern void runTestCreatePreSeal(unsigned char* pre_seal, uint64 nonce, unsigned char* block_bytes);
28-
extern void runTestCreateNonceBytes(unsigned long long nonce, unsigned char* nonce_bytes);
27+
extern void runTestCreatePreSeal(unsigned char* pre_seal, uint64 nonce, unsigned char* block_bytes, int dev_id);
28+
extern void runTestCreateNonceBytes(unsigned long long nonce, unsigned char* nonce_bytes, int dev_id);
2929
extern void runTestPreSealHash(unsigned char* seal, unsigned char* preseal_bytes);
30-
extern void runTestSealHash(unsigned char* seal, unsigned char* block_hash, uint64 nonce);
30+
extern void runTestSealHash(unsigned char* seal, unsigned char* block_hash, uint64 nonce, int dev_id);
3131
extern void runTest(unsigned char* data, unsigned long size, unsigned char* digest);
3232
extern void runTestKeccak(unsigned char* data, unsigned long size, unsigned char* digest);
3333
extern uint64 solve_cuda_c(int blockSize, unsigned char* seal, uint64 nonce_start, uint64 update_interval, unsigned long* limit, unsigned char* block_bytes, int dev_id);

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ build-backend = "setuptools.build_meta"
66

77
[project]
88
name = "cubit"
9-
version = "1.1.0"
9+
version = "1.1.1"
1010
description = "A python package for CUDA registration on bittensor. "
1111
readme = "README.md"
1212
authors = [{ name = "Opentensor Foundation", email = "cameron@opentensor.ai" }]

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ def build_extensions(self):
152152
author = 'Opentensor Foundation',
153153
author_email = 'cameron@opentensor.ai',
154154
url = 'https://github.com/opentensor/cubit',
155-
version = '1.1.0',
155+
version = '1.1.1',
156156

157157
ext_modules = cythonize(ext),
158158

test.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ def test_preseal_hash( self ) -> None:
120120
# Test create nonce bytes from nonce
121121
def test_create_nonce_bytes( self ) -> bytes:
122122
print(self._testMethodName)
123-
nonce = 100000000
124-
nonce_bytes: bytes = run_test_create_nonce_bytes(nonce)
123+
nonce = random.randint(int(math.pow(2, 45)), int(math.pow(2, 63)-1))
124+
nonce_bytes: bytes = run_test_create_nonce_bytes(nonce, self.dev_id)
125125
nonce_bytes_2 = self.get_nonce_bytes(nonce)
126126
# Unhexlify to compare
127127
nonce_bytes_2 = binascii.unhexlify(nonce_bytes_2)
@@ -131,8 +131,8 @@ def test_create_nonce_bytes( self ) -> bytes:
131131
# Test create pre seal
132132
def test_create_pre_seal( self ) -> bytes:
133133
print(self._testMethodName)
134-
nonce = 1304006780
135-
pre_seal = run_test_create_pre_seal(nonce, self.block_bytes)
134+
nonce = random.randint(int(math.pow(2, 45)), int(math.pow(2, 63)-1))
135+
pre_seal = run_test_create_pre_seal(nonce, self.block_bytes, self.dev_id)
136136

137137
nonce_bytes = self.get_nonce_bytes(nonce)
138138
pre_seal_2 = bytearray(self.hex_bytes_to_u8_list(nonce_bytes + self.block_bytes))
@@ -142,8 +142,8 @@ def test_create_pre_seal( self ) -> bytes:
142142
# Test block and nonce hash
143143
def test_seal_hash( self ) -> None:
144144
print(self._testMethodName)
145-
nonce = 0
146-
seal = run_test_seal_hash(self.block_bytes, nonce)
145+
nonce = random.randint(int(math.pow(2, 45)), int(math.pow(2, 63)-1))
146+
seal = run_test_seal_hash(self.block_bytes, nonce, self.dev_id)
147147
nonce_bytes = self.get_nonce_bytes(nonce)
148148
pre_seal = nonce_bytes + self.block_bytes
149149

0 commit comments

Comments
 (0)