Skip to content

Commit

Permalink
Merge pull request #56 from binance-chain/addtx
Browse files Browse the repository at this point in the history
[R4R]add interest txs to pool
  • Loading branch information
rickyyangz authored Jan 29, 2019
2 parents a9cc7e4 + 798339e commit a90d449
Show file tree
Hide file tree
Showing 13 changed files with 65 additions and 43 deletions.
52 changes: 29 additions & 23 deletions baseapp/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ const (
// BaseApp reflects the ABCI application implementation.
type BaseApp struct {
// initialized on creation
Logger log.Logger
name string // application name from abci.Info
db dbm.DB // common DB backend
cms sdk.CommitMultiStore // Main (uncached) state
router Router // handle any kind of message
queryRouter QueryRouter // router for redirecting query calls
codespacer *sdk.Codespacer // handle module codespacing
isPublishAccountBalance bool
Logger log.Logger
name string // application name from abci.Info
db dbm.DB // common DB backend
cms sdk.CommitMultiStore // Main (uncached) state
router Router // handle any kind of message
queryRouter QueryRouter // router for redirecting query calls
codespacer *sdk.Codespacer // handle module codespacing
collect sdk.CollectConfig

TxDecoder sdk.TxDecoder // unmarshal []byte into sdk.Tx

Expand Down Expand Up @@ -89,23 +89,23 @@ var _ abci.Application = (*BaseApp)(nil)
// NOTE: The db is used to store the version number for now.
// Accepts a user-defined TxDecoder
// Accepts variable number of option functions, which act on the BaseApp to set configuration choices
func NewBaseApp(name string, logger log.Logger, db dbm.DB, txDecoder sdk.TxDecoder, isPublish bool, options ...func(*BaseApp)) *BaseApp {
func NewBaseApp(name string, logger log.Logger, db dbm.DB, txDecoder sdk.TxDecoder, collectConfig sdk.CollectConfig, options ...func(*BaseApp)) *BaseApp {
cache, err := lru.New(TxMsgCacheSize)
if err != nil {
panic(err)
}
app := &BaseApp{
Logger: logger,
name: name,
db: db,
cms: store.NewCommitMultiStore(db),
router: NewRouter(),
queryRouter: NewQueryRouter(),
codespacer: sdk.NewCodespacer(),
TxDecoder: txDecoder,
isPublishAccountBalance: isPublish,
txMsgCache: cache,
Pool: new(sdk.Pool),
Logger: logger,
name: name,
db: db,
cms: store.NewCommitMultiStore(db),
router: NewRouter(),
queryRouter: NewQueryRouter(),
codespacer: sdk.NewCodespacer(),
TxDecoder: txDecoder,
collect: collectConfig,
txMsgCache: cache,
Pool: new(sdk.Pool),
}

// Register the undefined & root codespaces, which should not be used by
Expand Down Expand Up @@ -832,8 +832,14 @@ func (app *BaseApp) RunTx(mode sdk.RunTxMode, txBytes []byte, tx sdk.Tx, txHash

// only update state if all messages pass
if result.IsOK() {
if (mode == sdk.RunTxModeDeliver || mode == sdk.RunTxModeDeliverAfterPre) && app.isPublishAccountBalance {
app.Pool.AddAddrs(msgs[0].GetInvolvedAddresses())
if mode == sdk.RunTxModeDeliver || mode == sdk.RunTxModeDeliverAfterPre {
if app.collect.CollectAccountBalance {
app.Pool.AddAddrs(msgs[0].GetInvolvedAddresses())
}
if app.collect.CollectTxs {
// Should we add all msg here with no distinction ?
app.Pool.AddTx(tx, txHash)
}
}
accountCache.Write()
msCache.Write()
Expand Down Expand Up @@ -938,7 +944,7 @@ func (app *BaseApp) Commit() (res abci.ResponseCommit) {

// Empty the Deliver state
app.DeliverState = nil
app.Pool.ClearTxRelatedAddrs()
app.Pool.Clear()

return abci.ResponseCommit{
Data: commitID.Hash,
Expand Down
18 changes: 9 additions & 9 deletions baseapp/baseapp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func newBaseApp(name string, options ...func(*BaseApp)) *BaseApp {
db := dbm.NewMemDB()
codec := codec.New()
registerTestCodec(codec)
return NewBaseApp(name, logger, db, testTxDecoder(codec), false, options...)
return NewBaseApp(name, logger, db, testTxDecoder(codec), sdk.CollectConfig{}, options...)
}

func registerTestCodec(cdc *codec.Codec) {
Expand Down Expand Up @@ -101,8 +101,8 @@ func (app *MockBaseApp) initFromStore(mainKey sdk.StoreKey) error {
return nil
}

func NewMockBaseApp(name string, logger log.Logger, db dbm.DB, txDecoder sdk.TxDecoder, isPublish bool, options ...func(*BaseApp)) *MockBaseApp {
return &MockBaseApp{NewBaseApp(name, logger, db, txDecoder, isPublish, options...)}
func NewMockBaseApp(name string, logger log.Logger, db dbm.DB, txDecoder sdk.TxDecoder, collect sdk.CollectConfig, options ...func(*BaseApp)) *MockBaseApp {
return &MockBaseApp{NewBaseApp(name, logger, db, txDecoder, collect, options...)}
}

//------------------------------------------------------------------------------------------
Expand All @@ -124,7 +124,7 @@ func TestLoadVersion(t *testing.T) {
logger := defaultLogger()
db := dbm.NewMemDB()
name := t.Name()
app := NewMockBaseApp(name, logger, db, nil, false)
app := NewMockBaseApp(name, logger, db, nil, sdk.CollectConfig{})

// make a cap key and mount the store
capKey := sdk.NewKVStoreKey("main")
Expand Down Expand Up @@ -153,15 +153,15 @@ func TestLoadVersion(t *testing.T) {
commitID2 := sdk.CommitID{2, res.Data}

// reload with LoadLatestVersion
app = NewMockBaseApp(name, logger, db, nil, false)
app = NewMockBaseApp(name, logger, db, nil, sdk.CollectConfig{})
app.MountStoresIAVL(capKey)
err = app.LoadLatestVersion(capKey)
require.Nil(t, err)
testLoadVersionHelper(t, app, int64(2), commitID2)

// reload with LoadVersion, see if you can commit the same block and get
// the same result
app = NewMockBaseApp(name, logger, db, nil, false)
app = NewMockBaseApp(name, logger, db, nil, sdk.CollectConfig{})
app.MountStoresIAVL(capKey)
err = app.LoadVersion(1, capKey)
require.Nil(t, err)
Expand All @@ -181,7 +181,7 @@ func testLoadVersionHelper(t *testing.T, app *MockBaseApp, expectedHeight int64,
func TestOptionFunction(t *testing.T) {
logger := defaultLogger()
db := dbm.NewMemDB()
bap := NewMockBaseApp("starting name", logger, db, nil, false, testChangeNameHelper("new name"))
bap := NewMockBaseApp("starting name", logger, db, nil, sdk.CollectConfig{}, testChangeNameHelper("new name"))
require.Equal(t, bap.name, "new name", "BaseApp should have had name changed via option function")
}

Expand Down Expand Up @@ -253,7 +253,7 @@ func TestInitChainer(t *testing.T) {
// we can reload the same app later
db := dbm.NewMemDB()
logger := defaultLogger()
app := NewMockBaseApp(name, logger, db, nil, false)
app := NewMockBaseApp(name, logger, db, nil, sdk.CollectConfig{})
capKey := sdk.NewKVStoreKey("main")
capKey2 := sdk.NewKVStoreKey("key2")
app.MountStoresIAVL(capKey, capKey2)
Expand Down Expand Up @@ -297,7 +297,7 @@ func TestInitChainer(t *testing.T) {
require.Equal(t, value, res.Value)

// reload app
app = NewMockBaseApp(name, logger, db, nil, false)
app = NewMockBaseApp(name, logger, db, nil, sdk.CollectConfig{})
app.SetInitChainer(initChainer)
app.MountStoresIAVL(capKey, capKey2)
err = app.LoadLatestVersion(capKey) // needed to make stores non-nil
Expand Down
2 changes: 1 addition & 1 deletion cmd/gaia/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ type GaiaApp struct {
func NewGaiaApp(logger log.Logger, db dbm.DB, traceStore io.Writer, baseAppOptions ...func(*bam.BaseApp)) *GaiaApp {
cdc := MakeCodec()

bApp := bam.NewBaseApp(appName, logger, db, auth.DefaultTxDecoder(cdc), false, baseAppOptions...)
bApp := bam.NewBaseApp(appName, logger, db, auth.DefaultTxDecoder(cdc), sdk.CollectConfig{}, baseAppOptions...)
bApp.SetCommitMultiStoreTracer(traceStore)

var app = &GaiaApp{
Expand Down
2 changes: 1 addition & 1 deletion cmd/gaia/app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type MockGaiaApp struct {
func NewMockGaiaApp(logger log.Logger, db dbm.DB, traceStore io.Writer, baseAppOptions ...func(*bam.BaseApp)) *MockGaiaApp {
cdc := MakeCodec()

bApp := bam.NewBaseApp(appName, logger, db, auth.DefaultTxDecoder(cdc), false, baseAppOptions...)
bApp := bam.NewBaseApp(appName, logger, db, auth.DefaultTxDecoder(cdc), sdk.CollectConfig{}, baseAppOptions...)
bApp.SetCommitMultiStoreTracer(traceStore)

gApp := &GaiaApp{
Expand Down
2 changes: 1 addition & 1 deletion cmd/gaia/cmd/gaiadebug/hack.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ type GaiaApp struct {
func NewGaiaApp(logger log.Logger, db dbm.DB, baseAppOptions ...func(*bam.BaseApp)) *GaiaApp {
cdc := MakeCodec()

bApp := bam.NewBaseApp(appName, logger, db, auth.DefaultTxDecoder(cdc), false, baseAppOptions...)
bApp := bam.NewBaseApp(appName, logger, db, auth.DefaultTxDecoder(cdc), sdk.CollectConfig{}, baseAppOptions...)
bApp.SetCommitMultiStoreTracer(os.Stdout)

// create your application object
Expand Down
2 changes: 1 addition & 1 deletion docs/sdk/core/examples/app1.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const (
func NewApp1(logger log.Logger, db dbm.DB) *bapp.BaseApp {

// Create the base application object.
app := bapp.NewBaseApp(app1Name, logger, db, tx1Decoder, false)
app := bapp.NewBaseApp(app1Name, logger, db, tx1Decoder, sdk.CollectConfig{})

// Create a key for accessing the account store.
keyAccount := sdk.NewKVStoreKey("acc")
Expand Down
2 changes: 1 addition & 1 deletion docs/sdk/core/examples/app2.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func NewApp2(logger log.Logger, db dbm.DB) *bapp.BaseApp {
cdc := NewCodec()

// Create the base application object.
app := bapp.NewBaseApp(app2Name, logger, db, tx2Decoder(cdc), false)
app := bapp.NewBaseApp(app2Name, logger, db, tx2Decoder(cdc), sdk.CollectConfig{})

// Create a key for accessing the account store.
keyAccount := sdk.NewKVStoreKey("acc")
Expand Down
2 changes: 1 addition & 1 deletion docs/sdk/core/examples/app3.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func NewApp3(logger log.Logger, db dbm.DB) *bapp.BaseApp {
cdc := UpdatedCodec()

// Create the base application object.
app := bapp.NewBaseApp(app3Name, logger, db, auth.DefaultTxDecoder(cdc), false)
app := bapp.NewBaseApp(app3Name, logger, db, auth.DefaultTxDecoder(cdc), sdk.CollectConfig{})

// Create a key for accessing the account store.
keyAccount := sdk.NewKVStoreKey("acc")
Expand Down
2 changes: 1 addition & 1 deletion docs/sdk/core/examples/app4.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func NewApp4(logger log.Logger, db dbm.DB) *bapp.BaseApp {
cdc := UpdatedCodec()

// Create the base application object.
app := bapp.NewBaseApp(app4Name, logger, db, auth.DefaultTxDecoder(cdc), false)
app := bapp.NewBaseApp(app4Name, logger, db, auth.DefaultTxDecoder(cdc), sdk.CollectConfig{})

// Create a key for accessing the account store.
keyAccount := sdk.NewKVStoreKey("acc")
Expand Down
2 changes: 1 addition & 1 deletion server/mock/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func NewApp(rootDir string, logger log.Logger) (abci.Application, error) {
capKeyMainStore := sdk.NewKVStoreKey("main")

// Create BaseApp.
baseApp := bam.NewBaseApp("kvstore", logger, db, decodeTx, false)
baseApp := bam.NewBaseApp("kvstore", logger, db, decodeTx, sdk.CollectConfig{})

// Set mounts for BaseApp's MultiStore.
baseApp.MountStoresIAVL(capKeyMainStore)
Expand Down
8 changes: 7 additions & 1 deletion types/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,10 @@ func (config *Config) GetBech32ValidatorPubPrefix() string {
// GetBech32ConsensusPubPrefix returns the Bech32 prefix for consensus node public key
func (config *Config) GetBech32ConsensusPubPrefix() string {
return config.bech32AddressPrefix["consensus_pub"]
}
}

// CollectConfig is the structure that holds configuration parameters whether to collect specified info during apply blocks.
type CollectConfig struct {
CollectAccountBalance bool
CollectTxs bool
}
12 changes: 11 additions & 1 deletion types/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ import (
// deliver state
type Pool struct {
accounts sync.Map // save tx/gov related addresses (string wrapped bytes) to be published
txs sync.Map
}

func (p *Pool) AddTx(tx Tx, txHash string) {
p.txs.Store(txHash, tx)
}

func (p Pool) GetTxs() sync.Map{
return p.txs
}

func (p *Pool) AddAddrs(addrs []AccAddress) {
Expand All @@ -28,6 +37,7 @@ func (p Pool) TxRelatedAddrs() []string {
return addrs
}

func (p *Pool) ClearTxRelatedAddrs() {
func (p *Pool) Clear() {
p.accounts = sync.Map{}
p.txs = sync.Map{}
}
2 changes: 1 addition & 1 deletion x/mock/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func NewApp() *App {

// Create your application object
app := &App{
BaseApp: bam.NewBaseApp("mock", logger, db, auth.DefaultTxDecoder(cdc), false),
BaseApp: bam.NewBaseApp("mock", logger, db, auth.DefaultTxDecoder(cdc), sdk.CollectConfig{}),
Cdc: cdc,
KeyMain: sdk.NewKVStoreKey("main"),
KeyAccount: sdk.NewKVStoreKey("acc"),
Expand Down

0 comments on commit a90d449

Please sign in to comment.