@@ -25,61 +25,61 @@ pub fn generateAuth(allocator: std.mem.Allocator, user: []const u8, pass: []cons
25
25
return authorization_buffer ;
26
26
}
27
27
28
- fn generateBody (allocator : std.mem.Allocator , rpcId : []const u8 , method : []const u8 , params : ? std .ArrayList (RpcParams )) ! []const u8 {
28
+ fn generateBody (allocator : std.mem.Allocator , rpc_id : []const u8 , method : []const u8 , params : ? std .ArrayList (RpcParams )) ! []const u8 {
29
29
// Number of chars in rpc body (static ones).
30
30
var cap : usize = 49 ;
31
- cap += rpcId .len + method .len ;
32
- var paramsCap : usize = 0 ;
31
+ cap += rpc_id .len + method .len ;
32
+ var params_cap : usize = 0 ;
33
33
if (params != null ) {
34
34
// Number of commas in params.
35
35
cap += params .? .items .len - 1 ;
36
- paramsCap += params .? .items .len - 1 ;
36
+ params_cap += params .? .items .len - 1 ;
37
37
// Number of chars in each param.
38
38
for (0.. params .? .items .len ) | i | {
39
39
const item = params .? .items [i ];
40
40
switch (item ) {
41
41
RpcParams .num = > | num | {
42
- const currentcap = if (num != 0 ) std .math .log10 (num ) + 1 else 1 ;
43
- cap += currentcap ;
44
- paramsCap += currentcap ;
42
+ const current_cap = if (num != 0 ) std .math .log10 (num ) + 1 else 1 ;
43
+ cap += current_cap ;
44
+ params_cap += current_cap ;
45
45
},
46
46
RpcParams .str = > | str | {
47
47
cap += str .len + 2 ;
48
- paramsCap += str .len + 2 ; // 2 is for ""
48
+ params_cap += str .len + 2 ; // 2 is for ""
49
49
},
50
50
}
51
51
}
52
52
}
53
53
54
54
const buffer = try allocator .alloc (u8 , cap );
55
55
if (params != null ) {
56
- var paramsBuffer = try allocator .alloc (u8 , paramsCap );
57
- defer allocator .free (paramsBuffer );
56
+ var params_buffer = try allocator .alloc (u8 , params_cap );
57
+ defer allocator .free (params_buffer );
58
58
var current : usize = 0 ;
59
59
for (0.. params .? .items .len ) | i | {
60
60
const param : RpcParams = params .? .items [i ];
61
61
switch (param ) {
62
62
RpcParams .num = > {
63
- const currentcap = if (param .num != 0 ) std .math .log10 (param .num ) + 1 else 1 ;
64
- _ = try std .fmt .bufPrint (paramsBuffer [current .. current + currentcap ], "{d}" , .{param .num });
65
- current += currentcap ;
63
+ const current_cap = if (param .num != 0 ) std .math .log10 (param .num ) + 1 else 1 ;
64
+ _ = try std .fmt .bufPrint (params_buffer [current .. current + current_cap ], "{d}" , .{param .num });
65
+ current += current_cap ;
66
66
},
67
67
RpcParams .str = > {
68
- paramsBuffer [current ] = '"' ;
69
- @memcpy (paramsBuffer [current + 1 .. current + param .str .len + 1 ], param .str );
70
- paramsBuffer [current + param .str .len + 1 ] = '"' ;
68
+ params_buffer [current ] = '"' ;
69
+ @memcpy (params_buffer [current + 1 .. current + param .str .len + 1 ], param .str );
70
+ params_buffer [current + param .str .len + 1 ] = '"' ;
71
71
current += param .str .len + 2 ;
72
72
},
73
73
}
74
74
if (i < params .? .items .len - 1 ) {
75
75
// not the last param, add comma
76
- paramsBuffer [current ] = ',' ;
76
+ params_buffer [current ] = ',' ;
77
77
current += 1 ;
78
78
}
79
79
}
80
- _ = try std .fmt .bufPrint (buffer , "{{\" jsonrpc\" :\" 1.0\" ,\" id\" :\" {s}\" ,\" method\" :\" {s}\" ,\" params\" :[{s}]}}" , .{ rpcId , method , paramsBuffer });
80
+ _ = try std .fmt .bufPrint (buffer , "{{\" jsonrpc\" :\" 1.0\" ,\" id\" :\" {s}\" ,\" method\" :\" {s}\" ,\" params\" :[{s}]}}" , .{ rpc_id , method , params_buffer });
81
81
} else {
82
- _ = try std .fmt .bufPrint (buffer , "{{\" jsonrpc\" :\" 1.0\" ,\" id\" :\" {s}\" ,\" method\" :\" {s}\" ,\" params\" :[]}}" , .{ rpcId , method });
82
+ _ = try std .fmt .bufPrint (buffer , "{{\" jsonrpc\" :\" 1.0\" ,\" id\" :\" {s}\" ,\" method\" :\" {s}\" ,\" params\" :[]}}" , .{ rpc_id , method });
83
83
}
84
84
return buffer ;
85
85
}
@@ -103,9 +103,9 @@ fn req(client: *std.http.Client, uri: std.Uri, auth: []const u8, body: []const u
103
103
104
104
pub fn getBlockCount (allocator : std.mem.Allocator , client : * std.http.Client , location : []const u8 , auth : []const u8 ) ! usize {
105
105
const uri = try std .Uri .parse (location );
106
- const rpcId = "walle" .* ;
107
- const rpcMethod = "getblockcount" .* ;
108
- const body = try generateBody (allocator , & rpcId , & rpcMethod , null );
106
+ const rpc_id = "walle" .* ;
107
+ const rpc_method = "getblockcount" .* ;
108
+ const body = try generateBody (allocator , & rpc_id , & rpc_method , null );
109
109
defer allocator .free (body );
110
110
var request = try req (client , uri , auth , body );
111
111
defer request .deinit ();
@@ -119,19 +119,19 @@ pub fn getBlockCount(allocator: std.mem.Allocator, client: *std.http.Client, loc
119
119
}
120
120
end += 1 ;
121
121
}
122
- const blockcount = try std .fmt .parseInt (usize , response [start .. end - 1 ], 10 );
123
- return blockcount ;
122
+ const block_count = try std .fmt .parseInt (usize , response [start .. end - 1 ], 10 );
123
+ return block_count ;
124
124
}
125
125
126
126
pub fn getBlockHash (allocator : std.mem.Allocator , client : * std.http.Client , location : []const u8 , auth : []const u8 , blockcount : usize ) ! [64 ]u8 {
127
127
const uri = try std .Uri .parse (location );
128
- const rpcId = "walle" .* ;
129
- const rpcMethod = "getblockhash" .* ;
128
+ const rpc_id = "walle" .* ;
129
+ const method = "getblockhash" .* ;
130
130
var params = std .ArrayList (RpcParams ).init (allocator );
131
131
defer params .deinit ();
132
132
const p = RpcParams { .num = blockcount };
133
133
try params .append (p );
134
- const body = try generateBody (allocator , & rpcId , & rpcMethod , params );
134
+ const body = try generateBody (allocator , & rpc_id , & method , params );
135
135
defer allocator .free (body );
136
136
var request = try req (client , uri , auth , body );
137
137
defer request .deinit ();
@@ -142,15 +142,15 @@ pub fn getBlockHash(allocator: std.mem.Allocator, client: *std.http.Client, loca
142
142
143
143
pub fn getBlockRawTx (allocator : std.mem.Allocator , client : * std.http.Client , location : []const u8 , auth : []const u8 , blockhash : [64 ]u8 ) ! [][]u8 {
144
144
const uri = try std .Uri .parse (location );
145
- const rpcId = "walle" .* ;
146
- const rpcMethod = "getblock" .* ;
145
+ const rpc_id = "walle" .* ;
146
+ const rpc_method = "getblock" .* ;
147
147
var params = std .ArrayList (RpcParams ).init (allocator );
148
148
defer params .deinit ();
149
149
const p1 = RpcParams { .str = @constCast (& blockhash ) };
150
150
const p2 = RpcParams { .num = 2 }; // verbosity
151
151
try params .append (p1 );
152
152
try params .append (p2 );
153
- const body = try generateBody (allocator , & rpcId , & rpcMethod , params );
153
+ const body = try generateBody (allocator , & rpc_id , & rpc_method , params );
154
154
defer allocator .free (body );
155
155
var request = try req (client , uri , auth , body );
156
156
defer request .deinit ();
@@ -166,15 +166,15 @@ pub fn getBlockRawTx(allocator: std.mem.Allocator, client: *std.http.Client, loc
166
166
return result ;
167
167
}
168
168
169
- pub fn sendRawTx (allocator : std.mem.Allocator , client : * std.http.Client , location : []const u8 , auth : []const u8 , signedTxHex : []u8 ) ! void {
169
+ pub fn sendRawTx (allocator : std.mem.Allocator , client : * std.http.Client , location : []const u8 , auth : []const u8 , signed_tx_hex : []u8 ) ! void {
170
170
const uri = try std .Uri .parse (location );
171
- const rpcId = "walle" .* ;
172
- const rpcMethod = "getblock" .* ;
171
+ const rpc_id = "walle" .* ;
172
+ const rpc_method = "getblock" .* ;
173
173
var params = std .ArrayList (RpcParams ).init (allocator );
174
174
defer params .deinit ();
175
- const p = RpcParams { .str = signedTxHex };
175
+ const p = RpcParams { .str = signed_tx_hex };
176
176
try params .append (p );
177
- const body = try generateBody (allocator , & rpcId , & rpcMethod , params );
177
+ const body = try generateBody (allocator , & rpc_id , & rpc_method , params );
178
178
defer allocator .free (body );
179
179
var request = try req (client , uri , auth , body );
180
180
defer request .deinit ();
@@ -194,31 +194,31 @@ test "generateAuth" {
194
194
195
195
test "generateBodyNoParams" {
196
196
const allocator = std .testing .allocator ;
197
- const rpcId = "walle" .* ;
197
+ const rpc_id = "walle" .* ;
198
198
const method = "getblockcount" .* ;
199
- const body = try generateBody (allocator , & rpcId , & method , null );
199
+ const body = try generateBody (allocator , & rpc_id , & method , null );
200
200
defer allocator .free (body );
201
201
const expectedString = "{\" jsonrpc\" :\" 1.0\" ,\" id\" :\" walle\" ,\" method\" :\" getblockcount\" ,\" params\" :[]}" .* ;
202
202
try std .testing .expectEqualStrings (& expectedString , body );
203
203
}
204
204
205
205
test "generateBodyParams" {
206
206
const allocator = std .testing .allocator ;
207
- const rpcId = "walle" .* ;
207
+ const rpc_id = "walle" .* ;
208
208
const method = "getblockcount" .* ;
209
209
var params = std .ArrayList (RpcParams ).init (allocator );
210
210
defer params .deinit ();
211
211
const p = RpcParams { .num = 300 };
212
212
try params .append (p );
213
- const body = try generateBody (allocator , & rpcId , & method , params );
213
+ const body = try generateBody (allocator , & rpc_id , & method , params );
214
214
defer allocator .free (body );
215
- const expectedString = "{\" jsonrpc\" :\" 1.0\" ,\" id\" :\" walle\" ,\" method\" :\" getblockcount\" ,\" params\" :[300]}" .* ;
216
- try std .testing .expectEqualStrings (& expectedString , body );
215
+ const expected = "{\" jsonrpc\" :\" 1.0\" ,\" id\" :\" walle\" ,\" method\" :\" getblockcount\" ,\" params\" :[300]}" .* ;
216
+ try std .testing .expectEqualStrings (& expected , body );
217
217
}
218
218
219
219
test "generateBodyMultipleParams" {
220
220
const allocator = std .testing .allocator ;
221
- const rpcId = "walle" .* ;
221
+ const rpc_id = "walle" .* ;
222
222
const method = "test" .* ;
223
223
var params = std .ArrayList (RpcParams ).init (allocator );
224
224
defer params .deinit ();
@@ -229,10 +229,10 @@ test "generateBodyMultipleParams" {
229
229
try params .append (p1 );
230
230
try params .append (p2 );
231
231
try params .append (p3 );
232
- const body = try generateBody (allocator , & rpcId , & method , params );
232
+ const body = try generateBody (allocator , & rpc_id , & method , params );
233
233
defer allocator .free (body );
234
- const expectedString = "{\" jsonrpc\" :\" 1.0\" ,\" id\" :\" walle\" ,\" method\" :\" test\" ,\" params\" :[300,500,\" 2031c78ac5e8aaafd25f6697eb23564238cce4b24116b2750e96808bc0311384\" ]}" .* ;
235
- try std .testing .expectEqualStrings (& expectedString , body );
234
+ const expected = "{\" jsonrpc\" :\" 1.0\" ,\" id\" :\" walle\" ,\" method\" :\" test\" ,\" params\" :[300,500,\" 2031c78ac5e8aaafd25f6697eb23564238cce4b24116b2750e96808bc0311384\" ]}" .* ;
235
+ try std .testing .expectEqualStrings (& expected , body );
236
236
}
237
237
238
238
test "getBlockCount" {
0 commit comments