Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update marshaling to use hex format, add github ci, update readme with explanation #1

Merged
merged 2 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# For now, tag everyone who wants to participate
* @deso-protocol/reviewers
60 changes: 60 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
name: ci

on:
pull_request: {}
push:
branches:
- main

jobs:
# go-fmt:
# name: go-fmt
# runs-on: ubuntu-20.04
#
# steps:
# - name: Install Go
# uses: actions/setup-go@v2
# with:
# go-version: "1.20"
#
# - name: Checkout branch
# uses: actions/checkout@v3
#
# # Fail if go-fmt recommends any changes.
# - name: Run go fmt
# run: if [ "$(gofmt -s -l . | wc -l)" -gt 0 ]; then exit 1; fi

go-test:
name: go-test
runs-on: ubuntu-20.04

steps:
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: "1.20"

- name: Checkout branch
uses: actions/checkout@v3

- name: Cache dependencies
id: cache-dependencies
uses: actions/cache@v3
with:
path: |
~/.cache/go-build
~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
# Install dependencies if not found in cache.
- name: Install dependencies
if: steps.cache-dependencies.outputs.cache-hit != 'true'
run: go mod download && go install

- name: Build package
run: go build

- name: Run go test
run: go test -v ./
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# deso-protocol/Uint256
This is a forked version of (https://github.com/holiman/uint256)[https://github.com/holiman/uint256].
When deso-protocol go repositories began using this dependency, uint256 would marshal as a hex string.
The source updated its code to better mirror the BigInt library to marshal as a decimal string.
In order to maintain our APIs as they were but still take advantage of the latest updates, we have fork
the repository and only made changes to revert to using a hex string for marshaling.

# Fixed size 256-bit math library

This is a library specialized at replacing the big.Int library for math based on 256-bit types, used by both
Expand Down
6 changes: 3 additions & 3 deletions conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,7 @@ func (z *Int) EncodeRLP(w io.Writer) error {
// MarshalText implements encoding.TextMarshaler
// MarshalText marshals using the decimal representation (compatible with big.Int)
func (z *Int) MarshalText() ([]byte, error) {
return []byte(z.Dec()), nil
return []byte(z.Hex()), nil
}

// MarshalJSON implements json.Marshaler.
Expand All @@ -703,7 +703,7 @@ func (z *Int) MarshalText() ([]byte, error) {
// integer space. Thus, U256 uses string-format, which is not compatible with
// big.int (big.Int refuses to unmarshal a string representation).
func (z *Int) MarshalJSON() ([]byte, error) {
return []byte(`"` + z.Dec() + `"`), nil
return []byte(`"` + z.Hex() + `"`), nil
}

// UnmarshalJSON implements json.Unmarshaler. UnmarshalJSON accepts either
Expand All @@ -719,7 +719,7 @@ func (z *Int) UnmarshalJSON(input []byte) error {

// String returns the decimal encoding of b.
func (z *Int) String() string {
return z.Dec()
return z.Hex()
}

const (
Expand Down
6 changes: 3 additions & 3 deletions conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1278,19 +1278,19 @@ func TestEnDecode(t *testing.T) {
if have, want := intSample.Hex(), fmt.Sprintf("0x%s", bigSample.Text(16)); have != want {
t.Fatalf("test %d #1, have %v, want %v", i, have, want)
}
if have, want := intSample.String(), bigSample.String(); have != want {
if have, want := intSample.String(), wantHex; have != want {
t.Fatalf("test %d String(), have %v, want %v", i, have, want)
}
{
have, _ := intSample.MarshalText()
want, _ := bigSample.MarshalText()
want := []byte(wantHex)
if !bytes.Equal(have, want) {
t.Fatalf("test %d MarshalText, have %q, want %q", i, have, want)
}
}
{
have, _ := intSample.MarshalJSON()
want := []byte(fmt.Sprintf(`"%s"`, bigSample.Text(10)))
want := []byte(fmt.Sprintf(`"0x%s"`, bigSample.Text(16)))
if !bytes.Equal(have, want) {
t.Fatalf("test %d MarshalJSON, have %q, want %q", i, have, want)
}
Expand Down
Loading