Skip to content

Commit

Permalink
implement shadow feature for juno sequencer
Browse files Browse the repository at this point in the history
  • Loading branch information
rianhughes committed Sep 23, 2024
1 parent aa241f1 commit 8441202
Show file tree
Hide file tree
Showing 56 changed files with 28,661 additions and 450 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ build/
coverage/*
config/
.envrc
seq-db/

# pyenv
.python-version

# Default path for Juno DB files. It will get created if you follow the
# README and/or run `./build/juno` command.
juno/
Expand Down
59 changes: 50 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
.PHONY: vm

ifeq ($(VM_DEBUG),true)
GO_TAGS = -tags vm_debug
GO_TAGS = -tags 'vm_debug,no_coverage'
VM_TARGET = debug
else
GO_TAGS =
GO_TAGS = -tags 'no_coverage'
VM_TARGET = all
endif

Expand Down Expand Up @@ -151,12 +151,53 @@ node3:
--p2p-private-key="54a695e2a5d5717d5ba8730efcafe6f17251a1955733cffc55a4085fbf7f5d2c1b4009314092069ef7ca9b364ce3eb3072531c64dfb2799c6bad76720a5bdff0" \
--metrics-port=9093

sequencer:
./build/juno \
--http \
--http-port=6060 \
--http-host=0.0.0.0 \
--db-path=./seq-db \
--log-level=debug \
--seq-enable \
--seq-block-time=1 \
--network sequencer \
--rpc-call-max-steps=4123000

sequencer-with-accounts:
./build/juno \
--http \
--http-port=6060 \
--http-host=0.0.0.0 \
--db-path=../seq-db \
--log-level=debug \
--seq-enable \
--seq-block-time=1 \
--network sequencer \
--seq-genesis-file "./genesis/genesis_prefund_accounts.json" \
--rpc-call-max-steps=4123000


sequencer-shadow-sepolia:
./build/juno \
--http \
--http-port=6066 \
--http-host=0.0.0.0 \
--db-path=../seq-db\
--log-level=info \
--seq-enable \
--seq-shadow-mode \
--seq-block-time=5 \
--seq-shadow-mode-sync-to=0 \
--seq-rpc-endpoint="" \
--network sepolia \
--rpc-call-max-steps=4123000

pathfinder: juno-cached
./build/juno \
--network=sepolia \
--log-level=debug \
--db-path=./p2p-dbs/node-pathfinder \
--p2p \
--p2p-peers=/ip4/127.0.0.1/tcp/8888/p2p/12D3KooWF1JrZWQoBiBSjsFSuLbDiDvqcmJQRLaFQLmpVkHA9duk \
--p2p-private-key="54a695e2a5d5717d5ba8730efcafe6f17251a1955733cffc55a4085fbf7f5d2c1b4009314092069ef7ca9b364ce3eb3072531c64dfb2799c6bad76720a5bdff0" \
--metrics-port=9094
--network=sepolia \
--log-level=debug \
--db-path=./p2p-dbs/node-pathfinder \
--p2p \
--p2p-peers=/ip4/127.0.0.1/tcp/8888/p2p/12D3KooWF1JrZWQoBiBSjsFSuLbDiDvqcmJQRLaFQLmpVkHA9duk \
--p2p-private-key="54a695e2a5d5717d5ba8730efcafe6f17251a1955733cffc55a4085fbf7f5d2c1b4009314092069ef7ca9b364ce3eb3072531c64dfb2799c6bad76720a5bdff0" \
--metrics-port=9094
104 changes: 102 additions & 2 deletions adapters/vm2core/vm2core.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import (
"slices"

"github.com/NethermindEth/juno/core"
"github.com/NethermindEth/juno/core/felt"
"github.com/NethermindEth/juno/utils"
"github.com/NethermindEth/juno/vm"
"github.com/ethereum/go-ethereum/common"
)

func AdaptExecutionResources(resources *vm.ExecutionResources) *core.ExecutionResources {
func AdaptExecutionResources(resources *vm.ExecutionResources, totalGas *vm.GasConsumed) *core.ExecutionResources {
return &core.ExecutionResources{
BuiltinInstanceCounter: core.BuiltinInstanceCounter{
Pedersen: resources.Pedersen,
Expand All @@ -29,7 +30,7 @@ func AdaptExecutionResources(resources *vm.ExecutionResources) *core.ExecutionRe
MemoryHoles: resources.MemoryHoles,
Steps: resources.Steps,
DataAvailability: adaptDA(resources.DataAvailability),
TotalGasConsumed: nil, // todo: fill after 0.13.2
TotalGasConsumed: &core.GasConsumed{L1Gas: totalGas.L1Gas, L1DataGas: totalGas.L1DataGas},
}
}

Expand Down Expand Up @@ -73,3 +74,102 @@ func adaptDA(da *vm.DataAvailability) *core.DataAvailability {
L1DataGas: da.L1DataGas,
}
}

func AdaptStateDiff(sd *vm.StateDiff) *core.StateDiff {
result := core.StateDiff{
StorageDiffs: make(map[felt.Felt]map[felt.Felt]*felt.Felt),
Nonces: make(map[felt.Felt]*felt.Felt),
DeployedContracts: make(map[felt.Felt]*felt.Felt),
DeclaredV0Classes: []*felt.Felt{},
DeclaredV1Classes: make(map[felt.Felt]*felt.Felt),
ReplacedClasses: make(map[felt.Felt]*felt.Felt),

Check warning on line 85 in adapters/vm2core/vm2core.go

View check run for this annotation

Codecov / codecov/patch

adapters/vm2core/vm2core.go#L78-L85

Added lines #L78 - L85 were not covered by tests
}
if sd == nil {
return &result

Check warning on line 88 in adapters/vm2core/vm2core.go

View check run for this annotation

Codecov / codecov/patch

adapters/vm2core/vm2core.go#L87-L88

Added lines #L87 - L88 were not covered by tests
}
for _, entries := range sd.StorageDiffs {
KeyVals := map[felt.Felt]*felt.Felt{}
for _, entry := range entries.StorageEntries {
KeyVals[entry.Key] = &entry.Value

Check warning on line 93 in adapters/vm2core/vm2core.go

View check run for this annotation

Codecov / codecov/patch

adapters/vm2core/vm2core.go#L90-L93

Added lines #L90 - L93 were not covered by tests
}
result.StorageDiffs[entries.Address] = KeyVals

Check warning on line 95 in adapters/vm2core/vm2core.go

View check run for this annotation

Codecov / codecov/patch

adapters/vm2core/vm2core.go#L95

Added line #L95 was not covered by tests
}
for _, addrNonce := range sd.Nonces {
result.Nonces[addrNonce.ContractAddress] = &addrNonce.Nonce

Check warning on line 98 in adapters/vm2core/vm2core.go

View check run for this annotation

Codecov / codecov/patch

adapters/vm2core/vm2core.go#L97-L98

Added lines #L97 - L98 were not covered by tests
}
for _, addrClassHash := range sd.DeployedContracts {
result.Nonces[addrClassHash.Address] = &addrClassHash.ClassHash

Check warning on line 101 in adapters/vm2core/vm2core.go

View check run for this annotation

Codecov / codecov/patch

adapters/vm2core/vm2core.go#L100-L101

Added lines #L100 - L101 were not covered by tests
}
for _, hashes := range sd.DeclaredClasses {
result.DeclaredV1Classes[hashes.ClassHash] = &hashes.CompiledClassHash

Check warning on line 104 in adapters/vm2core/vm2core.go

View check run for this annotation

Codecov / codecov/patch

adapters/vm2core/vm2core.go#L103-L104

Added lines #L103 - L104 were not covered by tests
}
for _, addrClassHash := range sd.ReplacedClasses {
result.ReplacedClasses[addrClassHash.ClassHash] = &addrClassHash.ClassHash

Check warning on line 107 in adapters/vm2core/vm2core.go

View check run for this annotation

Codecov / codecov/patch

adapters/vm2core/vm2core.go#L106-L107

Added lines #L106 - L107 were not covered by tests
}
result.DeclaredV0Classes = append(result.DeclaredV0Classes, sd.DeprecatedDeclaredClasses...)
return &result

Check warning on line 110 in adapters/vm2core/vm2core.go

View check run for this annotation

Codecov / codecov/patch

adapters/vm2core/vm2core.go#L109-L110

Added lines #L109 - L110 were not covered by tests
}

func StateDiff(trace *vm.TransactionTrace) *core.StateDiff {
if trace.StateDiff == nil {
return nil

Check warning on line 115 in adapters/vm2core/vm2core.go

View check run for this annotation

Codecov / codecov/patch

adapters/vm2core/vm2core.go#L115

Added line #L115 was not covered by tests
}
stateDiff := trace.StateDiff
newStorageDiffs := make(map[felt.Felt]map[felt.Felt]*felt.Felt)
for _, sd := range stateDiff.StorageDiffs {
entries := make(map[felt.Felt]*felt.Felt)
for _, entry := range sd.StorageEntries {
val := entry.Value
entries[entry.Key] = &val
}
newStorageDiffs[sd.Address] = entries
}

newNonces := make(map[felt.Felt]*felt.Felt)
for _, nonce := range stateDiff.Nonces {
nonc := nonce.Nonce
newNonces[nonce.ContractAddress] = &nonc
}

newDeployedContracts := make(map[felt.Felt]*felt.Felt)
for _, dc := range stateDiff.DeployedContracts {
ch := dc.ClassHash
newDeployedContracts[dc.Address] = &ch
}

newDeclaredV1Classes := make(map[felt.Felt]*felt.Felt)
for _, dc := range stateDiff.DeclaredClasses {
cch := dc.CompiledClassHash
newDeclaredV1Classes[dc.ClassHash] = &cch
}

newReplacedClasses := make(map[felt.Felt]*felt.Felt)
for _, rc := range stateDiff.ReplacedClasses {
ch := rc.ClassHash
newReplacedClasses[rc.ContractAddress] = &ch
}

return &core.StateDiff{
StorageDiffs: newStorageDiffs,
Nonces: newNonces,
DeployedContracts: newDeployedContracts,
DeclaredV0Classes: stateDiff.DeprecatedDeclaredClasses,
DeclaredV1Classes: newDeclaredV1Classes,
ReplacedClasses: newReplacedClasses,
}
}

func Receipt(fee *felt.Felt, feeUnit core.FeeUnit, txHash *felt.Felt,
trace *vm.TransactionTrace, txnReceipt *vm.TransactionReceipt,
) *core.TransactionReceipt {
return &core.TransactionReceipt{ //nolint:exhaustruct
Fee: fee,
FeeUnit: feeUnit,
Events: AdaptOrderedEvents(trace.AllEvents()),
ExecutionResources: AdaptExecutionResources(trace.TotalExecutionResources(), &txnReceipt.Gas),
L2ToL1Message: AdaptOrderedMessagesToL1(trace.AllMessages()),
TransactionHash: txHash,
Reverted: trace.IsReverted(),
RevertReason: trace.RevertReason(),
}
}
77 changes: 74 additions & 3 deletions adapters/vm2core/vm2core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,9 @@ func TestAdaptExecutionResources(t *testing.T) {
SegmentArena: 8,
Output: 11,
},
MemoryHoles: 9,
Steps: 10,
MemoryHoles: 9,
Steps: 10,
TotalGasConsumed: &core.GasConsumed{L1Gas: 1, L1DataGas: 2},
}, vm2core.AdaptExecutionResources(&vm.ExecutionResources{
ComputationResources: vm.ComputationResources{
Pedersen: 1,
Expand All @@ -98,5 +99,75 @@ func TestAdaptExecutionResources(t *testing.T) {
Steps: 10,
Output: 11,
},
}))
}, &vm.GasConsumed{L1Gas: 1, L1DataGas: 2}))
}

func TestStateDiff(t *testing.T) {
address1 := *new(felt.Felt).SetUint64(1)
key1 := *new(felt.Felt).SetUint64(2)
value1 := *new(felt.Felt).SetUint64(3)
nonce1 := *new(felt.Felt).SetUint64(4)
classHash1 := *new(felt.Felt).SetUint64(5)
compiledClassHash1 := *new(felt.Felt).SetUint64(6)
trace := &vm.TransactionTrace{
StateDiff: &vm.StateDiff{
StorageDiffs: []vm.StorageDiff{
{
Address: address1,
StorageEntries: []vm.Entry{
{Key: key1, Value: value1},
},
},
},
Nonces: []vm.Nonce{{ContractAddress: address1, Nonce: nonce1}},
DeployedContracts: []vm.DeployedContract{{Address: address1, ClassHash: classHash1}},
DeclaredClasses: []vm.DeclaredClass{{ClassHash: classHash1, CompiledClassHash: compiledClassHash1}},
ReplacedClasses: []vm.ReplacedClass{{ContractAddress: address1, ClassHash: classHash1}},
DeprecatedDeclaredClasses: []*felt.Felt{&classHash1},
},
}

expected := &core.StateDiff{
StorageDiffs: map[felt.Felt]map[felt.Felt]*felt.Felt{
address1: {key1: &value1},
},
Nonces: map[felt.Felt]*felt.Felt{address1: &nonce1},
DeployedContracts: map[felt.Felt]*felt.Felt{address1: &classHash1},
DeclaredV0Classes: []*felt.Felt{&classHash1},
DeclaredV1Classes: map[felt.Felt]*felt.Felt{classHash1: &compiledClassHash1},
ReplacedClasses: map[felt.Felt]*felt.Felt{address1: &classHash1},
}
require.Equal(t, expected, vm2core.StateDiff(trace))
}

func TestReceipt(t *testing.T) {
fee := new(felt.Felt).SetUint64(1)
feeUnit := core.WEI
txHash := new(felt.Felt).SetUint64(2)
trace := &vm.TransactionTrace{
Type: vm.TxnInvoke,
ValidateInvocation: &vm.FunctionInvocation{},
ExecuteInvocation: &vm.ExecuteInvocation{},
FeeTransferInvocation: &vm.FunctionInvocation{},
ConstructorInvocation: &vm.FunctionInvocation{},
FunctionInvocation: &vm.FunctionInvocation{},
StateDiff: &vm.StateDiff{},
ExecutionResources: &vm.ExecutionResources{},
}
txnReceipt := &vm.TransactionReceipt{
Fee: fee,
Gas: vm.GasConsumed{L1Gas: 1, L1DataGas: 2},
DAGas: vm.DataAvailability{L1Gas: 1, L1DataGas: 2},
}
expectedReceipt := &core.TransactionReceipt{
Fee: fee,
FeeUnit: feeUnit,
Events: vm2core.AdaptOrderedEvents(trace.AllEvents()),
ExecutionResources: vm2core.AdaptExecutionResources(trace.TotalExecutionResources(), &txnReceipt.Gas),
L2ToL1Message: vm2core.AdaptOrderedMessagesToL1(trace.AllMessages()),
TransactionHash: txHash,
Reverted: trace.IsReverted(),
RevertReason: trace.RevertReason(),
}
require.Equal(t, expectedReceipt, vm2core.Receipt(fee, feeUnit, txHash, trace, txnReceipt))
}
Loading

0 comments on commit 8441202

Please sign in to comment.