Skip to content

Commit

Permalink
EvmBuilder and its dependencies #1
Browse files Browse the repository at this point in the history
  • Loading branch information
x-senpai-x committed Oct 28, 2024
1 parent 1a35104 commit b89406a
Show file tree
Hide file tree
Showing 9 changed files with 1,022 additions and 0 deletions.
11 changes: 11 additions & 0 deletions execution/evm/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package evm
import(
"github.com/BlocSoc-iitr/selene/common"
Common "github.com/ethereum/go-ethereum/common"
"math/big"
)
type BlockTag = common.BlockTag
type U256 = *big.Int
type B256 = Common.Hash
type Address = common.Address
type Bytes=[]byte
136 changes: 136 additions & 0 deletions execution/evm/context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package evm
import (
"math/big"
)
var BLOCK_HASH_HISTORY = big.NewInt(256)
type Context[EXT interface{}, DB Database] struct {

Check failure on line 6 in execution/evm/context.go

View workflow job for this annotation

GitHub Actions / test

undefined: Database

Check failure on line 6 in execution/evm/context.go

View workflow job for this annotation

GitHub Actions / build (1.22.3)

undefined: Database

Check failure on line 6 in execution/evm/context.go

View workflow job for this annotation

GitHub Actions / golangci-lint (/home/runner/work/selene/selene)

undefined: Database
Evm EvmContext[DB]
External interface{}
}
func DefaultContext[EXT interface{}]() Context[EXT, *EmptyDB] {
return Context[EXT, *EmptyDB]{
Evm: NewEvmContext(new(EmptyDB)),
External: nil,
}
}
func NewContext[EXT interface{}, DB Database](evm EvmContext[DB], external EXT) Context[EXT, DB] {
return Context[EXT, DB]{
Evm: evm,
External: external,
}
}
type ContextWithHandlerCfg[EXT interface{}, DB Database] struct {
Context Context[EXT, DB]
Cfg HandlerCfg
}
func (js JournaledState) SetSpecId(spec SpecId) {
js.Spec = spec
}
//Not being used
func (c EvmContext[DB]) WithDB(db DB) EvmContext[DB] {
return EvmContext[DB]{
Inner: c.Inner.WithDB(db),
Precompiles: DefaultContextPrecompiles[DB](),
}
}
////////////////////////////////////
/// impl Host for Context /////////
////////////////////////////////////

// func (c *Context[EXT, DB]) Env() *Env {
// return c.Evm.Inner.Env
// }

// func (c *Context[EXT, DB]) EnvMut() *Env {
// return c.Evm.Inner.Env
// }

// func (c *Context[EXT, DB]) LoadAccount(address Address) (LoadAccountResult, bool) {
// res, err := c.Evm.Inner.LoadAccountExist(address)
// if err != nil {
// c.Evm.Inner.Error = err
// return LoadAccountResult{}, false
// }
// return res, true
// }

// //Get the block hash of the given block `number`.
// func (c *Context[EXT, DB]) BlockHash(number uint64) (B256, bool) {
// blockNumber := c.Evm.Inner.Env.Block.Number
// requestedNumber := big.NewInt(int64(number))

// diff := new(big.Int).Sub(blockNumber, requestedNumber)

// if diff.Cmp(big.NewInt(0)) == 0 {
// return common.Hash{}, true
// }

// if diff.Cmp(BLOCK_HASH_HISTORY) <= 0 {
// hash, err := c.Evm.Inner.DB.BlockHash(number)
// if err != nil {
// c.Evm.Inner.Error = err
// return common.Hash{}, false
// }
// return hash, true
// }

// return common.Hash{}, true
// }

// // Get balance of `address` and if the account is cold.
// func (c *Context[EXT, DB]) Balance(address Address) (U256, bool, bool)

// // Get code of `address` and if the account is cold.
// func (c *Context[EXT, DB]) Code(address Address) (Bytes, bool, bool)

// // Get code hash of `address` and if the account is cold.
// func (c *Context[EXT, DB]) CodeHash(address Address) (B256, bool, bool)

// // Get storage value of `address` at `index` and if the account is cold.
// func (c *Context[EXT, DB]) SLoad(address Address, index U256) (U256, bool, bool)

// // Set storage value of account address at index.
// // Returns (original, present, new, is_cold).
// func (c *Context[EXT, DB]) SStore(address Address, index U256, value U256) (SStoreResult, bool)

// // Get the transient storage value of `address` at `index`.
// func (c *Context[EXT, DB]) TLoad(address Address, index U256) U256

// // Set the transient storage value of `address` at `index`.
// func (c *Context[EXT, DB]) TStore(address Address, index U256, value U256)

// // Emit a log owned by `address` with given `LogData`.
// func (c *Context[EXT, DB]) Log(log Log [any])

// // Mark `address` to be deleted, with funds transferred to `target`.
// func (c *Context[EXT, DB]) SelfDestruct(address Address, target Address) (SelfDestructResult, bool)

////////////////////////////////////
////////////////////////////////////
////////////////////////////////////

/*
func (c EvmContext[DB1]) WithNewEvmDB[DB2 Database](db DB2) *EvmContext[ DB2] {
return &EvmContext[DB2]{
Inner: c.Inner.WithDB(db),
context: NewContext[EXT, DB2](
eb.context.Evm.WithNewDB(db),
eb.context.External.(EXT),
),
handler: Handler[Context[EXT, DB2], EXT, DB2]{Cfg: eb.handler.Cfg},
phantom: struct{}{},
}
}*/













77 changes: 77 additions & 0 deletions execution/evm/evmBuilder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package evm
type EvmBuilder[EXT interface{}, DB Database] struct {
context Context[EXT, DB]
handler Handler[Context[EXT, DB], EXT, DB]
phantom struct{}
}
func NewDefaultEvmBuilder[EXT interface{}]() *EvmBuilder[EXT, *EmptyDB] {
handlerCfg := NewHandlerCfg(LATEST)
return &EvmBuilder[EXT, *EmptyDB]{
context: DefaultContext[EXT](),
handler: Handler[Context[EXT, *EmptyDB], EXT, *EmptyDB]{Cfg: handlerCfg},
phantom: struct{}{},
}
}
func WithNewDB[EXT interface{}, DB1, DB2 Database](
eb *EvmBuilder[EXT, DB1],
db DB2,
) *EvmBuilder[EXT, DB2] {
return &EvmBuilder[EXT, DB2]{
context: NewContext[EXT, DB2](
WithNewEvmDB(eb.context.Evm, db),
eb.context.External.(EXT),
),
handler: Handler[Context[EXT, DB2], EXT, DB2]{Cfg: eb.handler.Cfg},
phantom: struct{}{},
}
}
func (eb *EvmBuilder[EXT, DB]) WithEnv(env *Env) *EvmBuilder[EXT, DB] {
eb.context.Evm.Inner.Env = env
return eb
}

func (eb *EvmBuilder[EXT, DB]) Build() Evm[EXT,DB] {
return NewEvm(eb.context, eb.handler)
}

func WithContextWithHandlerCfg[EXT interface{},DB1,DB2 Database](eb *EvmBuilder[EXT, DB1],contextWithHandlerCfg ContextWithHandlerCfg[EXT, DB2]) *EvmBuilder[EXT, DB2] {
return &EvmBuilder[EXT, DB2]{
context: contextWithHandlerCfg.Context,
handler: Handler[Context[EXT, DB2], EXT, DB2]{Cfg: contextWithHandlerCfg.Cfg},
phantom: struct{}{},
}
}
//Not in use
func (eb EvmBuilder[EXT, DB]) WithDB(db DB) *EvmBuilder[EXT, DB] {
return &EvmBuilder[EXT, DB]{
context: NewContext[EXT, DB](eb.context.Evm.WithDB(db), eb.context.External.(EXT)), //Doubt
handler: Handler[Context[EXT, DB], EXT, DB]{Cfg: eb.handler.Cfg}, //Doubt
phantom: struct{}{},
}
}
//Not in use
type PhantomData[T any] struct{}
//Not in use
/*
func WithContextWithHandlerCfg[EXT interface{}, DB Database](eb *EvmBuilder[EXT, Database], contextWithHandlerCfg ContextWithHandlerCfg[EXT, Database]) *EvmBuilder[EXT, Database] {
return &EvmBuilder[EXT, Database]{
context: contextWithHandlerCfg.Context,
handler: Handler[Context[EXT, Database], EXT, Database]{Cfg: contextWithHandlerCfg.Cfg},
phantom: struct{}{},
}
}*/
/*
func (eb EvmBuilder[EXT, DB]) WithContextWithHandlerCfg(contextWithHandlerCfg ContextWithHandlerCfg[EXT, DB]) *EvmBuilder[EXT, DB] {
return &EvmBuilder[EXT, DB]{
context: contextWithHandlerCfg.Context,
handler: Handler[Context[EXT, DB], EXT, DB]{Cfg: contextWithHandlerCfg.Cfg},
phantom: struct{}{},
}
}*/


// Tuple implementation in golang:

// LogData represents the data structure of a log entry.


20 changes: 20 additions & 0 deletions execution/evm/evm_context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package evm
type EvmContext[DB Database] struct {

Check failure on line 2 in execution/evm/evm_context.go

View workflow job for this annotation

GitHub Actions / test

undefined: Database

Check failure on line 2 in execution/evm/evm_context.go

View workflow job for this annotation

GitHub Actions / build (1.22.3)

undefined: Database

Check failure on line 2 in execution/evm/evm_context.go

View workflow job for this annotation

GitHub Actions / golangci-lint (/home/runner/work/selene/selene)

undefined: Database
Inner InnerEvmContext[DB]
Precompiles ContextPrecompiles[DB]
}
func NewEvmContext[DB Database](db DB) EvmContext[DB] {
return EvmContext[DB]{
Inner: NewInnerEvmContext[DB](db),
Precompiles: DefaultContextPrecompiles[DB](),
}
}
func WithNewEvmDB[DB1, DB2 Database](
ec EvmContext[DB1],
db DB2,
) EvmContext[DB2] {
return EvmContext[DB2]{
Inner: WithNewInnerDB(ec.Inner,db),
Precompiles: DefaultContextPrecompiles[DB2](),
}
}
Loading

0 comments on commit b89406a

Please sign in to comment.