Skip to content
This repository has been archived by the owner on Aug 31, 2024. It is now read-only.

Commit

Permalink
erc20 (#13)
Browse files Browse the repository at this point in the history
* erc20

* fix

* check to juno sequecner branch

* use stateReaderWriter

* fix json files

* fix deployAccount
  • Loading branch information
Lawliet-Chan authored Jan 30, 2024
1 parent 6bb757a commit 80fa196
Show file tree
Hide file tree
Showing 7 changed files with 12,972 additions and 64 deletions.
34 changes: 19 additions & 15 deletions cairoVM/cairo.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

"github.com/NethermindEth/juno/core"
"github.com/NethermindEth/juno/core/felt"
"github.com/NethermindEth/juno/db/pebble"
"github.com/NethermindEth/juno/rpc"
"github.com/NethermindEth/juno/utils"
"github.com/NethermindEth/juno/vm"
Expand All @@ -28,16 +27,20 @@ func NewCairoVM(cfg *Config) (*Cairo, error) {
if err != nil {
return nil, err
}
db, err := pebble.NewMem()
if err != nil {
return nil, err
}
txn, err := db.NewTransaction(true)
if err != nil {
return nil, err
}
state := core.NewState(txn)
err = SetGenesis(state, "data/genesis/NoValidateAccount.sierra.json", "data/genesis/NoValidateAccount.casm.json")
//db, err := pebble.NewMem()
//if err != nil {
// return nil, err
//}
//txn, err := db.NewTransaction(true)
//if err != nil {
// return nil, err
//}
//state := core.NewState(txn)
//cairoFiles := make(map[string]string)
//cairoFiles["data/genesis/NoValidateAccount.sierra.json"] = "data/genesis/NoValidateAccount.casm.json"
//cairoFiles["data/genesis/erc20.sierra.json"] = "data/genesis/erc20.casm.json"

state, err := BuildGenesis([]string{"data/genesis/NoValidateAccount.sierra.json", "data/genesis/erc20.sierra.json"})
if err != nil {
return nil, err
}
Expand All @@ -55,7 +58,7 @@ func (c *Cairo) HandleCall(call *rpc.FunctionCall, classHash *felt.Felt) ([]*fel
return c.vm.Call(&call.ContractAddress, classHash, &call.EntryPointSelector, call.Calldata, 0, uint64(time.Now().Unix()), c.state, c.cfg.Network)
}

func (c *Cairo) DeployAccount(classHash, contractAddr *felt.Felt) (*felt.Felt, error) {
func (c *Cairo) DeployAccount(classHash, contractAddr *felt.Felt) (*vm.TransactionTrace, error) {
tx := &core.DeployAccountTransaction{
DeployTransaction: core.DeployTransaction{
ContractAddressSalt: c.acc.pubkey,
Expand All @@ -73,7 +76,8 @@ func (c *Cairo) DeployAccount(classHash, contractAddr *felt.Felt) (*felt.Felt, e
return c.HandleDeployAccountTx(tx)
}

func (c *Cairo) HandleDeployAccountTx(tx *core.DeployAccountTransaction) (*felt.Felt, error) {
func (c *Cairo) HandleDeployAccountTx(tx *core.DeployAccountTransaction) (*vm.TransactionTrace, error) {
fmt.Println("------------- DeployAccount TX -------------")
txnHash, err := core.TransactionHash(tx, c.cfg.Network)
if err != nil {
return nil, err
Expand All @@ -90,11 +94,11 @@ func (c *Cairo) HandleDeployAccountTx(tx *core.DeployAccountTransaction) (*felt.
if err != nil {
return nil, err
}
return &traces[0].ConstructorInvocation.CallerAddress, nil
return &traces[0], nil
}

func (c *Cairo) HandleDeclareTx(tx *core.DeclareTransaction, class core.Class) (*vm.TransactionTrace, error) {
fmt.Println(" Declare TX !!!")
fmt.Println("------------- Declare TX -------------")
txnHash, err := core.TransactionHash(tx, c.cfg.Network)
if err != nil {
return nil, err
Expand Down
13 changes: 11 additions & 2 deletions cairoVM/cmd/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"github.com/NethermindEth/juno/core/felt"
"github.com/davecgh/go-spew/spew"
"testbot/cairoVM"
// "github.com/NethermindEth/juno/core"
Expand All @@ -12,18 +13,26 @@ func main() {
panic(err)
}

// deployAccount TX
trace, err := vm.DeployAccount(cairoVM.AccountClassHash, &felt.Zero)
if err != nil {
panic(err)
}
spew.Dump(trace)

// declare TX
declareTx, class, err := cairoVM.NewDeclare(
"data/cool_sierra_contract_class.json",
"data/cool_compiled_class.casm",
)
if err != nil {
panic(err)
}
// fmt.Println(declare_tx)

trace, err := vm.HandleDeclareTx(declareTx, class)
trace, err = vm.HandleDeclareTx(declareTx, class)
if err != nil {
panic(err)
}
spew.Dump(trace)

}
46 changes: 0 additions & 46 deletions cairoVM/declare.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cairoVM
import (
"encoding/json"
"fmt"
"github.com/NethermindEth/juno/blockchain"
"github.com/NethermindEth/juno/core"
"github.com/NethermindEth/juno/core/felt"
"github.com/NethermindEth/starknet.go/contracts"
Expand All @@ -12,51 +11,6 @@ import (
"os"
)

func SetGenesis(state *core.State, sierraFileName, casmFileName string) error {

blockchain.RegisterCoreTypesToEncoder()

declaredClasses := make(map[felt.Felt]core.Class)
deployedContracts := make(map[felt.Felt]*felt.Felt)
declaredV1Classes := make(map[felt.Felt]*felt.Felt)
var (
class core.Class
classHash *felt.Felt
compiledClassHash *felt.Felt
)

class, classHash, err := adaptClassAndHash(sierraFileName)
if err != nil {
return err
}
fmt.Println("genesis classHash = ", classHash.String())
declaredClasses[*classHash] = class
deployedContracts[felt.Zero] = classHash
casmClass, err := contracts.UnmarshalCasmClass(casmFileName)
if err != nil {
return err
}
compiledClassHash = hash.CompiledClassHash(*casmClass)

declaredV1Classes[*classHash] = compiledClassHash

newRoot, err := new(felt.Felt).SetString("0x42e2546d91d85d60f1ee5ac114884831953c61f0b141a101ceae8efba9eda3a")
if err != nil {
return err
}

return state.Update(0, &core.StateUpdate{
BlockHash: &felt.Zero,
NewRoot: newRoot,
OldRoot: &felt.Zero,
StateDiff: &core.StateDiff{
Nonces: map[felt.Felt]*felt.Felt{felt.Zero: &felt.Zero},
DeployedContracts: deployedContracts,
DeclaredV1Classes: declaredV1Classes,
},
}, declaredClasses)
}

func NewDeclare(sierraFileName, casmFileName string) (*core.DeclareTransaction, core.Class, error) {
// ref to https://github.com/NethermindEth/starknet.go/blob/915109ab5bc1c9c5bae7a71553a96e6665c0dcb2/account/account_test.go#L1116

Expand Down
139 changes: 139 additions & 0 deletions cairoVM/genesis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
package cairoVM

import (
"encoding/json"
"fmt"
"github.com/NethermindEth/juno/adapters/sn2core"
"github.com/NethermindEth/juno/blockchain"
"github.com/NethermindEth/juno/core"
"github.com/NethermindEth/juno/core/felt"
"github.com/NethermindEth/juno/db"
"github.com/NethermindEth/juno/starknet"
"os"
)

func init() {
blockchain.RegisterCoreTypesToEncoder()
}

//func SetGenesis(state *core.State, cairoFiles map[string]string) error {
//
// declaredClasses := make(map[felt.Felt]core.Class)
// deployedContracts := make(map[felt.Felt]*felt.Felt)
// declaredV1Classes := make(map[felt.Felt]*felt.Felt)
// nonces := make(map[felt.Felt]*felt.Felt)
// var (
// class core.Class
// classHash *felt.Felt
// compiledClassHash *felt.Felt
// err error
// )
//
// var addr uint64 = 0
//
// for sierraFileName, casmFileName := range cairoFiles {
// class, classHash, err = adaptClassAndHash(sierraFileName)
// if err != nil {
// return err
// }
// fmt.Println("genesis classHash = ", classHash.String())
// declaredClasses[*classHash] = class
// addrFelt := new(felt.Felt).SetUint64(addr)
// deployedContracts[*addrFelt] = classHash
// casmClass, err := contracts.UnmarshalCasmClass(casmFileName)
// if err != nil {
// return err
// }
// compiledClassHash = hash.CompiledClassHash(*casmClass)
//
// declaredV1Classes[*classHash] = compiledClassHash
//
// nonces[*addrFelt] = &felt.Zero
// addr++
// }
//
// newRoot, err := new(felt.Felt).SetString("0x56f007b0f69daa75af325ecfa0d717bfd4d72bfa102151912fe4a15b9dfd30f")
// if err != nil {
// return err
// }
//
// return state.Update(0, core.EmptyStateDiff(), declaredClasses)
//}

var AccountClassHash *felt.Felt

func BuildGenesis(classesPaths []string) (*blockchain.PendingStateWriter, error) {
classes, err := loadClasses(classesPaths)
if err != nil {
return nil, err
}
genesisState := blockchain.NewPendingStateWriter(core.EmptyStateDiff(), make(map[felt.Felt]core.Class), core.NewState(db.NewMemTransaction()))

var addr uint64
for classHash, class := range classes {
// Sets pending.newClasses, DeclaredV0Classes, (not DeclaredV1Classes)
if err = genesisState.SetContractClass(&classHash, class); err != nil {
return nil, fmt.Errorf("declare class: %v", err)
}

if cairo1Class, isCairo1 := class.(*core.Cairo1Class); isCairo1 {
if err = genesisState.SetCompiledClassHash(&classHash, cairo1Class.Compiled.Hash()); err != nil {
return nil, fmt.Errorf("set compiled class hash: %v", err)
}
addrFelt := new(felt.Felt).SetUint64(addr)
err = genesisState.SetClassHash(addrFelt, &classHash)
if err != nil {
return nil, err
}
addr++
}
}

//for addrFelt, classHash := range contractsData {
// err = genesisState.SetClassHash(&addrFelt, &classHash)
// if err != nil {
// return nil, err
// }
//}

//stateDiff, newClasses := genesisState.StateDiffAndClasses()
//return state.Update(0, stateDiff, newClasses)
return genesisState, nil
}

// return map[classHash]Class
func loadClasses(classes []string) (map[felt.Felt]core.Class, error) {
classMap := make(map[felt.Felt]core.Class)
for i, classPath := range classes {
bytes, err := os.ReadFile(classPath)
if err != nil {
return nil, fmt.Errorf("read class file: %v", err)
}

var response *starknet.ClassDefinition
if err = json.Unmarshal(bytes, &response); err != nil {
return nil, fmt.Errorf("unmarshal class: %v", err)
}

var coreClass core.Class
if response.V0 != nil {
if coreClass, err = sn2core.AdaptCairo0Class(response.V0); err != nil {
return nil, err
}
} else if compiledClass, cErr := starknet.Compile(response.V1); cErr != nil {
return nil, cErr
} else if coreClass, err = sn2core.AdaptCairo1Class(response.V1, compiledClass); err != nil {
return nil, err
}

classhash, err := coreClass.Hash()
if err != nil {
return nil, fmt.Errorf("calculate class hash: %v", err)
}
if i == 0 {
AccountClassHash = classhash
}
classMap[*classhash] = coreClass
}
return classMap, nil
}
2 changes: 1 addition & 1 deletion cairoVM/juno
Submodule juno updated 56 files
+ .github/Juno_Dark_Powered_by_Nethermind.png
+ .github/Juno_Light.png
+1 −1 CONTRIBUTING.md
+5 −5 README.md
+143 −36 blockchain/blockchain.go
+278 −43 blockchain/blockchain_test.go
+74 −0 blockchain/pending.go
+135 −0 blockchain/pending_test.go
+297 −0 builder/builder.go
+330 −0 builder/builder_test.go
+17 −0 builder/event_listener.go
+17 −0 clients/gateway/event_listener.go
+22 −12 clients/gateway/gateway.go
+4 −3 clients/gateway/gateway_test.go
+14 −1 cmd/juno/juno.go
+5 −0 core/block.go
+7 −3 core/felt/felt.go
+12 −40 core/state.go
+44 −59 core/state_test.go
+113 −0 docs/versioned_docs/version-0.9.2/example_config.md
+54 −0 docs/versioned_docs/version-0.9.2/intro.md
+64 −0 docs/versioned_docs/version-0.9.2/snapshots.md
+8 −0 docs/versioned_sidebars/version-0.9.2-sidebars.json
+1 −0 docs/versions.json
+149 −0 genesis/genesis.go
+101 −0 genesis/genesis_test.go
+1 −0 genesis/testdata/simpleAccount.json
+5,980 −0 genesis/testdata/simpleStore.json
+1 −0 genesis/testdata/universalDeployer.json
+2 −2 l1/l1_pkg_test.go
+3 −3 l1/l1_test.go
+231 −0 mempool/mempool.go
+120 −0 mempool/mempool_test.go
+8 −3 migration/migration.go
+19 −2 migration/migration_pkg_test.go
+15 −0 mocks/mock_blockchain.go
+5 −4 mocks/mock_gateway_handler.go
+30 −0 mocks/mock_state.go
+39 −0 node/genesis.go
+5 −4 node/http.go
+16 −0 node/metrics.go
+96 −46 node/node.go
+1 −1 node/node_test.go
+110 −54 rpc/handlers.go
+161 −106 rpc/handlers_test.go
+8 −8 sync/sync_test.go
+15 −0 utils/clone.go
+1 −1 vm/rust/Cargo.toml
+6 −9 vm/rust/src/jsonrpc.rs
+105 −7 vm/rust/src/juno_state.rs
+40 −11 vm/rust/src/lib.rs
+64 −0 vm/state.go
+23 −3 vm/trace.go
+94 −22 vm/trace_test.go
+59 −27 vm/vm.go
+14 −30 vm/vm_test.go
Loading

0 comments on commit 80fa196

Please sign in to comment.