neo-go/pkg/core/state/nep5_test.go
Roman Khimov eb1986d2fc state: use big.Int for NEP5 balances and transfer amounts
In general, NEP5 contracts are not limited to int64. And we have an example
of pnWETH Flamingo token now (with 18 decimals) that easily overflows int64,
so for correctness we need to store big.Int.

And as TransferLog is shared for different purposes I've decided to not make
it variable-length on Neo 2.
2021-02-18 17:28:19 +03:00

85 lines
2 KiB
Go

package state
import (
"math/big"
"math/rand"
"testing"
"time"
"github.com/nspcc-dev/neo-go/pkg/internal/random"
"github.com/nspcc-dev/neo-go/pkg/internal/testserdes"
"github.com/nspcc-dev/neo-go/pkg/io"
"github.com/nspcc-dev/neo-go/pkg/util"
"github.com/stretchr/testify/require"
)
func TestNEP5TransferLog_Append(t *testing.T) {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
expected := []*NEP5Transfer{
randomTransfer(r),
randomTransfer(r),
randomTransfer(r),
randomTransfer(r),
}
lg := new(TransferLog)
for _, tr := range expected {
require.NoError(t, lg.Append(tr))
}
require.Equal(t, len(expected), lg.Size()/NEP5TransferSize)
i := len(expected) - 1
tr := new(NEP5Transfer)
cont, err := lg.ForEach(NEP5TransferSize, tr, func() (bool, error) {
require.Equal(t, expected[i], tr)
i--
return true, nil
})
require.NoError(t, err)
require.True(t, cont)
}
func TestNEP5Tracker_EncodeBinary(t *testing.T) {
expected := &NEP5Tracker{
Balance: big.NewInt(int64(rand.Uint64())),
LastUpdatedBlock: rand.Uint32(),
}
testserdes.EncodeDecodeBinary(t, expected, new(NEP5Tracker))
}
func TestNEP5Transfer_DecodeBinary(t *testing.T) {
expected := &NEP5Transfer{
Asset: util.Uint160{1, 2, 3},
From: util.Uint160{5, 6, 7},
To: util.Uint160{8, 9, 10},
Amount: big.NewInt(42),
Block: 12345,
Timestamp: 54321,
Tx: util.Uint256{8, 5, 3},
}
testserdes.EncodeDecodeBinary(t, expected, new(NEP5Transfer))
}
func TestNEP5TransferSize(t *testing.T) {
tr := randomTransfer(rand.New(rand.NewSource(0)))
size := io.GetVarSize(tr)
require.EqualValues(t, NEP5TransferSize, size)
}
func randomTransfer(r *rand.Rand) *NEP5Transfer {
return &NEP5Transfer{
Amount: big.NewInt(int64(r.Uint64())),
Block: r.Uint32(),
Asset: random.Uint160(),
From: random.Uint160(),
To: random.Uint160(),
Tx: random.Uint256(),
}
}
func TestTransfer_Size(t *testing.T) {
require.Equal(t, TransferSize, io.GetVarSize(new(Transfer)))
}