make-software / casper-net-sdk

.NET SDK to interact with the Casper Network nodes via RPC
Apache License 2.0
13 stars 14 forks source link

Can't serialize results to JSON #28

Open GuybrushX opened 1 year ago

GuybrushX commented 1 year ago

What happened? Can't serialize results to JSON.

Since switch blocks are more than 10MB per era I wanted to dump a few switch blocks as JSON to process them offline so I don't need to request them again and again from the network. Seems like this isn't supported?

What did you expect to happen? That serialization just works. Indeed I saw that quite a few Write methods aren't implemented yet: BidsListConverter: throw new NotImplementedException("Write method for Bid not yet implemented"); Delegator: throw new NotImplementedException("Write method for Delegator not yet implemented"); EntryPoint: throw new NotImplementedException("Write method for EntryPointAccess not yet implemented"); SeigniorageAllocation: throw new NotImplementedException("Write method for SeigniorageAllocation not yet implemented"); StoredValue: throw new NotImplementedException("Write method for StoredValue not yet implemented"); Transform: throw new NotImplementedException("Write method for Transform not yet implemented");

Are you willing to submit a pull request to fix this bug? Sorry, no.

davidatwhiletrue commented 1 year ago

Hi @GuybrushX . Thanks for reporting this. I'll look into it in detail.

You can save any RPC response to a JSON file before parsing the response. For instance, to save the block:

var rpcResponse = await casperSdk.GetBlock(1_479_046);

File.WriteAllText("switch_block.json", rpcResponse.Result.GetProperty("block").GetRawText());

Then, you can read the file later and get a Block object:

var switch_block = JsonSerializer.Deserialize<Block>(File.ReadAllText("switch_block.json"));

Console.WriteLine(switch_block.Header.EraEnd.EraReport.Rewards);
GuybrushX commented 1 year ago

Thank you for your fast reply.

Yeah, I found that after quite some time as well, I was just abusing the following method directly:

RpcResponse.Parse();

Your solution is much better, of course without modifying the SDK itself.

Serialization and Deserialization seem to work fine that way according to some quick tests I've done. I`m using the following code to get switch blocks btw. which you've sent me on Discord some time ago:

var globalStateByEra = await _casperClient.QueryGlobalState(GlobalStateKey.FromString($"era-{eraId}"));

I keep that issue here still open since the original issue still exists.