Skip to content

Commit

Permalink
Add coin flow record enable config
Browse files Browse the repository at this point in the history
  • Loading branch information
HaoyangLiu committed Feb 20, 2019
1 parent 744ee9c commit 9966fe3
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 12 deletions.
2 changes: 1 addition & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func NewIrisApp(logger log.Logger, db dbm.DB, config *cfg.InstrumentationConfig,
appPrometheusConfig := *config
//Change namespace to appName
appPrometheusConfig.Namespace = appPrometheusNamespace
engine.Add(v0.NewProtocolV0(0, logger, protocolKeeper, app.checkInvariant, &appPrometheusConfig))
engine.Add(v0.NewProtocolV0(0, logger, protocolKeeper, app.checkInvariant, app.coinFlowRecord, &appPrometheusConfig))
// engine.Add(v1.NewProtocolV1(1, ...))
// engine.Add(v2.NewProtocolV1(2, ...))

Expand Down
10 changes: 8 additions & 2 deletions app/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ type BaseApp struct {
// enable invariant check
checkInvariant bool

// enable coin flow record
coinFlowRecord bool

// flag for sealing
sealed bool
}
Expand Down Expand Up @@ -211,6 +214,9 @@ func (app *BaseApp) SetMinimumFees(fees sdk.Coins) { app.minimumFees = fees }
// SetInvariantCheck sets the invariant check config.
func (app *BaseApp) SetCheckInvariant(check bool) { app.checkInvariant = check }

// SetInvariantCheck sets the invariant check config.
func (app *BaseApp) SetCoinFlowRecord(enable bool) { app.coinFlowRecord = enable }

// NewContext returns a new Context with the correct store, the given header, and nil txBytes.
func (app *BaseApp) NewContext(isCheckTx bool, header abci.Header) sdk.Context {
if isCheckTx {
Expand Down Expand Up @@ -313,7 +319,7 @@ func (app *BaseApp) InitChain(req abci.RequestInitChain) (res abci.ResponseInitC

// add block gas meter for any genesis transactions (allow infinite gas)
app.deliverState.ctx = app.deliverState.ctx.
WithBlockGasMeter(sdk.NewInfiniteGasMeter()).WithCoinFlowTags(sdk.NewCoinFlowRecord())
WithBlockGasMeter(sdk.NewInfiniteGasMeter()).WithCoinFlowTags(sdk.NewCoinFlowRecord(app.coinFlowRecord))

res = initChainer(app.deliverState.ctx, app.DeliverTx, req)

Expand Down Expand Up @@ -500,7 +506,7 @@ func (app *BaseApp) BeginBlock(req abci.RequestBeginBlock) (res abci.ResponseBeg
gasMeter = sdk.NewInfiniteGasMeter()
}
app.deliverState.ctx = app.deliverState.ctx.WithBlockGasMeter(gasMeter).
WithLogger(app.deliverState.ctx.Logger().With("height", app.deliverState.ctx.BlockHeight())).WithCoinFlowTags(sdk.NewCoinFlowRecord())
WithLogger(app.deliverState.ctx.Logger().With("height", app.deliverState.ctx.BlockHeight())).WithCoinFlowTags(sdk.NewCoinFlowRecord(app.coinFlowRecord))

beginBlocker := app.Engine.GetCurrentProtocol().GetBeginBlocker()

Expand Down
5 changes: 5 additions & 0 deletions app/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ func SetCheckInvariant(check bool) func(*BaseApp) {
return func(bap *BaseApp) { bap.SetCheckInvariant(check) }
}

// SetCoinFlowRecord set app coin flow record config
func SetCoinFlowRecord(enable bool) func(*BaseApp) {
return func(bap *BaseApp) { bap.SetCoinFlowRecord(enable) }
}

// nolint - Setter functions
func (app *BaseApp) SetName(name string) {
if app.sealed {
Expand Down
9 changes: 6 additions & 3 deletions app/v0/protocol_v0.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type ProtocolV0 struct {
logger log.Logger
invariantLevel string
checkInvariant bool
coinFlowRecord bool

// Manage getting and setting accounts
accountMapper auth.AccountKeeper
Expand Down Expand Up @@ -66,13 +67,14 @@ type ProtocolV0 struct {
metrics *Metrics
}

func NewProtocolV0(version uint64, log log.Logger, pk sdk.ProtocolKeeper, checkInvariant bool, config *cfg.InstrumentationConfig) *ProtocolV0 {
func NewProtocolV0(version uint64, log log.Logger, pk sdk.ProtocolKeeper, checkInvariant bool, coinFlowRecord bool, config *cfg.InstrumentationConfig) *ProtocolV0 {
p0 := ProtocolV0{
version: version,
logger: log,
protocolKeeper: pk,
invariantLevel: strings.ToLower(sdk.InvariantLevel),
checkInvariant: checkInvariant,
coinFlowRecord: coinFlowRecord,
router: protocol.NewRouter(),
queryRouter: protocol.NewQueryRouter(),
config: config,
Expand Down Expand Up @@ -321,7 +323,9 @@ func (p *ProtocolV0) EndBlocker(ctx sdk.Context, req abci.RequestEndBlock) abci.
tags = tags.AppendTags(service.EndBlocker(ctx, p.serviceKeeper))
tags = tags.AppendTags(upgrade.EndBlocker(ctx, p.upgradeKeeper))
validatorUpdates := stake.EndBlocker(ctx, p.StakeKeeper)
tags = tags.AppendTags(extractCoinFlowTags(ctx))
if p.coinFlowRecord {
tags = tags.AppendTags(extractCoinFlowTags(ctx))
}
p.assertRuntimeInvariants(ctx)

return abci.ResponseEndBlock{
Expand All @@ -333,7 +337,6 @@ func (p *ProtocolV0) EndBlocker(ctx sdk.Context, req abci.RequestEndBlock) abci.
func extractCoinFlowTags(ctx sdk.Context) sdk.Tags {
var tags sdk.Tags
for _, tag := range ctx.CoinFlowTags().GetTags() {
//tagParts := strings.Split(tag, ":")
ctx.Logger().Error("CoinFlowRecord","key", string(tag.Key), "value", string(tag.Value))
tags = tags.AppendTag(string(tag.Key), tag.Value)
}
Expand Down
1 change: 1 addition & 0 deletions cmd/iris/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ func newApp(logger log.Logger, db dbm.DB, traceStore io.Writer, config *cfg.Inst
bam.SetPruning(viper.GetString("pruning")),
bam.SetMinimumFees(viper.GetString("minimum_fees")),
bam.SetCheckInvariant(viper.GetBool("check_invariant")),
bam.SetCoinFlowRecord(viper.GetBool("coin_flow_record")),
)
}

Expand Down
5 changes: 4 additions & 1 deletion server/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ type BaseConfig struct {

// Enable invariant check, ignore this flag on testnet
CheckInvariant bool `mapstructure:"check_invariant"`

// Enable coin flow record
CoinFlowRecord bool `mapstructure:"coin_flow_record"`
}

// Config defines the server's top level configuration
Expand All @@ -38,5 +41,5 @@ func (c *Config) MinimumFees() sdk.Coins {

// DefaultConfig returns server's default configuration.
func DefaultConfig() *Config {
return &Config{BaseConfig{MinFees: defaultMinimumFees, CheckInvariant: false}}
return &Config{BaseConfig{MinFees: defaultMinimumFees, CheckInvariant: false, CoinFlowRecord: false}}
}
5 changes: 4 additions & 1 deletion server/config/toml.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ const defaultConfigTemplate = `# This is a TOML config file.
minimum_fees = "{{ .BaseConfig.MinFees }}"
# Enable invariant check on mainnet, ignore this config on testnet
check_invariant = "{{ .BaseConfig.CheckInvariant }}"
check_invariant = {{ .BaseConfig.CheckInvariant }}
# Enable coin flow record
coin_flow_record = {{ .BaseConfig.CoinFlowRecord }}
`

Expand Down
2 changes: 1 addition & 1 deletion tools/debug/hack.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func NewIrisApp(logger log.Logger, db dbm.DB, baseAppOptions ...func(*bam.BaseAp
cmn.Exit(err.Error())
}

engine.Add(v0.NewProtocolV0(0, logger, protocolKeeper, true, cfg.DefaultInstrumentationConfig()))
engine.Add(v0.NewProtocolV0(0, logger, protocolKeeper, true, false, cfg.DefaultInstrumentationConfig()))
// engine.Add(v1.NewProtocolV1(1, ...))

engine.LoadCurrentProtocol(app.GetKVStore(protocol.KeyMain))
Expand Down
21 changes: 18 additions & 3 deletions types/contexttags.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,20 @@ type CoinFlowTags interface {
}

type CoinFlowRecord struct {
tags Tags
tags Tags
enable bool
}

func NewCoinFlowRecord() CoinFlowTags {
return &CoinFlowRecord{}
func NewCoinFlowRecord(enable bool) CoinFlowTags {
return &CoinFlowRecord{
enable: enable,
}
}

func (cfRecord *CoinFlowRecord) Append(key, value string) {
if !cfRecord.enable {
return
}
cfRecord.tags = append(cfRecord.tags, MakeTag(key, []byte(value)))
}

Expand All @@ -55,6 +61,9 @@ func (cfRecord *CoinFlowRecord) GetTags() Tags {
}

func (cfRecord *CoinFlowRecord) AppendAddCoinTag(ctx Context, recipient, amount string) {
if !cfRecord.enable {
return
}
var tagKeyBuffer bytes.Buffer
tagKeyBuffer.WriteString(ctx.CoinFlowTrigger())

Expand All @@ -74,6 +83,9 @@ func (cfRecord *CoinFlowRecord) AppendAddCoinTag(ctx Context, recipient, amount
}

func (cfRecord *CoinFlowRecord) AppendSubtractCoinTag(ctx Context, sender, amount string) {
if !cfRecord.enable {
return
}
var tagKeyBuffer bytes.Buffer
tagKeyBuffer.WriteString(ctx.CoinFlowTrigger())

Expand All @@ -93,6 +105,9 @@ func (cfRecord *CoinFlowRecord) AppendSubtractCoinTag(ctx Context, sender, amoun
}

func (cfRecord *CoinFlowRecord) AppendAddCoinSourceTag(ctx Context, recipient, sourceType, source, amount string) {
if !cfRecord.enable {
return
}
var tagKeyBuffer bytes.Buffer
tagKeyBuffer.WriteString(ctx.CoinFlowTrigger())

Expand Down

0 comments on commit 9966fe3

Please sign in to comment.