From d88c2f8a43a70eb2d5204e9ff2201a0252bfa61c Mon Sep 17 00:00:00 2001 From: Jakub Sztandera Date: Wed, 1 Apr 2020 22:26:14 +0200 Subject: [PATCH] Add Replace by Fee Signed-off-by: Jakub Sztandera --- chain/messagepool/messagepool.go | 25 +++++++++++++++++++++---- cli/send.go | 24 +++++++++++++++++++++--- scripts/dev/gen-daemon | 6 ++++-- 3 files changed, 46 insertions(+), 9 deletions(-) diff --git a/chain/messagepool/messagepool.go b/chain/messagepool/messagepool.go index c41be95c8..016e471d1 100644 --- a/chain/messagepool/messagepool.go +++ b/chain/messagepool/messagepool.go @@ -34,6 +34,13 @@ var log = logging.Logger("messagepool") const futureDebug = false +const ReplaceByFeeRatio = 1.25 + +var ( + rbfNum = types.NewInt(uint64((ReplaceByFeeRatio - 1) * 256)) + rbfDenom = types.NewInt(256) +) + var ( ErrMessageTooBig = errors.New("message too big") @@ -97,10 +104,20 @@ func (ms *msgSet) add(m *types.SignedMessage) error { if len(ms.msgs) == 0 || m.Message.Nonce >= ms.nextNonce { ms.nextNonce = m.Message.Nonce + 1 } - if _, has := ms.msgs[m.Message.Nonce]; has { - if m.Cid() != ms.msgs[m.Message.Nonce].Cid() { - log.Info("add with duplicate nonce") - return xerrors.Errorf("message to %s with nonce %d already in mpool", m.Message.To, m.Message.Nonce) + exms, has := ms.msgs[m.Message.Nonce] + if has { + if m.Cid() != exms.Cid() { + // check if RBF passes + minPrice := exms.Message.GasPrice + minPrice = types.BigAdd(minPrice, types.BigDiv(types.BigMul(minPrice, rbfNum), rbfDenom)) + minPrice = types.BigAdd(minPrice, types.NewInt(1)) + if types.BigCmp(m.Message.GasPrice, minPrice) > 0 { + log.Infow("add with RBF", "oldprice", exms.Message.GasPrice, + "newprice", m.Message.GasPrice, "addr", m.Message.From, "nonce", m.Message.Nonce) + } else { + log.Info("add with duplicate nonce") + return xerrors.Errorf("message to %s with nonce %d already in mpool", m.Message.To, m.Message.Nonce) + } } } ms.msgs[m.Message.Nonce] = m diff --git a/cli/send.go b/cli/send.go index 91fe78715..f26df3785 100644 --- a/cli/send.go +++ b/cli/send.go @@ -22,6 +22,11 @@ var sendCmd = &cli.Command{ Usage: "specify gas price to use in AttoFIL", Value: "0", }, + &cli.Int64Flag{ + Name: "nonce", + Usage: "specify the nonce to use", + Value: -1, + }, }, Action: func(cctx *cli.Context) error { api, closer, err := GetFullNodeAPI(cctx) @@ -76,9 +81,22 @@ var sendCmd = &cli.Command{ GasPrice: gp, } - _, err = api.MpoolPushMessage(ctx, msg) - if err != nil { - return err + if cctx.Int64("nonce") > 0 { + msg.Nonce = uint64(cctx.Int64("nonce")) + sm, err := api.WalletSignMessage(ctx, fromAddr, msg) + if err != nil { + return err + } + + _, err = api.MpoolPush(ctx, sm) + if err != nil { + return err + } + } else { + _, err = api.MpoolPushMessage(ctx, msg) + if err != nil { + return err + } } return nil diff --git a/scripts/dev/gen-daemon b/scripts/dev/gen-daemon index fdadc180f..ba896b5d6 100755 --- a/scripts/dev/gen-daemon +++ b/scripts/dev/gen-daemon @@ -4,5 +4,7 @@ set -o xtrace export TRUST_PARAMS=1 -go run -tags=debug ./cmd/lotus-seed pre-seal -go run -tags=debug ./cmd/lotus daemon --lotus-make-random-genesis=devel.gen --genesis-presealed-sectors=~/.genesis-sectors/pre-seal-t0101.json +go run -tags=debug ./cmd/lotus-seed pre-seal --sector-size 2048 --num-sectors 2 +go run -tags=debug ./cmd/lotus-seed genesis new localnet.json +go run -tags=debug ./cmd/lotus-seed genesis add-miner localnet.json ~/.genesis-sectors/pre-seal-t01000.json +go run -tags=debug ./cmd/lotus daemon --lotus-make-genesis=devel.gen --genesis-template=localnet.json --bootstrap=false