Skip to content

Commit

Permalink
Better cycling
Browse files Browse the repository at this point in the history
  • Loading branch information
benaadams committed Nov 5, 2024
1 parent 0d277e2 commit b64b0e5
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 28 deletions.
9 changes: 5 additions & 4 deletions packages/taiko-client/driver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ type Config struct {
BlobServerEndpoint *url.URL
SocialScanEndpoint *url.URL
SyntheticBlocks struct {
Enabled bool
BlockTime time.Duration
NumAccounts int
InitialKey *ecdsa.PrivateKey
Enabled bool
BlockTime time.Duration
NumAccounts int
InitialKey *ecdsa.PrivateKey
InitialNonce uint64
}
}

Expand Down
1 change: 1 addition & 0 deletions packages/taiko-client/driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ func (d *Driver) InitFromConfig(ctx context.Context, cfg *Config) (err error) {
cfg.SyntheticBlocks.BlockTime,
cfg.SyntheticBlocks.NumAccounts,
cfg.SyntheticBlocks.InitialKey,
cfg.SyntheticBlocks.InitialNonce,
)

// Pass generator to chain syncer
Expand Down
75 changes: 51 additions & 24 deletions packages/taiko-client/driver/synthetic_block_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,39 +11,39 @@ import (
"github.com/ethereum/go-ethereum/crypto"
)

const blockGasLimit = 60000000
const blockGasTarget = blockGasLimit / 2
const txGasLimit = 21000

type SyntheticBlockGenerator struct {
blockTime time.Duration
lastBlock *types.Header
accounts []*ecdsa.PrivateKey // Store multiple private keys
initialKey *ecdsa.PrivateKey // Store initial key
nonce uint64
}

func NewSyntheticBlockGenerator(blockTime time.Duration, numAccounts int, initialKey *ecdsa.PrivateKey) *SyntheticBlockGenerator {
func NewSyntheticBlockGenerator(blockTime time.Duration, numAccounts int, initialKey *ecdsa.PrivateKey, initialNonce uint64) *SyntheticBlockGenerator {
accounts := make([]*ecdsa.PrivateKey, numAccounts)
accounts[0] = initialKey // Use provided initial key

// Generate remaining accounts
for i := 1; i < numAccounts; i++ {
key, _ := crypto.GenerateKey()
accounts[i] = key
}
return &SyntheticBlockGenerator{
blockTime: blockTime,
accounts: accounts,
initialKey: initialKey,
nonce: initialNonce,
}
}

// Helper function to create a self-transfer transaction
func createSelfTransferTx(nonce uint64, privateKey *ecdsa.PrivateKey) []byte {
account := crypto.PubkeyToAddress(privateKey.PublicKey)
tx := types.NewTransaction(
nonce, // nonce
account, // to (same as sender)
big.NewInt(0), // value (0 ETH)
21000, // gas limit (standard transfer)
big.NewInt(1000000000), // gas price (1 gwei)
nil, // data
nonce, // nonce
account, // to (same as sender)
big.NewInt(0), // value (0 ETH)
txGasLimit, // gas limit (standard transfer)
big.NewInt(1), // gas price (1 wei)
nil, // data
)

signedTx, _ := types.SignTx(tx, types.NewEIP155Signer(big.NewInt(1)), privateKey)
Expand All @@ -58,7 +58,7 @@ func createTransferToNextTx(nonce uint64, fromKey *ecdsa.PrivateKey, toKey *ecds
nonce, // nonce (will be 127)
toAddr, // to (next account)
value, // transfer amount
21000, // gas limit
txGasLimit, // gas limit
big.NewInt(1), // gas price (1 wei)
nil, // data
)
Expand All @@ -68,20 +68,47 @@ func createTransferToNextTx(nonce uint64, fromKey *ecdsa.PrivateKey, toKey *ecds
}

func (g *SyntheticBlockGenerator) generateTransactions() [][]byte {
var transactions [][]byte
transferAmount := big.NewInt(1e18) // 1 ETH
// Generate accounts
for i := 0; i < len(g.accounts); i++ {
key, _ := crypto.GenerateKey()
g.accounts[i] = key
}

for i, account := range g.accounts {
// Generate 126 self-transfers (nonce 0-125)
for nonce := uint64(0); nonce < 126; nonce++ {
transactions = append(transactions, createSelfTransferTx(nonce, account))
var transactions [][]byte
transferAmount := big.NewInt(1e17) // 0.1 ETH

availableGas := blockGasTarget - txGasLimit*2

// initial funding transfer
lastAccount := g.accounts[0]
transactions = append(transactions, createTransferToNextTx(g.nonce, g.initialKey, lastAccount, transferAmount))
g.nonce++

lastNonce := uint64(0)
i := 0
for i, lastAccount = range g.accounts {
// Generate 126 self-transfers (nonce 0-126)
for ; lastNonce < 127; lastNonce++ {
if availableGas-txGasLimit < 0 {
break
}
transactions = append(transactions, createSelfTransferTx(lastNonce, lastAccount))
}

// Transfer to next account with nonce 126
transferAmount.Sub(transferAmount, big.NewInt(int64(lastNonce+1)*21000))

// Transfer to next account with nonce 127
if availableGas-txGasLimit < 0 {
break
}
nextIdx := (i + 1) % len(g.accounts)
transactions = append(transactions, createTransferToNextTx(126, account, g.accounts[nextIdx], transferAmount))
transactions = append(transactions, createTransferToNextTx(lastNonce, lastAccount, g.accounts[nextIdx], transferAmount))
lastNonce = 0
}

// Transfer remaining back to initial account
transactions = append(transactions, createTransferToNextTx(lastNonce, g.accounts[(i)%len(g.accounts)], lastAccount, transferAmount))

return transactions
}

Expand All @@ -101,8 +128,8 @@ func (g *SyntheticBlockGenerator) GenerateBlock(parent *types.Header) *engine.Ex
LogsBloom: types.Bloom{}.Bytes(),
Random: common.Hash{},
Number: new(big.Int).Add(parent.Number, common.Big1).Uint64(),
GasLimit: 30000000,
GasUsed: 21000 * uint64(len(transactions)), // Update gas used (21000 per transaction)
GasLimit: blockGasLimit,
GasUsed: txGasLimit * uint64(len(transactions)), // Update gas used (21000 per transaction)
Timestamp: timestamp,
ExtraData: []byte{},
BaseFeePerGas: big.NewInt(1), // 1 wei
Expand Down

0 comments on commit b64b0e5

Please sign in to comment.