Add gas guesstimation

Signed-off-by: Jakub Sztandera <kubuxu@protocol.ai>
This commit is contained in:
Jakub Sztandera 2020-07-31 14:23:56 +02:00
parent 483b33acd1
commit fc0bd2cda5
No known key found for this signature in database
GPG Key ID: 9A9AF56F8B3879BA
3 changed files with 151 additions and 2 deletions

74
miner/guessgas.go Normal file
View File

@ -0,0 +1,74 @@
package miner
import (
"context"
"github.com/ipfs/go-cid"
"golang.org/x/xerrors"
"github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/specs-actors/actors/abi"
"github.com/filecoin-project/specs-actors/actors/builtin"
)
const failedGasGuessRatio = 0.5
const failedGasGuessMax = 25_000_000
type costKey struct {
code cid.Cid
m abi.MethodNum
}
var costs = map[costKey]int64{
{builtin.InitActorCodeID, 2}: 8916753,
{builtin.StorageMarketActorCodeID, 2}: 6955002,
{builtin.StorageMarketActorCodeID, 4}: 245436108,
{builtin.StorageMinerActorCodeID, 4}: 2315133,
{builtin.StorageMinerActorCodeID, 5}: 1600271356,
{builtin.StorageMinerActorCodeID, 6}: 22864493,
{builtin.StorageMinerActorCodeID, 7}: 142002419,
{builtin.StorageMinerActorCodeID, 10}: 23008274,
{builtin.StorageMinerActorCodeID, 11}: 19303178,
{builtin.StorageMinerActorCodeID, 14}: 566356835,
{builtin.StorageMinerActorCodeID, 16}: 5325185,
{builtin.StorageMinerActorCodeID, 18}: 2328637,
{builtin.StoragePowerActorCodeID, 2}: 23600956,
}
func failedGuess(msg *types.SignedMessage) int64 {
guess := int64(float64(msg.Message.GasLimit) * failedGasGuessRatio)
if guess > failedGasGuessMax {
guess = failedGasGuessMax
}
return guess
}
func GuessGasUsed(ctx context.Context, tsk types.TipSetKey, msg *types.SignedMessage, al ActorLookup) (int64, error) {
if msg.Message.Method == builtin.MethodSend {
switch msg.Message.From.Protocol() {
case address.BLS:
return 1298450, nil
case address.SECP256K1:
return 1385999, nil
default:
// who knows?
return 1298450, nil
}
}
to, err := al(ctx, msg.Message.To, tsk)
if err != nil {
return failedGuess(msg), xerrors.Errorf("could not lookup actor: %w", err)
}
guess, ok := costs[costKey{to.Code, msg.Message.Method}]
if !ok {
return failedGuess(msg), xerrors.Errorf("unknown code-method combo")
}
if guess > msg.Message.GasLimit {
guess = msg.Message.GasLimit
}
return guess, nil
}

View File

@ -5,7 +5,9 @@ import (
"testing"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/specs-actors/actors/builtin"
"github.com/filecoin-project/lotus/build"
"github.com/filecoin-project/lotus/chain/types"
"github.com/stretchr/testify/assert"
)
@ -19,6 +21,71 @@ func mustIDAddr(i uint64) address.Address {
return a
}
func TestSelectNotOverLimited(t *testing.T) {
ctx := context.TODO()
a1 := mustIDAddr(1)
a2 := mustIDAddr(2)
a3 := mustIDAddr(3)
actors := map[address.Address]*types.Actor{
a1: {
Code: builtin.AccountActorCodeID,
Nonce: 1,
Balance: types.FromFil(1200),
},
a2: {
Code: builtin.AccountActorCodeID,
Nonce: 1,
Balance: types.FromFil(1200),
},
a3: {
Code: builtin.StorageMinerActorCodeID,
Nonce: 0,
Balance: types.FromFil(1000),
},
}
af := func(ctx context.Context, addr address.Address, tsk types.TipSetKey) (*types.Actor, error) {
return actors[addr], nil
}
gasUsed := costs[costKey{builtin.StorageMinerActorCodeID, 4}]
var goodMsgs []types.Message
for i := int64(0); i < build.BlockGasLimit/gasUsed+10; i++ {
goodMsgs = append(goodMsgs, types.Message{
From: a1,
To: a3,
Method: 4,
Nonce: uint64(1 + i),
Value: types.FromFil(0),
GasLimit: gasUsed + 1000,
GasPrice: types.NewInt(1),
})
}
var badMsgs []types.Message
for i := int64(0); i < build.BlockGasLimit/gasUsed+10; i++ {
badMsgs = append(badMsgs, types.Message{
From: a2,
To: a3,
Method: 4,
Nonce: uint64(1 + i),
Value: types.FromFil(0),
GasLimit: 10 * gasUsed,
GasPrice: types.NewInt(1),
})
}
outmsgs, err := SelectMessages(ctx, af, &types.TipSet{}, wrapMsgs(append(goodMsgs, badMsgs...)))
if err != nil {
t.Fatal(err)
}
for _, v := range outmsgs {
if v.Message.From == a2 {
t.Errorf("included bad actor message: %v", v)
}
}
}
func TestMessageFiltering(t *testing.T) {
ctx := context.TODO()
a1 := mustIDAddr(1)

View File

@ -42,6 +42,7 @@ func SelectMessages(ctx context.Context, al ActorLookup, ts *types.TipSet, msgs
start := build.Clock.Now()
vmValid := time.Duration(0)
getbal := time.Duration(0)
guessGasDur := time.Duration(0)
sort.Slice(msgs, func(i, j int) bool {
return msgs[i].Message.Nonce < msgs[j].Message.Nonce
@ -112,8 +113,14 @@ func SelectMessages(ctx context.Context, al ActorLookup, ts *types.TipSet, msgs
sm.gasLimit = append(sm.gasLimit, sm.lastGasLimit+msg.Message.GasLimit)
sm.lastGasLimit = sm.gasLimit[len(sm.gasLimit)-1]
estimatedReward := big.Mul(types.NewInt(uint64(msg.Message.GasLimit)), msg.Message.GasPrice)
// TODO: estimatedReward = estimatedReward * (guessActualGasUse(msg) / msg.GasLimit)
guessGasStart := build.Clock.Now()
guessedGas, err := GuessGasUsed(ctx, ts.Key(), msg, al)
guessGasDur += build.Clock.Since(guessGasStart)
if err != nil {
log.Infow("failed to guess gas", "to", msg.Message.To, "method", msg.Message.Method, "err", err)
}
estimatedReward := big.Mul(types.NewInt(uint64(guessedGas)), msg.Message.GasPrice)
sm.gasReward = append(sm.gasReward, big.Add(sm.lastReward, estimatedReward))
sm.lastReward = sm.gasReward[len(sm.gasReward)-1]
@ -203,6 +210,7 @@ func SelectMessages(ctx context.Context, al ActorLookup, ts *types.TipSet, msgs
"duration", sm.Sub(start),
"vmvalidate", vmValid,
"getbalance", getbal,
"guessgas", guessGasDur,
"msgs", len(msgs))
}