Skip to content

Commit

Permalink
[cgo] refs fibercrypto#116 Finalized coin.block and `coin.transact…
Browse files Browse the repository at this point in the history
…ions`
  • Loading branch information
Maykel Arias Torres committed Jan 17, 2020
1 parent 6c41926 commit 8bb1585
Show file tree
Hide file tree
Showing 6 changed files with 687 additions and 621 deletions.
5 changes: 5 additions & 0 deletions include/skytypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,11 @@ typedef Handle Hash_Handle;
* */
typedef Handle UnspentOutputsSummary_Handle;

/**
* Handle for coin.SortableTransactions
* */
typedef Handle SortableTransactions__Handle;

typedef GoUint32_ (*FeeCalcFunc)(Transaction__Handle handle, GoUint64_* pFee, void* context);

typedef struct {
Expand Down
3 changes: 2 additions & 1 deletion lib/cgo/cipher.address.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package main

import (
cipher "github.com/SkycoinProject/skycoin/src/cipher"
"reflect"
"unsafe"

cipher "github.com/SkycoinProject/skycoin/src/cipher"
)

/*
Expand Down
259 changes: 232 additions & 27 deletions lib/cgo/coin.block.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package main

import (
cipher "github.com/SkycoinProject/skycoin/src/cipher"
coin "github.com/SkycoinProject/skycoin/src/coin"
"errors"
"reflect"
"unsafe"

cipher "github.com/SkycoinProject/skycoin/src/cipher"
coin "github.com/SkycoinProject/skycoin/src/coin"
)

/*
Expand Down Expand Up @@ -33,37 +35,36 @@ func SKY_coin_SignedBlock_VerifySignature(_b *C.SignedBlock__Handle, _pubkey *C.
return
}

//export SKY_coin_NewBlock
func SKY_coin_NewBlock(_prev *C.Block__Handle, _currentTime uint64, _uxHash *C.cipher__SHA256, _txns *C.Transactions__Handle, _calc *C.coin__FeeCalculator, _arg5 *C.Block__Handle) (____error_code uint32) {
__prev, okprev := lookupBlockHandle(*_prev)
if !okprev {
func SKY_coin_NewBlock(_b C.Block__Handle, _currentTime uint64, _hash *C.cipher__SHA256, _txns C.Transactions__Handle, pFeeCalc *C.FeeCalculator, _arg2 *C.Block__Handle) (____error_code uint32) {
feeCalc := func(pTx *coin.Transaction) (uint64, error) {
var fee C.GoUint64_
handle := registerTransactionHandle(pTx)
result := C.callFeeCalculator(pFeeCalc, handle, &fee)
closeHandle(Handle(handle))
if result == SKY_OK {
return uint64(fee), nil
} else {
err := errorFromLibCode(uint32(result))
if err == nil {
err = errors.New("Error in libskycoin fee calculator")
}
return 0, err
}
}

b, ok := lookupBlockHandle(_b)
if !ok {
____error_code = SKY_BAD_HANDLE
return
}
prev := *__prev
currentTime := _currentTime
uxHash := *(*cipher.SHA256)(unsafe.Pointer(_uxHash))
__txns, oktxns := lookupTransactionsHandle(*_txns)
if !oktxns {
hash := *(*cipher.SHA256)(unsafe.Pointer(_hash))
txns, ok := lookupTransactionsHandle(_txns)
if !ok {
____error_code = SKY_BAD_HANDLE
return
}
txns := *__txns
calc := *(*coin.FeeCalculator)(unsafe.Pointer(_calc))
__arg5, ____return_err := coin.NewBlock(prev, currentTime, uxHash, txns, calc)
____error_code = libErrorCode(____return_err)
if ____return_err == nil {
*_arg5 = registerBlockHandle(__arg5)
}
return
}

//export SKY_coin_NewGenesisBlock
func SKY_coin_NewGenesisBlock(_genesisAddr *C.cipher__Address, _genesisCoins, _timestamp uint64, _arg2 *C.Block__Handle) (____error_code uint32) {
genesisAddr := *(*cipher.Address)(unsafe.Pointer(_genesisAddr))
genesisCoins := _genesisCoins
timestamp := _timestamp
__arg2, ____return_err := coin.NewGenesisBlock(genesisAddr, genesisCoins, timestamp)
currentTime := uint64(_currentTime)
__arg2, ____return_err := coin.NewBlock(*b, currentTime, hash, *txns, feeCalc)
____error_code = libErrorCode(____return_err)
if ____return_err == nil {
*_arg2 = registerBlockHandle(__arg2)
Expand Down Expand Up @@ -254,3 +255,207 @@ func SKY_coin_CreateUnspent(_bh *C.BlockHeader__Handle, _txn *C.Transaction__Han
}
return
}

// Helpers

//export SKY_coin_GetBlockObject
func SKY_coin_GetBlockObject(_b C.Block__Handle, _p **C.coin__Block) (____error_code uint32) {
b, ok := lookupBlockHandle(_b)
if !ok {
____error_code = SKY_BAD_HANDLE
} else {
*_p = (*C.coin__Block)(unsafe.Pointer(b))
}
return
}

//export SKY_coin_GetBlockBody
func SKY_coin_GetBlockBody(_b C.Block__Handle, _p *C.BlockBody__Handle) (____error_code uint32) {
b, ok := lookupBlockHandle(_b)
if !ok {
____error_code = SKY_BAD_HANDLE
} else {
*_p = registerBlockBodyHandle(&b.Body)
}
return
}

//export SKY_coin_NewEmptyBlock
func SKY_coin_NewEmptyBlock(_txns C.Transactions__Handle, handle *C.Block__Handle) (____error_code uint32) {
txns, ok := lookupTransactionsHandle(_txns)
if !ok {
____error_code = SKY_BAD_HANDLE
return
}
body := coin.BlockBody{
Transactions: *txns,
}
block := coin.Block{
Body: body,
Head: coin.BlockHeader{
Version: 0x02,
Time: 100,
BkSeq: 0,
Fee: 10,
PrevHash: cipher.SHA256{},
BodyHash: body.Hash(),
}}
*handle = registerBlockHandle(&block)
return
}

//export SKY_coin_Block_GetBlockHeader
func SKY_coin_Block_GetBlockHeader(_b C.Block__Handle, _bh *C.BlockHeader__Handle) (____error_code uint32) {
b, ok := lookupBlockHandle(_b)
if !ok {
____error_code = SKY_BAD_HANDLE
} else {
*_bh = registerBlockHeaderHandle(&b.Head)
}
return
}

//export SKY_coin_GetBlockHeaderObject
func SKY_coin_GetBlockHeaderObject(_b C.BlockHeader__Handle, _p **C.coin__BlockHeader) (____error_code uint32) {
b, ok := lookupBlockHeaderHandle(_b)
if !ok {
____error_code = SKY_BAD_HANDLE
} else {
*_p = (*C.coin__BlockHeader)(unsafe.Pointer(b))
}
return
}

//export SKY_coin_BlockHeader_Time
func SKY_coin_BlockHeader_Time(_b C.BlockHeader__Handle, _arg0 *uint64) (____error_code uint32) {
b, ok := lookupBlockHeaderHandle(_b)
if !ok {
____error_code = SKY_BAD_HANDLE
} else {
*_arg0 = uint64(b.Time)
}
return
}

//export SKY_coin_BlockHeader_BkSeq
func SKY_coin_BlockHeader_BkSeq(_b C.BlockHeader__Handle, _arg0 *uint64) (____error_code uint32) {
b, ok := lookupBlockHeaderHandle(_b)
if !ok {
____error_code = SKY_BAD_HANDLE
} else {
*_arg0 = uint64(b.BkSeq)
}
return
}

//export SKY_coin_BlockHeader_UxHash
func SKY_coin_BlockHeader_UxHash(_b C.BlockHeader__Handle, _arg0 *C.cipher__SHA256) (____error_code uint32) {
b, ok := lookupBlockHeaderHandle(_b)
if !ok {
____error_code = SKY_BAD_HANDLE
} else {
*_arg0 = *(*C.cipher__SHA256)(unsafe.Pointer(&b.UxHash))
}
return
}

//export SKY_coin_BlockHeader_Fee
func SKY_coin_BlockHeader_Fee(_b C.BlockHeader__Handle, _arg0 *uint64) (____error_code uint32) {
b, ok := lookupBlockHeaderHandle(_b)
if !ok {
____error_code = SKY_BAD_HANDLE
} else {
*_arg0 = uint64(b.Fee)
}
return
}

//export SKY_coin_BlockBody_Transactions
func SKY_coin_BlockBody_Transactions(_bb C.BlockBody__Handle, _arg0 *C.Transactions__Handle) (____error_code uint32) {
bb, ok := lookupBlockBodyHandle(_bb)
if !ok {
____error_code = SKY_BAD_HANDLE
return
}
__arg0 := bb.Transactions
*_arg0 = registerTransactionsHandle(&__arg0)
return
}

//export SKY_coin_BlockHeader_SetTime
// nolint megacheck
func SKY_coin_BlockHeader_SetTime(_bh C.BlockHeader__Handle, _arg0 uint64) (____error_code uint32) {

bh, ok := lookupBlockHeaderHandle(_bh)
if !ok {
____error_code = SKY_BAD_HANDLE
return
}
bh.Time = uint64(_arg0)
_bh = registerBlockHeaderHandle(bh)
return
}

//export SKY_coin_BlockHeader_SetBkSeq
// nolint megacheck
func SKY_coin_BlockHeader_SetBkSeq(_bh C.BlockHeader__Handle, _arg0 uint64) (____error_code uint32) {

bh, ok := lookupBlockHeaderHandle(_bh)
if !ok {
____error_code = SKY_BAD_HANDLE
return
}
bh.BkSeq = uint64(_arg0)
_bh = registerBlockHeaderHandle(bh)
return
}

//export SKY_coin_BlockHeader_SetFee
// nolint megacheck
func SKY_coin_BlockHeader_SetFee(_bh C.BlockHeader__Handle, _arg0 uint64) (____error_code uint32) {

bh, ok := lookupBlockHeaderHandle(_bh)
if !ok {
____error_code = SKY_BAD_HANDLE
return
}
bh.Fee = uint64(_arg0)
_bh = registerBlockHeaderHandle(bh)
return
}

//export SKY_coin_BlockHeader_SetVersion
// nolint megacheck
func SKY_coin_BlockHeader_SetVersion(_bh C.BlockHeader__Handle, _arg0 uint32) (____error_code uint32) {

bh, ok := lookupBlockHeaderHandle(_bh)
if !ok {
____error_code = SKY_BAD_HANDLE
return
}
bh.Version = uint32(_arg0)
_bh = registerBlockHeaderHandle(bh)
return
}

//export SKY_coin_BlockHeader_Version
func SKY_coin_BlockHeader_Version(_b C.BlockHeader__Handle, _arg0 *uint32) (____error_code uint32) {
b, ok := lookupBlockHeaderHandle(_b)
if !ok {
____error_code = SKY_BAD_HANDLE
} else {
*_arg0 = uint32(b.Version)
}
return
}

//export SKY_coin_BlockHeader_PrevHash
func SKY_coin_BlockHeader_PrevHash(_b C.BlockHeader__Handle, _arg0 *C.cipher__SHA256) (____error_code uint32) {
b, ok := lookupBlockHeaderHandle(_b)
if !ok {
____error_code = SKY_BAD_HANDLE
} else {
*_arg0 = *(*C.cipher__SHA256)(unsafe.Pointer(&b.PrevHash))
}
return
}
Loading

0 comments on commit 8bb1585

Please sign in to comment.