libplanet
libplanet copied to clipboard
Adding method to get random seed from `Transaction<T>` and `Block<T>`
In Nine Chronicles dev office hour, @TarogStar said
Can you add the seed used to HackAndSlash and similar action results so that we can replay the exact actions whenever we want from the transaction info?
I think that we can add a method for it by reconstructing seed from Block<T>.PreEvaluationHash and Transaction<T>.Signature. (see also)
cc @planetarium/libplanet
✋🏻I would like to try this out.
@hunkim98
This issue's main goal is, Obtaining seed value from the executed block to replay actions in it.
Strategy
https://github.com/planetarium/libplanet/blob/6a360554ec3e6db21be041afbf1eaf92e0f25e3b/Libplanet/Action/ActionEvaluator.cs#L276-L279
The random seed will be calculated by these values as below
preEvaluationHashBytes:Block<T>.PreEvaluationHashhashedSignature: Hashed value for all transactions in the blocksignature:Transaction<T>.signaturefor the tx containg the action
Also, there is one hidden variable.
https://github.com/planetarium/libplanet/blob/6a360554ec3e6db21be041afbf1eaf92e0f25e3b/Libplanet/Action/ActionEvaluator.cs#L380
As above, we're increasing a seed per actions. so we should consider about offset too. Therefore, we can define the calculating function as below...
int GenerateRandomSeed(byte[] preEvaluationHash, byte[] hashedSignature, byte[] txSignature, int actionOffset);
Of course, all parameters for this function can be obtain before executing. so we can refactor previous codes to leaverage new function too.
Test
Two tests can be considered.
For GenerateRandomSeed()
This test only verify the return value against given parameters. so you can easily write down I guess. 😅
For ActionEvalautor.EvaluateActions()
The test for GenerateRandomSeed() isn't enough this issue because that the random generated through ActionEvalautor will produce the same result as that. so I think that we need to check IActionContext.Random against parameters(i.e., preEvaluationHash, hashedSignature, txSignature). this might be similar to the test you wrote in #2179.
@planetarium/libplanet Feel free to point out about something I've missed. 🙏