Skip to content

Commit

Permalink
Support min fees-based anti spam mechanism
Browse files Browse the repository at this point in the history
Create cosmos.toml configuration file and handle minimum_fees
setting/flag to provide validators with a simple and flexible
anti-spam mechanism.

Closes: #1921
  • Loading branch information
Alessio Treglia committed Sep 15, 2018
1 parent 98005b0 commit 61fb968
Show file tree
Hide file tree
Showing 15 changed files with 339 additions and 49 deletions.
18 changes: 11 additions & 7 deletions baseapp/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ type BaseApp struct {
deliverState *state // for DeliverTx
signedValidators []abci.SigningValidator // absent validators from begin block

// minimum fees for spam prevention
minimumFees sdk.Coins

// flag for sealing
sealed bool
}
Expand Down Expand Up @@ -188,12 +191,12 @@ func (app *BaseApp) initFromStore(mainKey sdk.StoreKey) error {
return nil
}

// SetMinimumFees sets the minimum fees.
func (app *BaseApp) SetMinimumFees(fees sdk.Coins) { app.minimumFees = fees }

// 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 {
return sdk.NewContext(app.checkState.ms, header, true, app.Logger)
}
return sdk.NewContext(app.deliverState.ms, header, false, app.Logger)
return sdk.NewContext(app.checkState.ms, header, isCheckTx, app.Logger).WithMinimumFees(app.minimumFees)
}

type state struct {
Expand All @@ -209,15 +212,15 @@ func (app *BaseApp) setCheckState(header abci.Header) {
ms := app.cms.CacheMultiStore()
app.checkState = &state{
ms: ms,
ctx: sdk.NewContext(ms, header, true, app.Logger),
ctx: sdk.NewContext(ms, header, true, app.Logger).WithMinimumFees(app.minimumFees),
}
}

func (app *BaseApp) setDeliverState(header abci.Header) {
ms := app.cms.CacheMultiStore()
app.deliverState = &state{
ms: ms,
ctx: sdk.NewContext(ms, header, false, app.Logger),
ctx: sdk.NewContext(ms, header, false, app.Logger).WithMinimumFees(app.minimumFees),
}
}

Expand Down Expand Up @@ -386,7 +389,8 @@ func handleQueryCustom(app *BaseApp, path []string, req abci.RequestQuery) (res
sdk.ErrUnknownRequest(fmt.Sprintf("no custom querier found for route %s", path[1])).QueryResult()
}

ctx := sdk.NewContext(app.cms.CacheMultiStore(), app.checkState.ctx.BlockHeader(), true, app.Logger)
ctx := sdk.NewContext(app.cms.CacheMultiStore(), app.checkState.ctx.BlockHeader(), true, app.Logger).
WithMinimumFees(app.minimumFees)
// Passes the rest of the path as an argument to the querier.
// For example, in the path "custom/gov/proposal/test", the gov querier gets []string{"proposal", "test"} as the path
resBytes, err := querier(ctx, path[2:], req)
Expand Down
11 changes: 10 additions & 1 deletion baseapp/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,18 @@ func SetPruning(pruning string) func(*BaseApp) {
case "syncable":
pruningEnum = sdk.PruneSyncable
default:
panic(fmt.Sprintf("Invalid pruning strategy: %s", pruning))
panic(fmt.Sprintf("invalid pruning strategy: %s", pruning))
}
return func(bap *BaseApp) {
bap.cms.SetPruning(pruningEnum)
}
}

// SetMinimumFees returns an option that sets the minimum fees on the app.
func SetMinimumFees(minFees string) func(*BaseApp) {
fees, err := sdk.ParseCoins(minFees)
if err != nil {
panic(fmt.Sprintf("invalid minimum fees: %v", err))
}
return func(bap *BaseApp) { bap.SetMinimumFees(fees) }
}
24 changes: 24 additions & 0 deletions cmd/gaia/cli_test/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,30 @@ func init() {
gaiadHome, gaiacliHome = getTestingHomeDirs()
}

func TestGaiaCLIMinimumFees(t *testing.T) {
chainID, servAddr, port := initializeFixtures(t)
flags := fmt.Sprintf("--home=%s --node=%v --chain-id=%v", gaiacliHome, servAddr, chainID)

// start gaiad server with minimum fees
proc := tests.GoExecuteTWithStdout(t, fmt.Sprintf("gaiad start --home=%s --rpc.laddr=%v --minimum_fees=2feeToken", gaiadHome, servAddr))

defer proc.Stop(false)
tests.WaitForTMStart(port)
tests.WaitForNextNBlocksTM(2, port)

fooAddr, _ := executeGetAddrPK(t, fmt.Sprintf("gaiacli keys show foo --output=json --home=%s", gaiacliHome))
barAddr, _ := executeGetAddrPK(t, fmt.Sprintf("gaiacli keys show bar --output=json --home=%s", gaiacliHome))

fooAcc := executeGetAccount(t, fmt.Sprintf("gaiacli account %s %v", fooAddr, flags))
require.Equal(t, int64(50), fooAcc.GetCoins().AmountOf("steak").Int64())

success := executeWrite(t, fmt.Sprintf(
"gaiacli send %v --amount=10steak --to=%s --from=foo", flags, barAddr), app.DefaultKeyPass)
require.False(t, success)
tests.WaitForNextNBlocksTM(2, port)

}

func TestGaiaCLISend(t *testing.T) {
chainID, servAddr, port := initializeFixtures(t)
flags := fmt.Sprintf("--home=%s --node=%v --chain-id=%v", gaiacliHome, servAddr, chainID)
Expand Down
5 changes: 4 additions & 1 deletion cmd/gaia/cmd/gaiad/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ func main() {
}

func newApp(logger log.Logger, db dbm.DB, traceStore io.Writer) abci.Application {
return app.NewGaiaApp(logger, db, traceStore, baseapp.SetPruning(viper.GetString("pruning")))
return app.NewGaiaApp(logger, db, traceStore,
baseapp.SetPruning(viper.GetString("pruning")),
baseapp.SetMinimumFees(viper.GetString("minimum_fees")),
)
}

func exportAppStateAndTMValidators(
Expand Down
36 changes: 36 additions & 0 deletions server/config/config.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,41 @@
package config

import (
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
)

const (
defaultMinimumFees = ""
)

// BaseConfig defines the server's basic configuration
type BaseConfig struct {
// Tx minimum fee
MinFees string `mapstructure:"minimum_fees"`
}

// Config defines the server's top level configuration
type Config struct {
BaseConfig `mapstructure:",squash"`
}

// SetMinimumFee sets the minimum fee.
func (c *Config) SetMinimumFees(fees sdk.Coins) { c.MinFees = fees.String() }

// SetMinimumFee sets the minimum fee.
func (c *Config) MinimumFees() sdk.Coins {
fees, err := sdk.ParseCoins(c.MinFees)
if err != nil {
panic(fmt.Sprintf("invalid minimum fees: %v", err))
}
return fees
}

// DefaultConfig returns server's default configuration.
func DefaultConfig() *Config { return &Config{BaseConfig{MinFees: defaultMinimumFees}} }

//_____________________________________________________________________

// Configuration structure for command functions that share configuration.
Expand Down
19 changes: 19 additions & 0 deletions server/config/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package config

import (
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"
)

func TestDefaultConfig(t *testing.T) {
cfg := DefaultConfig()
require.True(t, cfg.MinimumFees().IsZero())
}

func TestSetMinimumFees(t *testing.T) {
cfg := DefaultConfig()
cfg.SetMinimumFees(sdk.Coins{sdk.NewCoin("foo", sdk.NewInt(100))})
require.Equal(t, "100foo", cfg.MinFees)
}
46 changes: 46 additions & 0 deletions server/config/toml.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package config

import (
"bytes"
"text/template"

"github.com/spf13/viper"
cmn "github.com/tendermint/tendermint/libs/common"
)

const defaultConfigTemplate = `# This is a TOML config file.
# For more information, see https://github.com/toml-lang/toml
##### main base config options #####
# Validators reject any tx from the mempool with less than the minimum fee per gas.
minimum_fees = "{{ .BaseConfig.MinFees }}"
`

var configTemplate *template.Template

func init() {
var err error
tmpl := template.New("cosmosConfigFileTemplate")
if configTemplate, err = tmpl.Parse(defaultConfigTemplate); err != nil {
panic(err)
}
}

// ParseConfig retrieves the default environment configuration for Cosmos.
func ParseConfig() (*Config, error) {
conf := DefaultConfig()
err := viper.Unmarshal(conf)
return conf, err
}

// WriteConfigFile renders config using the template and writes it to configFilePath.
func WriteConfigFile(configFilePath string, config *Config) {
var buffer bytes.Buffer

if err := configTemplate.Execute(&buffer, config); err != nil {
panic(err)
}

cmn.MustWriteFile(configFilePath, buffer.Bytes(), 0644)
}
2 changes: 2 additions & 0 deletions server/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const (
flagAddress = "address"
flagTraceStore = "trace-store"
flagPruning = "pruning"
flagMinimumFees = "minimum_fees"
)

// StartCmd runs the service passed in, either stand-alone or in-process with
Expand All @@ -45,6 +46,7 @@ func StartCmd(ctx *Context, appCreator AppCreator) *cobra.Command {
cmd.Flags().String(flagAddress, "tcp://0.0.0.0:26658", "Listen address")
cmd.Flags().String(flagTraceStore, "", "Enable KVStore tracing to an output file")
cmd.Flags().String(flagPruning, "syncable", "Pruning strategy: syncable, nothing, everything")
cmd.Flags().String(flagMinimumFees, "", "Minimum fees validator will accept for transactions")

// add support for all Tendermint-specific command line options
tcmd.AddNodeFlags(cmd)
Expand Down
15 changes: 15 additions & 0 deletions server/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/server/config"
"github.com/cosmos/cosmos-sdk/version"
tcmd "github.com/tendermint/tendermint/cmd/tendermint/commands"
cfg "github.com/tendermint/tendermint/config"
Expand Down Expand Up @@ -97,6 +98,20 @@ func interceptLoadConfig() (conf *cfg.Config, err error) {
if conf == nil {
conf, err = tcmd.ParseConfig()
}

cosmosConfigFilePath := filepath.Join(rootDir, "config/cosmos.toml")
viper.SetConfigName("cosmos")
_ = viper.MergeInConfig()
var cosmosConf *config.Config
if _, err := os.Stat(cosmosConfigFilePath); os.IsNotExist(err) {
cosmosConf, _ := config.ParseConfig()
config.WriteConfigFile(cosmosConfigFilePath, cosmosConf)
}

if cosmosConf == nil {
_, err = config.ParseConfig()
}

return
}

Expand Down
19 changes: 19 additions & 0 deletions types/coin.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ func (coin Coin) Minus(coinB Coin) Coin {
return Coin{coin.Denom, coin.Amount.Sub(coinB.Amount)}
}

// Mul makes a new coin and sets Amount to the product coin.Amount*m.
func (coin Coin) Mul(m Int) Coin {
return Coin{coin.Denom, coin.Amount.Mul(m)}
}

//----------------------------------------
// Coins

Expand Down Expand Up @@ -170,6 +175,20 @@ func (coins Coins) Minus(coinsB Coins) Coins {
return coins.Plus(coinsB.Negative())
}

// Mul multiplies a set of coin by a given factor.
// CONTRACT: Mul will never return Coins where one Coin has a 0 amount.
func (coins Coins) Mul(m Int) Coins {
res := make([]Coin, 0, len(coins))
for _, coin := range coins {
if coin.Mul(m).IsZero() {
// ignore 0 sum coin type
} else {
res = append(res, coin.Mul(m))
}
}
return res
}

// IsGTE returns True iff coins is NonNegative(), and for every
// currency in coinsB, the currency is present at an equal or greater
// amount in coinsB
Expand Down
40 changes: 40 additions & 0 deletions types/coin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,22 @@ func TestPlusCoin(t *testing.T) {
require.Equal(t, tc.expected, res.Amount.Int64())
}

func TestMulCoin(t *testing.T) {
cases := []struct {
inputOne Coin
multiplier Int
expected Coin
}{
{NewInt64Coin("A", 1), NewInt(0), NewInt64Coin("A", 0)},
{NewInt64Coin("A", 1), NewInt(-3), NewInt64Coin("A", -3)},
{NewInt64Coin("A", 0), NewInt(1), NewInt64Coin("A", 0)},
{NewInt64Coin("A", -1), NewInt(-3), NewInt64Coin("A", 3)},
}
for tcIndex, tc := range cases {
require.Equal(t, tc.expected, tc.inputOne.Mul(tc.multiplier), "multiplication of coins is incorrect, tc #%d", tcIndex)
}
}

func TestMinusCoin(t *testing.T) {
cases := []struct {
inputOne Coin
Expand Down Expand Up @@ -261,6 +277,30 @@ func TestPlusCoins(t *testing.T) {
}
}

func TestMulCoins(t *testing.T) {
one := NewInt(1)
zero := NewInt(0)
negone := NewInt(-1)
two := NewInt(2)

cases := []struct {
inputOne Coins
multiplier Int
expected Coins
}{
{Coins{{"A", two}, {"B", one}}, NewInt(0), Coins{}},
{Coins{{"A", one}, {"B", one}}, NewInt(1), Coins{{"A", one}, {"B", one}}},
{Coins{{"A", one}, {"B", zero}}, NewInt(-1), Coins{{"A", negone}}},
{Coins{{"A", one}, {"B", negone}}, NewInt(-1), Coins{{"A", negone}, {"B", one}}},
}

for tcIndex, tc := range cases {
res := tc.inputOne.Mul(tc.multiplier)
assert.True(t, res.IsValid())
require.Equal(t, tc.expected, res, "multiplication of coins is incorrect, tc #%d", tcIndex)
}
}

//Test the parsing of Coin and Coins
func TestParse(t *testing.T) {
one := NewInt(1)
Expand Down
Loading

0 comments on commit 61fb968

Please sign in to comment.