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

Fee filtering, plus tests #2400

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions cmd/go-quai/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/spf13/viper"

"github.com/dominant-strategies/go-quai/cmd/utils"
"github.com/dominant-strategies/go-quai/common"
"github.com/dominant-strategies/go-quai/log"
"github.com/dominant-strategies/go-quai/metrics_config"
"github.com/dominant-strategies/go-quai/p2p/node"
Expand Down Expand Up @@ -73,6 +74,7 @@ func runStart(cmd *cobra.Command, args []string) error {
// create a quit channel for services to signal for a clean shutdown
quitCh := make(chan struct{})

common.SanityCheck(quitCh)
// create a new p2p node
node, err := node.NewNode(ctx, quitCh)
if err != nil {
Expand Down Expand Up @@ -104,8 +106,14 @@ func runStart(cmd *cobra.Command, args []string) error {
// wait for a SIGINT or SIGTERM signal
ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
<-ch
log.Global.Warn("Received 'stop' signal, shutting down gracefully...")

select {
case <-ch:
log.Global.Warn("Received 'stop' signal, shutting down gracefully...")
case <-quitCh:
log.Global.Warn("Received 'quit' signal from child, shutting down...")
}

cancel()
// stop the hierarchical co-ordinator
hc.Stop()
Expand Down
52 changes: 51 additions & 1 deletion common/big.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ package common

import (
"math/big"
"time"

"github.com/dominant-strategies/go-quai/log"
"modernc.org/mathutil"
)

Expand All @@ -33,11 +35,15 @@ var (
Big2 = big.NewInt(2)
Big3 = big.NewInt(3)
Big8 = big.NewInt(8)
Big10 = big.NewInt(10)
Big32 = big.NewInt(32)
Big99 = big.NewInt(99)
Big100 = big.NewInt(100)
Big101 = big.NewInt(101)
Big256 = big.NewInt(256)
Big257 = big.NewInt(257)
Big2e256 = new(big.Int).Exp(big.NewInt(2), big.NewInt(256), big.NewInt(0))
Big2e64 = new(big.Int).Exp(big.NewInt(2), big.NewInt(64), big.NewInt(0))
Big2e256 = new(big.Int).Exp(big.NewInt(2), big.NewInt(256), big.NewInt(0))
)

func BigBitsToBits(original *big.Int) *big.Int {
Expand Down Expand Up @@ -77,3 +83,47 @@ func LogBig(diff *big.Int) *big.Int {
bigBits = new(big.Int).Add(bigBits, m)
return bigBits
}

// Continously verify that the common values have not been overwritten.
func SanityCheck(quitCh chan struct{}) {
big0 := big.NewInt(0)
big1 := big.NewInt(1)
big2 := big.NewInt(2)
big3 := big.NewInt(3)
big8 := big.NewInt(8)
big10 := big.NewInt(10)
big32 := big.NewInt(32)
big99 := big.NewInt(99)
big100 := big.NewInt(100)
big101 := big.NewInt(101)
big256 := big.NewInt(256)
big257 := big.NewInt(257)
big2e64 := new(big.Int).Exp(big.NewInt(2), big.NewInt(64), big.NewInt(0))
big2e256 := new(big.Int).Exp(big.NewInt(2), big.NewInt(256), big.NewInt(0))

go func(quitCh chan struct{}) {
for {
time.Sleep(1 * time.Minute)

// Verify that none of the values have mutated.
if Big0 != nil || big0.Cmp(Big0) != 0 ||
Big1 == nil || big1.Cmp(Big1) != 0 ||
Big2 == nil || big2.Cmp(Big2) != 0 ||
Big3 == nil || big3.Cmp(Big3) != 0 ||
Big8 == nil || big8.Cmp(Big8) != 0 ||
Big10 == nil || big10.Cmp(Big10) != 0 ||
Big32 == nil || big32.Cmp(Big32) != 0 ||
Big99 == nil || big99.Cmp(Big99) != 0 ||
Big100 == nil || big100.Cmp(Big100) != 0 ||
Big101 == nil || big101.Cmp(Big101) != 0 ||
Big256 == nil || big256.Cmp(Big256) != 0 ||
Big257 == nil || big257.Cmp(Big257) != 0 ||
Big2e64 == nil || big2e64.Cmp(Big2e64) != 0 ||
Big2e256 == nil || big2e256.Cmp(Big2e256) != 0 {
// Send a message to quitCh to abort.
log.Global.Error("A common value has mutated, exiting now")
quitCh <- struct{}{}
}
}
}(quitCh)
}
22 changes: 3 additions & 19 deletions consensus/blake3pow/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,9 @@ import (
var (
allowedFutureBlockTimeSeconds = int64(15) // Max seconds from current time allowed for blocks, before they're considered future blocks

ContextTimeFactor = big10
ZoneBlockReward = big.NewInt(5e+18)
RegionBlockReward = new(big.Int).Mul(ZoneBlockReward, big3)
PrimeBlockReward = new(big.Int).Mul(RegionBlockReward, big3)
)

// Some useful constants to avoid constant memory allocs for them.
var (
expDiffPeriod = big.NewInt(100000)
big0 = big.NewInt(0)
big1 = big.NewInt(1)
big2 = big.NewInt(2)
big3 = big.NewInt(3)
big8 = big.NewInt(8)
big9 = big.NewInt(9)
big10 = big.NewInt(10)
big32 = big.NewInt(32)
bigMinus99 = big.NewInt(-99)
big2e256 = new(big.Int).Exp(big.NewInt(2), big.NewInt(256), big.NewInt(0)) // 2^256
RegionBlockReward = new(big.Int).Mul(ZoneBlockReward, common.Big3)
PrimeBlockReward = new(big.Int).Mul(RegionBlockReward, common.Big3)
)

// Author implements consensus.Engine, returning the header's coinbase as the
Expand Down Expand Up @@ -592,7 +576,7 @@ func (blake3pow *Blake3pow) verifySeal(header *types.WorkObjectHeader) error {
return consensus.ErrInvalidDifficulty
}

target := new(big.Int).Div(big2e256, header.Difficulty())
target := new(big.Int).Div(common.Big2e256, header.Difficulty())
if new(big.Int).SetBytes(header.Hash().Bytes()).Cmp(target) > 0 {
return consensus.ErrInvalidPoW
}
Expand Down
8 changes: 4 additions & 4 deletions consensus/blake3pow/poem.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ func (blake3pow *Blake3pow) CalcOrder(chain consensus.BlockReader, header *types
}
nodeCtx := blake3pow.config.NodeLocation.Context()
if header.NumberU64(nodeCtx) == 0 {
return big0, common.PRIME_CTX, nil
return big.NewInt(0), common.PRIME_CTX, nil
}

expansionNum := header.ExpansionNumber()

// Verify the seal and get the powHash for the given header
err := blake3pow.verifySeal(header.WorkObjectHeader())
if err != nil {
return big0, -1, err
return big.NewInt(0), -1, err
}

// Get entropy reduction of this header
Expand Down Expand Up @@ -59,7 +59,7 @@ func (blake3pow *Blake3pow) CalcOrder(chain consensus.BlockReader, header *types
totalDeltaEntropyRegion := new(big.Int).Add(header.ParentDeltaEntropy(common.ZONE_CTX), intrinsicEntropy)

regionDeltaEntropyTarget := new(big.Int).Mul(zoneThresholdEntropy, params.RegionEntropyTarget(expansionNum))
regionDeltaEntropyTarget = new(big.Int).Div(regionDeltaEntropyTarget, big2)
regionDeltaEntropyTarget = new(big.Int).Div(regionDeltaEntropyTarget, common.Big2)

regionBlockEntropyThreshold := new(big.Int).Add(zoneThresholdEntropy, common.BitsToBigBits(params.RegionEntropyTarget(expansionNum)))
if intrinsicEntropy.Cmp(regionBlockEntropyThreshold) > 0 && totalDeltaEntropyRegion.Cmp(regionDeltaEntropyTarget) > 0 {
Expand All @@ -75,7 +75,7 @@ func (blake3pow *Blake3pow) CalcOrder(chain consensus.BlockReader, header *types
// IntrinsicLogEntropy returns the logarithm of the intrinsic entropy reduction of a PoW hash
func (blake3pow *Blake3pow) IntrinsicLogEntropy(powHash common.Hash) *big.Int {
x := new(big.Int).SetBytes(powHash.Bytes())
d := new(big.Int).Div(big2e256, x)
d := new(big.Int).Div(common.Big2e256, x)
c, m := mathutil.BinaryLog(d, consensus.MantBits)
bigBits := new(big.Int).Mul(big.NewInt(int64(c)), new(big.Int).Exp(big.NewInt(2), big.NewInt(consensus.MantBits), nil))
bigBits = new(big.Int).Add(bigBits, m)
Expand Down
21 changes: 3 additions & 18 deletions consensus/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import (
"github.com/dominant-strategies/go-quai/common"
"github.com/dominant-strategies/go-quai/core/state"
"github.com/dominant-strategies/go-quai/core/types"
"github.com/dominant-strategies/go-quai/ethdb"
"github.com/dominant-strategies/go-quai/crypto/multiset"
"github.com/dominant-strategies/go-quai/ethdb"
"github.com/dominant-strategies/go-quai/params"
)

Expand All @@ -35,21 +35,6 @@ const (
MantBits = 64
)

// Some useful constants to avoid constant memory allocs for them.
var (
ExpDiffPeriod = big.NewInt(100000)
Big0 = big.NewInt(0)
Big1 = big.NewInt(1)
Big2 = big.NewInt(2)
Big3 = big.NewInt(3)
Big8 = big.NewInt(8)
Big9 = big.NewInt(9)
Big10 = big.NewInt(10)
Big32 = big.NewInt(32)
BigMinus99 = big.NewInt(-99)
Big2e256 = new(big.Int).Exp(big.NewInt(2), big.NewInt(256), big.NewInt(0)) // 2^256
)

// Various error messages to mark blocks invalid. These should be private to
// prevent engine specific errors from being referenced in the remainder of the
// codebase, inherently breaking if the engine is swapped out. Please put common
Expand Down Expand Up @@ -259,8 +244,8 @@ func CalcWorkShareThreshold(workShare *types.WorkObjectHeader, workShareThreshol
return nil, ErrInvalidThresholdDiff
}
diff := workShare.Difficulty()
diffTarget := new(big.Int).Div(Big2e256, diff)
workShareTarget := new(big.Int).Exp(Big2, big.NewInt(int64(workShareThresholdDiff)), nil)
diffTarget := new(big.Int).Div(common.Big2e256, diff)
workShareTarget := new(big.Int).Exp(common.Big2, big.NewInt(int64(workShareThresholdDiff)), nil)

return workShareTarget.Mul(diffTarget, workShareTarget), nil
}
Expand Down
22 changes: 3 additions & 19 deletions consensus/progpow/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,9 @@ import (
var (
allowedFutureBlockTimeSeconds = int64(15) // Max seconds from current time allowed for blocks, before they're considered future blocks

ContextTimeFactor = big10
ZoneBlockReward = big.NewInt(5e+18)
RegionBlockReward = new(big.Int).Mul(ZoneBlockReward, big3)
PrimeBlockReward = new(big.Int).Mul(RegionBlockReward, big3)
)

// Some useful constants to avoid constant memory allocs for them.
var (
expDiffPeriod = big.NewInt(100000)
big0 = big.NewInt(0)
big1 = big.NewInt(1)
big2 = big.NewInt(2)
big3 = big.NewInt(3)
big8 = big.NewInt(8)
big9 = big.NewInt(9)
big10 = big.NewInt(10)
big32 = big.NewInt(32)
bigMinus99 = big.NewInt(-99)
big2e256 = new(big.Int).Exp(big.NewInt(2), big.NewInt(256), big.NewInt(0)) // 2^256
RegionBlockReward = new(big.Int).Mul(ZoneBlockReward, common.Big3)
PrimeBlockReward = new(big.Int).Mul(RegionBlockReward, common.Big3)
)

// Author implements consensus.Engine, returning the header's coinbase as the
Expand Down Expand Up @@ -636,7 +620,7 @@ func (progpow *Progpow) verifySeal(header *types.WorkObjectHeader) (common.Hash,
if err != nil {
return common.Hash{}, err
}
target := new(big.Int).Div(big2e256, header.Difficulty())
target := new(big.Int).Div(common.Big2e256, header.Difficulty())
if new(big.Int).SetBytes(powHash.Bytes()).Cmp(target) > 0 {
return powHash, consensus.ErrInvalidPoW
}
Expand Down
10 changes: 5 additions & 5 deletions consensus/progpow/poem.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ func (progpow *Progpow) CalcOrder(chain consensus.BlockReader, header *types.Wor
nodeCtx := progpow.config.NodeLocation.Context()
// Except for the slice [0,0] have to check if the header hash is the genesis hash
if header.NumberU64(nodeCtx) == 0 {
return big0, common.PRIME_CTX, nil
return big.NewInt(0), common.PRIME_CTX, nil
}
expansionNum := header.ExpansionNumber()

// Verify the seal and get the powHash for the given header
powHash, err := progpow.verifySeal(header.WorkObjectHeader())
if err != nil {
return big0, -1, err
return big.NewInt(0), -1, err
}

// Get entropy reduction of this header
Expand All @@ -45,7 +45,7 @@ func (progpow *Progpow) CalcOrder(chain consensus.BlockReader, header *types.Wor
totalDeltaEntropyPrime = new(big.Int).Add(totalDeltaEntropyPrime, intrinsicEntropy)

primeDeltaEntropyTarget := new(big.Int).Mul(params.PrimeEntropyTarget(expansionNum), zoneThresholdEntropy)
primeDeltaEntropyTarget = new(big.Int).Div(primeDeltaEntropyTarget, big2)
primeDeltaEntropyTarget = new(big.Int).Div(primeDeltaEntropyTarget, common.Big2)

primeBlockEntropyThreshold := new(big.Int).Add(zoneThresholdEntropy, common.BitsToBigBits(params.PrimeEntropyTarget(expansionNum)))
if intrinsicEntropy.Cmp(primeBlockEntropyThreshold) > 0 && totalDeltaEntropyPrime.Cmp(primeDeltaEntropyTarget) > 0 {
Expand All @@ -58,7 +58,7 @@ func (progpow *Progpow) CalcOrder(chain consensus.BlockReader, header *types.Wor
totalDeltaSRegion := new(big.Int).Add(header.ParentDeltaEntropy(common.ZONE_CTX), intrinsicEntropy)

regionDeltaSTarget := new(big.Int).Mul(zoneThresholdEntropy, params.RegionEntropyTarget(expansionNum))
regionDeltaSTarget = new(big.Int).Div(regionDeltaSTarget, big2)
regionDeltaSTarget = new(big.Int).Div(regionDeltaSTarget, common.Big2)

regionBlockEntropyThreshold := new(big.Int).Add(zoneThresholdEntropy, common.BitsToBigBits(params.RegionEntropyTarget(expansionNum)))
if intrinsicEntropy.Cmp(regionBlockEntropyThreshold) > 0 && totalDeltaSRegion.Cmp(regionDeltaSTarget) > 0 {
Expand All @@ -74,7 +74,7 @@ func (progpow *Progpow) CalcOrder(chain consensus.BlockReader, header *types.Wor
// IntrinsicLogEntropy returns the logarithm of the intrinsic entropy reduction of a PoW hash
func (progpow *Progpow) IntrinsicLogEntropy(powHash common.Hash) *big.Int {
x := new(big.Int).SetBytes(powHash.Bytes())
d := new(big.Int).Div(big2e256, x)
d := new(big.Int).Div(common.Big2e256, x)
c, m := mathutil.BinaryLog(d, consensus.MantBits)
bigBits := new(big.Int).Mul(big.NewInt(int64(c)), new(big.Int).Exp(big.NewInt(2), big.NewInt(consensus.MantBits), nil))
bigBits = new(big.Int).Add(bigBits, m)
Expand Down
4 changes: 4 additions & 0 deletions core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -1365,6 +1365,10 @@ func (c *Core) SendTxToSharingClients(tx *types.Transaction) {
c.sl.txPool.SendTxToSharingClients(tx)
}

func (c *Core) GetRollingFeeInfo() (min, max, avg *big.Int) {
return c.Processor().GetRollingFeeInfo()
}

func (c *Core) SuggestFinalityDepth(qiValue *big.Int, correlatedRisk *big.Int) *big.Int {
qiRewardPerBlock := misc.CalculateQiReward(c.CurrentHeader().WorkObjectHeader())

Expand Down
Loading
Loading