error handling to C

This commit is contained in:
Daniel Whitenack 2016-06-30 08:23:07 -05:00
parent 8dcb47d97f
commit e95e22f067
2 changed files with 41 additions and 18 deletions

View File

@ -11,13 +11,6 @@ import (
// unlock that account // unlock that account
func TestAccountBindings(t *testing.T) { func TestAccountBindings(t *testing.T) {
// create an account
address, _, err := createAccount("badpassword", ".ethereumtest/keystore")
if err != nil {
fmt.Println(err.Error())
t.Error("Test failed: could not create account")
}
// start geth node and wait for it to initialize // start geth node and wait for it to initialize
go createAndStartNode(".ethereumtest") go createAndStartNode(".ethereumtest")
time.Sleep(5 * time.Second) time.Sleep(5 * time.Second)
@ -25,6 +18,13 @@ func TestAccountBindings(t *testing.T) {
t.Error("Test failed: could not start geth node") t.Error("Test failed: could not start geth node")
} }
// create an account
address, _, err := createAccount("badpassword", ".ethereumtest/keystore")
if err != nil {
fmt.Println(err.Error())
t.Error("Test failed: could not create account")
}
// unlock the created account // unlock the created account
err = unlockAccount(address, "badpassword", 10) err = unlockAccount(address, "badpassword", 10)
if err != nil { if err != nil {

View File

@ -9,20 +9,27 @@ import (
//export CreateAccount //export CreateAccount
func CreateAccount(password, keydir *C.char) *C.char { func CreateAccount(password, keydir *C.char) *C.char {
// This is equivalent to creating an account from the command line, // This is equivalent to creating an account from the command line,
// just modified to handle the function arg passing // just modified to handle the function arg passing
address, pubKey, err := createAccount(C.GoString(password), C.GoString(keydir)) address, pubKey, err := createAccount(C.GoString(password), C.GoString(keydir))
var errString string
if err != nil {
fmt.Fprintln(os.Stderr, err)
errString = err.Error()
} else {
errString = ""
}
out := AccountInfo{ out := AccountInfo{
Address: address, Address: address,
PubKey: pubKey, PubKey: pubKey,
Error: err.Error(), Error: errString,
}
if err != nil {
fmt.Fprintln(os.Stderr, err)
} }
outBytes, _ := json.Marshal(&out) outBytes, _ := json.Marshal(&out)
return C.CString(string(outBytes))
return C.CString(string(outBytes))
} }
//export Login //export Login
@ -35,30 +42,46 @@ func Login(address, password *C.char) *C.char {
//export UnlockAccount //export UnlockAccount
func UnlockAccount(address, password *C.char, seconds int) *C.char { func UnlockAccount(address, password *C.char, seconds int) *C.char {
// This is equivalent to unlocking an account from the command line, // This is equivalent to unlocking an account from the command line,
// just modified to unlock the account for the currently running geth node // just modified to unlock the account for the currently running geth node
// based on the provided arguments // based on the provided arguments
err := unlockAccount(C.GoString(address), C.GoString(password), seconds) err := unlockAccount(C.GoString(address), C.GoString(password), seconds)
out := JSONError{
Error: err.Error(), var errString string
}
if err != nil { if err != nil {
fmt.Fprintln(os.Stderr, err) fmt.Fprintln(os.Stderr, err)
errString = err.Error()
} else {
errString = ""
}
out := JSONError{
Error: errString,
} }
outBytes, _ := json.Marshal(&out) outBytes, _ := json.Marshal(&out)
return C.CString(string(outBytes)) return C.CString(string(outBytes))
} }
//export StartNode //export StartNode
func StartNode(datadir *C.char) *C.char { func StartNode(datadir *C.char) *C.char {
// This starts a geth node with the given datadir // This starts a geth node with the given datadir
err := createAndStartNode(C.GoString(datadir)) err := createAndStartNode(C.GoString(datadir))
out := JSONError{
Error: err.Error(), var errString string
}
if err != nil { if err != nil {
fmt.Fprintln(os.Stderr, err) fmt.Fprintln(os.Stderr, err)
errString = err.Error()
} else {
errString = ""
}
out := JSONError{
Error: errString,
} }
outBytes, _ := json.Marshal(&out) outBytes, _ := json.Marshal(&out)
return C.CString(string(outBytes)) return C.CString(string(outBytes))
} }