Skip to content
Open
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
example/db
example/db
db
15 changes: 15 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch Package",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${fileDirname}"
}
]
}
2 changes: 1 addition & 1 deletion example/contract/example.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ contract Example {
function SetValB(int b) public {
valb = b;
}
}
}
2 changes: 1 addition & 1 deletion example/contract/example_sol_Example.abi
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[{"constant":true,"inputs":[],"name":"vala","outputs":[{"name":"","type":"int256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"b","type":"int256"}],"name":"SetValB","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"a","type":"int256"}],"name":"SetValA","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"valb","outputs":[{"name":"","type":"int256"}],"payable":false,"stateMutability":"view","type":"function"}]
[{"constant":true,"inputs":[],"name":"vala","outputs":[{"name":"","type":"int256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"b","type":"int256"}],"name":"SetValB","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"a","type":"int256"}],"name":"SetValA","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"valb","outputs":[{"name":"","type":"int256"}],"payable":false,"stateMutability":"view","type":"function"}]
35 changes: 27 additions & 8 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"fmt"
"math/big"

"github.com/cryptokass/levm"
"github.com/sledro/levm"

"github.com/cryptokass/levm/tools"
"github.com/sledro/levm/tools"
)

var (
Expand All @@ -19,21 +19,40 @@ func main() {
fromAddr = tools.NewRandomAddress()

//Load a contract from file
abiObject, binData, err := tools.LoadContract("contract/example_sol_Example.abi", "contract/example_sol_Example.bin")
fmt.Println("Abi\n", abiObject.Methods)
abiObject, binData, err := tools.LoadContract("./contract/example_sol_Example.abi", "./contract/example_sol_Example.bin")
if err != nil {
fmt.Println(err)
}
fmt.Println("Abi Methods: ", abiObject.Methods)

// create new LEVM instance
lvm := levm.New("./db", big.NewInt(0), fromAddr)
lvm := levm.New("./db", big.NewInt(8000000), fromAddr)

// create a new account and set the balance
// (needs enough balance to cover gas cost)
lvm.NewAccount(fromAddr, big.NewInt(1e18))
lvm.NewAccount(fromAddr, big.NewInt(5000000000000))

// deploy a contract
code, addr, gas, err := lvm.DeployContract(fromAddr, binData)
if err != nil {
fmt.Println(err)
}
fmt.Println("contract code length:", len(code))
fmt.Printf("contract address: %x\n", addr)
fmt.Println("unused gas:", gas)
fmt.Println("errors:", err)

fmt.Println("address:", lvm.GetAccount(fromAddr))

// call a contract: set
setOutput, err := lvm.CallContractABI(fromAddr, addr, big.NewInt(0), abiObject, "SetValA", big.NewInt(1))
if err != nil {
fmt.Println("set error : ", err)
}
fmt.Println("set output:", setOutput)

// call a contract: get
getOutput, err := lvm.CallContractABI(fromAddr, addr, big.NewInt(0), abiObject, "vala")
if err != nil {
fmt.Println("get error : ", err)
}
fmt.Println("get output:", getOutput)
}
10 changes: 10 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module github.com/sledro/levm

go 1.15

require (
github.com/CryptoKass/splashecdsa v0.0.0-20190106231736-c9d97441cf27 // indirect
github.com/CryptoKass/splashmerkle v0.0.0-20190705065406-87e8ae549b11 // indirect
github.com/cryptokass/splashecdsa v0.0.0-20190106231736-c9d97441cf27
github.com/ethereum/go-ethereum v1.10.15
)
621 changes: 621 additions & 0 deletions go.sum

Large diffs are not rendered by default.

19 changes: 10 additions & 9 deletions levm.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import (
"math/big"

"github.com/ethereum/go-ethereum/accounts/abi"

"github.com/ethereum/go-ethereum/params"

vmi "github.com/cryptokass/levm/vminterface"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/eth/tracers/logger"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/params"

vmi "github.com/sledro/levm/vminterface"
)

// LEVM is a container for the go-ethereum EVM
Expand Down Expand Up @@ -50,15 +50,16 @@ func (lvm *LEVM) NewEVM(blockNumber *big.Int, origin common.Address) {

// create contexted for the evm context
chainContext := vmi.NewChainContext(origin)
vmContext := vmi.NewVMContext(origin, origin, blockNumber, chainContext)
blockContext := vmi.NewBlockContext(origin, origin, blockNumber, chainContext)
txContext := vmi.NewTxContext(origin)

tcr := logger.NewStructLogger(&logger.Config{})

// create vm config
logConfig := vm.LogConfig{}
structLogger := vm.NewStructLogger(&logConfig)
vmConfig := vm.Config{Debug: true, Tracer: structLogger /*JumpTable: vm.NewByzantiumInstructionSet()*/}
vmConfig := vm.Config{Debug: true, Tracer: tcr, ExtraEips: []int{150, 1052, 1884}, NoBaseFee: true}

// create the evm
lvm.evm = vm.NewEVM(vmContext, lvm.stateDB, params.MainnetChainConfig, vmConfig)
lvm.evm = vm.NewEVM(blockContext, txContext, lvm.stateDB, params.MainnetChainConfig, vmConfig)
}

// DeployContract will create and deploy a new
Expand Down
8 changes: 5 additions & 3 deletions tools/addr.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package tools

import (
"github.com/CryptoKass/splashgo/splashkeys"
"crypto/elliptic"

"github.com/cryptokass/splashecdsa"
"github.com/ethereum/go-ethereum/common"
)

func NewRandomAddress() common.Address {
priv, _ := splashkeys.GenerateSplashKeys()
addrBytes := priv.GetAddress()
priv, _ := splashecdsa.GenerateKeys(elliptic.P256())
addrBytes := priv.GetAddress(true)
return common.BytesToAddress(addrBytes)
}
2 changes: 1 addition & 1 deletion tools/startdb.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package tools

import (
com "github.com/cryptokass/levm/common"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/trie"
com "github.com/sledro/levm/common"
)

func StartTrieDB(edb ethdb.Database) *trie.Trie {
Expand Down
6 changes: 2 additions & 4 deletions vminterface/vmcontext.go → vminterface/blockContext.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,11 @@ import (

// NewVMContext will construct a new EVM Context with default values.
// TODO: include gas price variable in params
func NewVMContext(origin common.Address, coinbase common.Address, blockNum *big.Int, chainContext ChainContext) vm.Context {
return vm.Context{
func NewBlockContext(origin common.Address, coinbase common.Address, blockNum *big.Int, chainContext ChainContext) vm.BlockContext {
return vm.BlockContext{
CanTransfer: CanTransfer,
Transfer: Transfer,
GetHash: core.GetHashFn(chainContext.GetHeader(sha256.Sum256([]byte("")), 0), chainContext),
Origin: origin,
GasPrice: big.NewInt(1),
Coinbase: coinbase,
GasLimit: uint64(1000000),
BlockNumber: blockNum,
Expand Down
6 changes: 3 additions & 3 deletions vminterface/state.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package vminterface

import (
com "github.com/cryptokass/levm/common"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/ethdb"
com "github.com/sledro/levm/common"
)

// NewStateDB - Create a new StateDB using levelDB instead of RAM
Expand All @@ -17,12 +17,12 @@ func NewStateDB(root common.Hash, dbPath string) (*state.StateDB, ethdb.Database
com.PanicErr(err)
*/

edb, _ := rawdb.NewLevelDBDatabase(dbPath, 100, 100, "")
edb, _ := rawdb.NewLevelDBDatabase(dbPath, 100, 100, "", false)
//edb := rawdb.NewMemoryDatabase()
db := state.NewDatabase(edb)

// make statedb
stateDB, err := state.New(root, db)
stateDB, err := state.New(root, db, nil)
com.PanicErr(err)

return stateDB, edb
Expand Down
17 changes: 17 additions & 0 deletions vminterface/txContext.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package vminterface

import (
"math/big"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/vm"
)

// NewVMContext will construct a new EVM Context with default values.
// TODO: include gas price variable in params
func NewTxContext(origin common.Address) vm.TxContext {
return vm.TxContext{
Origin: origin,
GasPrice: big.NewInt(1),
}
}