Skip to content

Commit

Permalink
define blocksize in a data size (byte) way.
Browse files Browse the repository at this point in the history
  • Loading branch information
iiinotlll committed Oct 29, 2024
1 parent 7449686 commit 04facf6
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 10 deletions.
8 changes: 7 additions & 1 deletion chain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,14 @@ func (bc *BlockChain) GetUpdateStatusTrie(txs []*core.Transaction) common.Hash {

// generate (mine) a block, this function return a block
func (bc *BlockChain) GenerateBlock(miner int32) *core.Block {
var txs []*core.Transaction
// pack the transactions from the txpool
txs := bc.Txpool.PackTxs(bc.ChainConfig.BlockSize)
if params.UseBlocksizeInBytes == 1 {
txs = bc.Txpool.PackTxsWithBytes(params.BlocksizeInBytes)
} else {
txs = bc.Txpool.PackTxs(bc.ChainConfig.BlockSize)
}

bh := &core.BlockHeader{
ParentBlockHash: bc.CurrentBlock.Hash,
Number: bc.CurrentBlock.Header.Number + 1,
Expand Down
10 changes: 10 additions & 0 deletions complie_run_IpAddr=127_0_0_1.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
start cmd /k go run main.go -n 0 -N 4 -s 0 -S 1

start cmd /k go run main.go -n 1 -N 4 -s 0 -S 1

start cmd /k go run main.go -n 2 -N 4 -s 0 -S 1

start cmd /k go run main.go -n 3 -N 4 -s 0 -S 1

start cmd /k go run main.go -c -N 4 -S 1

21 changes: 21 additions & 0 deletions core/txpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"blockEmulator/utils"
"sync"
"time"
"unsafe"
)

type TxPool struct {
Expand Down Expand Up @@ -64,6 +65,26 @@ func (txpool *TxPool) PackTxs(max_txs uint64) []*Transaction {
return txs_Packed
}

// Pack transaction for a proposal (use 'BlocksizeInBytes' to control)
func (txpool *TxPool) PackTxsWithBytes(max_bytes int) []*Transaction {
txpool.lock.Lock()
defer txpool.lock.Unlock()

txNum := len(txpool.TxQueue)
currentSize := 0
for tx_idx, tx := range txpool.TxQueue {
currentSize += int(unsafe.Sizeof(*tx))
if currentSize > max_bytes {
txNum = tx_idx
break
}
}

txs_Packed := txpool.TxQueue[:txNum]
txpool.TxQueue = txpool.TxQueue[txNum:]
return txs_Packed
}

// Relay transactions
func (txpool *TxPool) AddRelayTx(tx *Transaction, shardID uint64) {
txpool.lock.Lock()
Expand Down
9 changes: 9 additions & 0 deletions docs/versionUpdate.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Version Updates

## 2024/10/29
1. **Debug**: We found that the shardNum in `main.go` cannot affect the `params.ShardNum`. This bug originated from the updates made on 2024/09/01 and led to incorrect CLPA results. **The bug has now been fixed.**
2. **Debug**: When setting the shardNum as 1 and using the CLPA consensus, the supervisor node will enter a dead loop. **Now the CLPA parition algorithm will not be triggered when `shardNum` is 1.**
3. **A new method to control blocksize**: Now users can use datasize (in bytes) to control the size of a block, instead of the number of transactions. In `paramsConfig.json`, users can set `UseBlocksizeInBytes` as `1` to enable this method and specify `BlocksizeInBytes` to limit the size of block.

(Acknowledgement: https://github.com/HuangLab-SYSU/block-emulator/issues/40, https://github.com/HuangLab-SYSU/block-emulator/issues/42)



## 2024/09/01
1. **An IP Table**: A file named ./ipTable.json is provided for IP configuration. Users may modify the IP addresses of the nodes as needed. Please note that the first-level key in this JSON file is "ShardID", and the second-level key is "NodeID".
2. **Params Configuration File**: Users can define various parameters in the ./paramsConfig.json file, including ConsensusMethod, ExpDataRootDir, DatasetFile, and others.
Expand Down
29 changes: 20 additions & 9 deletions params/global_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,15 @@ var (

PbftViewChangeTimeOut = 10000 // The view change threshold of pbft. If the process of PBFT is too slow, the view change mechanism will be triggered.

Block_Interval = 5000 // The time interval for generating a new block
MaxBlockSize_global = 2000 // The maximum number of transactions a block contains
InjectSpeed = 2000 // The speed of transaction injection
TotalDataSize = 160000 // The total number of txs to be injected
TxBatchSize = 16000 // The supervisor read a batch of txs then send them. The size of a batch is 'TxBatchSize'
Block_Interval = 5000 // The time interval for generating a new block

MaxBlockSize_global = 2000 // The maximum number of transactions a block contains
BlocksizeInBytes = 20000 // The maximum size (in bytes) of block body
UseBlocksizeInBytes = 0 // Use blocksizeInBytes as the blocksize measurement if '1'.

InjectSpeed = 2000 // The speed of transaction injection
TotalDataSize = 160000 // The total number of txs to be injected
TxBatchSize = 16000 // The supervisor read a batch of txs then send them. The size of a batch is 'TxBatchSize'

BrokerNum = 10 // The # of Broker accounts used in Broker / CLPA_Broker.
RelayWithMerkleProof = 0 // When using a consensus about "Relay", nodes will send Tx Relay with proof if "RelayWithMerkleProof" = 1
Expand Down Expand Up @@ -55,10 +59,14 @@ type globalConfig struct {

ExpDataRootDir string `json:"ExpDataRootDir"`

BlockInterval int `json:"Block_Interval"`
MaxBlockSizeGlobal int `json:"BlockSize"`
InjectSpeed int `json:"InjectSpeed"`
TotalDataSize int `json:"TotalDataSize"`
BlockInterval int `json:"Block_Interval"`

BlocksizeInBytes int `json:"BlocksizeInBytes"`
MaxBlockSizeGlobal int `json:"BlockSize"`
UseBlocksizeInBytes int `json:"UseBlocksizeInBytes"`

InjectSpeed int `json:"InjectSpeed"`
TotalDataSize int `json:"TotalDataSize"`

TxBatchSize int `json:"TxBatchSize"`
BrokerNum int `json:"BrokerNum"`
Expand Down Expand Up @@ -99,7 +107,10 @@ func ReadConfigFile() {
DatabaseWrite_path = ExpDataRootDir + "/database/"

Block_Interval = config.BlockInterval

MaxBlockSize_global = config.MaxBlockSizeGlobal
BlocksizeInBytes = config.BlocksizeInBytes
UseBlocksizeInBytes = config.UseBlocksizeInBytes

InjectSpeed = config.InjectSpeed
TotalDataSize = config.TotalDataSize
Expand Down
4 changes: 4 additions & 0 deletions paramsConfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
"ExpDataRootDir": "expTest",

"Block_Interval": 5000,

"BlockSize": 2000,
"BlocksizeInBytes": 200000,
"UseBlocksizeInBytes": 0,

"InjectSpeed": 2000,
"TotalDataSize": 160000,
"TxBatchSize": 16000,
Expand Down

0 comments on commit 04facf6

Please sign in to comment.