Skip to content

Commit

Permalink
Merge pull request #22 from tomokazukozuma/fix-reverse-bytes
Browse files Browse the repository at this point in the history
fix ReverseBytes
  • Loading branch information
tomokazukozuma authored May 2, 2022
2 parents 7077596 + 1731a38 commit 8d6297b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
11 changes: 8 additions & 3 deletions internal/spv/spv.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import (
"bytes"
"encoding/hex"
"fmt"
"log"

"github.com/tomokazukozuma/bitcoin-spv/pkg/network"
"github.com/tomokazukozuma/bitcoin-spv/pkg/protocol/common"
"github.com/tomokazukozuma/bitcoin-spv/pkg/protocol/message"
"github.com/tomokazukozuma/bitcoin-spv/pkg/util"
"github.com/tomokazukozuma/bitcoin-spv/pkg/wallet"
"log"
)

type SPV interface {
Expand Down Expand Up @@ -101,7 +102,8 @@ func (s *spv) SendGetBlocks(startBlockHeaderHash string) error {
return err
}
var reversedStartBlockHeaderHash [32]byte
copy(reversedStartBlockHeaderHash[:], util.ReverseBytes(startBlockHash))
copy(reversedStartBlockHeaderHash[:], startBlockHash)
util.ReverseBytes(reversedStartBlockHeaderHash[:])
getblocks := message.NewGetBlocks(uint32(70015), [][32]byte{reversedStartBlockHeaderHash}, message.ZeroHash)
_, err = s.Client.SendMessage(getblocks)
if err != nil {
Expand Down Expand Up @@ -179,7 +181,10 @@ func (s *spv) MessageHandlerForBalance() error {
log.Printf("block hash: %s", mb.GetBlockHash())
txHashes := mb.Validate()
for _, txHash := range txHashes {
stringHash := hex.EncodeToString(util.ReverseBytes(txHash[:]))
var reverseTxHash [32]byte
copy(reverseTxHash[:], txHash[:])
util.ReverseBytes(reverseTxHash[:])
stringHash := hex.EncodeToString(reverseTxHash[:])
log.Printf("string txHash: %s", stringHash)
}
var inventory []*common.InvVect
Expand Down
7 changes: 4 additions & 3 deletions pkg/protocol/message/tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package message

import (
"encoding/hex"
"github.com/stretchr/testify/assert"
"testing"

"github.com/stretchr/testify/assert"
)

func Test_DecodeTx_Encode(t *testing.T) {
rawtx, _ :=hex.DecodeString("02000000012caa664f48f3631658e0588815c04b2c22f02d5af545506aa01e93dee90d0f4e0000000000feffffff02f8c8455300000000160014ecb7a596cc48b95a10bdb622fa75d87795c06b0b10270000000000001976a9142c26d5493277afcf2185b8901e0e0b26f282699e88ac29e62100")
rawtx, _ := hex.DecodeString("02000000012caa664f48f3631658e0588815c04b2c22f02d5af545506aa01e93dee90d0f4e0000000000feffffff02f8c8455300000000160014ecb7a596cc48b95a10bdb622fa75d87795c06b0b10270000000000001976a9142c26d5493277afcf2185b8901e0e0b26f282699e88ac29e62100")
tx, _ := DecodeTx(rawtx)
assert.Equal(t,rawtx , tx.Encode())
assert.Equal(t, rawtx, tx.Encode())
}
1 change: 1 addition & 0 deletions pkg/protocol/message/txin.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package message
import (
"bytes"
"encoding/binary"

"github.com/tomokazukozuma/bitcoin-spv/pkg/protocol/common"
"github.com/tomokazukozuma/bitcoin-spv/pkg/util"
)
Expand Down

0 comments on commit 8d6297b

Please sign in to comment.