Skip to content

Commit

Permalink
fix unmarshalling
Browse files Browse the repository at this point in the history
  • Loading branch information
joshklop committed Nov 15, 2023
1 parent f0157bd commit 781cc24
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 39 deletions.
72 changes: 53 additions & 19 deletions adapters/sn2core/sn2core.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,21 +141,25 @@ func AdaptTransaction(transaction *starknet.Transaction) (core.Transaction, erro
txType := transaction.Type
switch txType {
case starknet.TxnDeclare:
return AdaptDeclareTransaction(transaction), nil
return AdaptDeclareTransaction(transaction)
case starknet.TxnDeploy:
return AdaptDeployTransaction(transaction), nil
case starknet.TxnInvoke:
return AdaptInvokeTransaction(transaction), nil
return AdaptInvokeTransaction(transaction)
case starknet.TxnDeployAccount:
return AdaptDeployAccountTransaction(transaction), nil
return AdaptDeployAccountTransaction(transaction)
case starknet.TxnL1Handler:
return AdaptL1HandlerTransaction(transaction), nil
default:
return nil, fmt.Errorf("unknown transaction type %q", txType)
}
}

func AdaptDeclareTransaction(t *starknet.Transaction) *core.DeclareTransaction {
func AdaptDeclareTransaction(t *starknet.Transaction) (*core.DeclareTransaction, error) {
resourceBounds, err := adaptResourceBounds(t.ResourceBounds)
if err != nil {
return nil, err
}

Check warning on line 162 in adapters/sn2core/sn2core.go

View check run for this annotation

Codecov / codecov/patch

adapters/sn2core/sn2core.go#L161-L162

Added lines #L161 - L162 were not covered by tests
return &core.DeclareTransaction{
TransactionHash: t.Hash,
SenderAddress: t.SenderAddress,
Expand All @@ -165,24 +169,39 @@ func AdaptDeclareTransaction(t *starknet.Transaction) *core.DeclareTransaction {
Version: (*core.TransactionVersion)(t.Version),
ClassHash: t.ClassHash,
CompiledClassHash: t.CompiledClassHash,
ResourceBounds: adaptResourceBounds(t.ResourceBounds),
Tip: t.Tip,
ResourceBounds: resourceBounds,
Tip: safeFeltToUint64(t.Tip),
PaymasterData: t.PaymasterData,
AccountDeploymentData: t.AccountDeploymentData,
NonceDAMode: core.DataAvailabilityMode(t.NonceDAMode),
FeeDAMode: core.DataAvailabilityMode(t.FeeDAMode),
}
}, nil
}

func adaptResourceBounds(rb map[starknet.Resource]*starknet.ResourceBounds) map[core.Resource]*core.ResourceBounds {
func adaptResourceBounds(rb map[starknet.Resource]*starknet.ResourceBounds) (map[core.Resource]*core.ResourceBounds, error) {
coreBounds := make(map[core.Resource]*core.ResourceBounds, len(rb))
for resource, bounds := range rb {
coreBounds[core.Resource(resource)] = &core.ResourceBounds{
MaxAmount: bounds.MaxAmount,
coreResource, err := adaptResource(resource)
if err != nil {
return nil, err
}
coreBounds[coreResource] = &core.ResourceBounds{
MaxAmount: bounds.MaxAmount.Uint64(),
MaxPricePerUnit: bounds.MaxPricePerUnit,
}

Check warning on line 191 in adapters/sn2core/sn2core.go

View check run for this annotation

Codecov / codecov/patch

adapters/sn2core/sn2core.go#L184-L191

Added lines #L184 - L191 were not covered by tests
}
return coreBounds
return coreBounds, nil
}

func adaptResource(resource starknet.Resource) (core.Resource, error) {
switch resource {
case starknet.ResourceL1Gas:
return core.ResourceL1Gas, nil
case starknet.ResourceL2Gas:
return core.ResourceL2Gas, nil
default:
return 0, fmt.Errorf("unknown starknet.Resource %s", resource)

Check warning on line 203 in adapters/sn2core/sn2core.go

View check run for this annotation

Codecov / codecov/patch

adapters/sn2core/sn2core.go#L196-L203

Added lines #L196 - L203 were not covered by tests
}
}

func AdaptDeployTransaction(t *starknet.Transaction) *core.DeployTransaction {
Expand All @@ -199,7 +218,11 @@ func AdaptDeployTransaction(t *starknet.Transaction) *core.DeployTransaction {
}
}

func AdaptInvokeTransaction(t *starknet.Transaction) *core.InvokeTransaction {
func AdaptInvokeTransaction(t *starknet.Transaction) (*core.InvokeTransaction, error) {
resourceBounds, err := adaptResourceBounds(t.ResourceBounds)
if err != nil {
return nil, err

Check warning on line 224 in adapters/sn2core/sn2core.go

View check run for this annotation

Codecov / codecov/patch

adapters/sn2core/sn2core.go#L224

Added line #L224 was not covered by tests
}
return &core.InvokeTransaction{
TransactionHash: t.Hash,
ContractAddress: t.ContractAddress,
Expand All @@ -210,13 +233,13 @@ func AdaptInvokeTransaction(t *starknet.Transaction) *core.InvokeTransaction {
MaxFee: t.MaxFee,
Version: (*core.TransactionVersion)(t.Version),
SenderAddress: t.SenderAddress,
ResourceBounds: adaptResourceBounds(t.ResourceBounds),
Tip: t.Tip,
ResourceBounds: resourceBounds,
Tip: safeFeltToUint64(t.Tip),
PaymasterData: t.PaymasterData,
AccountDeploymentData: t.AccountDeploymentData,
NonceDAMode: core.DataAvailabilityMode(t.NonceDAMode),
FeeDAMode: core.DataAvailabilityMode(t.FeeDAMode),
}
}, nil
}

func AdaptL1HandlerTransaction(t *starknet.Transaction) *core.L1HandlerTransaction {
Expand All @@ -230,19 +253,23 @@ func AdaptL1HandlerTransaction(t *starknet.Transaction) *core.L1HandlerTransacti
}
}

func AdaptDeployAccountTransaction(t *starknet.Transaction) *core.DeployAccountTransaction {
func AdaptDeployAccountTransaction(t *starknet.Transaction) (*core.DeployAccountTransaction, error) {
resourceBounds, err := adaptResourceBounds(t.ResourceBounds)
if err != nil {
return nil, err

Check warning on line 259 in adapters/sn2core/sn2core.go

View check run for this annotation

Codecov / codecov/patch

adapters/sn2core/sn2core.go#L259

Added line #L259 was not covered by tests
}
return &core.DeployAccountTransaction{
DeployTransaction: *AdaptDeployTransaction(t),
MaxFee: t.MaxFee,
TransactionSignature: *t.Signature,
Nonce: t.Nonce,
ResourceBounds: adaptResourceBounds(t.ResourceBounds),
Tip: t.Tip,
ResourceBounds: resourceBounds,
Tip: safeFeltToUint64(t.Tip),
PaymasterData: t.PaymasterData,
AccountDeploymentData: t.AccountDeploymentData,
NonceDAMode: core.DataAvailabilityMode(t.NonceDAMode),
FeeDAMode: core.DataAvailabilityMode(t.FeeDAMode),
}
}, nil
}

func AdaptCairo1Class(response *starknet.SierraDefinition, compiledClass json.RawMessage) (core.Class, error) {
Expand Down Expand Up @@ -368,3 +395,10 @@ func AdaptStateUpdate(response *starknet.StateUpdate) (*core.StateUpdate, error)
StateDiff: stateDiff,
}, nil
}

func safeFeltToUint64(f *felt.Felt) uint64 {
if f != nil {
return f.Uint64()
}

Check warning on line 402 in adapters/sn2core/sn2core.go

View check run for this annotation

Codecov / codecov/patch

adapters/sn2core/sn2core.go#L401-L402

Added lines #L401 - L402 were not covered by tests
return 0
}
22 changes: 5 additions & 17 deletions starknet/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,25 +104,13 @@ func (t *TransactionType) UnmarshalJSON(data []byte) error {
return nil
}

type Resource uint32
type Resource string

const (
ResourceL1Gas Resource = iota + 1
ResourceL2Gas
ResourceL1Gas Resource = "L1_GAS"
ResourceL2Gas Resource = "L2_GAS"
)

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 (
Expand All @@ -131,7 +119,7 @@ const (
)

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

Expand All @@ -152,7 +140,7 @@ type Transaction struct {
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"`
Tip *felt.Felt `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"`
Expand Down
15 changes: 12 additions & 3 deletions starknetdata/feeder/feeder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ func TestTransaction(t *testing.T) {

ctx := context.Background()

//nolint:dupl
t.Run("invoke transaction", func(t *testing.T) {
hash := utils.HexToFelt(t, "0x7e3a229febf47c6edfd96582d9476dd91a58a5ba3df4553ae448a14a2f132d9")
response, err := clientGoerli.Transaction(ctx, hash)
Expand All @@ -119,7 +120,9 @@ func TestTransaction(t *testing.T) {
require.NoError(t, err)
invokeTx, ok := txn.(*core.InvokeTransaction)
require.True(t, ok)
assert.Equal(t, sn2core.AdaptInvokeTransaction(responseTx), invokeTx)
tx, err := sn2core.AdaptInvokeTransaction(responseTx)
require.NoError(t, err)
assert.Equal(t, tx, invokeTx)
})

t.Run("deploy transaction", func(t *testing.T) {
Expand All @@ -135,6 +138,7 @@ func TestTransaction(t *testing.T) {
assert.Equal(t, sn2core.AdaptDeployTransaction(responseTx), deployTx)
})

//nolint:dupl
t.Run("deploy account transaction", func(t *testing.T) {
hash := utils.HexToFelt(t, "0xd61fc89f4d1dc4dc90a014957d655d38abffd47ecea8e3fa762e3160f155f2")
response, err := clientMainnet.Transaction(ctx, hash)
Expand All @@ -145,9 +149,12 @@ func TestTransaction(t *testing.T) {
require.NoError(t, err)
deployAccountTx, ok := txn.(*core.DeployAccountTransaction)
require.True(t, ok)
assert.Equal(t, sn2core.AdaptDeployAccountTransaction(responseTx), deployAccountTx)
responseTxn, err := sn2core.AdaptDeployAccountTransaction(responseTx)
require.NoError(t, err)
assert.Equal(t, responseTxn, deployAccountTx)
})

//nolint:dupl
t.Run("declare transaction", func(t *testing.T) {
hash := utils.HexToFelt(t, "0x6eab8252abfc9bbfd72c8d592dde4018d07ce467c5ce922519d7142fcab203f")
response, err := clientGoerli.Transaction(ctx, hash)
Expand All @@ -158,7 +165,9 @@ func TestTransaction(t *testing.T) {
require.NoError(t, err)
declareTx, ok := txn.(*core.DeclareTransaction)
require.True(t, ok)
assert.Equal(t, sn2core.AdaptDeclareTransaction(responseTx), declareTx)
responseTxn, err := sn2core.AdaptDeclareTransaction(responseTx)
require.NoError(t, err)
assert.Equal(t, responseTxn, declareTx)
})

t.Run("l1handler transaction", func(t *testing.T) {
Expand Down

0 comments on commit 781cc24

Please sign in to comment.