Skip to content

Commit

Permalink
Merge pull request FactomProject#76 from FactomProject/FD-627
Browse files Browse the repository at this point in the history
Fd 627
  • Loading branch information
carryforward committed Sep 8, 2018
2 parents d4ac67b + 0473b0d commit c807dde
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 55 deletions.
35 changes: 35 additions & 0 deletions get.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,41 @@ func GetFactoidBalance(addr string) (int64, error) {
return balance.Balance, nil
}

// GetBalanceTotals return the total value of Factoids and Entry Credits in the
// wallet according to the the server acknowledgement and the value saved in the
// blockchain.
func GetBalanceTotals() (fs, fa, es, ea int64, err error) {
type multiBalanceResponse struct {
FactoidAccountBalances struct {
Ack int64 `json:"ack"`
Saved int64 `json:"saved"`
} `json:"fctaccountbalances"`
EntryCreditAccountBalances struct {
Ack int64 `json:"ack"`
Saved int64 `json:"saved"`
} `json:"ecaccountbalances"`
}

req := NewJSON2Request("wallet-balances", APICounter(), nil)
resp, err := walletRequest(req)
if err != nil {
return
}

balances := new(multiBalanceResponse)
err = json.Unmarshal(resp.JSONResult(), balances)
if err != nil {
return
}

fs = balances.FactoidAccountBalances.Saved
fa = balances.FactoidAccountBalances.Ack
es = balances.EntryCreditAccountBalances.Saved
ea = balances.EntryCreditAccountBalances.Ack

return
}

// GetRate returns the number of factoshis per entry credit
func GetRate() (uint64, error) {
type rateResponse struct {
Expand Down
30 changes: 16 additions & 14 deletions wallet/txdatabase.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package wallet
import (
"encoding/hex"
"fmt"
"os"

"github.com/FactomProject/factom"
"github.com/FactomProject/factomd/common/directoryBlock"
Expand All @@ -16,7 +17,6 @@ import (
"github.com/FactomProject/factomd/database/databaseOverlay"
"github.com/FactomProject/factomd/database/hybridDB"
"github.com/FactomProject/factomd/database/mapdb"
"os"
)

// Database keys and key prefixes
Expand Down Expand Up @@ -61,12 +61,12 @@ func NewTXLevelDB(ldbpath string) (*TXDatabaseOverlay, error) {

func NewTXBoltDB(boltPath string) (*TXDatabaseOverlay, error) {
fileInfo, err := os.Stat(boltPath)
if err == nil { //if it exists
if fileInfo.IsDir() { //if it is a folder though
return nil, fmt.Errorf("The path %s is a directory. Please specify a file name.", boltPath)
if err == nil {
if fileInfo.IsDir() {
return nil, fmt.Errorf("%s is not a Bolt databse file", boltPath)
}
}
if err != nil && !os.IsNotExist(err) { //some other error, besides the file not existing
if err != nil && !os.IsNotExist(err) {
fmt.Printf("database error %s\n", err)
return nil, err
}
Expand Down Expand Up @@ -141,8 +141,7 @@ func (db *TXDatabaseOverlay) GetAllTXs() ([]interfaces.ITransaction, error) {
}

// GetTX gets a transaction by the transaction id
func (db *TXDatabaseOverlay) GetTX(txid string) (
interfaces.ITransaction, error) {
func (db *TXDatabaseOverlay) GetTX(txid string) (interfaces.ITransaction, error) {
txs, err := db.GetAllTXs()
if err != nil {
return nil, err
Expand Down Expand Up @@ -261,7 +260,7 @@ func (db *TXDatabaseOverlay) update() (string, error) {
return "", err
}

//Making sure we didn't switch networks
// Make sure we didn't switch networks
genesis, err := db.DBO.FetchFBlockByHeight(0)
if err != nil {
return "", err
Expand All @@ -281,7 +280,7 @@ func (db *TXDatabaseOverlay) update() (string, error) {
}

if gensisFBlockKeyMr == nil {
return "", fmt.Errorf("there was an error fetching the genesis block via the api.")
return "", fmt.Errorf("unable to fetch the genesis block via the api")
}

if !gensisFBlockKeyMr.IsSameAs(genesis.GetKeyMR()) {
Expand All @@ -291,6 +290,8 @@ func (db *TXDatabaseOverlay) update() (string, error) {

newestHeight := newestFBlock.GetDatabaseHeight()

// If the newest block in the tx cashe has a greater height than the newest
// fblock then clear the cashe and start from 0.
if start >= newestHeight {
return newestFBlock.GetKeyMR().String(), nil
}
Expand All @@ -299,7 +300,7 @@ func (db *TXDatabaseOverlay) update() (string, error) {
for i := start; i <= newestHeight; i++ {
if i%1000 == 0 {
if newestHeight-start > 1000 {
fmt.Printf("Fetching block %v / %v\n", i, newestHeight)
fmt.Printf("Fetching block %v/%v\n", i, newestHeight)
}
}
fblock, err := getfblockbyheight(i)
Expand All @@ -320,12 +321,13 @@ func (db *TXDatabaseOverlay) update() (string, error) {
break
}
}
if !db.quit { // Printing this would be a lie if we quit
fmt.Printf("Fetching block %v / %v\n", newestHeight, newestHeight)

if !db.quit {
fmt.Printf("Fetching block %v/%v\n", newestHeight, newestHeight)
}

err = db.DBO.ExecuteMultiBatch() // Save remaining blocks
if err != nil {
// Save the remaining blocks
if err = db.DBO.ExecuteMultiBatch(); err != nil {
return "", err
}

Expand Down
18 changes: 12 additions & 6 deletions wallet/wsapi/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package wsapi

import (
"github.com/FactomProject/factom"
)
)

type TLSConfig struct {
TLSEnable bool
Expand Down Expand Up @@ -83,14 +83,20 @@ type multiAddressResponse struct {
}

type balanceResponse struct {
CurrentHeight uint32 `json:"current-height"`
LastSavedHeight uint `json:"last-saved-height"`
Balances []interface{} `json:"balances"`
CurrentHeight uint32 `json:"current-height"`
LastSavedHeight uint `json:"last-saved-height"`
Balances []interface{} `json:"balances"`
}

type multiBalanceResponse struct {
FactoidAccountBalances *StructToReturnValues `json:"fctaccountbalances"`
EntryCreditAccountBalances *StructToReturnValues `json:"ecaccountbalances"`
FactoidAccountBalances struct {
Ack int64 `json:"ack"`
Saved int64 `json:"saved"`
} `json:"fctaccountbalances"`
EntryCreditAccountBalances struct {
Ack int64 `json:"ack"`
Saved int64 `json:"saved"`
} `json:"ecaccountbalances"`
}

type walletBackupResponse struct {
Expand Down
68 changes: 33 additions & 35 deletions wallet/wsapi/wsapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package wsapi

import (
"bytes"
"crypto/sha256"
"crypto/subtle"
"crypto/tls"
Expand All @@ -17,6 +18,7 @@ import (
"log"
"net/http"
"os"
"reflect"
"strings"
"time"

Expand All @@ -27,9 +29,7 @@ import (
"github.com/FactomProject/factomd/common/interfaces"
"github.com/FactomProject/factomd/common/primitives"
"github.com/FactomProject/web"
"bytes"
"reflect"
)
)

const APIVersion string = "2.0"

Expand Down Expand Up @@ -144,16 +144,17 @@ func Stop() {
}

func checkAuthHeader(r *http.Request) error {
if "" == rpcUser {
//no username was specified in the config file or command line, meaning factomd API is open access
// Don't bother to check the autorization if the rpc user/pass is not
// specified.
if rpcUser == "" {
return nil
}

authhdr := r.Header["Authorization"]
if len(authhdr) == 0 {
fmt.Println("Username and Password expected, but none were received")
return errors.New("no auth")
}
fmt.Println(authhdr)

h := sha256.New()
h.Write([]byte(authhdr[0]))
Expand Down Expand Up @@ -272,10 +273,7 @@ func handleV2Request(j *factom.JSON2Request) (*factom.JSON2Response, *factom.JSO

return jsonResp, nil
}
type StructToReturnValues struct {
TempBal int64 `json:"ack"`
PermBal int64 `json:"saved"`
}

func handleWalletBalances(params []byte) (interface{}, *factom.JSONError) {
//Get all of the addresses in the wallet
fs, es, err := fctWallet.GetAllAddresses()
Expand All @@ -302,7 +300,7 @@ func handleWalletBalances(params []byte) (interface{}, *factom.JSONError) {
stringOfAccountsEC = strings.Join(ecAccounts, `", "`)
}

url := "http://"+factom.FactomdServer()+"/v2"
url := "http://" + factom.FactomdServer() + "/v2"
if url == "http:///v2" {
url = "http://localhost:8088/v2"
}
Expand All @@ -327,13 +325,15 @@ func handleWalletBalances(params []byte) (interface{}, *factom.JSONError) {
}

//Total up the balances
var ackBalTotalEC int64 = 0
var savedBalTotalEC int64 = 0
var badErrorEC = ""
var (
ackBalTotalEC int64
savedBalTotalEC int64
badErrorEC string
)

var floatType = reflect.TypeOf(int64(0))

for i, _ := range respEC.Result.Balances {
for i := range respEC.Result.Balances {
x, ok := respEC.Result.Balances[i].(map[string]interface{})
if ok != true {
fmt.Println(x)
Expand All @@ -354,10 +354,6 @@ func handleWalletBalances(params []byte) (interface{}, *factom.JSONError) {
}
}

ECreturns := new(StructToReturnValues)
ECreturns.TempBal = ackBalTotalEC
ECreturns.PermBal = savedBalTotalEC

stringOfAccountsFCT := ""
if len(fctAccounts) != 0 {
stringOfAccountsFCT = strings.Join(fctAccounts, `", "`)
Expand All @@ -384,12 +380,13 @@ func handleWalletBalances(params []byte) (interface{}, *factom.JSONError) {
}

// Total up the balances
var ackBalTotalFCT int64 = 0
var savedBalTotalFCT int64 = 0
var badErrorFCT = ""
var (
ackBalTotalFCT int64
savedBalTotalFCT int64
badErrorFCT string
)

for i, _ := range respFCT.Result.Balances {
fmt.Println(i)
for i := range respFCT.Result.Balances {
x, ok := respFCT.Result.Balances[i].(map[string]interface{})
if ok != true {
fmt.Println(x)
Expand All @@ -411,10 +408,6 @@ func handleWalletBalances(params []byte) (interface{}, *factom.JSONError) {

}

FCTreturns := new(StructToReturnValues)
FCTreturns.TempBal = ackBalTotalFCT
FCTreturns.PermBal = savedBalTotalFCT

if badErrorFCT == "Not fully booted" || badErrorEC == "Not fully booted" {
type nfb struct {
NotFullyBooted string `json:"Factomd Error"`
Expand All @@ -430,11 +423,14 @@ func handleWalletBalances(params []byte) (interface{}, *factom.JSONError) {
errDecReturn.NotFullyBooted = "There was an error decoding an address"
return errDecReturn, nil
}
finalResp := new(multiBalanceResponse)
finalResp.FactoidAccountBalances = FCTreturns
finalResp.EntryCreditAccountBalances = ECreturns

return finalResp, nil
resp := new(multiBalanceResponse)
resp.FactoidAccountBalances.Ack = ackBalTotalFCT
resp.FactoidAccountBalances.Saved = savedBalTotalFCT
resp.EntryCreditAccountBalances.Ack = ackBalTotalEC
resp.EntryCreditAccountBalances.Saved = savedBalTotalEC

return resp, nil
}

func handleRemoveAddress(params []byte) (interface{}, *factom.JSONError) {
Expand Down Expand Up @@ -1112,9 +1108,11 @@ func feesRequired(t interfaces.ITransaction) uint64 {
if err != nil {
rate = 0
}
if i, err := t.CalculateFee(rate); err != nil {

fee, err := t.CalculateFee(rate)
if err != nil {
return 0
} else {
return i
}

return fee
}

0 comments on commit c807dde

Please sign in to comment.