Skip to content

Commit

Permalink
Merge pull request btcsuite#64 from kcalvinalvin/add-json-marshal-for…
Browse files Browse the repository at this point in the history
…-txdesc

mining: Add json marshal for TxDesc
  • Loading branch information
kcalvinalvin authored Apr 17, 2023
2 parents 7a79d6e + 70105aa commit 173687e
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 0 deletions.
60 changes: 60 additions & 0 deletions mining/mining.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ package mining
import (
"bytes"
"container/heap"
"encoding/hex"
"encoding/json"
"fmt"
"time"

Expand Down Expand Up @@ -53,6 +55,64 @@ type TxDesc struct {
FeePerKB int64
}

// MarshalJSON implements the json.Marshaler interface for TxDesc.
func (td TxDesc) MarshalJSON() ([]byte, error) {
txBuf := bytes.NewBuffer(make([]byte, 0, td.Tx.MsgTx().SerializeSize()))
err := td.Tx.MsgTx().Serialize(txBuf)
if err != nil {
return nil, fmt.Errorf("failed to serialize tx %s. Error %v",
td.Tx.Hash(), err)
}
s := struct {
Tx string `json:"tx"`
Added time.Time `json:"added"`
Height int32 `json:"height"`
Fee int64 `json:"fee"`
FeePerKB int64 `json:"feeperkb"`
}{
Tx: hex.EncodeToString(txBuf.Bytes()),
Added: td.Added,
Height: td.Height,
Fee: td.Fee,
FeePerKB: td.FeePerKB,
}
return json.Marshal(s)
}

// UnmarshalJSON implements the json.Unmarshaler interface for TxDesc.
func (td *TxDesc) UnmarshalJSON(data []byte) error {
s := struct {
Tx string `json:"tx"`
Added time.Time `json:"added"`
Height int32 `json:"height"`
Fee int64 `json:"fee"`
FeePerKB int64 `json:"feeperkb"`
}{}
err := json.Unmarshal(data, &s)
if err != nil {
return err
}
txBytes, err := hex.DecodeString(s.Tx)
if err != nil {
return fmt.Errorf("Failed to decode tx string of %s. "+
"Error: %v", s.Tx, err)
}
msgTx := wire.NewMsgTx(1)
err = msgTx.Deserialize(bytes.NewBuffer(txBytes))
if err != nil {
return fmt.Errorf("Failed to deserialize tx hex of %s. "+
"Error: %v", hex.EncodeToString(txBytes), err)
}

td.Tx = btcutil.NewTx(msgTx)
td.Added = s.Added
td.Height = s.Height
td.Fee = s.Fee
td.FeePerKB = s.FeePerKB

return nil
}

// TxSource represents a source of transactions to consider for inclusion in
// new blocks.
//
Expand Down
70 changes: 70 additions & 0 deletions mining/mining_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ package mining

import (
"container/heap"
"encoding/hex"
"encoding/json"
"math/rand"
"reflect"
"testing"
"time"

"github.com/utreexo/utreexod/btcutil"
)
Expand Down Expand Up @@ -108,3 +112,69 @@ func TestTxFeePrioHeap(t *testing.T) {
highest = prioItem
}
}

func TestTxDescMarshal(t *testing.T) {
tests := []struct {
name string
tx TxDesc
}{
{
name: "txid 114351cae78bdb68d718c27a221f0127816810c124a096d6355d6f958264bbb7 on signet",
tx: TxDesc{
Tx: func() *btcutil.Tx {
rawStr := "020000000001014c4dcdaa45e3711f6e2147efccd1b106f81dc4b5205f306dff9e8c88d6b95dac0" +
"000000000fdffffff0321ff5f101b000000160014447fde1e37d97255b5821d2dee816e8f18f6bac9" +
"7cd1000000000000160014a8628755900c899f9de1a1bb07fa76ee9d773e5d00000000000000000f6" +
"a0d6c6561726e20426974636f696e0247304402207e7088b7a528089842feed081f0bb296ac2c0303" +
"7ea74918747def43025e666a02201d3a3000013509e0ce4ba73e8363aaaa51114eab6f66820aa430f" +
"604e599fa8c012102a8c3fa3dbc022ca7c9a2214c5e673833317b3cff37c0fc170fc347f1a2f6b6e2" +
"00000000"
bytes, err := hex.DecodeString(rawStr)
if err != nil {
panic(err)
}

tx, err := btcutil.NewTxFromBytes(bytes)
if err != nil {
panic(err)
}

return tx
}(),
Added: func() time.Time {
bytes := []byte("2023-04-17T13:26:48.250092856+09:00")

var t time.Time
err := t.UnmarshalText(bytes)
if err != nil {
panic(err)
}

return t
}(),
Height: 138955,
Fee: 165,
FeePerKB: 1000,
},
},
}

for _, test := range tests {
// Marshal the TxDesc to JSON.
bytes, err := json.Marshal(test.tx)
if err != nil {
t.Fatalf("failed to marshal TxDesc: %v", err)
}

// Unmarshal the JSON bytes back into a TxDesc struct.
var tx TxDesc
if err := json.Unmarshal(bytes, &tx); err != nil {
t.Fatalf("failed to unmarshal JSON into TxDesc: %v", err)
}

// Ensure the unmarshaled TxDesc struct is the same as the original.
if !reflect.DeepEqual(test.tx, tx) {
t.Fatalf("unmarshaled TxDesc does not match the original")
}
}
}

0 comments on commit 173687e

Please sign in to comment.