Skip to content

Commit f6b9134

Browse files
authored
Merge branch 'main' into feat/no-error-return
Signed-off-by: jkawan <kawanjenita@outlook.com>
2 parents 6163d32 + 9eaeafe commit f6b9134

File tree

158 files changed

+15736
-1653
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

158 files changed

+15736
-1653
lines changed

.github/workflows/conventional-commits.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ jobs:
1313
permissions:
1414
contents: read
1515
steps:
16-
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 https://github.com/actions/checkout/releases/tag/v4.2.2
16+
- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0 https://github.com/actions/checkout/releases/tag/v5.0.0
1717
- uses: webiny/action-conventional-commits@8bc41ff4e7d423d56fa4905f6ff79209a78776c7 # v1.3.0 https://github.com/webiny/action-conventional-commits/releases/tag/v1.3.0

.github/workflows/go-test.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ jobs:
2121
platform: [ubuntu-latest]
2222
runs-on: ${{ matrix.platform }}
2323
steps:
24-
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 https://github.com/actions/checkout/releases/tag/v4.2.2
24+
- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0 https://github.com/actions/checkout/releases/tag/v5.0.0
2525
with:
2626
submodules: true
27-
- uses: actions/setup-go@d35c59abb061a4a6fb18e82ac0862c26744d6ab5 # v5.5.0 https://github.com/actions/setup-go/releases/tag/v5.5.0
27+
- uses: actions/setup-go@44694675825211faa026b3c33043df3e48a5fa00 # v6.0.0 https://github.com/actions/setup-go/releases/tag/v6.0.0
2828
with:
2929
go-version: ${{ matrix.go-version }}
3030
- name: go-test

.github/workflows/golangci-lint.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ jobs:
1515
name: lint
1616
runs-on: ubuntu-latest
1717
steps:
18-
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 https://github.com/actions/checkout/releases/tag/v4.2.2
19-
- uses: actions/setup-go@d35c59abb061a4a6fb18e82ac0862c26744d6ab5 # v5.5.0 https://github.com/actions/setup-go/releases/tag/v5.5.0
18+
- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0 https://github.com/actions/checkout/releases/tag/v5.0.0
19+
- uses: actions/setup-go@44694675825211faa026b3c33043df3e48a5fa00 # v6.0.0 https://github.com/actions/setup-go/releases/tag/v6.0.0
2020
with:
2121
go-version: 1.24.x
2222
- name: golangci-lint
23-
uses: golangci/golangci-lint-action@4afd733a84b1f43292c63897423277bb7f4313a9 # v8.0.0 https://github.com/golangci/golangci-lint-action/releases/tag/v8.0.0
23+
uses: golangci/golangci-lint-action@0a35821d5c230e903fcfe077583637dea1b27b47 # v9.0.0 https://github.com/golangci/golangci-lint-action/releases/tag/v9.0.0

.github/workflows/nilaway.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: nilaway
2+
on:
3+
push:
4+
tags:
5+
- v*
6+
branches:
7+
- main
8+
pull_request:
9+
10+
permissions:
11+
contents: read
12+
13+
jobs:
14+
nilaway:
15+
name: nilaway
16+
runs-on: ubuntu-latest
17+
steps:
18+
- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0 https://github.com/actions/checkout/releases/tag/v5.0.0
19+
- uses: actions/setup-go@44694675825211faa026b3c33043df3e48a5fa00 # v6.0.0 https://github.com/actions/setup-go/releases/tag/v6.0.0
20+
with:
21+
go-version: 1.25.x
22+
- name: install nilaway
23+
run: go install go.uber.org/nilaway/cmd/nilaway@latest
24+
- name: run nilaway
25+
run: nilaway ./...

.github/workflows/publish.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
RELEASE_ID: ${{ steps.create-release.outputs.result }}
1717
steps:
1818
- run: "echo \"RELEASE_TAG=${GITHUB_REF#refs/tags/}\" >> $GITHUB_ENV"
19-
- uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1 https://github.com/actions/github-script/releases/tag/v7.0.1
19+
- uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0 https://github.com/actions/github-script/releases/tag/v8.0.0
2020
id: create-release
2121
with:
2222
github-token: ${{ secrets.GITHUB_TOKEN }}

.golangci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ run:
44
tests: false
55
linters:
66
enable:
7+
- asasalint
78
- asciicheck
89
- bidichk
910
- bodyclose
@@ -36,7 +37,6 @@ linters:
3637
- whitespace
3738
- zerologlint
3839
disable:
39-
- asasalint
4040
- depguard
4141
- noctx
4242
- recvcheck

cbor/decode_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,3 +271,52 @@ func TestDecodeById(t *testing.T) {
271271
}
272272
}
273273
}
274+
275+
func TestDecodeMaryTransactionCbor(t *testing.T) {
276+
// Mary transaction CBOR hex from real chain data
277+
maryTxHex := "83a50081825820377732953cbd7eb824e58291dd08599cfcfe6eedb49f590633610674fc3c33c50001818258390187acac5a3d0b41cd1c5e8c03af5be782f261f21beed70970ddee0873ae34f9d442c7f1a01de9f2dd520791122d6fbf3968c5f8328e9091331a597dbcf1021a0002fd99031a025c094a04828a03581c27b1b4470c84db78ce1ffbfff77bb068abb4e47d43cb6009caaa352358204a7537ce9eeaba1c650261f167827b4e12dd403c4bf13c56b2cba06288f7e9ab1a59682f001a1443fd00d81e82011864581de1ae34f9d442c7f1a01de9f2dd520791122d6fbf3968c5f8328e90913381581cae34f9d442c7f1a01de9f2dd520791122d6fbf3968c5f8328e9091338183011917706e33342e3139382e3234312e323336827468747470733a2f2f6769742e696f2f4a7543786e5820fa77d30bb41e2998233245d269ff5763ecf4371388214943ecef277cae45492783028200581cae34f9d442c7f1a01de9f2dd520791122d6fbf3968c5f8328e909133581c27b1b4470c84db78ce1ffbfff77bb068abb4e47d43cb6009caaa3523a10083825820922a22d07c0ca148105760cb767ece603574ea465d6697c87da8207c8936ebea58405594a100197379c0de715de0b5304e0546e661dae2f36b12173cc150a42215356a5600bf0c02954f02ce3620cfb7f12c23a19328fd00dd1194b4f363675ef407825820727c1891d01cf29ccd1146528221827dcf00a093498509404af77a8b15d77c925840f52e0e1403167212b11fe5d87b7cfdb2f39e5384979ac3625917127ad46763d864a7fcb7147c7b85322ada7ba8fe91c0b5152c74ef4ff0c8132b125e681af50382582073c16f2b67ff85307c4c5935bad1389b9ead473419dbad20f5d5e6436982992b58400572eed773b9a199fd486ebe61b480f05803d107ea97ff649f28b8874d3117f890f80657cbb6eea0d833c21e4e8bc7f1a27cddb9e24fc1ed79b04ddbdcd11d0ff6"
278+
279+
cborData, err := hex.DecodeString(maryTxHex)
280+
if err != nil {
281+
t.Fatalf("failed to decode Mary transaction hex: %s", err)
282+
}
283+
284+
var decoded any
285+
bytesRead, err := cbor.Decode(cborData, &decoded)
286+
if err != nil {
287+
t.Fatalf("failed to decode Mary transaction CBOR: %s", err)
288+
}
289+
290+
if bytesRead != len(cborData) {
291+
t.Fatalf(
292+
"did not read all bytes: read %d, expected %d",
293+
bytesRead,
294+
len(cborData),
295+
)
296+
}
297+
298+
// Verify it's an array (transactions are arrays)
299+
decodedSlice, ok := decoded.([]any)
300+
if !ok {
301+
t.Fatal("decoded Mary transaction is not an array")
302+
}
303+
304+
if len(decodedSlice) != 3 {
305+
t.Fatalf(
306+
"Mary transaction array length mismatch: got %d, expected 3",
307+
len(decodedSlice),
308+
)
309+
}
310+
}
311+
312+
func BenchmarkDecode(b *testing.B) {
313+
cborData, _ := hex.DecodeString("83010203")
314+
var dest any
315+
b.ResetTimer()
316+
for i := 0; i < b.N; i++ {
317+
_, err := cbor.Decode(cborData, &dest)
318+
if err != nil {
319+
b.Fatal(err)
320+
}
321+
}
322+
}

cbor/encode.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"bytes"
1919
"errors"
2020
"reflect"
21+
"sort"
2122
"sync"
2223

2324
_cbor "github.com/fxamacker/cbor/v2"
@@ -132,3 +133,54 @@ func (i IndefLengthByteString) MarshalCBOR() ([]byte, error) {
132133
)
133134
return ret, nil
134135
}
136+
137+
type IndefLengthMap map[any]any
138+
139+
func (i IndefLengthMap) MarshalCBOR() ([]byte, error) {
140+
ret := []byte{
141+
// Start indefinite-length map
142+
0xbf,
143+
}
144+
145+
// Collect keys and sort them by their CBOR encoding for deterministic output
146+
type keyValue struct {
147+
key any
148+
keyCbor []byte
149+
value any
150+
}
151+
152+
kvPairs := make([]keyValue, 0, len(map[any]any(i)))
153+
for key, value := range map[any]any(i) {
154+
keyData, err := Encode(key)
155+
if err != nil {
156+
return nil, err
157+
}
158+
kvPairs = append(kvPairs, keyValue{
159+
key: key,
160+
keyCbor: keyData,
161+
value: value,
162+
})
163+
}
164+
165+
// Sort by CBOR-encoded key for deterministic ordering
166+
sort.Slice(kvPairs, func(a, b int) bool {
167+
return bytes.Compare(kvPairs[a].keyCbor, kvPairs[b].keyCbor) < 0
168+
})
169+
170+
// Encode in sorted order
171+
for _, kv := range kvPairs {
172+
ret = append(ret, kv.keyCbor...)
173+
valueData, err := Encode(kv.value)
174+
if err != nil {
175+
return nil, err
176+
}
177+
ret = append(ret, valueData...)
178+
}
179+
180+
ret = append(
181+
ret,
182+
// End indefinite length map
183+
byte(0xff),
184+
)
185+
return ret, nil
186+
}

cbor/encode_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,14 @@ func TestEncodeIndefLengthByteString(t *testing.T) {
9090
)
9191
}
9292
}
93+
94+
func BenchmarkEncode(b *testing.B) {
95+
obj := []any{1, 2, 3}
96+
b.ResetTimer()
97+
for i := 0; i < b.N; i++ {
98+
_, err := cbor.Encode(obj)
99+
if err != nil {
100+
b.Fatal(err)
101+
}
102+
}
103+
}

cbor/tags.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package cbor
1616

1717
import (
18+
"encoding/json"
1819
"errors"
1920
"fmt"
2021
"math/big"
@@ -150,6 +151,24 @@ func (r *Rat) MarshalCBOR() ([]byte, error) {
150151
return Encode(&tmpData)
151152
}
152153

154+
func (r *Rat) UnmarshalJSON(data []byte) error {
155+
// Try as ratio
156+
var tmpData struct {
157+
Numerator int64 `json:"numerator"`
158+
Denominator int64 `json:"denominator"`
159+
}
160+
if err := json.Unmarshal(data, &tmpData); err == nil {
161+
r.Rat = big.NewRat(tmpData.Numerator, tmpData.Denominator)
162+
return nil
163+
}
164+
// Try as decimal value
165+
r.Rat = new(big.Rat)
166+
if _, ok := r.SetString(string(data)); !ok {
167+
return fmt.Errorf("math/big: cannot unmarshal %q into a *big.Rat", data)
168+
}
169+
return nil
170+
}
171+
153172
func (r *Rat) ToBigRat() *big.Rat {
154173
return r.Rat
155174
}
@@ -162,8 +181,8 @@ type Map map[any]any
162181

163182
// SetType is a generic type for wrapping other types in an optional CBOR set tag
164183
type SetType[T any] struct {
165-
useTag bool
166184
items []T
185+
useTag bool
167186
}
168187

169188
func NewSetType[T any](items []T, useTag bool) SetType[T] {

0 commit comments

Comments
 (0)