fix: refactor rand_replay

This commit is contained in:
Aayush 2023-08-21 16:26:51 -04:00
parent 2e9f4dc2ae
commit a6213f8825
2 changed files with 22 additions and 32 deletions

View File

@ -6,7 +6,6 @@ import (
"sync"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/crypto"
"github.com/filecoin-project/test-vectors/schema"
"github.com/filecoin-project/lotus/api/v0api"
@ -44,22 +43,20 @@ func (r *RecordingRand) loadHead() {
r.head = head.Key()
}
func (r *RecordingRand) GetChainRandomness(ctx context.Context, pers crypto.DomainSeparationTag, round abi.ChainEpoch, entropy []byte) ([]byte, error) {
func (r *RecordingRand) GetChainRandomness(ctx context.Context, round abi.ChainEpoch) ([32]byte, error) {
r.once.Do(r.loadHead)
// FullNode's v0 ChainGetRandomnessFromTickets handles whether we should be looking forward or back
ret, err := r.api.ChainGetRandomnessFromTickets(ctx, r.head, pers, round, entropy)
ret, err := r.api.ChainGetRandomnessFromTickets(ctx, r.head, round)
if err != nil {
return ret, err
}
r.reporter.Logf("fetched and recorded chain randomness for: dst=%d, epoch=%d, entropy=%x, result=%x", pers, round, entropy, ret)
r.reporter.Logf("fetched and recorded chain randomness for: epoch=%d, result=%x", round, ret)
match := schema.RandomnessMatch{
On: schema.RandomnessRule{
Kind: schema.RandomnessChain,
DomainSeparationTag: int64(pers),
Epoch: int64(round),
Entropy: entropy,
Kind: schema.RandomnessChain,
Epoch: int64(round),
},
Return: []byte(ret),
}
@ -70,21 +67,19 @@ func (r *RecordingRand) GetChainRandomness(ctx context.Context, pers crypto.Doma
return ret, err
}
func (r *RecordingRand) GetBeaconRandomness(ctx context.Context, pers crypto.DomainSeparationTag, round abi.ChainEpoch, entropy []byte) ([]byte, error) {
func (r *RecordingRand) GetBeaconRandomness(ctx context.Context, round abi.ChainEpoch) ([32]byte, error) {
r.once.Do(r.loadHead)
ret, err := r.api.StateGetRandomnessFromBeacon(ctx, pers, round, entropy, r.head)
ret, err := r.api.StateGetRandomnessFromBeacon(ctx, round, r.head)
if err != nil {
return ret, err
}
r.reporter.Logf("fetched and recorded beacon randomness for: dst=%d, epoch=%d, entropy=%x, result=%x", pers, round, entropy, ret)
r.reporter.Logf("fetched and recorded beacon randomness for: epoch=%d, result=%x", round, ret)
match := schema.RandomnessMatch{
On: schema.RandomnessRule{
Kind: schema.RandomnessBeacon,
DomainSeparationTag: int64(pers),
Epoch: int64(round),
Entropy: entropy,
Kind: schema.RandomnessBeacon,
Epoch: int64(round),
},
Return: []byte(ret),
}

View File

@ -5,7 +5,6 @@ import (
"context"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/crypto"
"github.com/filecoin-project/test-vectors/schema"
"github.com/filecoin-project/lotus/chain/vm"
@ -42,38 +41,34 @@ func (r *ReplayingRand) match(requested schema.RandomnessRule) ([]byte, bool) {
return nil, false
}
func (r *ReplayingRand) GetChainRandomness(ctx context.Context, pers crypto.DomainSeparationTag, round abi.ChainEpoch, entropy []byte) ([]byte, error) {
func (r *ReplayingRand) GetChainRandomness(ctx context.Context, round abi.ChainEpoch) ([32]byte, error) {
rule := schema.RandomnessRule{
Kind: schema.RandomnessChain,
DomainSeparationTag: int64(pers),
Epoch: int64(round),
Entropy: entropy,
Kind: schema.RandomnessChain,
Epoch: int64(round),
}
if ret, ok := r.match(rule); ok {
r.reporter.Logf("returning saved chain randomness: dst=%d, epoch=%d, entropy=%x, result=%x", pers, round, entropy, ret)
r.reporter.Logf("returning saved chain randomness: epoch=%d, result=%x", round, ret)
return ret, nil
}
r.reporter.Logf("returning fallback chain randomness: dst=%d, epoch=%d, entropy=%x", pers, round, entropy)
r.reporter.Logf("returning fallback chain randomness: epoch=%d", round)
return r.fallback.GetChainRandomness(ctx, pers, round, entropy)
return r.fallback.GetChainRandomness(ctx, round)
}
func (r *ReplayingRand) GetBeaconRandomness(ctx context.Context, pers crypto.DomainSeparationTag, round abi.ChainEpoch, entropy []byte) ([]byte, error) {
func (r *ReplayingRand) GetBeaconRandomness(ctx context.Context, round abi.ChainEpoch) ([32]byte, error) {
rule := schema.RandomnessRule{
Kind: schema.RandomnessBeacon,
DomainSeparationTag: int64(pers),
Epoch: int64(round),
Entropy: entropy,
Kind: schema.RandomnessBeacon,
Epoch: int64(round),
}
if ret, ok := r.match(rule); ok {
r.reporter.Logf("returning saved beacon randomness: dst=%d, epoch=%d, entropy=%x, result=%x", pers, round, entropy, ret)
r.reporter.Logf("returning saved beacon randomness: epoch=%d, result=%x", round, ret)
return ret, nil
}
r.reporter.Logf("returning fallback beacon randomness: dst=%d, epoch=%d, entropy=%x", pers, round, entropy)
r.reporter.Logf("returning fallback beacon randomness: epoch=%d, ", round)
return r.fallback.GetBeaconRandomness(ctx, pers, round, entropy)
return r.fallback.GetBeaconRandomness(ctx, round)
}