Skip to content
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 a cmd flag to allow locally submitted anchor transactions #614

Merged
merged 4 commits into from
Aug 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 11 additions & 3 deletions blockchain/tx_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package blockchain

import (
"errors"
"fmt"
"github.com/klaytn/klaytn/kerrors"
"math"
Expand Down Expand Up @@ -54,6 +55,8 @@ var (
statsReportInterval = 8 * time.Second // Time interval to report transaction pool stats

txPoolIsFullErr = fmt.Errorf("txpool is full")

errNotAllowedAnchoringTx = errors.New("locally anchoring chaindata tx is not allowed in this node")
)

var (
Expand Down Expand Up @@ -102,9 +105,10 @@ type blockChain interface {

// TxPoolConfig are the configuration parameters of the transaction pool.
type TxPoolConfig struct {
NoLocals bool // Whether local transaction handling should be disabled
Journal string // Journal of local transactions to survive node restarts
JournalInterval time.Duration // Time interval to regenerate the local transaction journal
NoLocals bool // Whether local transaction handling should be disabled
AllowLocalAnchorTx bool // if this is true, the txpool allow locally submitted anchor transactions
Journal string // Journal of local transactions to survive node restarts
JournalInterval time.Duration // Time interval to regenerate the local transaction journal

PriceLimit uint64 // Minimum gas price to enforce for acceptance into the pool
PriceBump uint64 // Minimum price bump percentage to replace an already existing transaction (nonce)
Expand Down Expand Up @@ -947,6 +951,10 @@ func (pool *TxPool) handleTxMsg() {
// the sender as a local one in the mean time, ensuring it goes around the local
// pricing constraints.
func (pool *TxPool) AddLocal(tx *types.Transaction) error {
if tx.Type().IsChainDataAnchoring() && !pool.config.AllowLocalAnchorTx {
return errNotAllowedAnchoringTx
}

poolSize := uint64(len(pool.all))
if poolSize >= pool.config.ExecSlotsAll+pool.config.NonExecSlotsAll {
return fmt.Errorf("txpool is full: %d", poolSize)
Expand Down
60 changes: 60 additions & 0 deletions blockchain/tx_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,66 @@ func TestInvalidTransactions(t *testing.T) {
}
}

func genAnchorTx(nonce uint64) *types.Transaction {
key, _ := crypto.HexToECDSA("45a915e4d060149eb4365960e6a7a45f334393093061116b197e3240065ff2d8")
from := crypto.PubkeyToAddress(key.PublicKey)

gasLimit := uint64(1000000)
gasPrice := big.NewInt(1)

data := []byte{0x11, 0x22}
values := map[types.TxValueKeyType]interface{}{
types.TxValueKeyNonce: nonce,
types.TxValueKeyFrom: from,
types.TxValueKeyGasLimit: gasLimit,
types.TxValueKeyGasPrice: gasPrice,
types.TxValueKeyAnchoredData: data,
}

tx, _ := types.NewTransactionWithMap(types.TxTypeChainDataAnchoring, values)

signer := types.MakeSigner(params.BFTTestChainConfig, big.NewInt(2))
tx.Sign(signer, key)

return tx
}

func TestAnchorTransactions(t *testing.T) {
t.Parallel()

pool, _ := setupTxPool()
defer pool.Stop()

poolAllow, _ := setupTxPool()
poolAllow.config.AllowLocalAnchorTx = true
defer poolAllow.Stop()

tx1 := genAnchorTx(1)
tx2 := genAnchorTx(2)

from, _ := tx1.From()
pool.currentState.AddBalance(from, big.NewInt(10000000))
poolAllow.currentState.AddBalance(from, big.NewInt(10000000))

// default txPool
{
err := pool.AddRemote(tx1)
assert.NoError(t, err)

err = pool.AddLocal(tx2)
assert.Error(t, errNotAllowedAnchoringTx, err)
}

// txPool which allow locally submitted anchor txs
{
err := poolAllow.AddRemote(tx1)
assert.NoError(t, err)

err = poolAllow.AddLocal(tx2)
assert.NoError(t, err)
}
}

func TestTransactionQueue(t *testing.T) {
t.Parallel()

Expand Down
1 change: 1 addition & 0 deletions cmd/kcn/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ var cnHelpFlagGroups = []utils.FlagGroup{
Name: "TXPOOL",
Flags: []cli.Flag{
utils.TxPoolNoLocalsFlag,
utils.TxPoolAllowLocalAnchorTxFlag,
utils.TxPoolJournalFlag,
utils.TxPoolJournalIntervalFlag,
utils.TxPoolPriceLimitFlag,
Expand Down
1 change: 1 addition & 0 deletions cmd/ken/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ var enHelpFlagGroups = []utils.FlagGroup{
Name: "TXPOOL",
Flags: []cli.Flag{
utils.TxPoolNoLocalsFlag,
utils.TxPoolAllowLocalAnchorTxFlag,
utils.TxPoolJournalFlag,
utils.TxPoolJournalIntervalFlag,
utils.TxPoolPriceLimitFlag,
Expand Down
1 change: 1 addition & 0 deletions cmd/kpn/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ var pnHelpFlagGroups = []utils.FlagGroup{
Name: "TXPOOL",
Flags: []cli.Flag{
utils.TxPoolNoLocalsFlag,
utils.TxPoolAllowLocalAnchorTxFlag,
utils.TxPoolJournalFlag,
utils.TxPoolJournalIntervalFlag,
utils.TxPoolPriceLimitFlag,
Expand Down
1 change: 1 addition & 0 deletions cmd/kscn/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ var scnHelpFlagGroups = []utils.FlagGroup{
Name: "TXPOOL",
Flags: []cli.Flag{
utils.TxPoolNoLocalsFlag,
utils.TxPoolAllowLocalAnchorTxFlag,
utils.TxPoolJournalFlag,
utils.TxPoolJournalIntervalFlag,
utils.TxPoolPriceLimitFlag,
Expand Down
1 change: 1 addition & 0 deletions cmd/ksen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ var senHelpFlagGroups = []utils.FlagGroup{
Name: "TXPOOL",
Flags: []cli.Flag{
utils.TxPoolNoLocalsFlag,
utils.TxPoolAllowLocalAnchorTxFlag,
utils.TxPoolJournalFlag,
utils.TxPoolJournalIntervalFlag,
utils.TxPoolPriceLimitFlag,
Expand Down
1 change: 1 addition & 0 deletions cmd/kspn/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ var spnHelpFlagGroups = []utils.FlagGroup{
Name: "TXPOOL",
Flags: []cli.Flag{
utils.TxPoolNoLocalsFlag,
utils.TxPoolAllowLocalAnchorTxFlag,
utils.TxPoolJournalFlag,
utils.TxPoolJournalIntervalFlag,
utils.TxPoolPriceLimitFlag,
Expand Down
7 changes: 7 additions & 0 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ var (
Name: "txpool.nolocals",
Usage: "Disables price exemptions for locally submitted transactions",
}
TxPoolAllowLocalAnchorTxFlag = cli.BoolFlag{
Name: "txpool.allow-local-anchortx",
Usage: "Allow locally submitted anchoring transactions",
}
TxPoolJournalFlag = cli.StringFlag{
Name: "txpool.journal",
Usage: "Disk journal for local transaction to survive node restarts",
Expand Down Expand Up @@ -1117,6 +1121,9 @@ func setTxPool(ctx *cli.Context, cfg *blockchain.TxPoolConfig) {
if ctx.GlobalIsSet(TxPoolNoLocalsFlag.Name) {
cfg.NoLocals = ctx.GlobalBool(TxPoolNoLocalsFlag.Name)
}
if ctx.GlobalIsSet(TxPoolAllowLocalAnchorTxFlag.Name) {
cfg.AllowLocalAnchorTx = ctx.GlobalBool(TxPoolAllowLocalAnchorTxFlag.Name)
}
if ctx.GlobalIsSet(TxPoolJournalFlag.Name) {
cfg.Journal = ctx.GlobalString(TxPoolJournalFlag.Name)
}
Expand Down
1 change: 1 addition & 0 deletions cmd/utils/nodecmd/nodeflags.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ var CommonNodeFlags = []cli.Flag{
utils.DataDirFlag,
utils.KeyStoreDirFlag,
utils.TxPoolNoLocalsFlag,
utils.TxPoolAllowLocalAnchorTxFlag,
utils.TxPoolJournalFlag,
utils.TxPoolJournalIntervalFlag,
utils.TxPoolPriceLimitFlag,
Expand Down