-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Turns out there's more differences in cgo bindings between macOS, Linux and Windows. Instead of mucking around and trying to decipher the madness, we shove all differentiating cgo calls into separate calls for each platform.
- Loading branch information
1 parent
d87de93
commit c4cac72
Showing
5 changed files
with
384 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,126 @@ | ||
//go:build darwin | ||
// +build darwin | ||
|
||
package drivechain | ||
|
||
/* | ||
#cgo LDFLAGS: ./drivechain/target/debug/libdrivechain_eth.a -ldl -lm | ||
#include "./bindings.h" | ||
*/ | ||
import "C" | ||
import ( | ||
"strings" | ||
"unsafe" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
) | ||
|
||
func newDeposits(deposits []Deposit) C.Deposits { | ||
depositsMemory := C.malloc(C.size_t(len(deposits)) * C.size_t(unsafe.Sizeof(C.Deposit{}))) | ||
depositsSlice := (*[1<<30 - 1]C.Deposit)(depositsMemory) | ||
for i, deposit := range deposits { | ||
depositsSlice[i] = C.Deposit{ | ||
address: C.CString(strings.ToLower(deposit.Amount.String())), | ||
amount: C.ulonglong(deposit.Amount.Uint64()), | ||
} | ||
} | ||
return C.Deposits{ | ||
ptr: &depositsSlice[0], | ||
len: C.ulong(len(deposits)), | ||
} | ||
} | ||
|
||
func newRefundsFromHash(refunds []common.Hash) C.Refunds { | ||
refundsMemory := C.malloc(C.size_t(len(refunds)) * C.size_t(unsafe.Sizeof(C.Refund{}))) | ||
refundsSlice := (*[1<<30 - 1]C.Refund)(refundsMemory) | ||
for i, id := range refunds { | ||
cRefund := C.Refund{ | ||
id: C.CString(id.Hex()), | ||
} | ||
refundsSlice[i] = cRefund | ||
} | ||
return C.Refunds{ | ||
ptr: &refundsSlice[0], | ||
len: C.ulong(len(refunds)), | ||
} | ||
} | ||
|
||
func newRefunds(refunds []Refund) C.Refunds { | ||
refundsMemory := C.malloc(C.size_t(len(refunds)) * C.size_t(unsafe.Sizeof(C.Refund{}))) | ||
refundsSlice := (*[1<<30 - 1]C.Refund)(refundsMemory) | ||
for i, r := range refunds { | ||
cRefund := C.Refund{ | ||
id: C.CString(r.Id.Hex()), | ||
amount: C.ulonglong(r.Amount.Uint64()), | ||
} | ||
refundsSlice[i] = cRefund | ||
} | ||
return C.Refunds{ | ||
ptr: &refundsSlice[0], | ||
len: C.ulong(len(refunds)), | ||
} | ||
} | ||
|
||
func newWithdrawalsFromHash(withdrawals []common.Hash) C.Withdrawals { | ||
withdrawalsMemory := C.malloc(C.size_t(len(withdrawals)) * C.size_t(unsafe.Sizeof(C.Withdrawal{}))) | ||
withdrawalsSlice := (*[1<<30 - 1]C.Withdrawal)(withdrawalsMemory) | ||
for i, id := range withdrawals { | ||
cWithdrawal := C.Withdrawal{ | ||
id: C.CString(id.Hex()), | ||
} | ||
withdrawalsSlice[i] = cWithdrawal | ||
} | ||
|
||
return C.Withdrawals{ | ||
ptr: &withdrawalsSlice[0], | ||
len: C.ulong(len(withdrawals)), | ||
} | ||
} | ||
|
||
func newWithdrawals(withdrawals map[common.Hash]Withdrawal) C.Withdrawals { | ||
withdrawalsMemory := C.malloc(C.size_t(len(withdrawals)) * C.size_t(unsafe.Sizeof(C.Withdrawal{}))) | ||
withdrawalsSlice := (*[1<<30 - 1]C.Withdrawal)(withdrawalsMemory) | ||
{ | ||
i := 0 | ||
for id, w := range withdrawals { | ||
cWithdrawal := C.Withdrawal{ | ||
id: C.CString(id.Hex()), | ||
address: w.Address, | ||
amount: C.ulonglong(w.Amount.Uint64()), | ||
fee: C.ulonglong(w.Fee.Uint64()), | ||
} | ||
withdrawalsSlice[i] = cWithdrawal | ||
i += 1 | ||
} | ||
} | ||
return C.Withdrawals{ | ||
ptr: &withdrawalsSlice[0], | ||
len: C.ulong(len(withdrawals)), | ||
} | ||
} | ||
|
||
func createDeposit(address common.Address, amount uint64, fee uint64) bool { | ||
cAddress := C.CString(strings.ToLower(address.Hex())) | ||
cAmount := C.ulonglong(amount) | ||
cFee := C.ulonglong(fee) | ||
result := C.create_deposit(cAddress, cAmount, cFee) | ||
C.free(unsafe.Pointer(cAddress)) | ||
return bool(result) | ||
} | ||
|
||
func attemptBmm(criticalHash string, prevMainBlockHash string, amount uint64) { | ||
cCriticalHash := C.CString(criticalHash) | ||
cPrevMainBlockHash := C.CString(prevMainBlockHash) | ||
C.attempt_bmm(cCriticalHash, cPrevMainBlockHash, C.ulonglong(amount)) | ||
C.free(unsafe.Pointer(cCriticalHash)) | ||
C.free(unsafe.Pointer(cPrevMainBlockHash)) | ||
} | ||
|
||
func initBmmEngine(dbPath, host, rpcUser, rpcPassword string, port uint16) { | ||
cDbPath := C.CString(dbPath) | ||
cHost := C.CString(host) | ||
cRpcUser := C.CString(rpcUser) | ||
cRpcPassword := C.CString(rpcPassword) | ||
|
||
func newUlong(in uint64) C.ulonglong { | ||
return C.ulonglong(in) | ||
C.init(cDbPath, C.ulong(THIS_SIDECHAIN), cHost, C.ushort(port), cRpcUser, cRpcPassword) | ||
C.free(unsafe.Pointer(cDbPath)) | ||
C.free(unsafe.Pointer(cRpcUser)) | ||
C.free(unsafe.Pointer(cRpcPassword)) | ||
} |
Oops, something went wrong.