extract txInfo.

pull/49/head
Richard Liu 6 years ago
parent ab73f27df2
commit 6d6742cd2d
  1. 78
      client/txgen/main.go

@ -28,6 +28,21 @@ var (
setting txGenSettings
)
type TxInfo struct {
// Global Input
shardID int
dataNodes []*node.Node
// Temp Input
id [32]byte
index int
value int
address string
// Output
txs []*blockchain.Transaction
crossTxs []*blockchain.Transaction
txCount int
}
// 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
// randomly select a subset of them as the input for each new transaction. The output
@ -59,11 +74,12 @@ func generateSimulatedTransactions(shardId int, dataNodes []*node.Node) ([]*bloc
]
]
*/
var txs []*blockchain.Transaction
var crossTxs []*blockchain.Transaction
txsCount := 0
utxoPoolMutex.Lock()
txInfo := TxInfo{}
txInfo.shardID = shardId
txInfo.dataNodes = dataNodes
txInfo.txCount = 0
UTXOLOOP:
// Loop over all addresses
@ -75,12 +91,14 @@ UTXOLOOP:
if err != nil {
continue
}
txId := [32]byte{}
copy(txId[:], id[:])
copy(txInfo.id[:], id[:])
txInfo.address = address
// Loop over all utxos for the txId
for index, value := range utxoMap {
if txsCount >= setting.maxNumTxsPerBatch {
txInfo.index = index
txInfo.value = value
if txInfo.txCount >= setting.maxNumTxsPerBatch {
break UTXOLOOP
}
randNum := rand.Intn(100)
@ -90,11 +108,24 @@ UTXOLOOP:
continue
}
if setting.crossShard && randNum < 10 { // 1/3 cross shard transactions: add another txinput from another shard
generateCrossShardTx(txInfo)
} else {
generateSingleShardTx(txInfo)
}
}
}
}
utxoPoolMutex.Unlock()
return txInfo.txs, txInfo.crossTxs
}
func generateCrossShardTx(txInfo TxInfo) {
// shard with neighboring Id
crossShardId := (int(dataNodes[shardId].Consensus.ShardID) + 1) % len(dataNodes)
crossShardId := (int(txInfo.dataNodes[txInfo.shardID].Consensus.ShardID) + 1) % len(txInfo.dataNodes)
crossShardNode := dataNodes[crossShardId]
crossShardUtxosMap := crossShardNode.UtxoPool.UtxoMap[address]
crossShardNode := txInfo.dataNodes[crossShardId]
crossShardUtxosMap := crossShardNode.UtxoPool.UtxoMap[txInfo.address]
// Get the cross shard utxo from another shard
var crossTxin *blockchain.TXInput
@ -111,7 +142,7 @@ UTXOLOOP:
for crossShardIndex, crossShardValue := range crossShardUtxos {
crossUtxoValue = crossShardValue
crossTxin = &blockchain.TXInput{crossTxId, crossShardIndex, address, uint32(crossShardId)}
crossTxin = &blockchain.TXInput{crossTxId, crossShardIndex, txInfo.address, uint32(crossShardId)}
break
}
if crossTxin != nil {
@ -120,7 +151,7 @@ UTXOLOOP:
}
// Add the utxo from current shard
txin := blockchain.TXInput{txId, index, address, dataNodes[shardId].Consensus.ShardID}
txin := blockchain.TXInput{txInfo.id, txInfo.index, txInfo.address, txInfo.dataNodes[txInfo.shardID].Consensus.ShardID}
txInputs := []blockchain.TXInput{txin}
// Add the utxo from the other shard, if any
@ -129,7 +160,7 @@ UTXOLOOP:
}
// Spend the utxo from the current shard to a random address in [0 - N)
txout := blockchain.TXOutput{value, strconv.Itoa(rand.Intn(setting.numOfAddress)), dataNodes[shardId].Consensus.ShardID}
txout := blockchain.TXOutput{txInfo.value, strconv.Itoa(rand.Intn(setting.numOfAddress)), txInfo.dataNodes[txInfo.shardID].Consensus.ShardID}
txOutputs := []blockchain.TXOutput{txout}
// Spend the utxo from the other shard, if any, to a random address in [0 - N)
@ -142,26 +173,21 @@ UTXOLOOP:
tx := blockchain.Transaction{[32]byte{}, txInputs, txOutputs, nil}
tx.SetID()
crossTxs = append(crossTxs, &tx)
txsCount++
} else {
txInfo.crossTxs = append(txInfo.crossTxs, &tx)
txInfo.txCount++
}
func generateSingleShardTx(txInfo TxInfo) {
// Add the utxo as new tx input
txin := blockchain.TXInput{txId, index, address, dataNodes[shardId].Consensus.ShardID}
txin := blockchain.TXInput{txInfo.id, txInfo.index, txInfo.address, txInfo.dataNodes[txInfo.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}
txout := blockchain.TXOutput{txInfo.value, strconv.Itoa(rand.Intn(setting.numOfAddress)), txInfo.dataNodes[txInfo.shardID].Consensus.ShardID}
tx := blockchain.Transaction{[32]byte{}, []blockchain.TXInput{txin}, []blockchain.TXOutput{txout}, nil}
tx.SetID()
txs = append(txs, &tx)
txsCount++
}
}
}
}
utxoPoolMutex.Unlock()
return txs, crossTxs
txInfo.txs = append(txInfo.txs, &tx)
txInfo.txCount++
}
// A utility func that counts the total number of utxos in a pool.

Loading…
Cancel
Save