mirror of
https://github.com/kaspanet/kaspad.git
synced 2026-02-21 19:22:53 +00:00
Compare commits
4 Commits
github-dep
...
nod-1429-w
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5d03d72bd3 | ||
|
|
a6fd979d85 | ||
|
|
7ae0229f2d | ||
|
|
6cd3e45664 |
@@ -1,77 +0,0 @@
|
||||
// Copyright (c) 2015-2017 The btcsuite developers
|
||||
// Use of this source code is governed by an ISC
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package blocklogger
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
|
||||
"github.com/kaspanet/kaspad/util/mstime"
|
||||
)
|
||||
|
||||
// BlockLogger is a type tracking the amount of blocks/headers/transactions to log the time it took to receive them
|
||||
type BlockLogger struct {
|
||||
receivedLogBlocks int64
|
||||
receivedLogHeaders int64
|
||||
receivedLogTransactions int64
|
||||
lastBlockLogTime time.Time
|
||||
}
|
||||
|
||||
// NewBlockLogger creates a new instance with zeroed blocks/headers/transactions/time counters.
|
||||
func NewBlockLogger() *BlockLogger {
|
||||
return &BlockLogger{
|
||||
receivedLogBlocks: 0,
|
||||
receivedLogHeaders: 0,
|
||||
receivedLogTransactions: 0,
|
||||
lastBlockLogTime: time.Now(),
|
||||
}
|
||||
}
|
||||
|
||||
// LogBlock logs a new block blue score as an information message
|
||||
// to show progress to the user. In order to prevent spam, it limits logging to
|
||||
// one message every 10 seconds with duration and totals included.
|
||||
func (bl *BlockLogger) LogBlock(block *externalapi.DomainBlock) {
|
||||
if len(block.Transactions) == 0 {
|
||||
bl.receivedLogHeaders++
|
||||
} else {
|
||||
bl.receivedLogBlocks++
|
||||
}
|
||||
|
||||
bl.receivedLogTransactions += int64(len(block.Transactions))
|
||||
|
||||
now := time.Now()
|
||||
duration := now.Sub(bl.lastBlockLogTime)
|
||||
if duration < time.Second*10 {
|
||||
return
|
||||
}
|
||||
|
||||
// Truncate the duration to 10s of milliseconds.
|
||||
truncatedDuration := duration.Round(10 * time.Millisecond)
|
||||
|
||||
// Log information about new block blue score.
|
||||
blockStr := "blocks"
|
||||
if bl.receivedLogBlocks == 1 {
|
||||
blockStr = "block"
|
||||
}
|
||||
|
||||
txStr := "transactions"
|
||||
if bl.receivedLogTransactions == 1 {
|
||||
txStr = "transaction"
|
||||
}
|
||||
|
||||
headerStr := "headers"
|
||||
if bl.receivedLogBlocks == 1 {
|
||||
headerStr = "header"
|
||||
}
|
||||
|
||||
log.Infof("Processed %d %s and %d %s in the last %s (%d %s, %s)",
|
||||
bl.receivedLogBlocks, blockStr, bl.receivedLogHeaders, headerStr, truncatedDuration, bl.receivedLogTransactions,
|
||||
txStr, mstime.UnixMilliseconds(block.Header.TimeInMilliseconds()))
|
||||
|
||||
bl.receivedLogBlocks = 0
|
||||
bl.receivedLogHeaders = 0
|
||||
bl.receivedLogTransactions = 0
|
||||
bl.lastBlockLogTime = now
|
||||
}
|
||||
326
domain/miningmanager/miningmanager_test.go
Normal file
326
domain/miningmanager/miningmanager_test.go
Normal file
@@ -0,0 +1,326 @@
|
||||
package miningmanager_test
|
||||
|
||||
import (
|
||||
//"bytes"
|
||||
//"github.com/kaspanet/kaspad/app/appmessage"
|
||||
"github.com/kaspanet/kaspad/domain"
|
||||
"github.com/kaspanet/kaspad/domain/consensus"
|
||||
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
|
||||
"github.com/kaspanet/kaspad/domain/consensus/utils/consensushashing"
|
||||
|
||||
//"github.com/kaspanet/kaspad/domain/consensus/ruleerrors"
|
||||
//"github.com/kaspanet/kaspad/domain/consensus/utils/coinbase"
|
||||
//"github.com/kaspanet/kaspad/domain/consensus/utils/consensushashing"
|
||||
//"github.com/kaspanet/kaspad/domain/consensus/utils/consensusserialization"
|
||||
"github.com/kaspanet/kaspad/domain/consensus/utils/constants"
|
||||
//"github.com/kaspanet/kaspad/domain/consensus/utils/hashes"
|
||||
"github.com/kaspanet/kaspad/domain/consensus/utils/subnetworks"
|
||||
"github.com/kaspanet/kaspad/domain/consensus/utils/testutils"
|
||||
"github.com/kaspanet/kaspad/domain/consensus/utils/txscript"
|
||||
"github.com/kaspanet/kaspad/domain/dagconfig"
|
||||
//"github.com/kaspanet/kaspad/domain/miningmanager"
|
||||
//infrastructuredatabase "github.com/kaspanet/kaspad/infrastructure/db/database"
|
||||
//"github.com/kaspanet/kaspad/infrastructure/db/database/ldb"
|
||||
"github.com/kaspanet/kaspad/util"
|
||||
//"github.com/pkg/errors"
|
||||
//"github.com/syndtr/goleveldb/leveldb/opt"
|
||||
//"io/ioutil"
|
||||
//"os"
|
||||
//"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
//func setupDBForTest(dbName string) (infrastructuredatabase.Database, func(), error) {
|
||||
// var err error
|
||||
// tmpDir, err := ioutil.TempDir("", "setupDBManager")
|
||||
// if err != nil {
|
||||
// return nil, nil, errors.Errorf("error creating temp dir: %s", err)
|
||||
// }
|
||||
//
|
||||
// dbPath := filepath.Join(tmpDir, dbName)
|
||||
// _ = os.RemoveAll(dbPath)
|
||||
// db, err := ldb.NewLevelDB(dbPath)
|
||||
// if err != nil {
|
||||
// return nil, nil, err
|
||||
// }
|
||||
//
|
||||
// originalLDBOptions := ldb.Options
|
||||
// ldb.Options = func() *opt.Options {
|
||||
// return nil
|
||||
// }
|
||||
//
|
||||
// teardown := func() {
|
||||
// db.Close()
|
||||
// ldb.Options = originalLDBOptions
|
||||
// os.RemoveAll(dbPath)
|
||||
// }
|
||||
//
|
||||
// return db, teardown, err
|
||||
//}
|
||||
|
||||
func createCoinbaseTransaction(t *testing.T, scriptPublicKey []byte, value uint64) *externalapi.DomainTransaction {
|
||||
dummyTxOut := externalapi.DomainTransactionOutput{
|
||||
Value: value,
|
||||
ScriptPublicKey: nil,
|
||||
}
|
||||
|
||||
transaction := &externalapi.DomainTransaction{
|
||||
Version: constants.MaxTransactionVersion,
|
||||
Inputs: []*externalapi.DomainTransactionInput{},
|
||||
Outputs: []*externalapi.DomainTransactionOutput{&dummyTxOut},
|
||||
LockTime: 0,
|
||||
SubnetworkID: subnetworks.SubnetworkIDCoinbase,
|
||||
}
|
||||
|
||||
return transaction
|
||||
}
|
||||
|
||||
//
|
||||
//func createTransaction(inputs []*externalapi.DomainTransactionInput, scriptPublicKey []byte, value uint64) *externalapi.DomainTransaction {
|
||||
// dummyTxOut := externalapi.DomainTransactionOutput{
|
||||
// Value: value,
|
||||
// ScriptPublicKey: scriptPublicKey,
|
||||
// }
|
||||
//
|
||||
// transaction := &externalapi.DomainTransaction{
|
||||
// Version: constants.MaxTransactionVersion,
|
||||
// Inputs: inputs,
|
||||
// Outputs: []*externalapi.DomainTransactionOutput{&dummyTxOut},
|
||||
// LockTime: 0,
|
||||
// SubnetworkID: subnetworks.SubnetworkIDNative,
|
||||
// Gas: 0,
|
||||
// }
|
||||
//
|
||||
// return transaction
|
||||
//}
|
||||
func TestMiningManager(t *testing.T) {
|
||||
testutils.ForAllNets(t, true, func(t *testing.T, params *dagconfig.Params) {
|
||||
|
||||
factory := consensus.NewFactory()
|
||||
tc, teardown, err := factory.NewTestConsensus(params, false, "TestBlockSize")
|
||||
if err != nil {
|
||||
t.Fatalf("Error setting up tc: %+v", err)
|
||||
}
|
||||
defer teardown(false)
|
||||
tc.Database()
|
||||
|
||||
miningAddrHash := [20]byte{0x99}
|
||||
miningAddr, err := util.NewAddressPubKeyHash(miningAddrHash[:], util.Bech32PrefixKaspaTest)
|
||||
if err != nil {
|
||||
t.Fatalf("NewAddressPubKeyHash: unexpected error: %v", err)
|
||||
}
|
||||
scriptPublicKey, err := txscript.PayToAddrScript(miningAddr)
|
||||
isArchivalNode := false
|
||||
domain, err := domain.New(params, tc.Database(), isArchivalNode)
|
||||
if err != nil {
|
||||
t.Fatalf("err")
|
||||
}
|
||||
//consensusInstance, err := consensusFactory.NewConsensus(dagParams, db)
|
||||
//if err != nil {
|
||||
// t.Fatalf("NewConsensus: %v", err)
|
||||
//}
|
||||
|
||||
// Insert 10 transactions
|
||||
miningManager := domain.MiningManager()
|
||||
//miningManager := miningManagerFactory.NewMiningManager(consensusInstance, constants.MaxMassAcceptedByBlock)
|
||||
transactions := make([]*externalapi.DomainTransaction, 10)
|
||||
for i := range transactions {
|
||||
transaction := createCoinbaseTransaction(t, scriptPublicKey.Script, uint64(100000000+i))
|
||||
transactions[i] = transaction
|
||||
err = miningManager.ValidateAndInsertTransaction(transaction, true)
|
||||
if err != nil {
|
||||
t.Fatalf("ValidateAndInsertTransaction: unexpected error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Spending 10 transactions
|
||||
miningManager.HandleNewBlockTransactions(transactions)
|
||||
block, err := miningManager.GetBlockTemplate(&externalapi.DomainCoinbaseData{
|
||||
ScriptPublicKey: scriptPublicKey,
|
||||
})
|
||||
if err != nil {
|
||||
// todo
|
||||
}
|
||||
if block == nil {
|
||||
t.Fatalf("GetBlockTemplate: failed building block")
|
||||
}
|
||||
|
||||
// Check 10 transactions are not exist
|
||||
for _, tx2 := range transactions {
|
||||
for _, tx1 := range block.Transactions {
|
||||
if consensushashing.TransactionID(tx1) == consensushashing.TransactionID(tx2) {
|
||||
t.Fatalf("Spent tranasaction is still exisit")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
//
|
||||
//func TestMiningManager(t *testing.T) {
|
||||
// dagParams := &dagconfig.SimnetParams
|
||||
// consensusFactory := consensus.NewFactory()
|
||||
// miningManagerFactory := miningmanager.NewFactory()
|
||||
// db, teardownFunc, err := setupDBForTest(t.Name())
|
||||
// if err != nil {
|
||||
// t.Fatalf("Failed to setup db: %v", err)
|
||||
// }
|
||||
// defer teardownFunc()
|
||||
//
|
||||
// miningAddrHash := [20]byte{0x99}
|
||||
// miningAddr, err := util.NewAddressPubKeyHash(miningAddrHash[:], util.Bech32PrefixKaspaTest)
|
||||
// if err != nil {
|
||||
// t.Fatalf("NewAddressPubKeyHash: unexpected error: %v", err)
|
||||
// }
|
||||
// scriptPublicKey, err := txscript.PayToAddrScript(miningAddr)
|
||||
//
|
||||
// t.Run("Spending all transactions", func(t *testing.T) {
|
||||
// consensusInstance, err := consensusFactory.NewConsensus(dagParams, db)
|
||||
// if err != nil {
|
||||
// t.Fatalf("NewConsensus: %v", err)
|
||||
// }
|
||||
//
|
||||
// // Insert 10 transactions
|
||||
// miningManager := miningManagerFactory.NewMiningManager(consensusInstance, constants.MaxMassAcceptedByBlock)
|
||||
// transactions := make([]*externalapi.DomainTransaction, 10)
|
||||
// for i := range transactions {
|
||||
// transaction := createCoinbaseTransaction(t, scriptPublicKey, uint64(100000000+i))
|
||||
// transactions[i] = transaction
|
||||
// err = miningManager.ValidateAndInsertTransaction(transaction, true)
|
||||
// if err != nil {
|
||||
// t.Fatalf("ValidateAndInsertTransaction: unexpected error: %v", err)
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// // Spending 10 transactions
|
||||
// miningManager.HandleNewBlockTransactions(transactions)
|
||||
// block := miningManager.GetBlockTemplate(&externalapi.DomainCoinbaseData{
|
||||
// ScriptPublicKey: scriptPublicKey,
|
||||
// })
|
||||
// if block == nil {
|
||||
// t.Fatalf("GetBlockTemplate: failed building block")
|
||||
// }
|
||||
//
|
||||
// // Check 10 transactions are not exist
|
||||
// for _, tx2 := range transactions {
|
||||
// for _, tx1 := range block.Transactions {
|
||||
// if consensusserialization.TransactionID(tx1) == consensusserialization.TransactionID(tx2) {
|
||||
// t.Fatalf("Spent tranasaction is still exisit")
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// })
|
||||
//
|
||||
// t.Run("Spending some transactions", func(t *testing.T) {
|
||||
// consensusInstance, err := consensusFactory.NewConsensus(dagParams, db)
|
||||
// if err != nil {
|
||||
// t.Fatalf("NewConsensus: %v", err)
|
||||
// }
|
||||
//
|
||||
// // Insert 10 transactions
|
||||
// miningManager := miningManagerFactory.NewMiningManager(consensusInstance, constants.MaxMassAcceptedByBlock)
|
||||
// transactions := make([]*externalapi.DomainTransaction, 10)
|
||||
// for i := range transactions {
|
||||
// transaction := createCoinbaseTransaction(t, scriptPublicKey, uint64(100000000+i))
|
||||
// transactions[i] = transaction
|
||||
// err = miningManager.ValidateAndInsertTransaction(transaction, true)
|
||||
// if err != nil {
|
||||
// t.Fatalf("ValidateAndInsertTransaction: unexpected error: %v", err)
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// // Spending first 5 transactions
|
||||
// miningManager.HandleNewBlockTransactions(transactions[0:5])
|
||||
// block := miningManager.GetBlockTemplate(&externalapi.DomainCoinbaseData{
|
||||
// ScriptPublicKey: scriptPublicKey,
|
||||
// })
|
||||
// if block == nil {
|
||||
// t.Fatalf("GetBlockTemplate: failed building block")
|
||||
// }
|
||||
//
|
||||
// // Check first 5 transactions are not exist
|
||||
// for _, tx2 := range transactions[0:5] {
|
||||
// for _, tx1 := range block.Transactions {
|
||||
// if consensusserialization.TransactionID(tx1) == consensusserialization.TransactionID(tx2) {
|
||||
// t.Fatalf("Spent tranasaction is still exisit")
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// })
|
||||
//
|
||||
// t.Run("Spending transactions with unknown parents", func(t *testing.T) {
|
||||
// consensusInstance, err := consensusFactory.NewConsensus(dagParams, db)
|
||||
// if err != nil {
|
||||
// t.Fatalf("NewConsensus: %v", err)
|
||||
// }
|
||||
//
|
||||
// miningManager := miningManagerFactory.NewMiningManager(consensusInstance, constants.MaxMassAcceptedByBlock)
|
||||
// transactions := make([]*externalapi.DomainTransaction, 10)
|
||||
// parentTransactions := make([]*externalapi.DomainTransaction, len(transactions))
|
||||
// for i := range parentTransactions {
|
||||
// parentTransaction := createCoinbaseTransaction(t, scriptPublicKey, uint64(100000000+i))
|
||||
// parentTransactions[i] = parentTransaction
|
||||
// }
|
||||
//
|
||||
// // Insert transactions with unknown parents
|
||||
// for i := range transactions {
|
||||
// parentTransaction := parentTransactions[i]
|
||||
// txIn := externalapi.DomainTransactionInput{
|
||||
// PreviousOutpoint: externalapi.DomainOutpoint{TransactionID: *consensusserialization.TransactionID(parentTransaction), Index: 1},
|
||||
// SignatureScript: bytes.Repeat([]byte{0x00}, 65),
|
||||
// Sequence: appmessage.MaxTxInSequenceNum,
|
||||
// }
|
||||
// transaction := createTransaction([]*externalapi.DomainTransactionInput{&txIn}, scriptPublicKey, uint64(10+i))
|
||||
// transactions[i] = transaction
|
||||
// err = miningManager.ValidateAndInsertTransaction(transaction, true)
|
||||
// if err != nil {
|
||||
// t.Fatalf("ValidateAndInsertTransaction: unexpected error: %v", err)
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// // Check transactions with unknown parents
|
||||
// block := miningManager.GetBlockTemplate(&externalapi.DomainCoinbaseData{
|
||||
// ScriptPublicKey: scriptPublicKey,
|
||||
// })
|
||||
// if block == nil {
|
||||
// t.Fatalf("GetBlockTemplate: failed building block")
|
||||
// }
|
||||
//
|
||||
// for _, tx1 := range transactions {
|
||||
// for _, tx2 := range block.Transactions {
|
||||
// if consensusserialization.TransactionID(tx1) == consensusserialization.TransactionID(tx2) {
|
||||
// t.Fatalf("Tranasaction with unknown parents is exisit")
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// // Add the missing parents
|
||||
// for _, parentTransaction := range parentTransactions {
|
||||
// err = miningManager.ValidateAndInsertTransaction(parentTransaction, true)
|
||||
// if err != nil {
|
||||
// t.Fatalf("ValidateAndInsertTransaction: unexpected error: %v", err)
|
||||
// }
|
||||
// }
|
||||
// block = miningManager.GetBlockTemplate(&externalapi.DomainCoinbaseData{
|
||||
// ScriptPublicKey: scriptPublicKey,
|
||||
// })
|
||||
// if block == nil {
|
||||
// t.Fatalf("GetBlockTemplate: failed building block")
|
||||
// }
|
||||
//
|
||||
// numberOfFoundTransactions := 0
|
||||
// for _, tx1 := range transactions {
|
||||
// for _, tx2 := range block.Transactions {
|
||||
// if consensusserialization.TransactionID(tx1) == consensusserialization.TransactionID(tx2) {
|
||||
// numberOfFoundTransactions++
|
||||
// break
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// if len(transactions) != numberOfFoundTransactions{
|
||||
// t.Fatalf("Not all transactions are exist, expected %v, but got %v", len(transactions), numberOfFoundTransactions)
|
||||
// }
|
||||
// })
|
||||
//}
|
||||
Reference in New Issue
Block a user