mirror of
https://github.com/alexgo-io/gaze-indexer.git
synced 2026-04-29 12:15:13 +08:00
refactor: leb128 to pkg
This commit is contained in:
@@ -10,7 +10,7 @@ import (
|
||||
"github.com/btcsuite/btcd/wire"
|
||||
"github.com/cockroachdb/errors"
|
||||
"github.com/gaze-network/indexer-network/common/errs"
|
||||
"github.com/gaze-network/indexer-network/lib/leb128"
|
||||
"github.com/gaze-network/indexer-network/pkg/leb128"
|
||||
"github.com/gaze-network/uint128"
|
||||
"github.com/samber/lo"
|
||||
)
|
||||
@@ -37,7 +37,7 @@ func (r Runestone) Encipher() ([]byte, error) {
|
||||
var payload []byte
|
||||
|
||||
encodeUint128 := func(value uint128.Uint128) {
|
||||
payload = append(payload, leb128.EncodeLEB128(value)...)
|
||||
payload = append(payload, leb128.EncodeUint128(value)...)
|
||||
}
|
||||
encodeTagValues := func(tag Tag, values ...uint128.Uint128) {
|
||||
for _, value := range values {
|
||||
@@ -350,7 +350,7 @@ func decodeLEB128VarIntsFromPayload(payload []byte) ([]uint128.Uint128, error) {
|
||||
i := 0
|
||||
|
||||
for i < len(payload) {
|
||||
n, length, err := leb128.DecodeLEB128(payload[i:])
|
||||
n, length, err := leb128.DecodeUint128(payload[i:])
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "cannot decode LEB128 varint")
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
"github.com/Cleverse/go-utilities/utils"
|
||||
"github.com/btcsuite/btcd/txscript"
|
||||
"github.com/btcsuite/btcd/wire"
|
||||
"github.com/gaze-network/indexer-network/lib/leb128"
|
||||
"github.com/gaze-network/indexer-network/pkg/leb128"
|
||||
"github.com/gaze-network/uint128"
|
||||
"github.com/samber/lo"
|
||||
"github.com/stretchr/testify/assert"
|
||||
@@ -16,7 +16,7 @@ import (
|
||||
func encodeLEB128VarIntsToPayload(integers []uint128.Uint128) []byte {
|
||||
payload := make([]byte, 0)
|
||||
for _, integer := range integers {
|
||||
payload = append(payload, leb128.EncodeLEB128(integer)...)
|
||||
payload = append(payload, leb128.EncodeUint128(integer)...)
|
||||
}
|
||||
return payload
|
||||
}
|
||||
@@ -124,8 +124,8 @@ func TestDecipherRunestone(t *testing.T) {
|
||||
AddOp(RUNESTONE_PAYLOAD_MAGIC_NUMBER).
|
||||
AddOp(txscript.OP_VERIFY).
|
||||
AddData([]byte{0}).
|
||||
AddData(leb128.EncodeLEB128(uint128.From64(1))).
|
||||
AddData(leb128.EncodeLEB128(uint128.From64(1))).
|
||||
AddData(leb128.EncodeUint128(uint128.From64(1))).
|
||||
AddData(leb128.EncodeUint128(uint128.From64(1))).
|
||||
AddData([]byte{2, 0}).
|
||||
Script()),
|
||||
&Runestone{
|
||||
@@ -139,8 +139,8 @@ func TestDecipherRunestone(t *testing.T) {
|
||||
AddOp(txscript.OP_RETURN).
|
||||
AddOp(RUNESTONE_PAYLOAD_MAGIC_NUMBER).
|
||||
AddData([]byte{0}).
|
||||
AddData(leb128.EncodeLEB128(uint128.From64(1))).
|
||||
AddData(leb128.EncodeLEB128(uint128.From64(2))).
|
||||
AddData(leb128.EncodeUint128(uint128.From64(1))).
|
||||
AddData(leb128.EncodeUint128(uint128.From64(2))).
|
||||
AddData([]byte{3, 0}).
|
||||
Script()),
|
||||
&Runestone{
|
||||
|
||||
@@ -10,7 +10,7 @@ const (
|
||||
ErrUnterminated = errs.ErrorKind("leb128: unterminated byte sequence")
|
||||
)
|
||||
|
||||
func EncodeLEB128(input uint128.Uint128) []byte {
|
||||
func EncodeUint128(input uint128.Uint128) []byte {
|
||||
bytes := make([]byte, 0)
|
||||
// for n >> 7 > 0
|
||||
for !input.Rsh(7).IsZero() {
|
||||
@@ -23,7 +23,7 @@ func EncodeLEB128(input uint128.Uint128) []byte {
|
||||
return bytes
|
||||
}
|
||||
|
||||
func DecodeLEB128(data []byte) (n uint128.Uint128, length int, err error) {
|
||||
func DecodeUint128(data []byte) (n uint128.Uint128, length int, err error) {
|
||||
if len(data) == 0 {
|
||||
return uint128.Uint128{}, 0, ErrEmpty
|
||||
}
|
||||
@@ -12,8 +12,8 @@ func TestRoundTrip(t *testing.T) {
|
||||
test := func(n uint128.Uint128) {
|
||||
t.Run(n.String(), func(t *testing.T) {
|
||||
t.Parallel()
|
||||
encoded := EncodeLEB128(n)
|
||||
decoded, length, err := DecodeLEB128(encoded)
|
||||
encoded := EncodeUint128(n)
|
||||
decoded, length, err := DecodeUint128(encoded)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, n, decoded)
|
||||
assert.Equal(t, len(encoded), length)
|
||||
@@ -40,7 +40,7 @@ func TestDecodeError(t *testing.T) {
|
||||
testError := func(name string, bytes []byte, expectedError error) {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
_, _, err := DecodeLEB128(bytes)
|
||||
_, _, err := DecodeUint128(bytes)
|
||||
if expectedError == nil {
|
||||
assert.NoError(t, err)
|
||||
} else {
|
||||
Reference in New Issue
Block a user