Skip to content

Commit 679f917

Browse files
committed
Fix receivers defined on cgo type aliases
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
1 parent 609c864 commit 679f917

File tree

6 files changed

+451
-387
lines changed

6 files changed

+451
-387
lines changed

cgo/bls.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ package cgo
99
import "C"
1010

1111
func Hash(message SliceRefUint8) *[96]byte {
12-
resp := C.hash(message)
12+
resp := ByteArray96{delegate: C.hash(message)}
1313
defer resp.destroy()
1414
return resp.copyAsArray()
1515
}
1616

1717
func Aggregate(flattenedSignatures SliceRefUint8) *[96]byte {
18-
resp := C.aggregate(flattenedSignatures)
18+
resp := ByteArray96{delegate: C.aggregate(flattenedSignatures)}
1919
defer resp.destroy()
2020
return resp.copyAsArray()
2121
}
@@ -31,31 +31,31 @@ func HashVerify(signature SliceRefUint8, flattenedMessages SliceRefUint8, messag
3131
}
3232

3333
func PrivateKeyGenerate() *[32]byte {
34-
resp := C.private_key_generate()
34+
resp := ByteArray32{delegate: C.private_key_generate()}
3535
defer resp.destroy()
3636
return resp.copyAsArray()
3737
}
3838

3939
func PrivateKeyGenerateWithSeed(rawSeed *ByteArray32) *[32]byte {
40-
resp := C.private_key_generate_with_seed(rawSeed)
40+
resp := ByteArray32{delegate: C.private_key_generate_with_seed(rawSeed.delegate)}
4141
defer resp.destroy()
4242
return resp.copyAsArray()
4343
}
4444

4545
func PrivateKeySign(rawPrivateKey SliceRefUint8, message SliceRefUint8) *[96]byte {
46-
resp := C.private_key_sign(rawPrivateKey, message)
46+
resp := ByteArray96{delegate: C.private_key_sign(rawPrivateKey, message)}
4747
defer resp.destroy()
4848
return resp.copyAsArray()
4949
}
5050

5151
func PrivateKeyPublicKey(rawPrivateKey SliceRefUint8) *[48]byte {
52-
resp := C.private_key_public_key(rawPrivateKey)
52+
resp := ByteArray48{delegate: C.private_key_public_key(rawPrivateKey)}
5353
defer resp.destroy()
5454
return resp.copyAsArray()
5555
}
5656

5757
func CreateZeroSignature() *[96]byte {
58-
resp := C.create_zero_signature()
58+
resp := ByteArray96{delegate: C.create_zero_signature()}
5959
defer resp.destroy()
6060
return resp.copyAsArray()
6161
}

cgo/fvm.go

Lines changed: 53 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -9,84 +9,91 @@ package cgo
99
import "C"
1010

1111
func CreateFvmMachine(fvmVersion FvmRegisteredVersion, chainEpoch, chainTimestamp, chainId, baseFeeHi, baseFeeLo, baseCircSupplyHi, baseCircSupplyLo, networkVersion uint64, stateRoot SliceRefUint8, tracing bool, blockstoreId, externsId uint64) (*FvmMachine, error) {
12-
resp := C.create_fvm_machine(
13-
fvmVersion,
14-
C.uint64_t(chainEpoch),
15-
C.uint64_t(chainTimestamp),
16-
C.uint64_t(chainId),
17-
C.uint64_t(baseFeeHi),
18-
C.uint64_t(baseFeeLo),
19-
C.uint64_t(baseCircSupplyHi),
20-
C.uint64_t(baseCircSupplyLo),
21-
C.uint32_t(networkVersion),
22-
stateRoot,
23-
C.bool(tracing),
24-
C.uint64_t(blockstoreId),
25-
C.uint64_t(externsId),
26-
)
12+
resp := &resultFvmMachine{
13+
delegate: C.create_fvm_machine(
14+
fvmVersion,
15+
C.uint64_t(chainEpoch),
16+
C.uint64_t(chainTimestamp),
17+
C.uint64_t(chainId),
18+
C.uint64_t(baseFeeHi),
19+
C.uint64_t(baseFeeLo),
20+
C.uint64_t(baseCircSupplyHi),
21+
C.uint64_t(baseCircSupplyLo),
22+
C.uint32_t(networkVersion),
23+
stateRoot,
24+
C.bool(tracing),
25+
C.uint64_t(blockstoreId),
26+
C.uint64_t(externsId),
27+
),
28+
}
2729
// take out the pointer from the result to ensure it doesn't get freed
28-
executor := resp.value
29-
resp.value = nil
30+
executor := resp.delegate.value
31+
resp.delegate.value = nil
3032
defer resp.destroy()
3133

3234
if err := CheckErr(resp); err != nil {
3335
return nil, err
3436
}
3537

36-
return executor, nil
38+
return &FvmMachine{delegate: executor}, nil
3739
}
3840

3941
func CreateFvmDebugMachine(fvmVersion FvmRegisteredVersion, chainEpoch, chainTimestamp, chainId, baseFeeHi, baseFeeLo, baseCircSupplyHi, baseCircSupplyLo, networkVersion uint64, stateRoot SliceRefUint8, actorRedirect SliceRefUint8, tracing bool, blockstoreId, externsId uint64) (*FvmMachine, error) {
40-
resp := C.create_fvm_debug_machine(
41-
fvmVersion,
42-
C.uint64_t(chainEpoch),
43-
C.uint64_t(chainTimestamp),
44-
C.uint64_t(chainId),
45-
C.uint64_t(baseFeeHi),
46-
C.uint64_t(baseFeeLo),
47-
C.uint64_t(baseCircSupplyHi),
48-
C.uint64_t(baseCircSupplyLo),
49-
C.uint32_t(networkVersion),
50-
stateRoot,
51-
actorRedirect,
52-
C.bool(tracing),
53-
C.uint64_t(blockstoreId),
54-
C.uint64_t(externsId),
55-
)
42+
resp := &resultFvmMachine{
43+
delegate: C.create_fvm_debug_machine(
44+
fvmVersion,
45+
C.uint64_t(chainEpoch),
46+
C.uint64_t(chainTimestamp),
47+
C.uint64_t(chainId),
48+
C.uint64_t(baseFeeHi),
49+
C.uint64_t(baseFeeLo),
50+
C.uint64_t(baseCircSupplyHi),
51+
C.uint64_t(baseCircSupplyLo),
52+
C.uint32_t(networkVersion),
53+
stateRoot,
54+
actorRedirect,
55+
C.bool(tracing),
56+
C.uint64_t(blockstoreId),
57+
C.uint64_t(externsId),
58+
),
59+
}
5660
// take out the pointer from the result to ensure it doesn't get freed
57-
executor := resp.value
58-
resp.value = nil
61+
executor := resp.delegate.value
62+
resp.delegate.value = nil
5963
defer resp.destroy()
6064

6165
if err := CheckErr(resp); err != nil {
6266
return nil, err
6367
}
6468

65-
return executor, nil
69+
return &FvmMachine{delegate: executor}, nil
6670
}
6771

6872
func FvmMachineExecuteMessage(executor *FvmMachine, message SliceRefUint8, chainLen, applyKind uint64) (FvmMachineExecuteResponseGo, error) {
69-
resp := C.fvm_machine_execute_message(
70-
executor,
71-
message,
72-
C.uint64_t(chainLen),
73-
C.uint64_t(applyKind),
74-
)
73+
resp := &resultFvmMachineExecuteResponse{
74+
delegate: C.fvm_machine_execute_message(
75+
executor.delegate,
76+
message,
77+
C.uint64_t(chainLen),
78+
C.uint64_t(applyKind),
79+
),
80+
}
7581
defer resp.destroy()
7682

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

81-
return resp.value.copy(), nil
87+
response := &FvmMachineExecuteResponse{delegate: &resp.delegate.value}
88+
return response.copy(), nil
8289
}
8390

8491
func FvmMachineFlush(executor *FvmMachine) ([]byte, error) {
85-
resp := C.fvm_machine_flush(executor)
92+
resp := &resultSliceBoxedUint8{delegate: C.fvm_machine_flush(executor.delegate)}
8693
defer resp.destroy()
8794

8895
if err := CheckErr(resp); err != nil {
8996
return nil, err
9097
}
91-
return resp.value.copy(), nil
98+
return SliceBoxedUint8{delegate: resp.delegate.value}.copy(), nil
9299
}

cgo/helpers.go

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,7 @@ func AsSliceRefUint64(goBytes []uint64) SliceRefUint64 {
5959

6060
func AllocSliceBoxedUint8(goBytes []byte) SliceBoxedUint8 {
6161
len := len(goBytes)
62-
63-
ptr := C.alloc_boxed_slice(C.size_t(len))
62+
ptr := SliceBoxedUint8{delegate: C.alloc_boxed_slice(C.size_t(len))}
6463
copy(ptr.slice(), goBytes)
6564

6665
return ptr
@@ -207,7 +206,10 @@ func AsByteArray32(goSlice []byte) ByteArray32 {
207206
l := len(goSlice)
208207
for idx := range goSlice {
209208
if idx < l {
210-
ary.idx[idx] = C.uchar(goSlice[idx])
209+
if ary.delegate == nil {
210+
ary.delegate = &C.uint8_32_array_t{}
211+
}
212+
ary.delegate.idx[idx] = C.uchar(goSlice[idx])
211213
}
212214
}
213215
return ary
@@ -227,49 +229,55 @@ func CheckErr(resp result) error {
227229

228230
func NewAggregationInputs(commR ByteArray32, commD ByteArray32, sectorId uint64, ticket ByteArray32, seed ByteArray32) AggregationInputs {
229231
return AggregationInputs{
230-
comm_r: commR,
231-
comm_d: commD,
232+
comm_r: *commR.delegate,
233+
comm_d: *commD.delegate,
232234
sector_id: C.uint64_t(sectorId),
233-
ticket: ticket,
234-
seed: seed,
235+
ticket: *ticket.delegate,
236+
seed: *seed.delegate,
235237
}
236238
}
237239

238240
func NewPrivateReplicaInfo(pp RegisteredPoStProof, cacheDirPath string, commR ByteArray32, replicaPath string, sectorId uint64) PrivateReplicaInfo {
239241
return PrivateReplicaInfo{
240-
registered_proof: pp,
241-
cache_dir_path: AllocSliceBoxedUint8([]byte(cacheDirPath)),
242-
replica_path: AllocSliceBoxedUint8([]byte(replicaPath)),
243-
sector_id: C.uint64_t(sectorId),
244-
comm_r: commR,
242+
delegate: C.PrivateReplicaInfo_t{
243+
registered_proof: pp,
244+
cache_dir_path: AllocSliceBoxedUint8([]byte(cacheDirPath)).delegate,
245+
replica_path: AllocSliceBoxedUint8([]byte(replicaPath)).delegate,
246+
sector_id: C.uint64_t(sectorId),
247+
comm_r: *commR.delegate,
248+
},
245249
}
246250
}
247251

248252
func NewPublicReplicaInfo(pp RegisteredPoStProof, commR ByteArray32, sectorId uint64) PublicReplicaInfo {
249253
return PublicReplicaInfo{
250254
registered_proof: pp,
251255
sector_id: C.uint64_t(sectorId),
252-
comm_r: commR,
256+
comm_r: *commR.delegate,
253257
}
254258
}
255259

256260
func NewPoStProof(pp RegisteredPoStProof, proof []byte) PoStProof {
257261
return PoStProof{
258-
registered_proof: pp,
259-
proof: AllocSliceBoxedUint8(proof),
262+
delegate: C.PoStProof_t{
263+
registered_proof: pp,
264+
proof: AllocSliceBoxedUint8(proof).delegate,
265+
},
260266
}
261267
}
262268

263269
func NewPublicPieceInfo(numBytes uint64, commP ByteArray32) PublicPieceInfo {
264270
return PublicPieceInfo{
265271
num_bytes: C.uint64_t(numBytes),
266-
comm_p: commP,
272+
comm_p: *commP.delegate,
267273
}
268274
}
269275

270276
func NewPartitionSnarkProof(pp RegisteredPoStProof, proof []byte) PartitionSnarkProof {
271277
return PartitionSnarkProof{
272-
registered_proof: pp,
273-
proof: AllocSliceBoxedUint8(proof),
278+
delegate: C.PartitionSnarkProof_t{
279+
registered_proof: pp,
280+
proof: AllocSliceBoxedUint8(proof).delegate,
281+
},
274282
}
275283
}

0 commit comments

Comments
 (0)