-
Notifications
You must be signed in to change notification settings - Fork 3
/
drivechain_linux.go
126 lines (114 loc) · 3.52 KB
/
drivechain_linux.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
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.ulong(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.ulong(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.ulong(w.Amount.Uint64()),
fee: C.ulong(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.ulong(amount)
cFee := C.ulong(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.ulong(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)
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))
}