graphql: protect against nil pointer deref on cases like TX with nil … (#7047)

…To address (contract creation)
This commit is contained in:
Stéphane Loeuillet 2023-03-07 13:20:18 +01:00 committed by GitHub
parent 5b1392a7c3
commit e59d37e61d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 71 additions and 50 deletions

View File

@ -15,6 +15,10 @@ import (
func convertDataToStringP(abstractMap map[string]interface{}, field string) *string {
var result string
if reflect.ValueOf(abstractMap[field]).IsZero() {
return nil
}
switch v := abstractMap[field].(type) {
case int64:
result = strconv.FormatInt(v, 10)
@ -44,6 +48,7 @@ func convertDataToStringP(abstractMap map[string]interface{}, field string) *str
fmt.Println("unhandled/string", reflect.TypeOf(abstractMap[field]), field, abstractMap[field])
result = "unhandled"
}
return &result
}

View File

@ -60,63 +60,79 @@ func (r *queryResolver) Block(ctx context.Context, number *string, hash *string)
return nil, err
}
absBlk := res["block"]
blk := absBlk.(map[string]interface{})
block := &model.Block{}
block.Difficulty = *convertDataToStringP(blk, "difficulty")
block.ExtraData = *convertDataToStringP(blk, "extraData")
block.GasLimit = uint64(*convertDataToUint64P(blk, "gasLimit"))
block.GasUsed = *convertDataToUint64P(blk, "gasUsed")
block.Hash = *convertDataToStringP(blk, "hash")
block.Miner = &model.Account{}
block.Miner.Address = strings.ToLower(*convertDataToStringP(blk, "miner"))
block.MixHash = *convertDataToStringP(blk, "mixHash")
block.Nonce = *convertDataToStringP(blk, "nonce")
block.Number = *convertDataToUint64P(blk, "number")
block.Ommers = []*model.Block{}
block.Parent = &model.Block{}
block.Parent.Hash = *convertDataToStringP(blk, "parentHash")
block.ReceiptsRoot = *convertDataToStringP(blk, "receiptsRoot")
block.StateRoot = *convertDataToStringP(blk, "stateRoot")
block.Timestamp = *convertDataToUint64P(blk, "timestamp") // int in the schema but Geth displays in HEX !!!
block.TransactionCount = convertDataToIntP(blk, "transactionCount")
block.TransactionsRoot = *convertDataToStringP(blk, "transactionsRoot")
block.TotalDifficulty = *convertDataToStringP(blk, "totalDifficulty")
block.Transactions = []*model.Transaction{}
absBlk := res["block"]
block.LogsBloom = "0x" + *convertDataToStringP(blk, "logsBloom")
block.OmmerHash = "" // OmmerHash: gointerfaces.ConvertHashToH256(header.UncleHash),
if absBlk != nil {
blk := absBlk.(map[string]interface{})
/*
Missing Block fields to fill :
- ommerHash
*/
block.Difficulty = *convertDataToStringP(blk, "difficulty")
block.ExtraData = *convertDataToStringP(blk, "extraData")
block.GasLimit = uint64(*convertDataToUint64P(blk, "gasLimit"))
block.GasUsed = *convertDataToUint64P(blk, "gasUsed")
block.Hash = *convertDataToStringP(blk, "hash")
block.Miner = &model.Account{}
address := convertDataToStringP(blk, "miner")
if address != nil {
block.Miner.Address = strings.ToLower(*address)
}
mixHash := convertDataToStringP(blk, "mixHash")
if mixHash != nil {
block.MixHash = *mixHash
}
blockNonce := convertDataToStringP(blk, "nonce")
if blockNonce != nil {
block.Nonce = *blockNonce
}
block.Number = *convertDataToUint64P(blk, "number")
block.Ommers = []*model.Block{}
block.Parent = &model.Block{}
block.Parent.Hash = *convertDataToStringP(blk, "parentHash")
block.ReceiptsRoot = *convertDataToStringP(blk, "receiptsRoot")
block.StateRoot = *convertDataToStringP(blk, "stateRoot")
block.Timestamp = *convertDataToUint64P(blk, "timestamp") // int in the schema but Geth displays in HEX !!!
block.TransactionCount = convertDataToIntP(blk, "transactionCount")
block.TransactionsRoot = *convertDataToStringP(blk, "transactionsRoot")
block.TotalDifficulty = *convertDataToStringP(blk, "totalDifficulty")
block.Transactions = []*model.Transaction{}
absRcp := res["receipts"]
rcp := absRcp.([]map[string]interface{})
for _, transReceipt := range rcp {
trans := &model.Transaction{}
trans.CumulativeGasUsed = convertDataToUint64P(transReceipt, "cumulativeGasUsed")
trans.InputData = *convertDataToStringP(transReceipt, "data")
trans.EffectiveGasPrice = convertDataToStringP(transReceipt, "effectiveGasPrice")
trans.GasPrice = *convertDataToStringP(transReceipt, "gasPrice")
trans.GasUsed = convertDataToUint64P(transReceipt, "gasUsed")
trans.Hash = *convertDataToStringP(transReceipt, "transactionHash")
trans.Index = convertDataToIntP(transReceipt, "transactionIndex")
trans.Nonce = *convertDataToUint64P(transReceipt, "nonce")
trans.Status = convertDataToUint64P(transReceipt, "status")
trans.Type = convertDataToIntP(transReceipt, "type")
trans.Value = *convertDataToStringP(transReceipt, "value")
trans.Logs = make([]*model.Log, 0)
block.LogsBloom = "0x" + *convertDataToStringP(blk, "logsBloom")
block.OmmerHash = "" // OmmerHash: gointerfaces.ConvertHashToH256(header.UncleHash),
trans.From = &model.Account{}
trans.From.Address = strings.ToLower(*convertDataToStringP(transReceipt, "from"))
/*
Missing Block fields to fill :
- ommerHash
*/
trans.To = &model.Account{}
trans.To.Address = strings.ToLower(*convertDataToStringP(transReceipt, "to"))
absRcp := res["receipts"]
rcp := absRcp.([]map[string]interface{})
for _, transReceipt := range rcp {
trans := &model.Transaction{}
trans.CumulativeGasUsed = convertDataToUint64P(transReceipt, "cumulativeGasUsed")
trans.InputData = *convertDataToStringP(transReceipt, "data")
trans.EffectiveGasPrice = convertDataToStringP(transReceipt, "effectiveGasPrice")
trans.GasPrice = *convertDataToStringP(transReceipt, "gasPrice")
trans.GasUsed = convertDataToUint64P(transReceipt, "gasUsed")
trans.Hash = *convertDataToStringP(transReceipt, "transactionHash")
trans.Index = convertDataToIntP(transReceipt, "transactionIndex")
trans.Nonce = *convertDataToUint64P(transReceipt, "nonce")
trans.Status = convertDataToUint64P(transReceipt, "status")
trans.Type = convertDataToIntP(transReceipt, "type")
trans.Value = *convertDataToStringP(transReceipt, "value")
trans.Logs = make([]*model.Log, 0)
block.Transactions = append(block.Transactions, trans)
trans.From = &model.Account{}
trans.From.Address = strings.ToLower(*convertDataToStringP(transReceipt, "from"))
trans.To = &model.Account{}
address := convertDataToStringP(transReceipt, "to")
// To address could be nil in case of contract creation
if address != nil {
trans.To.Address = strings.ToLower(*convertDataToStringP(transReceipt, "to"))
}
block.Transactions = append(block.Transactions, trans)
}
}
return block, ctx.Err()