2020-10-07 18:52:37 +00:00
|
|
|
package conformance
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/filecoin-project/go-state-types/abi"
|
|
|
|
"github.com/filecoin-project/test-vectors/schema"
|
2020-10-07 19:12:35 +00:00
|
|
|
|
|
|
|
"github.com/filecoin-project/lotus/chain/vm"
|
2020-10-07 18:52:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type ReplayingRand struct {
|
|
|
|
reporter Reporter
|
|
|
|
recorded schema.Randomness
|
|
|
|
fallback vm.Rand
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ vm.Rand = (*ReplayingRand)(nil)
|
|
|
|
|
|
|
|
// NewReplayingRand replays recorded randomness when requested, falling back to
|
|
|
|
// fixed randomness if the value cannot be found; hence this is a safe
|
|
|
|
// backwards-compatible replacement for fixedRand.
|
|
|
|
func NewReplayingRand(reporter Reporter, recorded schema.Randomness) *ReplayingRand {
|
|
|
|
return &ReplayingRand{
|
|
|
|
reporter: reporter,
|
|
|
|
recorded: recorded,
|
|
|
|
fallback: NewFixedRand(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-21 21:09:44 +00:00
|
|
|
func (r *ReplayingRand) match(requested schema.RandomnessRule) ([32]byte, bool) {
|
2020-10-07 18:52:37 +00:00
|
|
|
for _, other := range r.recorded {
|
|
|
|
if other.On.Kind == requested.Kind &&
|
2023-08-21 21:09:44 +00:00
|
|
|
other.On.Epoch == requested.Epoch {
|
|
|
|
return *(*[32]byte)(other.Return), true
|
2020-10-07 18:52:37 +00:00
|
|
|
}
|
|
|
|
}
|
2023-08-21 21:09:44 +00:00
|
|
|
return [32]byte{}, false
|
2020-10-07 18:52:37 +00:00
|
|
|
}
|
|
|
|
|
2023-08-21 20:26:51 +00:00
|
|
|
func (r *ReplayingRand) GetChainRandomness(ctx context.Context, round abi.ChainEpoch) ([32]byte, error) {
|
2020-10-07 18:52:37 +00:00
|
|
|
rule := schema.RandomnessRule{
|
2023-08-21 20:26:51 +00:00
|
|
|
Kind: schema.RandomnessChain,
|
|
|
|
Epoch: int64(round),
|
2020-10-07 18:52:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ret, ok := r.match(rule); ok {
|
2023-08-21 20:26:51 +00:00
|
|
|
r.reporter.Logf("returning saved chain randomness: epoch=%d, result=%x", round, ret)
|
2020-10-07 18:52:37 +00:00
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
2023-08-21 20:26:51 +00:00
|
|
|
r.reporter.Logf("returning fallback chain randomness: epoch=%d", round)
|
2021-05-26 23:03:46 +00:00
|
|
|
|
2023-08-21 20:26:51 +00:00
|
|
|
return r.fallback.GetChainRandomness(ctx, round)
|
2020-10-07 18:52:37 +00:00
|
|
|
}
|
|
|
|
|
2023-08-21 20:26:51 +00:00
|
|
|
func (r *ReplayingRand) GetBeaconRandomness(ctx context.Context, round abi.ChainEpoch) ([32]byte, error) {
|
2020-10-07 18:52:37 +00:00
|
|
|
rule := schema.RandomnessRule{
|
2023-08-21 20:26:51 +00:00
|
|
|
Kind: schema.RandomnessBeacon,
|
|
|
|
Epoch: int64(round),
|
2020-10-07 18:52:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ret, ok := r.match(rule); ok {
|
2023-08-21 20:26:51 +00:00
|
|
|
r.reporter.Logf("returning saved beacon randomness: epoch=%d, result=%x", round, ret)
|
2020-10-07 18:52:37 +00:00
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
2023-08-21 20:26:51 +00:00
|
|
|
r.reporter.Logf("returning fallback beacon randomness: epoch=%d, ", round)
|
2020-10-07 18:52:37 +00:00
|
|
|
|
2023-08-21 20:26:51 +00:00
|
|
|
return r.fallback.GetBeaconRandomness(ctx, round)
|
2020-10-07 18:52:37 +00:00
|
|
|
}
|