From a03a026dc02eb617ceb074a3680e8cf120c834b1 Mon Sep 17 00:00:00 2001 From: Michael Boeckli Date: Fri, 13 Oct 2023 16:44:25 +0200 Subject: [PATCH 1/3] Fix test --- wire/msgextendedtx_test.go | 113 ++++++++++++++++--------------------- 1 file changed, 50 insertions(+), 63 deletions(-) diff --git a/wire/msgextendedtx_test.go b/wire/msgextendedtx_test.go index 8a4e48a..c8fbf49 100644 --- a/wire/msgextendedtx_test.go +++ b/wire/msgextendedtx_test.go @@ -188,75 +188,62 @@ func TestExtendedTxSerialize(t *testing.T) { } tests := []struct { + name string in *MsgExtendedTx // Message to encode out *MsgExtendedTx // Expected decoded message buf []byte // Serialized data pkScriptLocs []int // Expected output script locations }{ - // No transactions. { - noTx, - noTx, - noTxEncoded, - nil, + name: "No transactions", + in: noTx, + out: noTx, + buf: noTxEncoded, + pkScriptLocs: nil, }, - - // Multiple transactions. { - multiExtendedTx, - multiExtendedTx, - multiExtendedTxEncoded, - multiExtendedTxPkScriptLocs, + name: "Multiple transactions.", + in: multiExtendedTx, + out: multiExtendedTx, + buf: multiExtendedTxEncoded, + pkScriptLocs: multiExtendedTxPkScriptLocs, }, } t.Logf("Running %d tests", len(tests)) for i, test := range tests { - // Serialize the transaction. - var buf bytes.Buffer - err := test.in.Serialize(&buf) - if err != nil { - t.Errorf("Serialize #%d error %v", i, err) - continue - } - if !bytes.Equal(buf.Bytes(), test.buf) { - t.Errorf("Serialize #%d\n got: %s want: %s", i, - spew.Sdump(buf.Bytes()), spew.Sdump(test.buf)) - continue - } - - // Deserialize the transaction. - var tx MsgExtendedTx - rbuf := bytes.NewReader(test.buf) - err = tx.Deserialize(rbuf) - if err != nil { - t.Errorf("Deserialize #%d error %v", i, err) - continue - } - if !reflect.DeepEqual(&tx, test.out) { - t.Errorf("Deserialize #%d\n got: %s want: %s", i, - spew.Sdump(&tx), spew.Sdump(test.out)) - continue - } - - // Ensure the public key script locations are accurate. - pkScriptLocs := test.in.PkScriptLocs() - if !reflect.DeepEqual(pkScriptLocs, test.pkScriptLocs) { - t.Errorf("PkScriptLocs #%d\n got: %s want: %s", i, - spew.Sdump(pkScriptLocs), - spew.Sdump(test.pkScriptLocs)) - continue - } - //for j, loc := range pkScriptLocs { - // wantPkScript := test.in.TxOut[j].PkScript - // gotPkScript := test.buf[loc : loc+len(wantPkScript)] - // if !bytes.Equal(gotPkScript, wantPkScript) { - // t.Errorf("PkScriptLocs #%d:%d\n unexpected "+ - // "script got: %s want: %s", i, j, - // spew.Sdump(gotPkScript), - // spew.Sdump(wantPkScript)) - // } - //} + t.Run(test.name, func(t *testing.T) { + // Serialize the transaction. + var buf bytes.Buffer + err := test.in.Serialize(&buf) + if err != nil { + t.Fatalf("Serialize #%d error %v", i, err) + } + if !bytes.Equal(buf.Bytes(), test.buf) { + t.Fatalf("Serialize #%d\n got: %s want: %s", i, + spew.Sdump(buf.Bytes()), spew.Sdump(test.buf)) + } + + // Deserialize the transaction. + var tx MsgExtendedTx + rbuf := bytes.NewReader(test.buf) + err = tx.Deserialize(rbuf) + if err != nil { + t.Fatalf("Deserialize #%d error %v", i, err) + } + if !reflect.DeepEqual(&tx, test.out) { + t.Fatalf("Deserialize #%d\n got: %s want: %s", i, + spew.Sdump(&tx), spew.Sdump(test.out)) + } + + // Ensure the public key script locations are accurate. + pkScriptLocs := test.in.PkScriptLocs() + if !reflect.DeepEqual(pkScriptLocs, test.pkScriptLocs) { + t.Fatalf("PkScriptLocs #%d\n got: %s want: %s", i, + spew.Sdump(pkScriptLocs), + spew.Sdump(test.pkScriptLocs)) + } + }) } } @@ -514,13 +501,13 @@ var multiExtendedTxEncoded = []byte{ 0xff, 0xff, 0xff, 0xff, // Previous output index 0x07, // Varint for length of signature script 0x04, 0x31, 0xdc, 0x00, 0x1b, 0x01, 0x62, // Signature script - 0x6F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Previous Satoshis - 0x1b, // Previous script length - 0x3b, 0xa2, 0x7a, 0xa2, 0x00, 0xb1, 0xce, - 0xca, 0xad, 0x47, 0x8d, 0x2b, 0x00, 0x43, - 0x23, 0x46, 0xc3, 0xf1, 0xf3, 0x98, 0x6d, - 0xa1, 0xaf, 0xd3, 0x3e, 0x50, 0x06, // Previous Tx Script - 0xff, 0xff, 0xff, 0xff, // Sequence + 0xFF, 0xFF, 0xFF, 0xFF, 0x6F, 0x00, 0x00, 0x00, // Previous Satoshis + 0x00, // Previous script length + 0x00, 0x00, 0x00, 0x1b, 0x3b, 0xa2, 0x7a, + 0xa2, 0x00, 0xb1, 0xce, 0xca, 0xad, 0x47, + 0x8d, 0x2b, 0x00, 0x43, 0x23, 0x46, 0xc3, + 0xf1, 0xf3, 0x98, 0x6d, 0xa1, 0xaf, 0xd3, // Previous Tx Script + 0x3e, 0x50, 0x06, // Sequence 0x02, // Varint for number of output transactions 0x00, 0xf2, 0x05, 0x2a, 0x01, 0x00, 0x00, 0x00, // Transaction amount 0x43, // Varint for length of pk script From 9af314202dd4a3bc9a869d66515a25b171b6b9ba Mon Sep 17 00:00:00 2001 From: Michael Boeckli Date: Fri, 13 Oct 2023 16:47:29 +0200 Subject: [PATCH 2/3] Run golangci-lint --- wire/msgextendedtx.go | 2 +- wire/msgextendedtx_test.go | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/wire/msgextendedtx.go b/wire/msgextendedtx.go index 08a0b19..e38e117 100644 --- a/wire/msgextendedtx.go +++ b/wire/msgextendedtx.go @@ -164,7 +164,7 @@ func (msg *MsgExtendedTx) Bsvdecode(r io.Reader, pver uint32, enc MessageEncodin if err != nil { return err } - if count != 0 || bytes.Compare(efHeader, []byte{0x00, 0x00, 0x00, 0x00, 0xEF}) != 0 { + if count != 0 || !bytes.Equal(efHeader, []byte{0x00, 0x00, 0x00, 0x00, 0xEF}) { return fmt.Errorf("invalid extended tx EF header") } diff --git a/wire/msgextendedtx_test.go b/wire/msgextendedtx_test.go index c8fbf49..6eb77de 100644 --- a/wire/msgextendedtx_test.go +++ b/wire/msgextendedtx_test.go @@ -18,7 +18,7 @@ import ( ) // TestExtendedTx tests the MsgTx API. -func _TestExtendedTx(t *testing.T) { +func TestExtendedTx(t *testing.T) { pver := ProtocolVersion // Block 100000 hash. @@ -128,7 +128,7 @@ func _TestExtendedTx(t *testing.T) { } // TestTxHash tests the ability to generate the hash of a transaction accurately. -func _TestExtendedTxTxHash(t *testing.T) { +func TestExtendedTxTxHash(t *testing.T) { // Hash of first transaction from block 113875. hashStr := "f051e59b5e2503ac626d03aaeac8ab7be2d72ba4b7e97119c5852d70d52dcb86" wantHash, err := chainhash.NewHashFromStr(hashStr) @@ -249,7 +249,7 @@ func TestExtendedTxSerialize(t *testing.T) { // TestTxSerializeErrors performs negative tests against wire encode and decode // of MsgTx to confirm error paths work correctly. -func _TestExtendedTxSerializeErrors(t *testing.T) { +func TestExtendedTxSerializeErrors(t *testing.T) { tests := []struct { in *MsgTx // Value to encode buf []byte // Serialized data @@ -310,7 +310,7 @@ func _TestExtendedTxSerializeErrors(t *testing.T) { // which are intentionally crafted to use large values for the variable number // of inputs and outputs are handled properly. This could otherwise potentially // be used as an attack vector. -func _TestExtendedTxOverflowErrors(t *testing.T) { +func TestExtendedTxOverflowErrors(t *testing.T) { // Use protocol version 70001 and transaction version 1 specifically // here instead of the latest values because the test data is using // bytes encoded with those versions. @@ -405,7 +405,7 @@ func _TestExtendedTxOverflowErrors(t *testing.T) { // TestExtendedTxSerializeSizeStripped performs tests to ensure the serialize size for // various transactions is accurate. -func _TestExtendedTxSerializeSizeStripped(t *testing.T) { +func TestExtendedTxSerializeSizeStripped(t *testing.T) { // Empty tx message. noTx := NewMsgTx(1) noTx.Version = 1 From 5f0be3ba1d0bd760f95a01d3455fd13a5640b28d Mon Sep 17 00:00:00 2001 From: Michael Boeckli Date: Fri, 13 Oct 2023 16:47:50 +0200 Subject: [PATCH 3/3] Add Go workflow --- .github/workflows/go.yaml | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 .github/workflows/go.yaml diff --git a/.github/workflows/go.yaml b/.github/workflows/go.yaml new file mode 100644 index 0000000..44b8bc7 --- /dev/null +++ b/.github/workflows/go.yaml @@ -0,0 +1,34 @@ +# This workflow will build a golang project +# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go + +name: Go + +on: + push: + branches: ["feature/structured-logs"] + pull_request: + branches: ["master"] + +jobs: + build: + runs-on: ubuntu-latest + steps: + - name: Check out the repo + uses: actions/checkout@v4 + + - name: Set up Go + uses: actions/setup-go@v3 + with: + go-version-file: "./go.mod" + + - name: Verify dependencies + run: go mod verify + + - name: Build + run: go build -v ./... + + - name: Run go vet + run: go vet ./... + + - name: Run tests + run: make test