Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature update state tests #2

Merged
merged 3 commits into from
Dec 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
4 changes: 3 additions & 1 deletion chain/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type Forks struct {
Byzantium *Fork `json:"byzantium,omitempty"`
Constantinople *Fork `json:"constantinople,omitempty"`
Petersburg *Fork `json:"petersburg,omitempty"`
Istanbul *Fork `json:"istanbul,omitempty"`
EIP150 *Fork `json:"EIP150,omitempty"`
EIP158 *Fork `json:"EIP158,omitempty"`
EIP155 *Fork `json:"EIP155,omitempty"`
Expand Down Expand Up @@ -71,6 +72,7 @@ func (f *Forks) At(block uint64) ForksInTime {
Byzantium: f.active(f.Byzantium, block),
Constantinople: f.active(f.Constantinople, block),
Petersburg: f.active(f.Petersburg, block),
Istanbul: f.active(f.Istanbul, block),
EIP150: f.active(f.EIP150, block),
EIP158: f.active(f.EIP158, block),
EIP155: f.active(f.EIP155, block),
Expand All @@ -93,5 +95,5 @@ func (f Fork) Int() *big.Int {
}

type ForksInTime struct {
Homestead, Byzantium, Constantinople, Petersburg, EIP150, EIP158, EIP155 bool
Homestead, Byzantium, Constantinople, Petersburg, Istanbul, EIP150, EIP158, EIP155 bool
}
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ github.com/hashicorp/serf v0.8.2 h1:YZ7UKsJv+hKjqGVUUbtE3HNj79Eln2oQ75tniF6iPt0=
github.com/hashicorp/serf v0.8.2/go.mod h1:6hOLApaqBFA1NXqRQAsxw9QxuDEvNxSQRwA/JwenrHc=
github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/huin/goupnp v1.0.0 h1:wg75sLpL6DZqwHQN6E1Cfk6mtfzS45z8OV+ic+DtHRo=
github.com/huin/goupnp v1.0.0/go.mod h1:n9v9KO1tAxYH82qOn+UTIFQDmx5n1Zxd/ClZDMX7Bnc=
github.com/huin/goutil v0.0.0-20170803182201-1ca381bf3150/go.mod h1:PpLOETDnJ0o3iZrZfqZzyLl6l7F3c6L1oWn7OICBi6o=
github.com/imdario/mergo v0.3.7 h1:Y+UAYTZ7gDEuOfhxKWy+dvb5dRQ6rJjFSdX2HZY1/gI=
Expand Down
12 changes: 9 additions & 3 deletions state/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ func (e *Executor) BeginTxn(parentRoot types.Hash, header *types.Header) (*Trans
Number: int64(header.Number),
Difficulty: types.BytesToHash(new(big.Int).SetUint64(header.Difficulty).Bytes()),
GasLimit: int64(header.GasLimit),
ChainID: int64(e.config.ChainID),
}

// Mainnet (TODO: Do this in a preHookFn)
Expand Down Expand Up @@ -385,7 +386,12 @@ func (t *Transition) transactionGasCost(msg *types.Transaction) uint64 {
}
nonZeros := len(payload) - zeros
cost += uint64(zeros) * 4
cost += uint64(nonZeros) * 68

nonZeroCost := uint64(68)
if t.config.Istanbul {
nonZeroCost = 16
}
cost += uint64(nonZeros) * nonZeroCost
}

return uint64(cost)
Expand Down Expand Up @@ -610,8 +616,8 @@ func (t *Transition) applyCreate(msg *runtime.Contract, host runtime.Host) ([]by
return code, gas, nil
}

func (t *Transition) SetStorage(addr types.Address, key types.Hash, value types.Hash, discount bool) runtime.StorageStatus {
return t.state.SetStorage(addr, key, value, discount)
func (t *Transition) SetStorage(addr types.Address, key types.Hash, value types.Hash, config *chain.ForksInTime) runtime.StorageStatus {
return t.state.SetStorage(addr, key, value, config)
}

func (t *Transition) GetTxContext() runtime.TxContext {
Expand Down
5 changes: 3 additions & 2 deletions state/runtime/evm/dispatch_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,12 @@ func init() {

register(POP, handler{opPop, 1, 2})

register(EXTCODEHASH, handler{opExtCodeHash, 1, 400})
register(EXTCODEHASH, handler{opExtCodeHash, 1, 0})

// context operations
register(ADDRESS, handler{opAddress, 0, 2})
register(BALANCE, handler{opBalance, 1, 0})
register(SELFBALANCE, handler{opSelfBalance, 0, 5})
register(ORIGIN, handler{opOrigin, 0, 2})
register(CALLER, handler{opCaller, 0, 2})
register(CALLVALUE, handler{opCallValue, 0, 2})
Expand All @@ -103,7 +104,7 @@ func init() {
register(EXTCODESIZE, handler{opExtCodeSize, 1, 0})
register(GASPRICE, handler{opGasPrice, 0, 2})
register(RETURNDATASIZE, handler{opReturnDataSize, 0, 2})

register(CHAINID, handler{opChainID, 0, 2})
register(PC, handler{opPC, 0, 2})
register(MSIZE, handler{opMSize, 0, 2})
register(GAS, handler{opGas, 0, 2})
Expand Down
12 changes: 0 additions & 12 deletions state/runtime/evm/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,6 @@ func NewEVM() *EVM {
return &EVM{}
}

/*
// TODO
func (c *EVM) getValue() *state {
if cap(c.vs) > len(c.vs) {
c.vs = c.vs[:len(c.vs)+1]
} else {
c.vs = append(c.vs, state{})
}
return &c.vs[len(c.vs)-1]
}
*/

// CanRun implements the runtime interface
func (e *EVM) CanRun(c *runtime.Contract, host runtime.Host, config *chain.ForksInTime) bool {
return true
Expand Down
62 changes: 56 additions & 6 deletions state/runtime/evm/instructions.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,10 @@ func opSload(c *state) {
loc := c.top()

var gas uint64
if c.config.EIP150 {
if c.config.Istanbul {
// eip-1884
gas = 800
} else if c.config.EIP150 {
gas = 200
} else {
gas = 50
Expand All @@ -464,31 +467,46 @@ func opSStore(c *state) {
return
}

if c.config.Istanbul && c.gas <= 2300 {
c.exit(errOutOfGas)
return
}

key := c.popHash()
val := c.popHash()

discount := c.config.Constantinople && !c.config.Petersburg
legacyGasMetering := !c.config.Istanbul && (c.config.Petersburg || !c.config.Constantinople)

status := c.host.SetStorage(c.msg.Address, key, val, discount)
status := c.host.SetStorage(c.msg.Address, key, val, c.config)
cost := uint64(0)

switch status {
case runtime.StorageUnchanged:
if !discount {
if c.config.Istanbul {
// eip-2200
cost = 800
} else if legacyGasMetering {
cost = 5000
} else {
cost = 200
}

case runtime.StorageModified:
cost = 5000

case runtime.StorageModifiedAgain:
if !discount {
if c.config.Istanbul {
// eip-2200
cost = 800
} else if legacyGasMetering {
cost = 5000
} else {
cost = 200
}

case runtime.StorageAdded:
cost = 20000

case runtime.StorageDeleted:
cost = 5000
}
Expand Down Expand Up @@ -533,18 +551,40 @@ func opBalance(c *state) {
addr, _ := c.popAddr()

var gas uint64
if c.config.EIP150 {
if c.config.Istanbul {
// eip-1884
gas = 700
} else if c.config.EIP150 {
gas = 400
} else {
gas = 20
}

if !c.consumeGas(gas) {
return
}

c.push1().Set(c.host.GetBalance(addr))
}

func opSelfBalance(c *state) {
if !c.config.Istanbul {
c.exit(errOpCodeNotFound)
return
}

c.push1().Set(c.host.GetBalance(c.msg.Address))
}

func opChainID(c *state) {
if !c.config.Istanbul {
c.exit(errOpCodeNotFound)
return
}

c.push1().SetUint64(uint64(c.host.GetTxContext().ChainID))
}

func opOrigin(c *state) {
c.push1().SetBytes(c.host.GetTxContext().Origin.Bytes())
}
Expand Down Expand Up @@ -622,6 +662,16 @@ func opExtCodeHash(c *state) {

address, _ := c.popAddr()

var gas uint64
if c.config.Istanbul {
gas = 700
} else {
gas = 400
}
if !c.consumeGas(gas) {
return
}

v := c.push1()
if c.host.Empty(address) {
v.Set(zero)
Expand Down
8 changes: 8 additions & 0 deletions state/runtime/evm/opcodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,12 @@ const (
// GASLIMIT returns the current block's gas limit
GASLIMIT = 0x45

// CHAINID returns the id of the chain
CHAINID = 0x46

// SELFBALANCE returns the balance of the current account
SELFBALANCE = 0x47

// POP pops a (u)int256 off the stack and discards it
POP = 0x50

Expand Down Expand Up @@ -323,6 +329,8 @@ var opCodeToString = map[OpCode]string{
STATICCALL: "STATICCALL",
REVERT: "REVERT",
SELFDESTRUCT: "SELFDESTRUCT",
CHAINID: "CHAINID",
SELFBALANCE: "SELFBALANCE",
}

func opCodesToString(from, to OpCode, str string) {
Expand Down
2 changes: 2 additions & 0 deletions state/runtime/evm/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,8 @@ func (c *state) Run() ([]byte, error) {
// execute the instruction
inst.inst(c)

// fmt.Printf("[%d] %s %d\n", c.ip, op.String(), c.gas)

// check if stack size exceeds the max size
if c.sp > stackSize {
c.exit(errStackOverflow)
Expand Down
9 changes: 5 additions & 4 deletions state/runtime/precompiled/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"golang.org/x/crypto/ripemd160"

"github.com/0xPolygon/minimal/chain"
"github.com/0xPolygon/minimal/crypto"
"github.com/0xPolygon/minimal/helper/keccak"
)
Expand All @@ -13,7 +14,7 @@ type ecrecover struct {
p *Precompiled
}

func (e *ecrecover) gas(input []byte) uint64 {
func (e *ecrecover) gas(input []byte, config *chain.ForksInTime) uint64 {
return 3000
}

Expand Down Expand Up @@ -45,7 +46,7 @@ func (e *ecrecover) run(input []byte) ([]byte, error) {
type identity struct {
}

func (i *identity) gas(input []byte) uint64 {
func (i *identity) gas(input []byte, config *chain.ForksInTime) uint64 {
return baseGasCalc(input, 15, 3)
}

Expand All @@ -56,7 +57,7 @@ func (i *identity) run(in []byte) ([]byte, error) {
type sha256h struct {
}

func (s *sha256h) gas(input []byte) uint64 {
func (s *sha256h) gas(input []byte, config *chain.ForksInTime) uint64 {
return baseGasCalc(input, 60, 12)
}

Expand All @@ -69,7 +70,7 @@ type ripemd160h struct {
p *Precompiled
}

func (r *ripemd160h) gas(input []byte) uint64 {
func (r *ripemd160h) gas(input []byte, config *chain.ForksInTime) uint64 {
return baseGasCalc(input, 600, 120)
}

Expand Down
Loading