Skip to content

Commit

Permalink
fix tokenvm integration error
Browse files Browse the repository at this point in the history
  • Loading branch information
wlawt committed Apr 26, 2024
1 parent eb4e1b1 commit 0d6c2a2
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 34 deletions.
2 changes: 1 addition & 1 deletion codec/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const (
maxBech32Size = 90
)

type ActionID Address
type ActionID [AddressLen]byte

type Address [AddressLen]byte

Expand Down
2 changes: 1 addition & 1 deletion codec/packer.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (p *Packer) PackActionID(a ActionID) {
func (p *Packer) UnpackActionID(required bool, dest *ActionID) {
copy((*dest)[:], p.p.UnpackFixedBytes(AddressLen))
if required && *dest == EmptyAddress {
p.addErr(fmt.Errorf("%w: Address field is not populated", ErrFieldNotPopulated))
p.addErr(fmt.Errorf("%w: ActionID field is not populated", ErrFieldNotPopulated))
}
}

Expand Down
8 changes: 4 additions & 4 deletions examples/tokenvm/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ var (

balanceKeyPool = sync.Pool{
New: func() any {
return make([]byte, 1+codec.AddressLen+consts.IDLen+consts.Uint16Len)
return make([]byte, 1+codec.AddressLen*2+consts.Uint16Len)
},
}
)
Expand Down Expand Up @@ -462,11 +462,11 @@ func DeleteOrder(ctx context.Context, mu state.Mutable, order codec.ActionID) er

// [loanPrefix] + [asset] + [destination]
func LoanKey(asset codec.ActionID, destination ids.ID) (k []byte) {
k = make([]byte, 1+consts.IDLen*2+consts.Uint16Len)
k = make([]byte, 1+codec.AddressLen+consts.IDLen+consts.Uint16Len)
k[0] = loanPrefix
copy(k[1:], asset[:])
copy(k[1+consts.IDLen:], destination[:])
binary.BigEndian.PutUint16(k[1+consts.IDLen*2:], LoanChunks)
copy(k[1+codec.AddressLen:], destination[:])
binary.BigEndian.PutUint16(k[1+codec.AddressLen+consts.IDLen:], LoanChunks)
return
}

Expand Down
6 changes: 3 additions & 3 deletions examples/tokenvm/tests/integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,19 +431,19 @@ var _ = ginkgo.Describe("[Tx Processing]", func() {
// read: 2 keys reads, 1 had 0 chunks
// allocate: 1 key created
// write: 1 key modified, 1 key new
transferTxConsumed := fees.Dimensions{223, 7, 12, 25, 26}
transferTxConsumed := fees.Dimensions{228, 7, 12, 25, 26}
gomega.Ω(results[0].Consumed).Should(gomega.Equal(transferTxConsumed))

// Fee explanation
//
// Multiply all unit consumption by 1 and sum
gomega.Ω(results[0].Fee).Should(gomega.Equal(uint64(293)))
gomega.Ω(results[0].Fee).Should(gomega.Equal(uint64(298)))
})

ginkgo.By("ensure balance is updated", func() {
balance, err := instances[1].tcli.Balance(context.Background(), sender, codec.EmptyAddress)
gomega.Ω(err).To(gomega.BeNil())
gomega.Ω(balance).To(gomega.Equal(uint64(9899707)))
gomega.Ω(balance).To(gomega.Equal(uint64(9899702)))
balance2, err := instances[1].tcli.Balance(context.Background(), sender2, codec.EmptyAddress)
gomega.Ω(err).To(gomega.BeNil())
gomega.Ω(balance2).To(gomega.Equal(uint64(100000)))
Expand Down
103 changes: 78 additions & 25 deletions heap/heap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,41 @@ package heap
import (
"testing"

"github.com/ava-labs/hypersdk/codec"

"github.com/ava-labs/avalanchego/ids"
"github.com/stretchr/testify/require"
)

type testItem struct {
id ids.ID
type testItem[T comparable] struct {
id T
value uint64
}

func TestUnit64HeapPushPopMin(t *testing.T) {
require := require.New(t)
minHeap := New[ids.ID, *testItem, uint64](0, true)
minHeap := New[ids.ID, *testItem[ids.ID], uint64](0, true)
require.Zero(minHeap.Len(), "heap not initialized properly.")
mempoolItem1 := &testItem{ids.GenerateTestID(), 10}
mempoolItem2 := &testItem{ids.GenerateTestID(), 7}
mempoolItem3 := &testItem{ids.GenerateTestID(), 15}
mempoolItem1 := &testItem[ids.ID]{ids.GenerateTestID(), 10}
mempoolItem2 := &testItem[ids.ID]{ids.GenerateTestID(), 7}
mempoolItem3 := &testItem[ids.ID]{ids.GenerateTestID(), 15}

// Middle UnitPrice
med := &Entry[ids.ID, *testItem, uint64]{
med := &Entry[ids.ID, *testItem[ids.ID], uint64]{
ID: mempoolItem1.id,
Item: mempoolItem1,
Val: mempoolItem1.value,
Index: minHeap.Len(),
}
// Lesser UnitPrice
low := &Entry[ids.ID, *testItem, uint64]{
low := &Entry[ids.ID, *testItem[ids.ID], uint64]{
ID: mempoolItem2.id,
Item: mempoolItem2,
Val: mempoolItem2.value,
Index: minHeap.Len(),
}
// Greatest UnitPrice
high := &Entry[ids.ID, *testItem, uint64]{
high := &Entry[ids.ID, *testItem[ids.ID], uint64]{
ID: mempoolItem3.id,
Item: mempoolItem3,
Val: mempoolItem3.value,
Expand Down Expand Up @@ -67,29 +69,29 @@ func TestUnit64HeapPushPopMin(t *testing.T) {

func TestUnit64HeapPushPopMax(t *testing.T) {
require := require.New(t)
maxHeap := New[ids.ID, *testItem, uint64](0, false)
maxHeap := New[ids.ID, *testItem[ids.ID], uint64](0, false)
require.Zero(maxHeap.Len(), "heap not initialized properly.")

mempoolItem1 := &testItem{ids.GenerateTestID(), 10}
mempoolItem2 := &testItem{ids.GenerateTestID(), 7}
mempoolItem3 := &testItem{ids.GenerateTestID(), 15}
mempoolItem1 := &testItem[ids.ID]{ids.GenerateTestID(), 10}
mempoolItem2 := &testItem[ids.ID]{ids.GenerateTestID(), 7}
mempoolItem3 := &testItem[ids.ID]{ids.GenerateTestID(), 15}

// Middle UnitPrice
med := &Entry[ids.ID, *testItem, uint64]{
med := &Entry[ids.ID, *testItem[ids.ID], uint64]{
ID: mempoolItem1.id,
Item: mempoolItem1,
Val: mempoolItem1.value,
Index: maxHeap.Len(),
}
// Lesser UnitPrice
low := &Entry[ids.ID, *testItem, uint64]{
low := &Entry[ids.ID, *testItem[ids.ID], uint64]{
ID: mempoolItem2.id,
Item: mempoolItem2,
Val: mempoolItem2.value,
Index: maxHeap.Len(),
}
// Greatest UnitPrice
high := &Entry[ids.ID, *testItem, uint64]{
high := &Entry[ids.ID, *testItem[ids.ID], uint64]{
ID: mempoolItem3.id,
Item: mempoolItem3,
Val: mempoolItem3.value,
Expand Down Expand Up @@ -119,10 +121,10 @@ func TestUnit64HeapPushPopMax(t *testing.T) {
func TestUnit64HeapPushExists(t *testing.T) {
// Push an item already in heap
require := require.New(t)
minHeap := New[ids.ID, *testItem, uint64](0, true)
minHeap := New[ids.ID, *testItem[ids.ID], uint64](0, true)
require.Zero(minHeap.Len(), "heap not initialized properly.")
mempoolItem := &testItem{ids.GenerateTestID(), 10}
entry := &Entry[ids.ID, *testItem, uint64]{
mempoolItem := &testItem[ids.ID]{ids.GenerateTestID(), 10}
entry := &Entry[ids.ID, *testItem[ids.ID], uint64]{
ID: mempoolItem.id,
Item: mempoolItem,
Val: mempoolItem.value,
Expand All @@ -142,11 +144,11 @@ func TestUnit64HeapPushExists(t *testing.T) {
func TestUnit64HeapGetID(t *testing.T) {
// Push an item and grab its ID
require := require.New(t)
minHeap := New[ids.ID, *testItem, uint64](0, true)
minHeap := New[ids.ID, *testItem[ids.ID], uint64](0, true)
require.Zero(minHeap.Len(), "heap not initialized properly.")

mempoolItem := &testItem{ids.GenerateTestID(), 10}
entry := &Entry[ids.ID, *testItem, uint64]{
mempoolItem := &testItem[ids.ID]{ids.GenerateTestID(), 10}
entry := &Entry[ids.ID, *testItem[ids.ID], uint64]{
ID: mempoolItem.id,
Item: mempoolItem,
Val: mempoolItem.value,
Expand All @@ -164,10 +166,10 @@ func TestUnit64HeapGetID(t *testing.T) {

func TestUnit64HeapHasID(t *testing.T) {
require := require.New(t)
minHeap := New[ids.ID, *testItem, uint64](0, true)
minHeap := New[ids.ID, *testItem[ids.ID], uint64](0, true)
require.Zero(minHeap.Len(), "heap not initialized properly.")
mempoolItem := &testItem{ids.GenerateTestID(), 10}
entry := &Entry[ids.ID, *testItem, uint64]{
mempoolItem := &testItem[ids.ID]{ids.GenerateTestID(), 10}
entry := &Entry[ids.ID, *testItem[ids.ID], uint64]{
ID: mempoolItem.id,
Item: mempoolItem,
Val: mempoolItem.value,
Expand All @@ -181,3 +183,54 @@ func TestUnit64HeapHasID(t *testing.T) {
ok = minHeap.Has(mempoolItem.id)
require.True(ok, "Entry was not found in heap.")
}

func TestUnit64HeapPushPopMinForActionID(t *testing.T) {
require := require.New(t)
minHeap := New[codec.ActionID, *testItem[codec.ActionID], uint64](0, true)
require.Zero(minHeap.Len(), "heap not initialized properly.")
txID := ids.GenerateTestID()
mempoolItem1 := &testItem[codec.ActionID]{codec.CreateActionID(0, txID), 10}
mempoolItem2 := &testItem[codec.ActionID]{codec.CreateActionID(1, txID), 7}
mempoolItem3 := &testItem[codec.ActionID]{codec.CreateActionID(2, txID), 15}

// Middle UnitPrice
med := &Entry[codec.ActionID, *testItem[codec.ActionID], uint64]{
ID: mempoolItem1.id,
Item: mempoolItem1,
Val: mempoolItem1.value,
Index: minHeap.Len(),
}
// Lesser UnitPrice
low := &Entry[codec.ActionID, *testItem[codec.ActionID], uint64]{
ID: mempoolItem2.id,
Item: mempoolItem2,
Val: mempoolItem2.value,
Index: minHeap.Len(),
}
// Greatest UnitPrice
high := &Entry[codec.ActionID, *testItem[codec.ActionID], uint64]{
ID: mempoolItem3.id,
Item: mempoolItem3,
Val: mempoolItem3.value,
Index: minHeap.Len(),
}
minHeap.Push(med)
minHeap.Push(low)
minHeap.Push(high)
// Added all three
require.Equal(3, minHeap.Len(), "Not pushed correctly.")
// Check if added to lookup table
ok := minHeap.Has(med.ID)
require.True(ok, "Item not found in lookup.")
ok = minHeap.Has(low.ID)
require.True(ok, "Item not found in lookup.")
ok = minHeap.Has(high.ID)
require.True(ok, "Item not found in lookup.")
// Pop and check popped correctly. Order should be 2, 1, 3
popped := minHeap.Pop()
require.Equal(low, popped, "Incorrect item removed.")
popped = minHeap.Pop()
require.Equal(med, popped, "Incorrect item removed.")
popped = minHeap.Pop()
require.Equal(high, popped, "Incorrect item removed.")
}

0 comments on commit 0d6c2a2

Please sign in to comment.