libplanet icon indicating copy to clipboard operation
libplanet copied to clipboard

Adding method to get random seed from `Transaction<T>` and `Block<T>`

Open longfin opened this issue 3 years ago • 2 comments

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

longfin avatar Jul 01 '22 11:07 longfin

✋🏻I would like to try this out.

hunkim98 avatar Jul 31 '22 07:07 hunkim98

@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>.PreEvaluationHash
  • hashedSignature: Hashed value for all transactions in the block
  • signature: Transaction<T>.signature for 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. 🙏

longfin avatar Aug 04 '22 13:08 longfin