From 2a97045317dbd79d4f93f0e9a599869961679188 Mon Sep 17 00:00:00 2001 From: Jeromy Date: Wed, 22 Apr 2020 09:37:38 -0700 Subject: [PATCH] add an api for estimating gas prices --- api/api_full.go | 1 + api/apistruct/struct.go | 15 ++++++++++----- chain/messagepool/messagepool.go | 14 ++++++++++++++ node/impl/full/mpool.go | 4 ++++ 4 files changed, 29 insertions(+), 5 deletions(-) diff --git a/api/api_full.go b/api/api_full.go index 2e9a8d575..00da2f12a 100644 --- a/api/api_full.go +++ b/api/api_full.go @@ -66,6 +66,7 @@ type FullNode interface { MpoolPushMessage(context.Context, *types.Message) (*types.SignedMessage, error) // get nonce, sign, push MpoolGetNonce(context.Context, address.Address) (uint64, error) MpoolSub(context.Context) (<-chan MpoolUpdate, error) + MpoolEstimateGasPrice(context.Context, uint64) (types.BigInt, error) // FullNodeStruct diff --git a/api/apistruct/struct.go b/api/apistruct/struct.go index aff01078b..3383ec05c 100644 --- a/api/apistruct/struct.go +++ b/api/apistruct/struct.go @@ -81,11 +81,12 @@ type FullNodeStruct struct { SyncMarkBad func(ctx context.Context, bcid cid.Cid) error `perm:"admin"` SyncCheckBad func(ctx context.Context, bcid cid.Cid) (string, error) `perm:"read"` - MpoolPending func(context.Context, types.TipSetKey) ([]*types.SignedMessage, error) `perm:"read"` - MpoolPush func(context.Context, *types.SignedMessage) (cid.Cid, error) `perm:"write"` - MpoolPushMessage func(context.Context, *types.Message) (*types.SignedMessage, error) `perm:"sign"` - MpoolGetNonce func(context.Context, address.Address) (uint64, error) `perm:"read"` - MpoolSub func(context.Context) (<-chan api.MpoolUpdate, error) `perm:"read"` + MpoolPending func(context.Context, types.TipSetKey) ([]*types.SignedMessage, error) `perm:"read"` + MpoolPush func(context.Context, *types.SignedMessage) (cid.Cid, error) `perm:"write"` + MpoolPushMessage func(context.Context, *types.Message) (*types.SignedMessage, error) `perm:"sign"` + MpoolGetNonce func(context.Context, address.Address) (uint64, error) `perm:"read"` + MpoolSub func(context.Context) (<-chan api.MpoolUpdate, error) `perm:"read"` + MpoolEstimateGasPrice func(context.Context, uint64) (types.BigInt, error) `perm:"read"` MinerGetBaseInfo func(context.Context, address.Address, abi.ChainEpoch, types.TipSetKey) (*api.MiningBaseInfo, error) `perm:"read"` MinerCreateBlock func(context.Context, *api.BlockTemplate) (*types.BlockMsg, error) `perm:"write"` @@ -332,6 +333,10 @@ func (c *FullNodeStruct) MpoolSub(ctx context.Context) (<-chan api.MpoolUpdate, return c.Internal.MpoolSub(ctx) } +func (c *FullNodeStruct) MpoolEstimateGasPrice(ctx context.Context, priority uint64) (types.BigInt, error) { + return c.Internal.MpoolEstimateGasPrice(ctx, priority) +} + func (c *FullNodeStruct) MinerGetBaseInfo(ctx context.Context, maddr address.Address, epoch abi.ChainEpoch, tsk types.TipSetKey) (*api.MiningBaseInfo, error) { return c.Internal.MinerGetBaseInfo(ctx, maddr, epoch, tsk) } diff --git a/chain/messagepool/messagepool.go b/chain/messagepool/messagepool.go index 466a9ff8f..af4600541 100644 --- a/chain/messagepool/messagepool.go +++ b/chain/messagepool/messagepool.go @@ -822,3 +822,17 @@ func (mp *MessagePool) loadLocal() error { return nil } + +const MinGasPrice = 0 + +func (mp *MessagePool) EstimateGasPrice(ctx context.Context, priority uint64) (types.BigInt, error) { + // TODO: something smarter obviously + switch priority { + case 0: + return types.NewInt(MinGasPrice + 2), nil + case 1: + return types.NewInt(MinGasPrice + 1), nil + default: + return types.NewInt(MinGasPrice), nil + } +} diff --git a/node/impl/full/mpool.go b/node/impl/full/mpool.go index 834d149ab..10895955b 100644 --- a/node/impl/full/mpool.go +++ b/node/impl/full/mpool.go @@ -118,3 +118,7 @@ func (a *MpoolAPI) MpoolGetNonce(ctx context.Context, addr address.Address) (uin func (a *MpoolAPI) MpoolSub(ctx context.Context) (<-chan api.MpoolUpdate, error) { return a.Mpool.Updates(ctx) } + +func (a *MpoolAPI) MpoolEstimateGasPrice(ctx context.Context, priority uint64) (types.BigInt, error) { + return a.Mpool.EstimateGasPrice(ctx, priority) +}