Skip to content

Commit

Permalink
fixed unmarshalling issue with getblock (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tíghearnán Carroll authored Feb 23, 2022
1 parent dbeabdb commit 5d4d27c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
8 changes: 4 additions & 4 deletions blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,22 +70,22 @@ func (c *client) BlockHexByHeight(ctx context.Context, height int) (string, erro
}

func (c *client) BlockDecodeHeader(ctx context.Context, hash string) (*models.BlockDecodeHeader, error) {
var resp models.BlockDecodeHeader
resp := models.BlockDecodeHeader{BlockHeader: models.BlockHeader{BlockHeader: &bc.BlockHeader{}}}
return &resp, c.rpc.Do(ctx, "getblock", &resp, hash, models.VerbosityDecodeHeader)
}

func (c *client) BlockDecodeHeaderByHeight(ctx context.Context, height int) (*models.BlockDecodeHeader, error) {
var resp models.BlockDecodeHeader
resp := models.BlockDecodeHeader{BlockHeader: models.BlockHeader{BlockHeader: &bc.BlockHeader{}}}
return &resp, c.rpc.Do(ctx, "getblockbyheight", &resp, height, models.VerbosityDecodeHeader)
}

func (c *client) Block(ctx context.Context, hash string) (*models.Block, error) {
var resp models.Block
resp := models.Block{BlockHeader: models.BlockHeader{BlockHeader: &bc.BlockHeader{}}}
return &resp, c.rpc.Do(ctx, "getblock", &resp, hash, models.VerbosityDecodeTransactions)
}

func (c *client) BlockByHeight(ctx context.Context, height int) (*models.Block, error) {
var resp models.Block
resp := models.Block{BlockHeader: models.BlockHeader{BlockHeader: &bc.BlockHeader{}}}
return &resp, c.rpc.Do(ctx, "getblockbyheight", &resp, height, models.VerbosityDecodeTransactions)
}

Expand Down
20 changes: 15 additions & 5 deletions models/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,31 @@ type Block struct {

// UnmarshalJSON unmarshal response.
func (b *Block) UnmarshalJSON(bb []byte) error {
bj := struct {
Txs json.RawMessage `json:"tx"`
bh := struct {
BlockHeader
}{
BlockHeader: BlockHeader{
BlockHeader: &bc.BlockHeader{},
},
}
if err := json.Unmarshal(bb, &bh); err != nil {
return err
}

btxs := struct {
Txs json.RawMessage `json:"tx"`
}{}
if err := json.Unmarshal(bb, &bj); err != nil {
if err := json.Unmarshal(bb, &btxs); err != nil {
return err
}

var txs bt.Txs
if err := json.Unmarshal(bj.Txs, txs.NodeJSON()); err != nil {
if err := json.Unmarshal(btxs.Txs, txs.NodeJSON()); err != nil {
return err
}

b.Txs = txs
b.BlockHeader = bj.BlockHeader
b.BlockHeader = bh.BlockHeader
return nil
}

Expand Down

0 comments on commit 5d4d27c

Please sign in to comment.