Skip to content

Commit 4733421

Browse files
committed
refactor rpc
1 parent 2bb76e1 commit 4733421

File tree

1 file changed

+45
-45
lines changed

1 file changed

+45
-45
lines changed

src/rpc/rpc.zig

+45-45
Original file line numberDiff line numberDiff line change
@@ -25,61 +25,61 @@ pub fn generateAuth(allocator: std.mem.Allocator, user: []const u8, pass: []cons
2525
return authorization_buffer;
2626
}
2727

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 {
2929
// Number of chars in rpc body (static ones).
3030
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;
3333
if (params != null) {
3434
// Number of commas in params.
3535
cap += params.?.items.len - 1;
36-
paramsCap += params.?.items.len - 1;
36+
params_cap += params.?.items.len - 1;
3737
// Number of chars in each param.
3838
for (0..params.?.items.len) |i| {
3939
const item = params.?.items[i];
4040
switch (item) {
4141
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;
4545
},
4646
RpcParams.str => |str| {
4747
cap += str.len + 2;
48-
paramsCap += str.len + 2; // 2 is for ""
48+
params_cap += str.len + 2; // 2 is for ""
4949
},
5050
}
5151
}
5252
}
5353

5454
const buffer = try allocator.alloc(u8, cap);
5555
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);
5858
var current: usize = 0;
5959
for (0..params.?.items.len) |i| {
6060
const param: RpcParams = params.?.items[i];
6161
switch (param) {
6262
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;
6666
},
6767
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] = '"';
7171
current += param.str.len + 2;
7272
},
7373
}
7474
if (i < params.?.items.len - 1) {
7575
// not the last param, add comma
76-
paramsBuffer[current] = ',';
76+
params_buffer[current] = ',';
7777
current += 1;
7878
}
7979
}
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 });
8181
} 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 });
8383
}
8484
return buffer;
8585
}
@@ -103,9 +103,9 @@ fn req(client: *std.http.Client, uri: std.Uri, auth: []const u8, body: []const u
103103

104104
pub fn getBlockCount(allocator: std.mem.Allocator, client: *std.http.Client, location: []const u8, auth: []const u8) !usize {
105105
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);
109109
defer allocator.free(body);
110110
var request = try req(client, uri, auth, body);
111111
defer request.deinit();
@@ -119,19 +119,19 @@ pub fn getBlockCount(allocator: std.mem.Allocator, client: *std.http.Client, loc
119119
}
120120
end += 1;
121121
}
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;
124124
}
125125

126126
pub fn getBlockHash(allocator: std.mem.Allocator, client: *std.http.Client, location: []const u8, auth: []const u8, blockcount: usize) ![64]u8 {
127127
const uri = try std.Uri.parse(location);
128-
const rpcId = "walle".*;
129-
const rpcMethod = "getblockhash".*;
128+
const rpc_id = "walle".*;
129+
const method = "getblockhash".*;
130130
var params = std.ArrayList(RpcParams).init(allocator);
131131
defer params.deinit();
132132
const p = RpcParams{ .num = blockcount };
133133
try params.append(p);
134-
const body = try generateBody(allocator, &rpcId, &rpcMethod, params);
134+
const body = try generateBody(allocator, &rpc_id, &method, params);
135135
defer allocator.free(body);
136136
var request = try req(client, uri, auth, body);
137137
defer request.deinit();
@@ -142,15 +142,15 @@ pub fn getBlockHash(allocator: std.mem.Allocator, client: *std.http.Client, loca
142142

143143
pub fn getBlockRawTx(allocator: std.mem.Allocator, client: *std.http.Client, location: []const u8, auth: []const u8, blockhash: [64]u8) ![][]u8 {
144144
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".*;
147147
var params = std.ArrayList(RpcParams).init(allocator);
148148
defer params.deinit();
149149
const p1 = RpcParams{ .str = @constCast(&blockhash) };
150150
const p2 = RpcParams{ .num = 2 }; // verbosity
151151
try params.append(p1);
152152
try params.append(p2);
153-
const body = try generateBody(allocator, &rpcId, &rpcMethod, params);
153+
const body = try generateBody(allocator, &rpc_id, &rpc_method, params);
154154
defer allocator.free(body);
155155
var request = try req(client, uri, auth, body);
156156
defer request.deinit();
@@ -166,15 +166,15 @@ pub fn getBlockRawTx(allocator: std.mem.Allocator, client: *std.http.Client, loc
166166
return result;
167167
}
168168

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 {
170170
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".*;
173173
var params = std.ArrayList(RpcParams).init(allocator);
174174
defer params.deinit();
175-
const p = RpcParams{ .str = signedTxHex };
175+
const p = RpcParams{ .str = signed_tx_hex };
176176
try params.append(p);
177-
const body = try generateBody(allocator, &rpcId, &rpcMethod, params);
177+
const body = try generateBody(allocator, &rpc_id, &rpc_method, params);
178178
defer allocator.free(body);
179179
var request = try req(client, uri, auth, body);
180180
defer request.deinit();
@@ -194,31 +194,31 @@ test "generateAuth" {
194194

195195
test "generateBodyNoParams" {
196196
const allocator = std.testing.allocator;
197-
const rpcId = "walle".*;
197+
const rpc_id = "walle".*;
198198
const method = "getblockcount".*;
199-
const body = try generateBody(allocator, &rpcId, &method, null);
199+
const body = try generateBody(allocator, &rpc_id, &method, null);
200200
defer allocator.free(body);
201201
const expectedString = "{\"jsonrpc\":\"1.0\",\"id\":\"walle\",\"method\":\"getblockcount\",\"params\":[]}".*;
202202
try std.testing.expectEqualStrings(&expectedString, body);
203203
}
204204

205205
test "generateBodyParams" {
206206
const allocator = std.testing.allocator;
207-
const rpcId = "walle".*;
207+
const rpc_id = "walle".*;
208208
const method = "getblockcount".*;
209209
var params = std.ArrayList(RpcParams).init(allocator);
210210
defer params.deinit();
211211
const p = RpcParams{ .num = 300 };
212212
try params.append(p);
213-
const body = try generateBody(allocator, &rpcId, &method, params);
213+
const body = try generateBody(allocator, &rpc_id, &method, params);
214214
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);
217217
}
218218

219219
test "generateBodyMultipleParams" {
220220
const allocator = std.testing.allocator;
221-
const rpcId = "walle".*;
221+
const rpc_id = "walle".*;
222222
const method = "test".*;
223223
var params = std.ArrayList(RpcParams).init(allocator);
224224
defer params.deinit();
@@ -229,10 +229,10 @@ test "generateBodyMultipleParams" {
229229
try params.append(p1);
230230
try params.append(p2);
231231
try params.append(p3);
232-
const body = try generateBody(allocator, &rpcId, &method, params);
232+
const body = try generateBody(allocator, &rpc_id, &method, params);
233233
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);
236236
}
237237

238238
test "getBlockCount" {

0 commit comments

Comments
 (0)