Skip to content

Commit 8c79a5f

Browse files
authored
Merge pull request #412 from filecoin-project/asr/randomness
FVM: Simplify Randomness externs, perform hashing on the Rust side
2 parents ba86847 + caab733 commit 8c79a5f

File tree

6 files changed

+243
-224
lines changed

6 files changed

+243
-224
lines changed

cgo/extern.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,11 @@ import (
1111
"github.com/filecoin-project/go-address"
1212

1313
"github.com/filecoin-project/go-state-types/abi"
14-
"github.com/filecoin-project/go-state-types/crypto"
1514
)
1615

1716
//export cgo_extern_get_chain_randomness
1817
func cgo_extern_get_chain_randomness(
19-
handle C.uint64_t, pers C.int64_t, round C.int64_t,
20-
entropy C.buf_t, entropyLen C.int32_t,
18+
handle C.uint64_t, round C.int64_t,
2119
output C.buf_t,
2220
) (res C.int32_t) {
2321
defer func() {
@@ -33,11 +31,11 @@ func cgo_extern_get_chain_randomness(
3331
return ErrInvalidHandle
3432
}
3533

36-
rand, err := externs.GetChainRandomness(ctx, crypto.DomainSeparationTag(pers), abi.ChainEpoch(round), C.GoBytes(unsafe.Pointer(entropy), entropyLen))
34+
rand, err := externs.GetChainRandomness(ctx, abi.ChainEpoch(round))
3735

3836
switch err {
3937
case nil:
40-
copy(out[:], rand)
38+
copy(out[:], rand[:])
4139
return 0
4240
default:
4341
return ErrIO
@@ -46,8 +44,7 @@ func cgo_extern_get_chain_randomness(
4644

4745
//export cgo_extern_get_beacon_randomness
4846
func cgo_extern_get_beacon_randomness(
49-
handle C.uint64_t, pers C.int64_t, round C.int64_t,
50-
entropy C.buf_t, entropyLen C.int32_t,
47+
handle C.uint64_t, round C.int64_t,
5148
output C.buf_t,
5249
) (res C.int32_t) {
5350
defer func() {
@@ -63,11 +60,11 @@ func cgo_extern_get_beacon_randomness(
6360
return ErrInvalidHandle
6461
}
6562

66-
rand, err := externs.GetBeaconRandomness(ctx, crypto.DomainSeparationTag(pers), abi.ChainEpoch(round), C.GoBytes(unsafe.Pointer(entropy), entropyLen))
63+
rand, err := externs.GetBeaconRandomness(ctx, abi.ChainEpoch(round))
6764

6865
switch err {
6966
case nil:
70-
copy(out[:], rand)
67+
copy(out[:], rand[:])
7168
return 0
7269
default:
7370
return ErrIO

cgo/interface.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55

66
"github.com/filecoin-project/go-address"
77
"github.com/filecoin-project/go-state-types/abi"
8-
"github.com/filecoin-project/go-state-types/crypto"
98
"github.com/ipfs/go-cid"
109
blockstore "github.com/ipfs/go-ipfs-blockstore"
1110
)
@@ -29,8 +28,8 @@ const (
2928
)
3029

3130
type Externs interface {
32-
GetChainRandomness(ctx context.Context, personalization crypto.DomainSeparationTag, epoch abi.ChainEpoch, entropy []byte) ([]byte, error)
33-
GetBeaconRandomness(ctx context.Context, personalization crypto.DomainSeparationTag, epoch abi.ChainEpoch, entropy []byte) ([]byte, error)
31+
GetChainRandomness(ctx context.Context, epoch abi.ChainEpoch) ([32]byte, error)
32+
GetBeaconRandomness(ctx context.Context, epoch abi.ChainEpoch) ([32]byte, error)
3433
VerifyConsensusFault(ctx context.Context, h1, h2, extra []byte) (*ConsensusFault, int64)
3534
TipsetCid(ctx context.Context, epoch abi.ChainEpoch) (cid.Cid, error)
3635

0 commit comments

Comments
 (0)