Skip to content
This repository was archived by the owner on Oct 25, 2024. It is now read-only.

Commit dcfffd9

Browse files
lmittmannRuteri
authored andcommitted
Add small test contracts, refactor test setup
1 parent e287e3e commit dcfffd9

3 files changed

Lines changed: 478 additions & 5 deletions

File tree

miner/algo_common_test.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -186,22 +186,27 @@ func genGenesisAlloc(sign signerList, contractAddr []common.Address, contractCod
186186

187187
func genTestSetup() (*state.StateDB, chainData, signerList) {
188188
config := params.AllEthashProtocolChanges
189-
db := rawdb.NewMemoryDatabase()
190-
signerList := genSignerList(10, config)
191-
189+
signerList := genSignerList(10, params.AllEthashProtocolChanges)
192190
genesisAlloc := genGenesisAlloc(signerList, []common.Address{payProxyAddress, logContractAddress}, [][]byte{payProxyCode, logContractCode})
193191

192+
stateDB, chainData := genTestSetupWithAlloc(config, genesisAlloc)
193+
return stateDB, chainData, signerList
194+
}
195+
196+
func genTestSetupWithAlloc(config *params.ChainConfig, alloc core.GenesisAlloc) (*state.StateDB, chainData) {
197+
db := rawdb.NewMemoryDatabase()
198+
194199
gspec := &core.Genesis{
195200
Config: config,
196-
Alloc: genesisAlloc,
201+
Alloc: alloc,
197202
}
198203
_ = gspec.MustCommit(db)
199204

200205
chain, _ := core.NewBlockChain(db, &core.CacheConfig{TrieDirtyDisabled: true}, gspec.Config, ethash.NewFaker(), vm.Config{}, nil, nil)
201206

202207
stateDB, _ := state.New(chain.CurrentHeader().Root, state.NewDatabase(db), nil)
203208

204-
return stateDB, chainData{config, chain, nil}, signerList
209+
return stateDB, chainData{config, chain, nil}
205210
}
206211

207212
func newEnvironment(data chainData, state *state.StateDB, coinbase common.Address, gasLimit uint64, baseFee *big.Int) *environment {

miner/algo_contracts_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package miner
2+
3+
import (
4+
"encoding/hex"
5+
)
6+
7+
// Contracts used for testing.
8+
var (
9+
// Always revert and consume all gas.
10+
//
11+
// pc op bytecode
12+
// 0x00 INVALID 0xfe
13+
contractRevert = parseCode("0xfe")
14+
15+
// Send the entire balance of the contract to the caller or revert and
16+
// consume all gas, if the contracts balance is zero.
17+
//
18+
// pc op stack bytecode
19+
// 0x00 SELFBALANCE bal 0x47
20+
// 0x01 PUSH1 0x05 0x05 bal 0x6005
21+
// 0x03 JUMPI . 0x57
22+
// 0x04 INVALID . 0xfe
23+
// 0x05 JUMPDEST . 0x5b
24+
//
25+
// 0x06 MSIZE 0 0x59
26+
// 0x07 MSIZE 0 0 0x59
27+
// 0x08 MSIZE 0 0 0 0x59
28+
// 0x09 MSIZE 0 0 0 0 0x59
29+
// 0x0a SELFBALANCE bal 0 0 0 0 0 0x47
30+
// 0x0b CALLER clr bal 0 0 0 0 0 0x33
31+
// 0x0c GAS gas clr bal 0 0 0 0 0 0x5a
32+
// 0x0d CALL . 0xf1
33+
contractSendBalance = parseCode("0x47600557fe5b5959595947335af100")
34+
)
35+
36+
// parseCode converts a hex bytecode to a byte slice, or panics if the hex
37+
// bytecode is invalid.
38+
func parseCode(hexStr string) []byte {
39+
if hexStr[0] == '0' && (hexStr[1] == 'x' || hexStr[1] == 'X') {
40+
hexStr = hexStr[2:]
41+
}
42+
data, err := hex.DecodeString(hexStr)
43+
if err != nil {
44+
panic(err)
45+
}
46+
return data
47+
}

0 commit comments

Comments
 (0)