lotus/conformance/rand_record.go

107 lines
3.0 KiB
Go
Raw Normal View History

package conformance
import (
"context"
2020-10-07 19:12:35 +00:00
"fmt"
"sync"
"github.com/filecoin-project/go-state-types/network"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/crypto"
2020-10-07 19:12:35 +00:00
"github.com/filecoin-project/test-vectors/schema"
2021-04-06 11:36:16 +00:00
"github.com/filecoin-project/lotus/api/v0api"
"github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/chain/vm"
)
type RecordingRand struct {
reporter Reporter
2021-04-05 18:12:47 +00:00
api v0api.FullNode
2020-10-07 19:12:35 +00:00
// once guards the loading of the head tipset.
// can be removed when https://github.com/filecoin-project/lotus/issues/4223
// is fixed.
once sync.Once
head types.TipSetKey
lk sync.Mutex
recorded schema.Randomness
}
var _ vm.Rand = (*RecordingRand)(nil)
// NewRecordingRand returns a vm.Rand implementation that proxies calls to a
// full Lotus node via JSON-RPC, and records matching rules and responses so
// they can later be embedded in test vectors.
2021-04-05 18:12:47 +00:00
func NewRecordingRand(reporter Reporter, api v0api.FullNode) *RecordingRand {
return &RecordingRand{reporter: reporter, api: api}
}
2020-10-07 19:12:35 +00:00
func (r *RecordingRand) loadHead() {
head, err := r.api.ChainHead(context.Background())
if err != nil {
panic(fmt.Sprintf("could not fetch chain head while fetching randomness: %s", err))
}
r.head = head.Key()
}
func (r *RecordingRand) GetChainRandomness(ctx context.Context, nv network.Version, pers crypto.DomainSeparationTag, round abi.ChainEpoch, entropy []byte) ([]byte, error) {
2020-10-07 19:12:35 +00:00
r.once.Do(r.loadHead)
// FullNode's v0 ChainGetRandomnessFromTickets handles whether we should be looking forward or back
2020-10-07 19:12:35 +00:00
ret, err := r.api.ChainGetRandomnessFromTickets(ctx, r.head, pers, round, entropy)
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)
match := schema.RandomnessMatch{
On: schema.RandomnessRule{
Kind: schema.RandomnessChain,
DomainSeparationTag: int64(pers),
Epoch: int64(round),
Entropy: entropy,
},
Return: []byte(ret),
}
r.lk.Lock()
r.recorded = append(r.recorded, match)
r.lk.Unlock()
return ret, err
}
func (r *RecordingRand) GetBeaconRandomness(ctx context.Context, nv network.Version, pers crypto.DomainSeparationTag, round abi.ChainEpoch, entropy []byte) ([]byte, error) {
2020-10-07 19:12:35 +00:00
r.once.Do(r.loadHead)
2021-09-18 17:57:04 +00:00
ret, err := r.api.StateGetRandomnessFromBeacon(ctx, pers, round, entropy, 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)
match := schema.RandomnessMatch{
On: schema.RandomnessRule{
Kind: schema.RandomnessBeacon,
DomainSeparationTag: int64(pers),
Epoch: int64(round),
Entropy: entropy,
},
Return: []byte(ret),
}
r.lk.Lock()
r.recorded = append(r.recorded, match)
r.lk.Unlock()
return ret, err
}
func (r *RecordingRand) Recorded() schema.Randomness {
r.lk.Lock()
defer r.lk.Unlock()
return r.recorded
}