From 50babd6a6723e4082792f0f3d88a3bd3b9056b90 Mon Sep 17 00:00:00 2001 From: Raul Iglesias Date: Fri, 20 Sep 2019 21:58:45 -0400 Subject: [PATCH] [models] refs #142 - Change all value receiver to pointer receiver and fix the code related to it --- src/coin/skycoin/models/account.go | 30 ++++++------ src/coin/skycoin/models/blockchain.go | 12 ++--- src/coin/skycoin/models/cipher.go | 15 +++--- src/coin/skycoin/models/coin.go | 4 +- src/coin/skycoin/models/networking.go | 16 +++---- src/coin/skycoin/models/wallet.go | 68 +++++++++++++-------------- src/models/blockchainModels.go | 2 +- 7 files changed, 72 insertions(+), 75 deletions(-) diff --git a/src/coin/skycoin/models/account.go b/src/coin/skycoin/models/account.go index 2face518..a4a90b7b 100644 --- a/src/coin/skycoin/models/account.go +++ b/src/coin/skycoin/models/account.go @@ -13,7 +13,7 @@ import ( "github.com/fibercrypto/FiberCryptoWallet/src/util" ) -func (addr SkycoinAddress) GetBalance(ticker string) (uint64, error) { +func (addr *SkycoinAddress) GetBalance(ticker string) (uint64, error) { c, err := NewSkycoinApiClient(PoolSection) if err != nil { @@ -34,10 +34,10 @@ func (addr SkycoinAddress) GetBalance(ticker string) (uint64, error) { return 0, errorTickerInvalid{ticker} } } -func (addr SkycoinAddress) ListAssets() []string { +func (addr *SkycoinAddress) ListAssets() []string { return []string{Sky, CoinHour} } -func (addr SkycoinAddress) ScanUnspentOutputs() core.TransactionOutputIterator { +func (addr *SkycoinAddress) ScanUnspentOutputs() core.TransactionOutputIterator { c, err := NewSkycoinApiClient(PoolSection) if err != nil { println(err.Error()) @@ -68,7 +68,7 @@ func (addr SkycoinAddress) ScanUnspentOutputs() core.TransactionOutputIterator { return NewSkycoinTransactionOutputIterator(skyOutputs) } -func (addr SkycoinAddress) ListTransactions() core.TransactionIterator { +func (addr *SkycoinAddress) ListTransactions() core.TransactionIterator { c, err := NewSkycoinApiClient(PoolSection) if err != nil { @@ -94,11 +94,11 @@ func (addr SkycoinAddress) ListTransactions() core.TransactionIterator { return NewSkycoinTransactionIterator(transactions) } -func (addr SkycoinAddress) ListPendingTransactions() (core.TransactionIterator, error) { //------TODO +func (addr *SkycoinAddress) ListPendingTransactions() (core.TransactionIterator, error) { //------TODO return nil,nil } -func (wlt RemoteWallet) GetBalance(ticker string) (uint64, error) { +func (wlt *RemoteWallet) GetBalance(ticker string) (uint64, error) { c, err := NewSkycoinApiClient(wlt.poolSection) if err != nil { return 0, err @@ -120,11 +120,11 @@ func (wlt RemoteWallet) GetBalance(ticker string) (uint64, error) { } -func (wlt RemoteWallet) ListAssets() []string { +func (wlt *RemoteWallet) ListAssets() []string { return []string{Sky, CoinHour} } -func (wlt RemoteWallet) ScanUnspentOutputs() core.TransactionOutputIterator { +func (wlt *RemoteWallet) ScanUnspentOutputs() core.TransactionOutputIterator { addressesIter, err := wlt.GetLoadedAddresses() if err != nil { return nil @@ -139,7 +139,7 @@ func (wlt RemoteWallet) ScanUnspentOutputs() core.TransactionOutputIterator { return NewSkycoinTransactionOutputIterator(unOuts) } -func (wlt RemoteWallet) ListTransactions() core.TransactionIterator { +func (wlt *RemoteWallet) ListTransactions() core.TransactionIterator { addressesIter, err := wlt.GetLoadedAddresses() if err != nil { return nil @@ -155,7 +155,7 @@ func (wlt RemoteWallet) ListTransactions() core.TransactionIterator { return NewSkycoinTransactionIterator(txns) } -func (wlt RemoteWallet) ListPendingTransactions() (core.TransactionIterator, error) { +func (wlt *RemoteWallet) ListPendingTransactions() (core.TransactionIterator, error) { c, err := NewSkycoinApiClient(PoolSection) if err != nil { return nil, err @@ -172,7 +172,7 @@ func (wlt RemoteWallet) ListPendingTransactions() (core.TransactionIterator, err return NewSkycoinTransactionIterator(txns), nil } -func (wlt LocalWallet) GetBalance(ticker string) (uint64, error) { +func (wlt *LocalWallet) GetBalance(ticker string) (uint64, error) { walletName := filepath.Join(wlt.WalletDir, wlt.Id) walletLoaded, err := wallet.Load(walletName) if err != nil { @@ -223,11 +223,11 @@ func (wlt LocalWallet) GetBalance(ticker string) (uint64, error) { } -func (wlt LocalWallet) ListAssets() []string { +func (wlt *LocalWallet) ListAssets() []string { return []string{Sky, CoinHour} } -func (wlt LocalWallet) ScanUnspentOutputs() core.TransactionOutputIterator { +func (wlt *LocalWallet) ScanUnspentOutputs() core.TransactionOutputIterator { addressesIter, err := wlt.GetLoadedAddresses() if err != nil { return nil @@ -242,7 +242,7 @@ func (wlt LocalWallet) ScanUnspentOutputs() core.TransactionOutputIterator { return NewSkycoinTransactionOutputIterator(unOuts) } -func (wlt LocalWallet) ListTransactions() core.TransactionIterator { +func (wlt *LocalWallet) ListTransactions() core.TransactionIterator { addressesIter, err := wlt.GetLoadedAddresses() if err != nil { return nil @@ -258,7 +258,7 @@ func (wlt LocalWallet) ListTransactions() core.TransactionIterator { return NewSkycoinTransactionIterator(txns) } -func (wlt LocalWallet) ListPendingTransactions() (core.TransactionIterator, error) { //------TODO +func (wlt *LocalWallet) ListPendingTransactions() (core.TransactionIterator, error) { //------TODO c, err := NewSkycoinApiClient(PoolSection) if err != nil { return nil, err diff --git a/src/coin/skycoin/models/blockchain.go b/src/coin/skycoin/models/blockchain.go index 103cc9d3..dcff56bf 100644 --- a/src/coin/skycoin/models/blockchain.go +++ b/src/coin/skycoin/models/blockchain.go @@ -84,7 +84,7 @@ type SkycoinBlockchainStatus struct { //Implements BlockchainStatus interface func NewSkycoinBlockchainStatus(invalidCacheTime uint64) *SkycoinBlockchainStatus { return &SkycoinBlockchainStatus{CacheTime: invalidCacheTime} } -func (ss SkycoinBlockchainStatus) GetCoinValue(coinvalue core.CoinValueKey, ticker string) (uint64, error) { +func (ss *SkycoinBlockchainStatus) GetCoinValue(coinvalue core.CoinValueKey, ticker string) (uint64, error) { elapsed := uint64(time.Now().UTC().UnixNano()) - ss.lastTimeSupplyRequested if elapsed > ss.CacheTime || ss.cachedStatus == nil { if ss.cachedStatus == nil { @@ -111,7 +111,7 @@ func (ss SkycoinBlockchainStatus) GetCoinValue(coinvalue core.CoinValueKey, tick } } -func (ss SkycoinBlockchainStatus) GetLastBlock() (core.Block, error) { +func (ss *SkycoinBlockchainStatus) GetLastBlock() (core.Block, error) { elapsed := uint64(time.Now().UTC().UnixNano()) - ss.lastTimeSupplyRequested if elapsed > ss.CacheTime || ss.cachedStatus == nil { if ss.cachedStatus == nil { @@ -124,7 +124,7 @@ func (ss SkycoinBlockchainStatus) GetLastBlock() (core.Block, error) { return ss.cachedStatus.LastBlockInfo, nil } -func (ss SkycoinBlockchainStatus) GetNumberOfBlocks() (uint64, error) { +func (ss *SkycoinBlockchainStatus) GetNumberOfBlocks() (uint64, error) { if ss.cachedStatus == nil { if ss.cachedStatus == nil { ss.cachedStatus = new(SkycoinBlockchainInfo) @@ -137,11 +137,11 @@ func (ss SkycoinBlockchainStatus) GetNumberOfBlocks() (uint64, error) { return ss.cachedStatus.NumberOfBlocks.Current, nil } -func (ss SkycoinBlockchainStatus) SetCacheTime(time uint64) { +func (ss *SkycoinBlockchainStatus) SetCacheTime(time uint64) { ss.CacheTime = time } -func (ss SkycoinBlockchainStatus) requestSupplyInfo() error { +func (ss *SkycoinBlockchainStatus) requestSupplyInfo() error { c, err := NewSkycoinApiClient(PoolSection) if err != nil { @@ -181,7 +181,7 @@ func (ss SkycoinBlockchainStatus) requestSupplyInfo() error { return nil } -func (ss SkycoinBlockchainStatus) requestStatusInfo() error { +func (ss *SkycoinBlockchainStatus) requestStatusInfo() error { c, err := NewSkycoinApiClient(PoolSection) if err != nil { diff --git a/src/coin/skycoin/models/cipher.go b/src/coin/skycoin/models/cipher.go index 05173cd0..d97fb423 100644 --- a/src/coin/skycoin/models/cipher.go +++ b/src/coin/skycoin/models/cipher.go @@ -6,7 +6,7 @@ import ( type SkycoinAddressIterator struct { //Implements AddressIterator interfaces current int - addresses []SkycoinAddress + addresses []core.Address } func (it *SkycoinAddressIterator) Value() core.Address { @@ -22,13 +22,10 @@ func (it *SkycoinAddressIterator) Next() bool { } func (it *SkycoinAddressIterator) HasNext() bool { - if (it.current + 1) >= len(it.addresses) { - return false - } - return true + return (it.current + 1) < len(it.addresses) } -func NewSkycoinAddressIterator(addresses []SkycoinAddress) *SkycoinAddressIterator { +func NewSkycoinAddressIterator(addresses []core.Address) *SkycoinAddressIterator { return &SkycoinAddressIterator{addresses: addresses, current: -1} } @@ -37,14 +34,14 @@ type SkycoinAddress struct { //Implements Address and CryptoAccount interfaces poolSection string } -func (addr SkycoinAddress) IsBip32() bool { +func (addr *SkycoinAddress) IsBip32() bool { return false } -func (addr SkycoinAddress) String() string { +func (addr *SkycoinAddress) String() string { return addr.address } -func (addr SkycoinAddress) GetCryptoAccount() core.CryptoAccount { +func (addr *SkycoinAddress) GetCryptoAccount() core.CryptoAccount { return addr } \ No newline at end of file diff --git a/src/coin/skycoin/models/coin.go b/src/coin/skycoin/models/coin.go index 16674955..fa445ee1 100644 --- a/src/coin/skycoin/models/coin.go +++ b/src/coin/skycoin/models/coin.go @@ -120,7 +120,7 @@ func (sto *SkycoinPendingTransactionOutput) IsSpent() bool { } func (sto *SkycoinPendingTransactionOutput) GetAddress() core.Address { - return SkycoinAddress{address: sto.Output.Address} + return &SkycoinAddress{address: sto.Output.Address} } func (sto *SkycoinPendingTransactionOutput) GetCoins(ticker string) (uint64, error) { @@ -357,7 +357,7 @@ func (out *SkycoinTransactionOutput) GetId() string { } func (out *SkycoinTransactionOutput) GetAddress() core.Address { - return SkycoinAddress{address:out.skyOut.Address} + return &SkycoinAddress{address:out.skyOut.Address} } func (out *SkycoinTransactionOutput) GetCoins(ticker string) (uint64, error) { diff --git a/src/coin/skycoin/models/networking.go b/src/coin/skycoin/models/networking.go index e995b195..10bc9165 100644 --- a/src/coin/skycoin/models/networking.go +++ b/src/coin/skycoin/models/networking.go @@ -78,32 +78,32 @@ type SkycoinPexNode struct { LastSeenOut int64 } -func (network SkycoinPexNode) GetIp() string { +func (network *SkycoinPexNode) GetIp() string { return network.Ip } -func (network SkycoinPexNode) GetPort() uint16 { +func (network *SkycoinPexNode) GetPort() uint16 { return network.Port } -func (network SkycoinPexNode) GetBlockHeight() uint64 { +func (network *SkycoinPexNode) GetBlockHeight() uint64 { return network.Block } -func (network SkycoinPexNode) IsTrusted() bool { +func (network *SkycoinPexNode) IsTrusted() bool { return network.Source } -func (network SkycoinPexNode) GetLastSeenIn() int64 { +func (network *SkycoinPexNode) GetLastSeenIn() int64 { return network.LastSeenIn } -func (network SkycoinPexNode) GetLastSeenOut() int64 { +func (network *SkycoinPexNode) GetLastSeenOut() int64 { return network.LastSeenOut } -func connectionsToNetwork(connection readable.Connection) SkycoinPexNode { - return SkycoinPexNode{ +func connectionsToNetwork(connection readable.Connection) *SkycoinPexNode { + return &SkycoinPexNode{ Ip: strings.Split(connection.Addr, ":")[0], Port: connection.ListenPort, LastSeenIn: connection.LastSent, diff --git a/src/coin/skycoin/models/wallet.go b/src/coin/skycoin/models/wallet.go index 75bc8ec1..703f941a 100644 --- a/src/coin/skycoin/models/wallet.go +++ b/src/coin/skycoin/models/wallet.go @@ -88,7 +88,7 @@ func (wltSrv *SkycoinRemoteWallet) ListWallets() core.WalletIterator { } func (wltSrv *SkycoinRemoteWallet) CreateWallet(label string, seed string, IsEncrypted bool, pwd core.PasswordReader, scanAddressesN int) (core.Wallet, error) { - wlt := RemoteWallet{} + wlt := &RemoteWallet{} c, err := NewSkycoinApiClient(wltSrv.poolSection) if err != nil { return nil, err @@ -124,7 +124,7 @@ func (wltSrv *SkycoinRemoteWallet) CreateWallet(label string, seed string, IsEnc wlt = walletResponseToWallet(*wltR) } wlt.poolSection = wltSrv.poolSection - return &wlt, nil + return wlt, nil } func (wltSrv *SkycoinRemoteWallet) Encrypt(walletName string, pwd core.PasswordReader) { @@ -273,11 +273,11 @@ type RemoteWallet struct { //Implements Wallet and CryptoAccount interfaces poolSection string } -func (wlt RemoteWallet) GetLabel() string { +func (wlt *RemoteWallet) GetLabel() string { return wlt.Label } -func (wlt RemoteWallet) SetLabel(name string) { +func (wlt *RemoteWallet) SetLabel(name string) { c, err := NewSkycoinApiClient(wlt.poolSection) if err != nil { return @@ -287,23 +287,23 @@ func (wlt RemoteWallet) SetLabel(name string) { _ = c.UpdateWallet(wlt.Id, name) } -func (wlt RemoteWallet) GetId() string { +func (wlt *RemoteWallet) GetId() string { return wlt.Id } -func (wlt RemoteWallet) Transfer(to core.Address, amount uint64) { //------TODO +func (wlt *RemoteWallet) Transfer(to core.Address, amount uint64) { //------TODO return } -func (wlt RemoteWallet) SendFromAddress(from, to core.Address, amount uint64) { //------TODO +func (wlt *RemoteWallet) SendFromAddress(from, to core.Address, amount uint64) { //------TODO return } -func (wlt RemoteWallet) Spend(unspent, new []core.TransactionOutput) { //------TODO +func (wlt *RemoteWallet) Spend(unspent, new []core.TransactionOutput) { //------TODO return } -func (wlt RemoteWallet) GenAddresses(addrType core.AddressType, startIndex, count uint32, pwd core.PasswordReader) core.AddressIterator { +func (wlt *RemoteWallet) GenAddresses(addrType core.AddressType, startIndex, count uint32, pwd core.PasswordReader) core.AddressIterator { c, err := NewSkycoinApiClient(wlt.poolSection) if err != nil { return nil @@ -314,7 +314,7 @@ func (wlt RemoteWallet) GenAddresses(addrType core.AddressType, startIndex, coun if err != nil { return nil } - addresses := make([]SkycoinAddress, 0) + addresses := make([]core.Address, 0) for _, entry := range wltR.Entries[startIndex:int(util.Min(len(wltR.Entries), int(startIndex+count)))] { addresses = append(addresses, walletEntryToAddress(entry, wlt.poolSection)) } @@ -326,7 +326,7 @@ func (wlt RemoteWallet) GenAddresses(addrType core.AddressType, startIndex, coun return nil } for _, addr := range newAddrs { - addresses = append(addresses, SkycoinAddress{address:addr}) + addresses = append(addresses, &SkycoinAddress{address:addr}) } } @@ -334,11 +334,11 @@ func (wlt RemoteWallet) GenAddresses(addrType core.AddressType, startIndex, coun } -func (wlt RemoteWallet) GetCryptoAccount() core.CryptoAccount { +func (wlt *RemoteWallet) GetCryptoAccount() core.CryptoAccount { return wlt } -func (wlt RemoteWallet) GetLoadedAddresses() (core.AddressIterator, error) { +func (wlt *RemoteWallet) GetLoadedAddresses() (core.AddressIterator, error) { c, err := NewSkycoinApiClient(wlt.poolSection) if err != nil { return nil, err @@ -348,7 +348,7 @@ func (wlt RemoteWallet) GetLoadedAddresses() (core.AddressIterator, error) { if err != nil { return nil, err } - addresses := make([]SkycoinAddress, 0) + addresses := make([]core.Address, 0) for _, entry := range wltR.Entries { addresses = append(addresses, walletEntryToAddress(entry, wlt.poolSection)) } @@ -356,8 +356,8 @@ func (wlt RemoteWallet) GetLoadedAddresses() (core.AddressIterator, error) { return NewSkycoinAddressIterator(addresses), nil } -func walletResponseToWallet(wltR api.WalletResponse) RemoteWallet { - wlt := RemoteWallet{} +func walletResponseToWallet(wltR api.WalletResponse) *RemoteWallet { + wlt := &RemoteWallet{} wlt.CoinType = string(wltR.Meta.Coin) wlt.Encrypted = wltR.Meta.Encrypted wlt.Label = wltR.Meta.Label @@ -365,8 +365,8 @@ func walletResponseToWallet(wltR api.WalletResponse) RemoteWallet { return wlt } -func walletEntryToAddress(wltE readable.WalletEntry, poolSection string) SkycoinAddress { - return SkycoinAddress{address:wltE.Address, poolSection: poolSection} +func walletEntryToAddress(wltE readable.WalletEntry, poolSection string) *SkycoinAddress { + return &SkycoinAddress{address:wltE.Address, poolSection: poolSection} } type WalletDirectory struct { //Implements WallentEnv interface @@ -411,7 +411,7 @@ func (wltSrv *SkycoinLocalWallet) ListWallets() core.WalletIterator { if err != nil { return nil } - wallets = append(wallets, LocalWallet{ + wallets = append(wallets, &LocalWallet{ Id: name, Label: w.Label(), Encrypted: w.IsEncrypted(), @@ -431,7 +431,7 @@ func (wltSrv *SkycoinLocalWallet) GetWallet(id string) core.Wallet { if err != nil { return nil } - return LocalWallet{ + return &LocalWallet{ Id: id, Label: w.Label(), Encrypted: w.IsEncrypted(), @@ -473,7 +473,7 @@ func (wltSrv *SkycoinLocalWallet) CreateWallet(label string, seed string, IsEncr return nil, err } - return LocalWallet{ + return &LocalWallet{ Id: wltName, Label: wlt.Label(), Encrypted: wlt.IsEncrypted(), @@ -590,13 +590,13 @@ type LocalWallet struct { WalletDir string } -func (wlt LocalWallet) GetId() string { +func (wlt *LocalWallet) GetId() string { return wlt.Id } -func (wlt LocalWallet) GetLabel() string { +func (wlt *LocalWallet) GetLabel() string { return wlt.Label } -func (wlt LocalWallet) SetLabel(wltName string) { +func (wlt *LocalWallet) SetLabel(wltName string) { wltFile, err := wallet.Load(filepath.Join(wlt.WalletDir, wlt.GetId())) if err != nil { return @@ -610,16 +610,16 @@ func (wlt LocalWallet) SetLabel(wltName string) { wlt.Label = wltName } -func (wlt LocalWallet) Transfer(to core.Address, amount uint64) { +func (wlt *LocalWallet) Transfer(to core.Address, amount uint64) { } -func (wlt LocalWallet) SendFromAddress(from, to core.Address, amount uint64) { //------TODO +func (wlt *LocalWallet) SendFromAddress(from, to core.Address, amount uint64) { //------TODO } -func (wlt LocalWallet) Spend(unspent, new []core.TransactionOutput) { //------TODO +func (wlt *LocalWallet) Spend(unspent, new []core.TransactionOutput) { //------TODO } -func (wlt LocalWallet) GenAddresses(addrType core.AddressType, startIndex, count uint32, pwd core.PasswordReader) core.AddressIterator { +func (wlt *LocalWallet) GenAddresses(addrType core.AddressType, startIndex, count uint32, pwd core.PasswordReader) core.AddressIterator { walletName := filepath.Join(wlt.WalletDir, wlt.Id) walletLoaded, err := wallet.Load(walletName) if err != nil { @@ -668,26 +668,26 @@ func (wlt LocalWallet) GenAddresses(addrType core.AddressType, startIndex, count } addrs := walletLoaded.GetAddresses()[startIndex : startIndex+count] - skyAddrs := make([]SkycoinAddress, 0) + skyAddrs := make([]core.Address, 0) for _, addr := range addrs { - skyAddrs = append(skyAddrs, SkycoinAddress{address:addr.String()}) + skyAddrs = append(skyAddrs, &SkycoinAddress{address:addr.String()}) } return NewSkycoinAddressIterator(skyAddrs) } -func (wlt LocalWallet) GetCryptoAccount() core.CryptoAccount { +func (wlt *LocalWallet) GetCryptoAccount() core.CryptoAccount { return wlt } -func (wlt LocalWallet) GetLoadedAddresses() (core.AddressIterator, error) { +func (wlt *LocalWallet) GetLoadedAddresses() (core.AddressIterator, error) { walletName := filepath.Join(wlt.WalletDir, wlt.Id) walletLoaded, err := wallet.Load(walletName) if err != nil { return nil, err } - addrs := make([]SkycoinAddress, 0) + addrs := make([]core.Address, 0) addresses := walletLoaded.GetAddresses() for _, addr := range addresses { - addrs = append(addrs, SkycoinAddress{address:addr.String()}) + addrs = append(addrs, &SkycoinAddress{address:addr.String()}) } return NewSkycoinAddressIterator(addrs), nil diff --git a/src/models/blockchainModels.go b/src/models/blockchainModels.go index 9cda579a..47bbf75c 100644 --- a/src/models/blockchainModels.go +++ b/src/models/blockchainModels.go @@ -39,7 +39,7 @@ func (bs *BlockchainStatusModel) init() { bs.SetCurrentCoinHoursSupplyDefault("0") bs.SetTotalCoinHoursSupplyDefault("0") - bs.infoRequester = *skycoin.NewSkycoinBlockchainStatus(1000000) //FIXME: set correct value + bs.infoRequester = skycoin.NewSkycoinBlockchainStatus(1000000) //FIXME: set correct value } func (bs *BlockchainStatusModel) update() {