Skip to content

Commit

Permalink
best guesses at feeder types
Browse files Browse the repository at this point in the history
  • Loading branch information
joshklop committed Nov 15, 2023
1 parent 27ad625 commit 619d9f3
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 41 deletions.
74 changes: 52 additions & 22 deletions adapters/sn2core/sn2core.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ func AdaptBlock(response *starknet.Block, sig *starknet.Signature) (*core.Block,
TransactionCount: uint64(len(response.Transactions)),
EventCount: eventCount,
EventsBloom: core.EventsBloom(receipts),
GasPrice: response.GasPrice,
GasPrice: response.GasPrice(),
GasPriceSTRK: response.GasPriceSTRK,
Signatures: sigs,
},
Transactions: txns,
Expand Down Expand Up @@ -156,17 +157,34 @@ func AdaptTransaction(transaction *starknet.Transaction) (core.Transaction, erro

func AdaptDeclareTransaction(t *starknet.Transaction) *core.DeclareTransaction {
return &core.DeclareTransaction{
TransactionHash: t.Hash,
SenderAddress: t.SenderAddress,
MaxFee: t.MaxFee,
TransactionSignature: *t.Signature,
Nonce: t.Nonce,
Version: (*core.TransactionVersion)(t.Version),
ClassHash: t.ClassHash,
CompiledClassHash: t.CompiledClassHash,
TransactionHash: t.Hash,
SenderAddress: t.SenderAddress,
MaxFee: t.MaxFee,
TransactionSignature: *t.Signature,
Nonce: t.Nonce,
Version: (*core.TransactionVersion)(t.Version),
ClassHash: t.ClassHash,
CompiledClassHash: t.CompiledClassHash,
ResourceBounds: adaptResourceBounds(t.ResourceBounds),
Tip: t.Tip,
PaymasterData: t.PaymasterData,
AccountDeploymentData: t.AccountDeploymentData,
NonceDAMode: core.DataAvailabilityMode(t.NonceDAMode),
FeeDAMode: core.DataAvailabilityMode(t.FeeDAMode),
}
}

func adaptResourceBounds(rb map[starknet.Resource]*starknet.ResourceBounds) map[core.Resource]*core.ResourceBounds {
coreBounds := make(map[core.Resource]*core.ResourceBounds, len(rb))
for resource, bounds := range rb {
coreBounds[core.Resource(resource)] = &core.ResourceBounds{
MaxAmount: bounds.MaxAmount,
MaxPricePerUnit: bounds.MaxPricePerUnit,
}
}
return coreBounds
}

func AdaptDeployTransaction(t *starknet.Transaction) *core.DeployTransaction {
if t.ContractAddress == nil {
t.ContractAddress = core.ContractAddress(&felt.Zero, t.ClassHash, t.ContractAddressSalt, *t.ConstructorCallData)
Expand All @@ -183,15 +201,21 @@ func AdaptDeployTransaction(t *starknet.Transaction) *core.DeployTransaction {

func AdaptInvokeTransaction(t *starknet.Transaction) *core.InvokeTransaction {
return &core.InvokeTransaction{
TransactionHash: t.Hash,
ContractAddress: t.ContractAddress,
EntryPointSelector: t.EntryPointSelector,
Nonce: t.Nonce,
CallData: *t.CallData,
TransactionSignature: *t.Signature,
MaxFee: t.MaxFee,
Version: (*core.TransactionVersion)(t.Version),
SenderAddress: t.SenderAddress,
TransactionHash: t.Hash,
ContractAddress: t.ContractAddress,
EntryPointSelector: t.EntryPointSelector,
Nonce: t.Nonce,
CallData: *t.CallData,
TransactionSignature: *t.Signature,
MaxFee: t.MaxFee,
Version: (*core.TransactionVersion)(t.Version),
SenderAddress: t.SenderAddress,
ResourceBounds: adaptResourceBounds(t.ResourceBounds),
Tip: t.Tip,
PaymasterData: t.PaymasterData,
AccountDeploymentData: t.AccountDeploymentData,
NonceDAMode: core.DataAvailabilityMode(t.NonceDAMode),
FeeDAMode: core.DataAvailabilityMode(t.FeeDAMode),
}
}

Expand All @@ -208,10 +232,16 @@ func AdaptL1HandlerTransaction(t *starknet.Transaction) *core.L1HandlerTransacti

func AdaptDeployAccountTransaction(t *starknet.Transaction) *core.DeployAccountTransaction {
return &core.DeployAccountTransaction{
DeployTransaction: *AdaptDeployTransaction(t),
MaxFee: t.MaxFee,
TransactionSignature: *t.Signature,
Nonce: t.Nonce,
DeployTransaction: *AdaptDeployTransaction(t),
MaxFee: t.MaxFee,
TransactionSignature: *t.Signature,
Nonce: t.Nonce,
ResourceBounds: adaptResourceBounds(t.ResourceBounds),
Tip: t.Tip,
PaymasterData: t.PaymasterData,
AccountDeploymentData: t.AccountDeploymentData,
NonceDAMode: core.DataAvailabilityMode(t.NonceDAMode),
FeeDAMode: core.DataAvailabilityMode(t.FeeDAMode),
}
}

Expand Down
4 changes: 2 additions & 2 deletions clients/feeder/feeder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func TestBlockWithoutSequencerAddressUnmarshal(t *testing.T) {
assert.Equal(t, uint64(11817), block.Number)
assert.Equal(t, "0x3df24be7b5fed6b41de08d38686b6142944119ca2a345c38793590d6804bba4", block.StateRoot.String())
assert.Equal(t, "ACCEPTED_ON_L2", block.Status)
assert.Equal(t, "0x27ad16775", block.GasPrice.String())
assert.Equal(t, "0x27ad16775", block.GasPrice().String())
assert.Equal(t, 52, len(block.Transactions))
assert.Equal(t, 52, len(block.Receipts))
assert.Equal(t, uint64(1669465009), block.Timestamp)
Expand All @@ -161,7 +161,7 @@ func TestBlockWithSequencerAddressUnmarshal(t *testing.T) {
assert.Equal(t, uint64(19199), block.Number)
assert.Equal(t, "0x541b796ea02703d02ff31459815f65f410ceefe80a4e3499f7ef9ccc36d26ee", block.StateRoot.String())
assert.Equal(t, "ACCEPTED_ON_L2", block.Status)
assert.Equal(t, "0x31c4e2d75", block.GasPrice.String())
assert.Equal(t, "0x31c4e2d75", block.GasPrice().String())
assert.Equal(t, 324, len(block.Transactions))
assert.Equal(t, 324, len(block.Receipts))
assert.Equal(t, uint64(1674728186), block.Timestamp)
Expand Down
3 changes: 2 additions & 1 deletion rpc/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,8 @@ func adaptBlockHeader(header *core.Header) BlockHeader {
Timestamp: header.Timestamp,
SequencerAddress: sequencerAddress,
L1GasPrice: &ResourcePrice{
InWei: header.GasPrice,
InWei: header.GasPrice,
InStark: header.GasPriceSTRK,
},
StarknetVersion: header.ProtocolVersion,
}
Expand Down
1 change: 0 additions & 1 deletion rpc/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,6 @@ func adaptBroadcastedTransaction(broadcastedTxn *BroadcastedTransaction,
network utils.Network,
) (core.Transaction, core.Class, *felt.Felt, error) {
var feederTxn starknet.Transaction
// TODO does this still work in v3 world?
if err := copier.Copy(&feederTxn, broadcastedTxn.Transaction); err != nil {
return nil, nil, nil, err
}
Expand Down
15 changes: 14 additions & 1 deletion starknet/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,23 @@ type Block struct {
Number uint64 `json:"block_number"`
StateRoot *felt.Felt `json:"state_root"`
Status string `json:"status"`
GasPrice *felt.Felt `json:"gas_price"`
Transactions []*Transaction `json:"transactions"`
Timestamp uint64 `json:"timestamp"`
Version string `json:"starknet_version"`
Receipts []*TransactionReceipt `json:"transaction_receipts"`
SequencerAddress *felt.Felt `json:"sequencer_address"`
GasPriceSTRK *felt.Felt `json:"strk_l1_gas_price"`

// TODO we can remove the GasPrice method and the GasPriceLegacy field
// once v0.13 lands on mainnet. In the meantime, we include both to support
// pre-v0.13 jsons, where `eth_l1_gas_price` was called `gas_price`.
GasPriceLegacy *felt.Felt `json:"gas_price"`
GasPriceWEI *felt.Felt `json:"eth_l1_gas_price"`
}

func (b *Block) GasPrice() *felt.Felt {
if b.GasPriceWEI != nil {
return b.GasPriceWEI
}
return b.GasPriceLegacy
}
65 changes: 51 additions & 14 deletions starknet/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,22 +104,59 @@ func (t *TransactionType) UnmarshalJSON(data []byte) error {
return nil
}

type Resource uint32

const (
ResourceL1Gas Resource = iota + 1
ResourceL2Gas
)

func (r *Resource) UnmarshalJSON(data []byte) error {
switch string(data) {
case `"L1_GAS"`:
*r = ResourceL1Gas
case `"L2_GAS"`:
*r = ResourceL2Gas
default:
return fmt.Errorf("unknown resource: %s", string(data))
}
return nil
}

type DataAvailabilityMode uint32

const (
DAModeL1 DataAvailabilityMode = iota
DAModeL2
)

type ResourceBounds struct {
MaxAmount uint64 `json:"max_amount"`
MaxPricePerUnit *felt.Felt `json:"max_price_per_unit"`
}

// Transaction object returned by the feeder in JSON format for multiple endpoints
type Transaction struct {
Hash *felt.Felt `json:"transaction_hash,omitempty" copier:"must,nopanic"`
Version *felt.Felt `json:"version,omitempty"`
ContractAddress *felt.Felt `json:"contract_address,omitempty"`
ContractAddressSalt *felt.Felt `json:"contract_address_salt,omitempty"`
ClassHash *felt.Felt `json:"class_hash,omitempty"`
ConstructorCallData *[]*felt.Felt `json:"constructor_calldata,omitempty"`
Type TransactionType `json:"type,omitempty"`
SenderAddress *felt.Felt `json:"sender_address,omitempty"`
MaxFee *felt.Felt `json:"max_fee,omitempty"`
Signature *[]*felt.Felt `json:"signature,omitempty"`
CallData *[]*felt.Felt `json:"calldata,omitempty"`
EntryPointSelector *felt.Felt `json:"entry_point_selector,omitempty"`
Nonce *felt.Felt `json:"nonce,omitempty"`
CompiledClassHash *felt.Felt `json:"compiled_class_hash,omitempty"`
Hash *felt.Felt `json:"transaction_hash,omitempty" copier:"must,nopanic"`
Version *felt.Felt `json:"version,omitempty"`
ContractAddress *felt.Felt `json:"contract_address,omitempty"`
ContractAddressSalt *felt.Felt `json:"contract_address_salt,omitempty"`
ClassHash *felt.Felt `json:"class_hash,omitempty"`
ConstructorCallData *[]*felt.Felt `json:"constructor_calldata,omitempty"`
Type TransactionType `json:"type,omitempty"`
SenderAddress *felt.Felt `json:"sender_address,omitempty"`
MaxFee *felt.Felt `json:"max_fee,omitempty"`
Signature *[]*felt.Felt `json:"signature,omitempty"`
CallData *[]*felt.Felt `json:"calldata,omitempty"`
EntryPointSelector *felt.Felt `json:"entry_point_selector,omitempty"`
Nonce *felt.Felt `json:"nonce,omitempty"`
CompiledClassHash *felt.Felt `json:"compiled_class_hash,omitempty"`
ResourceBounds map[Resource]*ResourceBounds `json:"resource_bounds,omitempty"`
Tip uint64 `json:"tip,omitempty"`
NonceDAMode DataAvailabilityMode `json:"nonce_data_availability_mode,omitempty"`
FeeDAMode DataAvailabilityMode `json:"fee_data_availability_mode,omitempty"`
AccountDeploymentData []*felt.Felt `json:"account_deployment_data,omitempty"`
PaymasterData []*felt.Felt `json:"paymaster_data,omitempty"`
}

type TransactionStatus struct {
Expand Down

0 comments on commit 619d9f3

Please sign in to comment.