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

spec updated #1985

Merged
merged 16 commits into from
Aug 8, 2024
Merged
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ config/
# Default path for Juno DB files. It will get created if you follow the
# README and/or run `./build/juno` command.
juno/
p2p-dbs
11 changes: 9 additions & 2 deletions adapters/core2p2p/class.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ func AdaptClass(class core.Class) *spec.Class {
return nil
}

hash, err := class.Hash()
if err != nil {
panic(fmt.Errorf("failed to hash %t: %w", class, err))
}

switch v := class.(type) {
case *core.Cairo0Class:
return &spec.Class{
Expand All @@ -25,7 +30,8 @@ func AdaptClass(class core.Class) *spec.Class {
Program: v.Program,
},
},
Domain: 0, // todo(kirill) recheck
Domain: 0, // todo(kirill) recheck
ClassHash: AdaptHash(hash),
}
case *core.Cairo1Class:
return &spec.Class{
Expand All @@ -41,7 +47,8 @@ func AdaptClass(class core.Class) *spec.Class {
ContractClassVersion: v.SemanticVersion,
},
},
Domain: 0, // todo(kirill) recheck
Domain: 0, // todo(kirill) recheck
ClassHash: AdaptHash(hash),
}
default:
panic(fmt.Errorf("unsupported cairo class %T (version=%d)", v, class.Version()))
Expand Down
34 changes: 21 additions & 13 deletions adapters/core2p2p/receipt.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,25 +101,33 @@ func AdaptExecutionResources(er *core.ExecutionResources) *spec.Receipt_Executio
return nil
}

var l1Gas, l1DataGas *spec.Felt252
var l1Gas, l1DataGas, totalL1Gas *felt.Felt
if da := er.DataAvailability; da != nil { // todo(kirill) check that it might be null
l1Gas = AdaptFelt(new(felt.Felt).SetUint64(da.L1Gas))
l1DataGas = AdaptFelt(new(felt.Felt).SetUint64(da.L1DataGas))
l1Gas = new(felt.Felt).SetUint64(da.L1Gas)
l1DataGas = new(felt.Felt).SetUint64(da.L1DataGas)
}
if tgs := er.TotalGasConsumed; tgs != nil {
totalL1Gas = new(felt.Felt).SetUint64(tgs.L1Gas)
}

return &spec.Receipt_ExecutionResources{
Builtins: &spec.Receipt_ExecutionResources_BuiltinCounter{
Bitwise: uint32(er.BuiltinInstanceCounter.Bitwise),
Ecdsa: uint32(er.BuiltinInstanceCounter.Ecsda),
EcOp: uint32(er.BuiltinInstanceCounter.EcOp),
Pedersen: uint32(er.BuiltinInstanceCounter.Pedersen),
RangeCheck: uint32(er.BuiltinInstanceCounter.RangeCheck),
Poseidon: uint32(er.BuiltinInstanceCounter.Poseidon),
Keccak: uint32(er.BuiltinInstanceCounter.Keccak),
Output: uint32(er.BuiltinInstanceCounter.Output),
Bitwise: uint32(er.BuiltinInstanceCounter.Bitwise),
Ecdsa: uint32(er.BuiltinInstanceCounter.Ecsda),
EcOp: uint32(er.BuiltinInstanceCounter.EcOp),
Pedersen: uint32(er.BuiltinInstanceCounter.Pedersen),
RangeCheck: uint32(er.BuiltinInstanceCounter.RangeCheck),
Poseidon: uint32(er.BuiltinInstanceCounter.Poseidon),
Keccak: uint32(er.BuiltinInstanceCounter.Keccak),
Output: uint32(er.BuiltinInstanceCounter.Output),
AddMod: uint32(er.BuiltinInstanceCounter.AddMod),
MulMod: uint32(er.BuiltinInstanceCounter.MulMod),
RangeCheck96: uint32(er.BuiltinInstanceCounter.RangeCheck96),
},
Steps: uint32(er.Steps),
MemoryHoles: uint32(er.MemoryHoles),
L1Gas: l1Gas,
L1DataGas: l1DataGas,
L1Gas: AdaptFelt(l1Gas),
L1DataGas: AdaptFelt(l1DataGas),
TotalL1Gas: AdaptFelt(totalL1Gas),
}
}
2 changes: 2 additions & 0 deletions adapters/core2p2p/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ func AdaptTransaction(transaction core.Transaction) *spec.Transaction {
specTx.Txn = adaptL1HandlerTransaction(tx)
}

specTx.TransactionHash = AdaptHash(transaction.Hash())

return &specTx
}

Expand Down
42 changes: 11 additions & 31 deletions adapters/p2p2core/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,12 @@ func AdaptTransaction(t *spec.Transaction, network *utils.Network) core.Transact
return nil
}

hash := func(tx core.Transaction) *felt.Felt {
h, err := core.TransactionHash(tx, network)
if err != nil {
panic(fmt.Errorf("failed to compute transaction hash: %w", err))
}

return h
}

// can Txn be nil?
switch t.Txn.(type) {
case *spec.Transaction_DeclareV0_:
tx := t.GetDeclareV0()
declareTx := &core.DeclareTransaction{
TransactionHash: nil, // overridden below
TransactionHash: AdaptHash(t.TransactionHash),
Nonce: nil, // for v0 nonce is not used for hash calculation
ClassHash: AdaptHash(tx.ClassHash),
SenderAddress: AdaptAddress(tx.Sender),
Expand All @@ -46,13 +37,12 @@ func AdaptTransaction(t *spec.Transaction, network *utils.Network) core.Transact
NonceDAMode: 0,
FeeDAMode: 0,
}
declareTx.TransactionHash = hash(declareTx)

return declareTx
case *spec.Transaction_DeclareV1_:
tx := t.GetDeclareV1()
declareTx := &core.DeclareTransaction{
TransactionHash: nil, // overridden below
TransactionHash: AdaptHash(t.TransactionHash),
ClassHash: AdaptHash(tx.ClassHash),
SenderAddress: AdaptAddress(tx.Sender),
MaxFee: AdaptFelt(tx.MaxFee),
Expand All @@ -69,13 +59,12 @@ func AdaptTransaction(t *spec.Transaction, network *utils.Network) core.Transact
NonceDAMode: 0,
FeeDAMode: 0,
}
declareTx.TransactionHash = hash(declareTx)

return declareTx
case *spec.Transaction_DeclareV2_:
tx := t.GetDeclareV2()
declareTx := &core.DeclareTransaction{
TransactionHash: nil, // overridden below
TransactionHash: AdaptHash(t.TransactionHash),
ClassHash: AdaptHash(tx.ClassHash),
SenderAddress: AdaptAddress(tx.Sender),
MaxFee: AdaptFelt(tx.MaxFee),
Expand All @@ -91,7 +80,6 @@ func AdaptTransaction(t *spec.Transaction, network *utils.Network) core.Transact
NonceDAMode: 0,
FeeDAMode: 0,
}
declareTx.TransactionHash = hash(declareTx)

return declareTx
case *spec.Transaction_DeclareV3_:
Expand All @@ -108,7 +96,7 @@ func AdaptTransaction(t *spec.Transaction, network *utils.Network) core.Transact
}

declareTx := &core.DeclareTransaction{
TransactionHash: nil, // overridden below
TransactionHash: AdaptHash(t.TransactionHash),
ClassHash: AdaptHash(tx.ClassHash),
SenderAddress: AdaptAddress(tx.Sender),
MaxFee: nil, // in 3 version this field was removed
Expand All @@ -126,7 +114,6 @@ func AdaptTransaction(t *spec.Transaction, network *utils.Network) core.Transact
NonceDAMode: nDAMode,
FeeDAMode: fDAMode,
}
declareTx.TransactionHash = hash(declareTx)

return declareTx
case *spec.Transaction_Deploy_:
Expand All @@ -136,14 +123,13 @@ func AdaptTransaction(t *spec.Transaction, network *utils.Network) core.Transact
classHash := AdaptHash(tx.ClassHash)
callData := utils.Map(tx.Calldata, AdaptFelt)
deployTx := &core.DeployTransaction{
TransactionHash: nil, // overridden below
TransactionHash: AdaptHash(t.TransactionHash),
ContractAddress: core.ContractAddress(&felt.Zero, classHash, addressSalt, callData),
ContractAddressSalt: addressSalt,
ClassHash: classHash,
ConstructorCallData: callData,
Version: txVersion(0),
}
deployTx.TransactionHash = hash(deployTx)

return deployTx
case *spec.Transaction_DeployAccountV1_:
Expand All @@ -154,7 +140,7 @@ func AdaptTransaction(t *spec.Transaction, network *utils.Network) core.Transact
callData := utils.Map(tx.Calldata, AdaptFelt)
deployAccTx := &core.DeployAccountTransaction{
DeployTransaction: core.DeployTransaction{
TransactionHash: nil, // overridden below
TransactionHash: AdaptHash(t.TransactionHash),
ContractAddressSalt: addressSalt,
ContractAddress: core.ContractAddress(&felt.Zero, classHash, addressSalt, callData),
ClassHash: classHash,
Expand All @@ -171,7 +157,6 @@ func AdaptTransaction(t *spec.Transaction, network *utils.Network) core.Transact
NonceDAMode: 0,
FeeDAMode: 0,
}
deployAccTx.DeployTransaction.TransactionHash = hash(deployAccTx)

return deployAccTx
case *spec.Transaction_DeployAccountV3_:
Expand All @@ -192,7 +177,7 @@ func AdaptTransaction(t *spec.Transaction, network *utils.Network) core.Transact
callData := utils.Map(tx.Calldata, AdaptFelt)
deployAccTx := &core.DeployAccountTransaction{
DeployTransaction: core.DeployTransaction{
TransactionHash: nil, // overridden below
TransactionHash: AdaptHash(t.TransactionHash),
ContractAddressSalt: addressSalt,
ContractAddress: core.ContractAddress(&felt.Zero, classHash, addressSalt, callData),
ClassHash: classHash,
Expand All @@ -211,13 +196,12 @@ func AdaptTransaction(t *spec.Transaction, network *utils.Network) core.Transact
NonceDAMode: nDAMode,
FeeDAMode: fDAMode,
}
deployAccTx.DeployTransaction.TransactionHash = hash(deployAccTx)

return deployAccTx
case *spec.Transaction_InvokeV0_:
tx := t.GetInvokeV0()
invTx := &core.InvokeTransaction{
TransactionHash: nil, // overridden below
TransactionHash: AdaptHash(t.TransactionHash),
CallData: utils.Map(tx.Calldata, AdaptFelt),
TransactionSignature: adaptAccountSignature(tx.Signature),
MaxFee: AdaptFelt(tx.MaxFee),
Expand All @@ -235,13 +219,12 @@ func AdaptTransaction(t *spec.Transaction, network *utils.Network) core.Transact
NonceDAMode: 0,
FeeDAMode: 0,
}
invTx.TransactionHash = hash(invTx)

return invTx
case *spec.Transaction_InvokeV1_:
tx := t.GetInvokeV1()
invTx := &core.InvokeTransaction{
TransactionHash: nil, // overridden below
TransactionHash: AdaptHash(t.TransactionHash),
ContractAddress: nil, // todo call core.ContractAddress() ?
Nonce: AdaptFelt(tx.Nonce),
SenderAddress: AdaptAddress(tx.Sender),
Expand All @@ -258,7 +241,6 @@ func AdaptTransaction(t *spec.Transaction, network *utils.Network) core.Transact
NonceDAMode: 0,
FeeDAMode: 0,
}
invTx.TransactionHash = hash(invTx)

return invTx
case *spec.Transaction_InvokeV3_:
Expand All @@ -275,7 +257,7 @@ func AdaptTransaction(t *spec.Transaction, network *utils.Network) core.Transact
}

invTx := &core.InvokeTransaction{
TransactionHash: nil, // overridden below
TransactionHash: AdaptHash(t.TransactionHash),
ContractAddress: nil, // todo call core.ContractAddress() ?
CallData: utils.Map(tx.Calldata, AdaptFelt),
TransactionSignature: adaptAccountSignature(tx.Signature),
Expand All @@ -294,20 +276,18 @@ func AdaptTransaction(t *spec.Transaction, network *utils.Network) core.Transact
FeeDAMode: fDAMode,
AccountDeploymentData: nil, // todo(kirill) recheck
}
invTx.TransactionHash = hash(invTx)

return invTx
case *spec.Transaction_L1Handler:
tx := t.GetL1Handler()
l1Tx := &core.L1HandlerTransaction{
TransactionHash: nil, // overridden below
TransactionHash: AdaptHash(t.TransactionHash),
ContractAddress: AdaptAddress(tx.Address),
EntryPointSelector: AdaptFelt(tx.EntryPointSelector),
Nonce: AdaptFelt(tx.Nonce),
CallData: utils.Map(tx.Calldata, AdaptFelt),
Version: txVersion(0),
}
l1Tx.TransactionHash = hash(l1Tx)

return l1Tx
default:
Expand Down
55 changes: 28 additions & 27 deletions p2p/starknet/p2p/proto/class.proto
Original file line number Diff line number Diff line change
Expand Up @@ -5,53 +5,54 @@ option go_package = "github.com/NethermindEth/juno/p2p/starknet/spec";


message EntryPoint {
Felt252 selector = 1;
uint64 offset = 2;
Felt252 selector = 1;
uint64 offset = 2;
}

message Cairo0Class {
string abi = 1;
repeated EntryPoint externals = 2;
repeated EntryPoint l1_handlers = 3;
repeated EntryPoint constructors = 4;
// Compressed in base64 representation.
string program = 5;
string abi = 1;
repeated EntryPoint externals = 2;
repeated EntryPoint l1_handlers = 3;
repeated EntryPoint constructors = 4;
// Compressed in base64 representation.
string program = 5;
}

message SierraEntryPoint {
uint64 index = 1;
Felt252 selector = 2;
uint64 index = 1;
Felt252 selector = 2;
}

message Cairo1EntryPoints {
repeated SierraEntryPoint externals = 1;
repeated SierraEntryPoint l1_handlers = 2;
repeated SierraEntryPoint constructors = 3;
repeated SierraEntryPoint externals = 1;
repeated SierraEntryPoint l1_handlers = 2;
repeated SierraEntryPoint constructors = 3;
}

message Cairo1Class {
string abi = 1;
Cairo1EntryPoints entry_points = 2;
repeated Felt252 program = 3;
string contract_class_version = 4;
string abi = 1;
Cairo1EntryPoints entry_points = 2;
repeated Felt252 program = 3;
string contract_class_version = 4;
}

message Class {
oneof class {
Cairo0Class cairo0 = 1;
Cairo1Class cairo1 = 2;
}
uint32 domain = 3;
oneof class {
Cairo0Class cairo0 = 1;
Cairo1Class cairo1 = 2;
}
uint32 domain = 3;
Hash class_hash = 4;
}

message ClassesRequest {
Iteration iteration = 1;
Iteration iteration = 1;
}

// Responses are sent ordered by the order given in the request.
message ClassesResponse {
oneof class_message {
Class class = 1;
Fin fin = 2; // Fin is sent after the peer sent all the data or when it encountered a block that it doesn't have its classes.
}
oneof class_message {
Class class = 1;
Fin fin = 2; // Fin is sent after the peer sent all the data or when it encountered a block that it doesn't have its classes.
}
}
2 changes: 1 addition & 1 deletion p2p/starknet/p2p/proto/common.proto
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ message ConsensusSignature {

message Patricia {
uint64 n_leaves = 1; // needed to know the height, so as to how many nodes to expect in a proof.
// and also when receiving all leaves, how many to expect
// and also when receiving all leaves, how many to expect
Hash root = 2;
}

Expand Down
Loading