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

implement shadow feature for juno sequencer #2173

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
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
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
58 changes: 56 additions & 2 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 @@ -157,6 +157,60 @@ 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-plugin:
./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 \
--plugin-path="myplugin.so" \
--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 \
Expand Down
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 @@
"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 @@
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 @@
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),
}
if sd == nil {
return &result
}
for _, entries := range sd.StorageDiffs {
KeyVals := map[felt.Felt]*felt.Felt{}
for _, entry := range entries.StorageEntries {
KeyVals[entry.Key] = &entry.Value
}
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#L78-L95

Added lines #L78 - L95 were not covered by tests
}
for _, addrNonce := range sd.Nonces {
result.Nonces[addrNonce.ContractAddress] = &addrNonce.Nonce
}
for _, addrClassHash := range sd.DeployedContracts {
result.Nonces[addrClassHash.Address] = &addrClassHash.ClassHash
}
for _, hashes := range sd.DeclaredClasses {
result.DeclaredV1Classes[hashes.ClassHash] = &hashes.CompiledClassHash
}
for _, addrClassHash := range sd.ReplacedClasses {
result.ReplacedClasses[addrClassHash.ClassHash] = &addrClassHash.ClassHash
}
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#L97-L110

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

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

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

View check run for this annotation

Codecov / codecov/patch

adapters/vm2core/vm2core.go#L115-L116

Added lines #L115 - L116 were 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
Loading