Skip to content

Commit

Permalink
check conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
tudor-malene committed May 24, 2024
1 parent 4f39832 commit 9484c0d
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 61 deletions.
74 changes: 20 additions & 54 deletions go/common/gethencoding/geth_encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,54 +77,6 @@ func NewGethEncodingService(storage storage.Storage, logger gethlog.Logger) Enco
}
}

// ExtractEthCallMapString extracts the eth_call gethapi.TransactionArgs from an interface{}
// it ensures that :
// - All types are string
// - All keys are lowercase
// - There is only one key per value
// - From field is set by default
func ExtractEthCallMapString(paramBytes interface{}) (map[string]string, error) {
// geth lowercase the field name and uses the last seen value
var valString string
var ok bool
callMsg := map[string]string{
// From field is set by default
"from": gethcommon.HexToAddress("0x0").Hex(),
}
for field, val := range paramBytes.(map[string]interface{}) {
if val == nil {
continue
}
valString, ok = val.(string)
if !ok {
return nil, fmt.Errorf("unexpected type supplied in `%s` field", field)
}
if len(strings.TrimSpace(valString)) == 0 {
continue
}
switch strings.ToLower(field) {
case callFieldTo:
callMsg[callFieldTo] = valString
case CallFieldFrom:
callMsg[CallFieldFrom] = valString
case callFieldData, callFieldInput:
callMsg[callFieldInput] = valString
case callFieldValue:
callMsg[callFieldValue] = valString
case callFieldGas:
callMsg[callFieldGas] = valString
case callFieldMaxFeePerGas:
callMsg[callFieldMaxFeePerGas] = valString
case callFieldMaxPriorityFeePerGas:
callMsg[callFieldMaxPriorityFeePerGas] = valString
default:
callMsg[field] = valString
}
}

return callMsg, nil
}

// ExtractAddress returns a gethcommon.Address given an interface{}, errors if unexpected values are used
func ExtractAddress(param interface{}) (*gethcommon.Address, error) {
if param == nil {
Expand All @@ -140,7 +92,7 @@ func ExtractAddress(param interface{}) (*gethcommon.Address, error) {
return nil, fmt.Errorf("no address specified")
}

addr := gethcommon.HexToAddress(param.(string))
addr := gethcommon.HexToAddress(paramStr)
return &addr, nil
}

Expand Down Expand Up @@ -183,10 +135,13 @@ func ExtractBlockNumber(param interface{}) (*gethrpc.BlockNumberOrHash, error) {

blockAndHash, ok := param.(map[string]any)
if !ok {
return nil, fmt.Errorf("invalid block or hash parameter %s", param.(string))
return nil, fmt.Errorf("invalid block or hash parameter")
}
if blockAndHash["blockNumber"] != nil {
b := blockAndHash["blockNumber"].(string)
b, ok := blockAndHash["blockNumber"].(string)
if !ok {
return nil, fmt.Errorf("invalid blockNumber parameter")
}
blockNumber := gethrpc.BlockNumber(0)
err := blockNumber.UnmarshalJSON([]byte(b))
if err != nil {
Expand All @@ -195,11 +150,17 @@ func ExtractBlockNumber(param interface{}) (*gethrpc.BlockNumberOrHash, error) {
blockNo = &blockNumber
}
if blockAndHash["blockHash"] != nil {
bh := blockAndHash["blockHash"].(gethcommon.Hash)
bh, ok := blockAndHash["blockHash"].(gethcommon.Hash)
if !ok {
return nil, fmt.Errorf("invalid blockhash parameter")
}
blockHa = &bh
}
if blockAndHash["RequireCanonical"] != nil {
reqCanon = blockAndHash["RequireCanonical"].(bool)
reqCanon, ok = blockAndHash["RequireCanonical"].(bool)
if !ok {
return nil, fmt.Errorf("invalid RequireCanonical parameter")
}
}

return &gethrpc.BlockNumberOrHash{
Expand All @@ -222,7 +183,12 @@ func ExtractEthCall(param interface{}) (*gethapi.TransactionArgs, error) {
// if gas is not set it should be null
gas := (*hexutil.Uint64)(nil)

for field, val := range param.(map[string]interface{}) {
ethCallMap, ok := param.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("invalid eth call parameter")
}

for field, val := range ethCallMap {
if val == nil {
continue
}
Expand Down
2 changes: 1 addition & 1 deletion go/enclave/components/batch_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ func (executor *batchExecutor) populateOutboundCrossChainData(ctx context.Contex

encodedTree, err := json.Marshal(xchainTree)
if err != nil {
panic(err) //todo: figure out what to do
panic(err) // todo: figure out what to do
}

batch.Header.CrossChainTree = encodedTree
Expand Down
2 changes: 1 addition & 1 deletion go/enclave/crosschain/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ func (ms MessageStructs) HashPacked(index int) gethcommon.Hash {
},
}

//todo @siliev: err
// todo @siliev: err
packed, _ := args.Pack(messageStruct.Sender, messageStruct.Sequence, messageStruct.Nonce, messageStruct.Topic, messageStruct.Payload, messageStruct.ConsistencyLevel)
hash := crypto.Keccak256Hash(packed)
return hash
Expand Down
6 changes: 4 additions & 2 deletions go/enclave/genesis/testnet_genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import (
)

const TestnetPrefundedPK = "8dfb8083da6275ae3e4f41e3e8a8c19d028d32c9247e24530933782f2a05035b" // The genesis main account private key.
var GasBridgingKeys, _ = crypto.GenerateKey() // todo - make static
var GasWithdrawalKeys, _ = crypto.GenerateKey() // todo - make static
var (
GasBridgingKeys, _ = crypto.GenerateKey() // todo - make static
GasWithdrawalKeys, _ = crypto.GenerateKey() // todo - make static
)

var TestnetGenesis = Genesis{
Accounts: []Account{
Expand Down
2 changes: 1 addition & 1 deletion go/enclave/nodetype/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ func ExportCrossChainData(ctx context.Context, storage storage.Storage, fromSeqN
L1BlockHash: block.Hash(),
L1BlockNum: big.NewInt(0).Set(block.Header().Number),
CrossChainRootHashes: crossChainHashes,
} //todo: check fromSeqNo
} // todo: check fromSeqNo
return bundle, nil
}
1 change: 1 addition & 0 deletions go/enclave/nodetype/sequencer.go
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,7 @@ func (s *sequencer) signCrossChainBundle(bundle *common.ExtCrossChainBundle) err
}
return nil
}

func (s *sequencer) OnL1Block(ctx context.Context, block *types.Block, result *components.BlockIngestionType) error {
// nothing to do
return nil
Expand Down
7 changes: 6 additions & 1 deletion go/enclave/rpc/SubmitTx.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package rpc

import (
"errors"
"fmt"

gethcommon "github.com/ethereum/go-ethereum/common"
Expand All @@ -9,7 +10,11 @@ import (
)

func SubmitTxValidate(reqParams []any, builder *CallBuilder[common.L2Tx, gethcommon.Hash], _ *EncryptionManager) error {
l2Tx, err := ExtractTx(reqParams[0].(string))
txStr, ok := reqParams[0].(string)
if !ok {
return errors.New("unsupported format")
}
l2Tx, err := ExtractTx(txStr)
if err != nil {
builder.Err = fmt.Errorf("could not extract transaction. Cause: %w", err)
return nil
Expand Down
5 changes: 4 additions & 1 deletion go/enclave/storage/enclavedb/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,10 @@ func stringToHash(ns sql.NullString) gethcommon.Hash {
if err != nil {
return [32]byte{}
}
s := value.(string)
s, ok := value.(string)
if !ok {
return [32]byte{}
}
result := gethcommon.Hash{}
result.SetBytes([]byte(s))
return result
Expand Down

0 comments on commit 9484c0d

Please sign in to comment.