Skip to content

Commit

Permalink
Fix receivers defined on cgo type aliases
Browse files Browse the repository at this point in the history
As of Go 1.24, the compiler will now always fail if receivers are
defined on cgo-generated type aliases. Fix the issue by wrapping the
cgo-generated types and access to underlying type via `delegate` field.
Note that such types cannot be embedded in go structs. Hence, the
explicit field.

See:
 * https://tip.golang.org/doc/go1.24#compiler
  • Loading branch information
masih committed Feb 19, 2025
1 parent 609c864 commit 679f917
Show file tree
Hide file tree
Showing 6 changed files with 451 additions and 387 deletions.
14 changes: 7 additions & 7 deletions cgo/bls.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ package cgo
import "C"

func Hash(message SliceRefUint8) *[96]byte {
resp := C.hash(message)
resp := ByteArray96{delegate: C.hash(message)}
defer resp.destroy()
return resp.copyAsArray()
}

func Aggregate(flattenedSignatures SliceRefUint8) *[96]byte {
resp := C.aggregate(flattenedSignatures)
resp := ByteArray96{delegate: C.aggregate(flattenedSignatures)}
defer resp.destroy()
return resp.copyAsArray()
}
Expand All @@ -31,31 +31,31 @@ func HashVerify(signature SliceRefUint8, flattenedMessages SliceRefUint8, messag
}

func PrivateKeyGenerate() *[32]byte {
resp := C.private_key_generate()
resp := ByteArray32{delegate: C.private_key_generate()}
defer resp.destroy()
return resp.copyAsArray()
}

func PrivateKeyGenerateWithSeed(rawSeed *ByteArray32) *[32]byte {
resp := C.private_key_generate_with_seed(rawSeed)
resp := ByteArray32{delegate: C.private_key_generate_with_seed(rawSeed.delegate)}
defer resp.destroy()
return resp.copyAsArray()
}

func PrivateKeySign(rawPrivateKey SliceRefUint8, message SliceRefUint8) *[96]byte {
resp := C.private_key_sign(rawPrivateKey, message)
resp := ByteArray96{delegate: C.private_key_sign(rawPrivateKey, message)}
defer resp.destroy()
return resp.copyAsArray()
}

func PrivateKeyPublicKey(rawPrivateKey SliceRefUint8) *[48]byte {
resp := C.private_key_public_key(rawPrivateKey)
resp := ByteArray48{delegate: C.private_key_public_key(rawPrivateKey)}
defer resp.destroy()
return resp.copyAsArray()
}

func CreateZeroSignature() *[96]byte {
resp := C.create_zero_signature()
resp := ByteArray96{delegate: C.create_zero_signature()}
defer resp.destroy()
return resp.copyAsArray()
}
99 changes: 53 additions & 46 deletions cgo/fvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,84 +9,91 @@ package cgo
import "C"

func CreateFvmMachine(fvmVersion FvmRegisteredVersion, chainEpoch, chainTimestamp, chainId, baseFeeHi, baseFeeLo, baseCircSupplyHi, baseCircSupplyLo, networkVersion uint64, stateRoot SliceRefUint8, tracing bool, blockstoreId, externsId uint64) (*FvmMachine, error) {
resp := C.create_fvm_machine(
fvmVersion,
C.uint64_t(chainEpoch),
C.uint64_t(chainTimestamp),
C.uint64_t(chainId),
C.uint64_t(baseFeeHi),
C.uint64_t(baseFeeLo),
C.uint64_t(baseCircSupplyHi),
C.uint64_t(baseCircSupplyLo),
C.uint32_t(networkVersion),
stateRoot,
C.bool(tracing),
C.uint64_t(blockstoreId),
C.uint64_t(externsId),
)
resp := &resultFvmMachine{
delegate: C.create_fvm_machine(
fvmVersion,
C.uint64_t(chainEpoch),
C.uint64_t(chainTimestamp),
C.uint64_t(chainId),
C.uint64_t(baseFeeHi),
C.uint64_t(baseFeeLo),
C.uint64_t(baseCircSupplyHi),
C.uint64_t(baseCircSupplyLo),
C.uint32_t(networkVersion),
stateRoot,
C.bool(tracing),
C.uint64_t(blockstoreId),
C.uint64_t(externsId),
),
}
// take out the pointer from the result to ensure it doesn't get freed
executor := resp.value
resp.value = nil
executor := resp.delegate.value
resp.delegate.value = nil
defer resp.destroy()

if err := CheckErr(resp); err != nil {
return nil, err
}

return executor, nil
return &FvmMachine{delegate: executor}, nil
}

func CreateFvmDebugMachine(fvmVersion FvmRegisteredVersion, chainEpoch, chainTimestamp, chainId, baseFeeHi, baseFeeLo, baseCircSupplyHi, baseCircSupplyLo, networkVersion uint64, stateRoot SliceRefUint8, actorRedirect SliceRefUint8, tracing bool, blockstoreId, externsId uint64) (*FvmMachine, error) {
resp := C.create_fvm_debug_machine(
fvmVersion,
C.uint64_t(chainEpoch),
C.uint64_t(chainTimestamp),
C.uint64_t(chainId),
C.uint64_t(baseFeeHi),
C.uint64_t(baseFeeLo),
C.uint64_t(baseCircSupplyHi),
C.uint64_t(baseCircSupplyLo),
C.uint32_t(networkVersion),
stateRoot,
actorRedirect,
C.bool(tracing),
C.uint64_t(blockstoreId),
C.uint64_t(externsId),
)
resp := &resultFvmMachine{
delegate: C.create_fvm_debug_machine(
fvmVersion,
C.uint64_t(chainEpoch),
C.uint64_t(chainTimestamp),
C.uint64_t(chainId),
C.uint64_t(baseFeeHi),
C.uint64_t(baseFeeLo),
C.uint64_t(baseCircSupplyHi),
C.uint64_t(baseCircSupplyLo),
C.uint32_t(networkVersion),
stateRoot,
actorRedirect,
C.bool(tracing),
C.uint64_t(blockstoreId),
C.uint64_t(externsId),
),
}
// take out the pointer from the result to ensure it doesn't get freed
executor := resp.value
resp.value = nil
executor := resp.delegate.value
resp.delegate.value = nil
defer resp.destroy()

if err := CheckErr(resp); err != nil {
return nil, err
}

return executor, nil
return &FvmMachine{delegate: executor}, nil
}

func FvmMachineExecuteMessage(executor *FvmMachine, message SliceRefUint8, chainLen, applyKind uint64) (FvmMachineExecuteResponseGo, error) {
resp := C.fvm_machine_execute_message(
executor,
message,
C.uint64_t(chainLen),
C.uint64_t(applyKind),
)
resp := &resultFvmMachineExecuteResponse{
delegate: C.fvm_machine_execute_message(
executor.delegate,
message,
C.uint64_t(chainLen),
C.uint64_t(applyKind),
),
}
defer resp.destroy()

if err := CheckErr(resp); err != nil {
return FvmMachineExecuteResponseGo{}, err
}

return resp.value.copy(), nil
response := &FvmMachineExecuteResponse{delegate: &resp.delegate.value}
return response.copy(), nil
}

func FvmMachineFlush(executor *FvmMachine) ([]byte, error) {
resp := C.fvm_machine_flush(executor)
resp := &resultSliceBoxedUint8{delegate: C.fvm_machine_flush(executor.delegate)}
defer resp.destroy()

if err := CheckErr(resp); err != nil {
return nil, err
}
return resp.value.copy(), nil
return SliceBoxedUint8{delegate: resp.delegate.value}.copy(), nil
}
44 changes: 26 additions & 18 deletions cgo/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ func AsSliceRefUint64(goBytes []uint64) SliceRefUint64 {

func AllocSliceBoxedUint8(goBytes []byte) SliceBoxedUint8 {
len := len(goBytes)

ptr := C.alloc_boxed_slice(C.size_t(len))
ptr := SliceBoxedUint8{delegate: C.alloc_boxed_slice(C.size_t(len))}
copy(ptr.slice(), goBytes)

return ptr
Expand Down Expand Up @@ -207,7 +206,10 @@ func AsByteArray32(goSlice []byte) ByteArray32 {
l := len(goSlice)
for idx := range goSlice {
if idx < l {
ary.idx[idx] = C.uchar(goSlice[idx])
if ary.delegate == nil {
ary.delegate = &C.uint8_32_array_t{}
}
ary.delegate.idx[idx] = C.uchar(goSlice[idx])
}
}
return ary
Expand All @@ -227,49 +229,55 @@ func CheckErr(resp result) error {

func NewAggregationInputs(commR ByteArray32, commD ByteArray32, sectorId uint64, ticket ByteArray32, seed ByteArray32) AggregationInputs {
return AggregationInputs{
comm_r: commR,
comm_d: commD,
comm_r: *commR.delegate,
comm_d: *commD.delegate,
sector_id: C.uint64_t(sectorId),
ticket: ticket,
seed: seed,
ticket: *ticket.delegate,
seed: *seed.delegate,
}
}

func NewPrivateReplicaInfo(pp RegisteredPoStProof, cacheDirPath string, commR ByteArray32, replicaPath string, sectorId uint64) PrivateReplicaInfo {
return PrivateReplicaInfo{
registered_proof: pp,
cache_dir_path: AllocSliceBoxedUint8([]byte(cacheDirPath)),
replica_path: AllocSliceBoxedUint8([]byte(replicaPath)),
sector_id: C.uint64_t(sectorId),
comm_r: commR,
delegate: C.PrivateReplicaInfo_t{
registered_proof: pp,
cache_dir_path: AllocSliceBoxedUint8([]byte(cacheDirPath)).delegate,
replica_path: AllocSliceBoxedUint8([]byte(replicaPath)).delegate,
sector_id: C.uint64_t(sectorId),
comm_r: *commR.delegate,
},
}
}

func NewPublicReplicaInfo(pp RegisteredPoStProof, commR ByteArray32, sectorId uint64) PublicReplicaInfo {
return PublicReplicaInfo{
registered_proof: pp,
sector_id: C.uint64_t(sectorId),
comm_r: commR,
comm_r: *commR.delegate,
}
}

func NewPoStProof(pp RegisteredPoStProof, proof []byte) PoStProof {
return PoStProof{
registered_proof: pp,
proof: AllocSliceBoxedUint8(proof),
delegate: C.PoStProof_t{
registered_proof: pp,
proof: AllocSliceBoxedUint8(proof).delegate,
},
}
}

func NewPublicPieceInfo(numBytes uint64, commP ByteArray32) PublicPieceInfo {
return PublicPieceInfo{
num_bytes: C.uint64_t(numBytes),
comm_p: commP,
comm_p: *commP.delegate,
}
}

func NewPartitionSnarkProof(pp RegisteredPoStProof, proof []byte) PartitionSnarkProof {
return PartitionSnarkProof{
registered_proof: pp,
proof: AllocSliceBoxedUint8(proof),
delegate: C.PartitionSnarkProof_t{
registered_proof: pp,
proof: AllocSliceBoxedUint8(proof).delegate,
},
}
}
Loading

0 comments on commit 679f917

Please sign in to comment.