Skip to content

Commit

Permalink
fix marshalJSON test for BlockID (#137)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xcoburn committed Oct 3, 2022
1 parent 7cf59c8 commit 19aec11
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
13 changes: 10 additions & 3 deletions rpc/types/block.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package types

import (
"encoding/json"
"errors"
"fmt"
"strconv"
Expand All @@ -28,12 +27,20 @@ func (b BlockID) MarshalJSON() ([]byte, error) {
return []byte(strconv.Quote(b.Tag)), nil
}

type Alias BlockID
if b.Tag != "" && (b.Tag != "pending" && b.Tag != "latest") {
return nil, ErrInvalidBlockID
}

return json.Marshal((Alias)(b))
if b.Number != nil {
return []byte(fmt.Sprintf(`{"block_number":%d}`,*b.Number)), nil
}

if b.Hash != nil {
return []byte(fmt.Sprintf(`{"block_hash":"%s"}`,(*b.Hash).Hex())), nil
}

return nil, ErrInvalidBlockID

}

type BlockStatus string
Expand Down
4 changes: 2 additions & 2 deletions rpc/types/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ func TestBlockID_Marshal(t *testing.T) {
id: BlockID{
Tag: "latest",
},
want: `{"block_tag":"latest"}`,
want: `"latest"`,
}, {
id: BlockID{
Tag: "pending",
},
want: `{"block_tag":"pending"}`,
want: `"pending"`,
}, {
id: BlockID{
Tag: "bad tag",
Expand Down

0 comments on commit 19aec11

Please sign in to comment.