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,19 +60,30 @@ func (r *queryResolver) Block(ctx context.Context, number *string, hash *string)
return nil, err
}
block := &model.Block{}
absBlk := res["block"]
if absBlk != nil {
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")
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{}
@ -114,10 +125,15 @@ func (r *queryResolver) Block(ctx context.Context, number *string, hash *string)
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()
}