-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Transaction Command #120
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
8c6eb26
added transaction creation at console; removed hotwallet
chadlagore e8c8e72
add broadcast; add pending txns
chadlagore 90e8ccc
added pending functions
chadlagore 19ddb85
comments; handle transaction
chadlagore 284cd0f
use push method directly
chadlagore 33557a3
testing app.Pay, two cases handled
chadlagore 25856b1
re-enumerate steps
chadlagore f12ebbc
error language
chadlagore 7b02913
add test for getting balances
chadlagore f06601d
comments
chadlagore File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,7 @@ run-console: cumulus | |
deps: | ||
glide install | ||
|
||
clean: | ||
clean: cumulus | ||
rm cumulus | ||
|
||
install-glide: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,76 @@ | ||
package app | ||
|
||
import "github.com/ubclaunchpad/cumulus/blockchain" | ||
import ( | ||
"errors" | ||
|
||
"github.com/ubclaunchpad/cumulus/blockchain" | ||
"github.com/ubclaunchpad/cumulus/msg" | ||
|
||
crand "crypto/rand" | ||
) | ||
|
||
// User holds basic user information. | ||
type User struct { | ||
HotWallet | ||
*blockchain.Wallet | ||
BlockSize uint32 | ||
} | ||
|
||
// HotWallet is a representation of the users wallet. | ||
type HotWallet struct { | ||
Name string | ||
blockchain.Wallet | ||
} | ||
|
||
// NewUser creates a new user | ||
func NewUser() *User { | ||
return &User{ | ||
HotWallet: HotWallet{ | ||
Wallet: blockchain.NewWallet(), | ||
Name: "default", | ||
}, | ||
Wallet: blockchain.NewWallet(), | ||
BlockSize: blockchain.DefaultBlockSize, | ||
} | ||
} | ||
|
||
// getCurrentUser gets the current user. | ||
// getCurrentUser gets the current user function only used for app initalization. | ||
func getCurrentUser() *User { | ||
// TODO: Check for local user information on disk, | ||
// If doesnt exist, create new user. | ||
return NewUser() | ||
} | ||
|
||
// Pay pays an amount of coin to an address `to`. | ||
func (a *App) Pay(to string, amount uint64) error { | ||
// Four steps must occur. | ||
wallet := a.CurrentUser.Wallet | ||
pool := a.Pool | ||
|
||
// A legitimate transaction must be built. | ||
tbody := blockchain.TxBody{ | ||
Sender: wallet.Public(), | ||
// TODO: Collect inputs. | ||
Input: blockchain.TxHashPointer{}, | ||
Outputs: []blockchain.TxOutput{ | ||
blockchain.TxOutput{ | ||
Recipient: to, | ||
Amount: amount, | ||
}, | ||
}, | ||
} | ||
|
||
// The transaction must be signed. | ||
if txn, err := tbody.Sign(*a.CurrentUser.Wallet, crand.Reader); err == nil { | ||
|
||
// The transaction must be broadcasted to the peers. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The comment here doesn't fit what's happening There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed up in inputs PR 😅 |
||
if err := wallet.SetPending(txn); err != nil { | ||
return err | ||
} | ||
|
||
// The transaction must be added to the pool. | ||
if !pool.Push(txn, a.Chain) { | ||
return errors.New("transaction validation failed") | ||
} | ||
|
||
// The transaction must be broadcasted to the network. | ||
a.PeerStore.Broadcast(msg.Push{ | ||
ResourceType: msg.ResourceTransaction, | ||
Resource: txn, | ||
}) | ||
|
||
} else { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we need to make some changes to this function.
We should:
Independently of this function, the balance of the current user's wallet should be computed each time a new block is added to our chain.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, so the wallet balance is only updated when our transaction has landed in a block - makes total sense.