Skip to content

Commit

Permalink
Use leb128 encoding for token amounts (#571)
Browse files Browse the repository at this point in the history
This work towards #340.
  • Loading branch information
phritz authored Jun 21, 2018
1 parent 91a008c commit 0ea4ea2
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
2 changes: 1 addition & 1 deletion actor/builtin/miner/miner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func TestAddAsk(t *testing.T) {

result, err := core.ApplyMessage(ctx, st, msg, types.NewBlockHeight(0))
assert.NoError(err)
assert.Equal(types.NewAttoFILFromFIL(0), types.NewAttoFILFromBytes(result.Receipt.Return[0]))
assert.Equal(0, big.NewInt(0).Cmp(big.NewInt(0).SetBytes(result.Receipt.Return[0])))

storageMkt, err := st.GetActor(ctx, address.StorageMarketAddress)
assert.NoError(err)
Expand Down
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,12 @@
"hash": "QmPXvegq26x982cQjSfbTvSzZXn7GiaMwhhVPHkeTEhrPT",
"name": "sys",
"version": "0.1.0"
},
{
"author": "phritz",
"hash": "QmSKyB5faguXT4NqbrXpnRXqaVj5DhSm7x9BtzFydBY1UK",
"name": "go-leb128",
"version": "0.0.0"
}
],
"gxVersion": "0.12.1",
Expand Down
23 changes: 12 additions & 11 deletions types/atto_fil.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"math/big"

cbor "gx/ipfs/QmRiRJhn427YVuufBEHofLreKWNw7P7BWNq86Sb9kzqdbd/go-ipld-cbor"
"gx/ipfs/QmSKyB5faguXT4NqbrXpnRXqaVj5DhSm7x9BtzFydBY1UK/go-leb128"
"gx/ipfs/QmcrriCMhjb5ZWzmPNxmP53px47tSPcXBNaMtLdgcKFJYk/refmt/obj/atlas"
)

Expand Down Expand Up @@ -89,28 +90,28 @@ func NewAttoFILFromFIL(x uint64) *AttoFIL {
// NewAttoFILFromBytes allocates and returns a new AttoFIL set
// to the value of buf as the bytes of a big-endian unsigned integer.
func NewAttoFILFromBytes(buf []byte) *AttoFIL {
ta := NewZeroAttoFIL()
ta.val.SetBytes(buf)
return ta
af := NewZeroAttoFIL()
af.val = leb128.ToBigInt(buf)
return af
}

// NewAttoFILFromFILString allocates a new AttoFIL set to the value of s filecoin,
// interpreted in the given base, and returns it and a boolean indicating success.
func NewAttoFILFromFILString(s string, base int) (*AttoFIL, bool) {
ta := NewZeroAttoFIL()
_, ok := ta.val.SetString(s, base)
af := NewZeroAttoFIL()
_, ok := af.val.SetString(s, base)
if ok {
ta.val = ta.val.Mul(ta.val, tenToTheEighteen)
af.val = af.val.Mul(af.val, tenToTheEighteen)
}
return ta, ok
return af, ok
}

// NewAttoFILFromString allocates a new AttoFIL set to the value of s attofilecoin,
// interpreted in the given base, and returns it and a boolean indicating success.
func NewAttoFILFromString(s string, base int) (*AttoFIL, bool) {
ta := NewZeroAttoFIL()
_, ok := ta.val.SetString(s, base)
return ta, ok
af := NewZeroAttoFIL()
_, ok := af.val.SetString(s, base)
return af, ok
}

// Add sets z to the sum x+y and returns z.
Expand Down Expand Up @@ -182,7 +183,7 @@ func (z *AttoFIL) IsZero() bool {
// Bytes returns the absolute value of x as a big-endian byte slice.
func (z *AttoFIL) Bytes() []byte {
ensureZeroAmounts(&z)
return z.val.Bytes()
return leb128.FromBigInt(z.val)
}

// String prints the quantity of *FILECOIN* -- *NOT* attofilecoin -- that
Expand Down

0 comments on commit 0ea4ea2

Please sign in to comment.