Merge pull request #223 from filecoin-project/feat/block-reward

working on correct block reward
This commit is contained in:
Whyrusleeping 2019-09-27 15:08:54 -07:00 committed by GitHub
commit 47bf759122
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 76 additions and 4 deletions

View File

@ -1,5 +1,7 @@
package build
import "math/big"
// Core network constants
// /////
@ -56,6 +58,30 @@ const CollateralPrecision = 100
// Devnet settings
const TotalFilecoin = 2000000000
const MiningRewardTotal = 1400000000
const InitialRewardStr = "153856870367821447423"
var InitialReward *big.Int
const FilecoinPrecision = 1000000000000000000
// six years
// Blocks
const HalvingPeriodBlocks = 6 * 365 * 24 * 60 * 2
// Blocks
const AdjustmentPeriod = 7 * 24 * 60 * 2
// TODO: Move other important consts here
func init() {
InitialReward = new(big.Int)
var ok bool
InitialReward, ok = InitialReward.
SetString(InitialRewardStr, 10)
if !ok {
panic("could not parse InitialRewardStr")
}
}

View File

@ -42,9 +42,13 @@ func MinerCreateBlock(ctx context.Context, sm *stmgr.StateManager, w *wallet.Wal
if err != nil {
return nil, xerrors.Errorf("failed to get miner worker: %w", err)
}
networkBalance, err := vmi.ActorBalance(actors.NetworkAddress)
if err != nil {
return nil, xerrors.Errorf("failed to get network balance: %w", err)
}
// apply miner reward
if err := vmi.TransferFunds(actors.NetworkAddress, owner, vm.MiningRewardForBlock(parents)); err != nil {
if err := vmi.TransferFunds(actors.NetworkAddress, owner, vm.MiningReward(networkBalance)); err != nil {
return nil, err
}

View File

@ -441,8 +441,13 @@ func (syncer *Syncer) ValidateBlock(ctx context.Context, b *types.FullBlock) err
if err != nil {
return xerrors.Errorf("getting miner owner for block miner failed: %w", err)
}
networkBalance, err := vmi.ActorBalance(actors.NetworkAddress)
if err != nil {
return xerrors.Errorf("getting network balance")
}
if err := vmi.TransferFunds(actors.NetworkAddress, owner, vm.MiningRewardForBlock(baseTs)); err != nil {
if err := vmi.TransferFunds(actors.NetworkAddress, owner,
vm.MiningReward(networkBalance)); err != nil {
return xerrors.Errorf("fund transfer failed: %w", err)
}

View File

@ -3,7 +3,9 @@ package vm
import (
"context"
"fmt"
"math/big"
"github.com/filecoin-project/go-lotus/build"
"github.com/filecoin-project/go-lotus/chain/actors"
"github.com/filecoin-project/go-lotus/chain/actors/aerrors"
"github.com/filecoin-project/go-lotus/chain/address"
@ -631,6 +633,13 @@ func DepositFunds(act *types.Actor, amt types.BigInt) {
act.Balance = types.BigAdd(act.Balance, amt)
}
func MiningRewardForBlock(base *types.TipSet) types.BigInt {
return types.NewInt(10000)
var miningRewardTotal = types.FromFil(build.MiningRewardTotal)
// MiningReward returns correct mining reward
// coffer is amount of FIL in NetworkAddress
func MiningReward(remainingReward types.BigInt) types.BigInt {
ci := big.NewInt(0).Set(remainingReward.Int)
res := ci.Mul(ci, build.InitialReward)
res = res.Div(res, miningRewardTotal.Int)
return types.BigInt{res}
}

28
chain/vm/vm_test.go Normal file
View File

@ -0,0 +1,28 @@
package vm
import (
"fmt"
"math/big"
"testing"
"github.com/filecoin-project/go-lotus/build"
"github.com/filecoin-project/go-lotus/chain/types"
)
func TestBlockReward(t *testing.T) {
coffer := types.FromFil(build.MiningRewardTotal).Int
sum := new(big.Int)
N := build.HalvingPeriodBlocks
for i := 0; i < N; i++ {
a := MiningReward(types.BigInt{coffer})
sum = sum.Add(sum, a.Int)
coffer = coffer.Sub(coffer, a.Int)
}
//sum = types.BigMul(sum, types.NewInt(60))
fmt.Println("After a halving period")
fmt.Printf("Total reward: %d\n", build.MiningRewardTotal)
fmt.Printf("Remaining: %s\n", types.BigDiv(types.BigInt{coffer}, types.NewInt(build.FilecoinPrecision)))
fmt.Printf("Given out: %s\n", types.BigDiv(types.BigInt{sum}, types.NewInt(build.FilecoinPrecision)))
}