minor refine of txgen

pull/46/head
Richard Liu 6 years ago
parent 5958362d37
commit b21859108c
  1. 144
      client/txgen/main.go

@ -19,7 +19,16 @@ import (
"time" "time"
) )
var utxoPoolMutex sync.Mutex type txGenSettings struct {
numOfAddress int
crossShard bool
maxNumTxsPerBatch int
}
var (
utxoPoolMutex sync.Mutex
setting txGenSettings
)
// Generates at most "maxNumTxs" number of simulated transactions based on the current UtxoPools of all shards. // Generates at most "maxNumTxs" number of simulated transactions based on the current UtxoPools of all shards.
// The transactions are generated by going through the existing utxos and // The transactions are generated by going through the existing utxos and
@ -35,12 +44,10 @@ var utxoPoolMutex sync.Mutex
// Params: // Params:
// shardId - the shardId for current shard // shardId - the shardId for current shard
// dataNodes - nodes containing utxopools of all shards // dataNodes - nodes containing utxopools of all shards
// maxNumTxs - the max number of txs to generate
// crossShard - whether to generate cross shard txs
// Returns: // Returns:
// all single-shard txs // all single-shard txs
// all cross-shard txs // all cross-shard txs
func generateSimulatedTransactions(shardId int, dataNodes []*node.Node, maxNumTxs int, crossShard bool) ([]*blockchain.Transaction, []*blockchain.Transaction) { func generateSimulatedTransactions(shardId int, dataNodes []*node.Node) ([]*blockchain.Transaction, []*blockchain.Transaction) {
/* /*
UTXO map structure: UTXO map structure:
address - [ address - [
@ -75,80 +82,81 @@ UTXOLOOP:
// Loop over all utxos for the txId // Loop over all utxos for the txId
for index, value := range utxoMap { for index, value := range utxoMap {
if txsCount >= maxNumTxs { if txsCount >= setting.maxNumTxsPerBatch {
break UTXOLOOP break UTXOLOOP
} }
randNum := rand.Intn(100) randNum := rand.Intn(100)
// 30% sample rate to select UTXO to use for new transactions // 30% sample rate to select UTXO to use for new transactions
if randNum < 30 { if randNum >= 30 {
if crossShard && randNum < 10 { // 30% cross shard transactions: add another txinput from another shard continue
// shard with neighboring Id }
crossShardId := (int(dataNodes[shardId].Consensus.ShardID) + 1) % len(dataNodes) if setting.crossShard && randNum < 10 { // 1/3 cross shard transactions: add another txinput from another shard
// shard with neighboring Id
crossShardNode := dataNodes[crossShardId] crossShardId := (int(dataNodes[shardId].Consensus.ShardID) + 1) % len(dataNodes)
crossShardUtxosMap := crossShardNode.UtxoPool.UtxoMap[address]
crossShardNode := dataNodes[crossShardId]
// Get the cross shard utxo from another shard crossShardUtxosMap := crossShardNode.UtxoPool.UtxoMap[address]
var crossTxin *blockchain.TXInput
crossUtxoValue := 0 // Get the cross shard utxo from another shard
// Loop over utxos for the same address from the other shard and use the first utxo as the second cross tx input var crossTxin *blockchain.TXInput
for crossTxIdStr, crossShardUtxos := range crossShardUtxosMap { crossUtxoValue := 0
// Parse TxId // Loop over utxos for the same address from the other shard and use the first utxo as the second cross tx input
id, err := hex.DecodeString(crossTxIdStr) for crossTxIdStr, crossShardUtxos := range crossShardUtxosMap {
if err != nil { // Parse TxId
continue id, err := hex.DecodeString(crossTxIdStr)
} if err != nil {
crossTxId := [32]byte{} continue
copy(crossTxId[:], id[:])
for crossShardIndex, crossShardValue := range crossShardUtxos {
crossUtxoValue = crossShardValue
crossTxin = &blockchain.TXInput{crossTxId, crossShardIndex, address, uint32(crossShardId)}
break
}
if crossTxin != nil {
break
}
} }
crossTxId := [32]byte{}
copy(crossTxId[:], id[:])
// Add the utxo from current shard for crossShardIndex, crossShardValue := range crossShardUtxos {
txin := blockchain.TXInput{txId, index, address, dataNodes[shardId].Consensus.ShardID} crossUtxoValue = crossShardValue
txInputs := []blockchain.TXInput{txin} crossTxin = &blockchain.TXInput{crossTxId, crossShardIndex, address, uint32(crossShardId)}
break
// Add the utxo from the other shard, if any
if crossTxin != nil {
txInputs = append(txInputs, *crossTxin)
} }
// Spend the utxo from the current shard to a random address in [0 - N)
txout := blockchain.TXOutput{value, strconv.Itoa(rand.Intn(10000)), dataNodes[shardId].Consensus.ShardID}
txOutputs := []blockchain.TXOutput{txout}
// Spend the utxo from the other shard, if any, to a random address in [0 - N)
if crossTxin != nil { if crossTxin != nil {
crossTxout := blockchain.TXOutput{crossUtxoValue, strconv.Itoa(rand.Intn(10000)), uint32(crossShardId)} break
txOutputs = append(txOutputs, crossTxout)
} }
}
// Construct the new transaction // Add the utxo from current shard
tx := blockchain.Transaction{[32]byte{}, txInputs, txOutputs, nil} txin := blockchain.TXInput{txId, index, address, dataNodes[shardId].Consensus.ShardID}
tx.SetID() txInputs := []blockchain.TXInput{txin}
crossTxs = append(crossTxs, &tx) // Add the utxo from the other shard, if any
txsCount++ if crossTxin != nil {
} else { txInputs = append(txInputs, *crossTxin)
// Add the utxo as new tx input }
txin := blockchain.TXInput{txId, index, address, dataNodes[shardId].Consensus.ShardID}
// Spend the utxo to a random address in [0 - N) // Spend the utxo from the current shard to a random address in [0 - N)
txout := blockchain.TXOutput{value, strconv.Itoa(rand.Intn(10000)), dataNodes[shardId].Consensus.ShardID} txout := blockchain.TXOutput{value, strconv.Itoa(rand.Intn(setting.numOfAddress)), dataNodes[shardId].Consensus.ShardID}
tx := blockchain.Transaction{[32]byte{}, []blockchain.TXInput{txin}, []blockchain.TXOutput{txout}, nil} txOutputs := []blockchain.TXOutput{txout}
tx.SetID()
txs = append(txs, &tx) // Spend the utxo from the other shard, if any, to a random address in [0 - N)
txsCount++ if crossTxin != nil {
crossTxout := blockchain.TXOutput{crossUtxoValue, strconv.Itoa(rand.Intn(setting.numOfAddress)), uint32(crossShardId)}
txOutputs = append(txOutputs, crossTxout)
} }
// Construct the new transaction
tx := blockchain.Transaction{[32]byte{}, txInputs, txOutputs, nil}
tx.SetID()
crossTxs = append(crossTxs, &tx)
txsCount++
} else {
// Add the utxo as new tx input
txin := blockchain.TXInput{txId, index, address, dataNodes[shardId].Consensus.ShardID}
// Spend the utxo to a random address in [0 - N)
txout := blockchain.TXOutput{value, strconv.Itoa(rand.Intn(setting.numOfAddress)), dataNodes[shardId].Consensus.ShardID}
tx := blockchain.Transaction{[32]byte{}, []blockchain.TXInput{txin}, []blockchain.TXOutput{txout}, nil}
tx.SetID()
txs = append(txs, &tx)
txsCount++
} }
} }
} }
@ -250,8 +258,10 @@ func main() {
config := readConfigFile(*configFile) config := readConfigFile(*configFile)
leaders, shardIds := getLeadersAndShardIds(&config) leaders, shardIds := getLeadersAndShardIds(&config)
setting.numOfAddress = 10000
// Do cross shard tx if there are more than one shard // Do cross shard tx if there are more than one shard
crossShard := len(shardIds) > 1 setting.crossShard = len(shardIds) > 1
setting.maxNumTxsPerBatch = *maxNumTxsPerBatch
// TODO(Richard): refactor this chuck to a single method // TODO(Richard): refactor this chuck to a single method
// Setup a logger to stdout and log file. // Setup a logger to stdout and log file.
@ -267,8 +277,8 @@ func main() {
nodes := []*node.Node{} nodes := []*node.Node{}
for _, shardId := range shardIds { for _, shardId := range shardIds {
node := node.New(&consensus.Consensus{ShardID: shardId}) node := node.New(&consensus.Consensus{ShardID: shardId})
// Assign many fake addresses so we have enough address to place with at first // Assign many fake addresses so we have enough address to play with at first
node.AddTestingAddresses(10000) node.AddTestingAddresses(setting.numOfAddress)
nodes = append(nodes, node) nodes = append(nodes, node)
} }
@ -321,7 +331,7 @@ func main() {
allCrossTxs := []*blockchain.Transaction{} allCrossTxs := []*blockchain.Transaction{}
// Generate simulated transactions // Generate simulated transactions
for i, leader := range leaders { for i, leader := range leaders {
txs, crossTxs := generateSimulatedTransactions(i, nodes, *maxNumTxsPerBatch, crossShard) txs, crossTxs := generateSimulatedTransactions(i, nodes)
allCrossTxs = append(allCrossTxs, crossTxs...) allCrossTxs = append(allCrossTxs, crossTxs...)
log.Debug("[Generator] Sending single-shard txs ...", "leader", leader, "numTxs", len(txs), "numCrossTxs", len(crossTxs)) log.Debug("[Generator] Sending single-shard txs ...", "leader", leader, "numTxs", len(txs), "numCrossTxs", len(crossTxs))

Loading…
Cancel
Save