Skip to content

Commit

Permalink
Add small test contracts, refactor test setup
Browse files Browse the repository at this point in the history
  • Loading branch information
lmittmann authored and avalonche committed Mar 22, 2023
1 parent 4c14976 commit e521c64
Show file tree
Hide file tree
Showing 3 changed files with 478 additions and 5 deletions.
15 changes: 10 additions & 5 deletions miner/algo_common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,22 +186,27 @@ func genGenesisAlloc(sign signerList, contractAddr []common.Address, contractCod

func genTestSetup() (*state.StateDB, chainData, signerList) {
config := params.AllEthashProtocolChanges
db := rawdb.NewMemoryDatabase()
signerList := genSignerList(10, config)

signerList := genSignerList(10, params.AllEthashProtocolChanges)
genesisAlloc := genGenesisAlloc(signerList, []common.Address{payProxyAddress, logContractAddress}, [][]byte{payProxyCode, logContractCode})

stateDB, chainData := genTestSetupWithAlloc(config, genesisAlloc)
return stateDB, chainData, signerList
}

func genTestSetupWithAlloc(config *params.ChainConfig, alloc core.GenesisAlloc) (*state.StateDB, chainData) {
db := rawdb.NewMemoryDatabase()

gspec := &core.Genesis{
Config: config,
Alloc: genesisAlloc,
Alloc: alloc,
}
_ = gspec.MustCommit(db)

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

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

return stateDB, chainData{config, chain, nil}, signerList
return stateDB, chainData{config, chain, nil}
}

func newEnvironment(data chainData, state *state.StateDB, coinbase common.Address, gasLimit uint64, baseFee *big.Int) *environment {
Expand Down
47 changes: 47 additions & 0 deletions miner/algo_contracts_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package miner

import (
"encoding/hex"
)

// Contracts used for testing.
var (
// Always revert and consume all gas.
//
// pc op bytecode
// 0x00 INVALID 0xfe
contractRevert = parseCode("0xfe")

// Send the entire balance of the contract to the caller or revert and
// consume all gas, if the contracts balance is zero.
//
// pc op stack bytecode
// 0x00 SELFBALANCE bal 0x47
// 0x01 PUSH1 0x05 0x05 bal 0x6005
// 0x03 JUMPI . 0x57
// 0x04 INVALID . 0xfe
// 0x05 JUMPDEST . 0x5b
//
// 0x06 MSIZE 0 0x59
// 0x07 MSIZE 0 0 0x59
// 0x08 MSIZE 0 0 0 0x59
// 0x09 MSIZE 0 0 0 0 0x59
// 0x0a SELFBALANCE bal 0 0 0 0 0 0x47
// 0x0b CALLER clr bal 0 0 0 0 0 0x33
// 0x0c GAS gas clr bal 0 0 0 0 0 0x5a
// 0x0d CALL . 0xf1
contractSendBalance = parseCode("0x47600557fe5b5959595947335af100")
)

// parseCode converts a hex bytecode to a byte slice, or panics if the hex
// bytecode is invalid.
func parseCode(hexStr string) []byte {
if hexStr[0] == '0' && (hexStr[1] == 'x' || hexStr[1] == 'X') {
hexStr = hexStr[2:]
}
data, err := hex.DecodeString(hexStr)
if err != nil {
panic(err)
}
return data
}
Loading

0 comments on commit e521c64

Please sign in to comment.