-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
WIP: refactor basecoin example #332
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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 |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package app | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
apm "github.com/cosmos/cosmos-sdk/app" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/tendermint/abci/server" | ||
"github.com/tendermint/go-wire" | ||
cmn "github.com/tendermint/tmlibs/common" | ||
) | ||
|
||
const appName = "BasecoinApp" | ||
|
||
type BasecoinApp struct { | ||
*apm.App | ||
cdc *wire.Codec | ||
multiStore sdk.CommitMultiStore | ||
|
||
// The key to access the substores. | ||
mainStoreKey *sdk.KVStoreKey | ||
ibcStoreKey *sdk.KVStoreKey | ||
|
||
// Additional stores: | ||
accStore sdk.AccountStore | ||
} | ||
|
||
// TODO: This should take in more configuration options. | ||
func NewBasecoinApp() *BasecoinApp { | ||
|
||
// Create and configure app. | ||
var app = &BasecoinApp{} | ||
app.initKeys() | ||
app.initMultiStore() | ||
app.initAppStore() | ||
app.initSDKApp() | ||
app.initCodec() | ||
app.initTxDecoder() | ||
app.initAnteHandler() | ||
app.initRoutes() | ||
|
||
// TODO: Load genesis | ||
// TODO: InitChain with validators | ||
// TODO: Set the genesis accounts | ||
app.loadStores() | ||
|
||
return app | ||
} | ||
|
||
func (app *BasecoinApp) RunForever() { | ||
|
||
// Start the ABCI server | ||
srv, err := server.NewServer("0.0.0.0:46658", "socket", app) | ||
if err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
srv.Start() | ||
|
||
// Wait forever | ||
cmn.TrapSignal(func() { | ||
// Cleanup | ||
srv.Stop() | ||
}) | ||
|
||
} | ||
|
||
func (app *BasecoinApp) initKeys() { | ||
app.mainStoreKey = sdk.NewKVStoreKey("main") | ||
app.ibcStoreKey = sdk.NewKVStoreKey("ibc") | ||
} | ||
|
||
// depends on initMultiStore() | ||
func (app *BasecoinApp) initSDKApp() { | ||
app.App = apm.NewApp(appName, app.multiStore) | ||
} | ||
|
||
func (app *BasecoinApp) initCodec() { | ||
app.cdc = wire.NewCodec() | ||
} | ||
|
||
// depends on initSDKApp() | ||
func (app *BasecoinApp) initTxDecoder() { | ||
app.App.SetTxDecoder(app.decodeTx) | ||
} | ||
|
||
// initAnteHandler defined in app/routes.go | ||
// initRoutes defined in app/routes.go | ||
|
||
// Load the stores. | ||
func (app *BasecoinApp) loadStores() { | ||
if err := app.LoadLatestVersion(app.mainStoreKey); err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package app | ||
|
||
import ( | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/cosmos-sdk/x/bank" | ||
) | ||
|
||
// Set via `app.App.SetTxDecoder(app.decodeTx)` | ||
func (app *BasecoinApp) decodeTx(txBytes []byte) (sdk.Tx, error) { | ||
var tx = sdk.StdTx{} | ||
err := app.cdc.UnmarshalBinary(txBytes, &tx) | ||
return tx, err | ||
} | ||
|
||
// Wire requires registration of interfaces & concrete types. | ||
func (app *BasecoinApp) registerMsgs() { | ||
cdc := app.cdc | ||
|
||
// Register the Msg interface. | ||
cdc.RegisterInterface((*sdk.Msg)(nil), nil) | ||
cdc.RegisterConcrete(bank.SendMsg{}, "cosmos-sdk/SendMsg", nil) // XXX refactor out | ||
cdc.RegisterConcrete(bank.IssueMsg{}, "cosmos-sdk/IssueMsg", nil) // XXX refactor out to bank/msgs.go | ||
// more msgs here... | ||
|
||
// All interfaces to be encoded/decoded in a Msg must be | ||
// registered here, along with all the concrete types that | ||
// implement them. | ||
} |
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package app | ||
|
||
import ( | ||
"github.com/cosmos/cosmos-sdk/x/auth" | ||
"github.com/cosmos/cosmos-sdk/x/bank" | ||
) | ||
|
||
// Handle charging tx fees and checking signatures. | ||
func (app *BasecoinApp) initAnteHandler() { | ||
var authAnteHandler = auth.NewAnteHandler(app.accStore) | ||
app.App.SetDefaultAnteHandler(authAnteHandler) | ||
} | ||
|
||
// Constructs router to route handling of msgs. | ||
func (app *BasecoinApp) initRoutes() { | ||
var router = app.App.Router() | ||
// var multiStore = app.multiStore | ||
var accStore = app.accStore | ||
|
||
router.AddRoute("bank", bank.NewHandler(accStore)) | ||
// more routes here... (order matters) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package app | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/cosmos/cosmos-sdk/examples/basecoin/types" | ||
"github.com/cosmos/cosmos-sdk/store" | ||
"github.com/cosmos/cosmos-sdk/x/auth" | ||
dbm "github.com/tendermint/tmlibs/db" | ||
) | ||
|
||
// depends on initKeys() | ||
func (app *BasecoinApp) initMultiStore() { | ||
|
||
// Create the underlying leveldb datastore which will | ||
// persist the Merkle tree inner & leaf nodes. | ||
db, err := dbm.NewGoLevelDB("basecoin", "basecoin-data") | ||
if err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
|
||
// Create CommitStoreLoader. | ||
cacheSize := 10000 | ||
numHistory := int64(100) | ||
mainLoader := store.NewIAVLStoreLoader(db, cacheSize, numHistory) | ||
ibcLoader := store.NewIAVLStoreLoader(db, cacheSize, numHistory) | ||
|
||
// Create MultiStore | ||
multiStore := store.NewCommitMultiStore(db) | ||
multiStore.SetSubstoreLoader(app.mainStoreKey, mainLoader) | ||
multiStore.SetSubstoreLoader(app.ibcStoreKey, ibcLoader) | ||
|
||
// Finally, | ||
app.multiStore = multiStore | ||
} | ||
|
||
// depends on initKeys() | ||
func (app *BasecoinApp) initAppStore() { | ||
app.accStore = auth.NewAccountStore( | ||
app.mainStoreKey, | ||
types.AppAccountCodec{}, | ||
) | ||
} |
This file was deleted.
Oops, something went wrong.
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.
should call
crypto.RegisterWire(cdc)
.