Skip to content

Commit

Permalink
cmd/hivechain: use GetBalance() method for tx generation (ethereum#462)
Browse files Browse the repository at this point in the history
This option adds test transactions up to the block gas limit.
It also improves the generator to actually check the balance
of test accounts instead of estimating it.

Co-authored-by: Felix Lange <[email protected]>
  • Loading branch information
renaynay and fjl authored Apr 8, 2021
1 parent e1297ba commit 6bf0362
Show file tree
Hide file tree
Showing 4 changed files with 806 additions and 31 deletions.
47 changes: 33 additions & 14 deletions cmd/hivechain/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ var (

type generatorConfig struct {
txInterval int // frequency of blocks containing transactions
txCount int // number of txs in block
blockCount int // number of generated blocks
blockTimeSec int // block time in seconds, influences difficulty
powMode ethash.Mode
Expand Down Expand Up @@ -86,23 +87,41 @@ func (cfg generatorConfig) addTxForKnownAccounts(i int, gen *core.BlockGen) {
}

txType := (i / cfg.txInterval) % txTypeMax

var gasLimit uint64
if gen.Number().Uint64() > 0 {
gasLimit = core.CalcGasLimit(gen.PrevBlock(-1), 0, cfg.genesis.GasLimit)
}

var (
txGasSum uint64
txCount = 0
accounts = make(map[common.Address]*ecdsa.PrivateKey)
)
for addr, key := range knownAccounts {
if a, ok := cfg.genesis.Alloc[addr]; ok {
// It exists, check remaining balance. Would be nice if BlockGen had a way to
// check balance, but it doesn't, so we need to estimate the remaining
// balance.
txCost := big.NewInt(1)
spent := new(big.Int).Mul(txCost, big.NewInt(int64(i*cfg.txInterval)))
gbal := new(big.Int).Set(a.Balance)
if gbal.Sub(gbal, spent).Cmp(txCost) < 0 {
continue // no funds left in this account
}
// Add transaction.
if _, ok := cfg.genesis.Alloc[addr]; ok {
accounts[addr] = key
}
}

for txCount <= cfg.txCount && len(accounts) > 0 {
for addr, key := range accounts {
tx := generateTx(txType, key, &cfg.genesis, gen)
// Check if account has enough balance left to cover the tx.
if gen.GetBalance(addr).Cmp(tx.Cost()) < 0 {
delete(accounts, addr)
continue
}
// Check if block gas limit reached.
if (txGasSum + tx.Gas()) > gasLimit {
return
}

log.Printf("adding tx (type %d) from %s in block %d", txType, addr.String(), gen.Number())
log.Printf("0x%x (%d gas)", tx.Hash(), tx.Gas())
log.Printf("%v (%d gas)", tx.Hash(), tx.Gas())
gen.AddTx(tx)
return
txGasSum += tx.Gas()
txCount++
}
}
}
Expand Down Expand Up @@ -149,7 +168,7 @@ func generateTx(txType int, key *ecdsa.PrivateKey, genesis *core.Genesis, gen *c
func createTxGasLimit(gen *core.BlockGen, genesis *core.Genesis, data []byte) uint64 {
isHomestead := genesis.Config.IsHomestead(gen.Number())
isEIP2028 := genesis.Config.IsIstanbul(gen.Number())
igas, err := core.IntrinsicGas(data, true, isHomestead, isEIP2028)
igas, err := core.IntrinsicGas(data, nil, true, isHomestead, isEIP2028)
if err != nil {
panic(err)
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/hivechain/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,8 @@ func generateCommand(args []string) {
)
flag.IntVar(&cfg.blockCount, "length", 2, "The length of the chain to generate")
flag.IntVar(&cfg.blockTimeSec, "blocktime", 30, "The desired block time in seconds")
flag.IntVar(&cfg.txInterval, "tx-interval", 10, "Add transaction to chain every n blocks")
flag.IntVar(&cfg.txInterval, "tx-interval", 10, "Add transactions to chain every n blocks")
flag.IntVar(&cfg.txCount, "tx-count", 1, "Maximum number of txs per block")
flag.CommandLine.Parse(args)

if *genesis == "" {
Expand Down
12 changes: 5 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.12
require (
github.com/Microsoft/go-winio v0.4.15 // indirect
github.com/Microsoft/hcsshim v0.8.10 // indirect
github.com/StackExchange/wmi v0.0.0-20210224194228-fe8f1750fd46 // indirect
github.com/allegro/bigcache v1.2.1 // indirect
github.com/aristanetworks/goarista v0.0.0-20201012165903-2cb20defcd66 // indirect
github.com/btcsuite/btcd v0.21.0-beta // indirect
Expand All @@ -13,25 +14,22 @@ require (
github.com/davecgh/go-spew v1.1.1
github.com/deckarep/golang-set v1.7.1 // indirect
github.com/edsrzf/mmap-go v1.0.0 // indirect
github.com/ethereum/go-ethereum v1.9.23
github.com/ethereum/go-ethereum v1.10.2-0.20210330180930-4a37ae510eb0
github.com/fsouza/go-dockerclient v1.6.6
github.com/go-ole/go-ole v1.2.5 // indirect
github.com/golang/protobuf v1.4.3 // indirect
github.com/golang/snappy v0.0.2 // indirect
github.com/gorilla/mux v1.8.0
github.com/gorilla/websocket v1.4.2 // indirect
github.com/mattn/go-colorable v0.1.8 // indirect
github.com/mattn/go-runewidth v0.0.9 // indirect
github.com/moby/sys/mount v0.1.1 // indirect
github.com/moby/sys/mountinfo v0.4.0 // indirect
github.com/moby/term v0.0.0-20201101162038-25d840ce174a // indirect
github.com/olekukonko/tablewriter v0.0.4 // indirect
github.com/prometheus/tsdb v0.10.0 // indirect
github.com/sirupsen/logrus v1.7.0 // indirect
github.com/stretchr/testify v1.6.1 // indirect
golang.org/x/crypto v0.0.0-20201016220609-9e8e0b390897 // indirect
golang.org/x/net v0.0.0-20201026091529-146b70c837a4 // indirect
golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2 // indirect
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9 // indirect
golang.org/x/sys v0.0.0-20201109165425-215b40eba54c // indirect
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44 // indirect
google.golang.org/genproto v0.0.0-20201109203340-2640f1f9cdfb // indirect
google.golang.org/grpc v1.33.2 // indirect
gopkg.in/inconshreveable/log15.v2 v2.0.0-20200109203555-b30bc20e4fd1
Expand Down
Loading

0 comments on commit 6bf0362

Please sign in to comment.