2020-03-05 07:45:50 +00:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/rand"
|
|
|
|
"testing"
|
2020-03-05 12:16:03 +00:00
|
|
|
"time"
|
2020-03-05 07:45:50 +00:00
|
|
|
|
2020-03-27 07:14:40 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/internal/random"
|
2020-03-26 14:43:24 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/internal/testserdes"
|
2020-03-05 07:45:50 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2020-03-05 12:16:03 +00:00
|
|
|
func TestNEP5TransferLog_Append(t *testing.T) {
|
|
|
|
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
|
|
|
expected := []*NEP5Transfer{
|
2020-03-27 07:14:40 +00:00
|
|
|
randomTransfer(r),
|
|
|
|
randomTransfer(r),
|
|
|
|
randomTransfer(r),
|
|
|
|
randomTransfer(r),
|
2020-03-05 12:16:03 +00:00
|
|
|
}
|
|
|
|
|
2020-08-04 13:55:45 +00:00
|
|
|
lg := new(TransferLog)
|
2020-03-05 12:16:03 +00:00
|
|
|
for _, tr := range expected {
|
|
|
|
require.NoError(t, lg.Append(tr))
|
|
|
|
}
|
|
|
|
|
2020-08-04 13:55:45 +00:00
|
|
|
require.Equal(t, len(expected), lg.Size()/NEP5TransferSize)
|
2020-03-12 09:32:24 +00:00
|
|
|
|
2020-09-08 09:57:45 +00:00
|
|
|
i := len(expected) - 1
|
2020-08-04 13:55:45 +00:00
|
|
|
tr := new(NEP5Transfer)
|
2020-09-08 12:29:07 +00:00
|
|
|
cont, err := lg.ForEach(NEP5TransferSize, tr, func() (bool, error) {
|
2020-03-05 12:16:03 +00:00
|
|
|
require.Equal(t, expected[i], tr)
|
2020-09-08 09:57:45 +00:00
|
|
|
i--
|
2020-09-08 12:29:07 +00:00
|
|
|
return true, nil
|
2020-03-05 12:16:03 +00:00
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
2020-09-08 12:29:07 +00:00
|
|
|
require.True(t, cont)
|
2020-03-05 12:16:03 +00:00
|
|
|
}
|
|
|
|
|
2020-03-05 07:45:50 +00:00
|
|
|
func TestNEP5Tracker_EncodeBinary(t *testing.T) {
|
|
|
|
expected := &NEP5Tracker{
|
|
|
|
Balance: int64(rand.Uint64()),
|
|
|
|
LastUpdatedBlock: rand.Uint32(),
|
|
|
|
}
|
|
|
|
|
2020-03-26 14:43:24 +00:00
|
|
|
testserdes.EncodeDecodeBinary(t, expected, new(NEP5Tracker))
|
2020-03-05 07:45:50 +00:00
|
|
|
}
|
|
|
|
|
2020-03-05 14:11:58 +00:00
|
|
|
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: 42,
|
|
|
|
Block: 12345,
|
|
|
|
Timestamp: 54321,
|
|
|
|
Tx: util.Uint256{8, 5, 3},
|
|
|
|
}
|
|
|
|
|
2020-03-26 14:43:24 +00:00
|
|
|
testserdes.EncodeDecodeBinary(t, expected, new(NEP5Transfer))
|
2020-03-05 14:11:58 +00:00
|
|
|
}
|
|
|
|
|
2020-03-05 17:46:29 +00:00
|
|
|
func TestNEP5TransferSize(t *testing.T) {
|
2020-03-27 07:14:40 +00:00
|
|
|
tr := randomTransfer(rand.New(rand.NewSource(0)))
|
2020-03-05 17:46:29 +00:00
|
|
|
size := io.GetVarSize(tr)
|
|
|
|
require.EqualValues(t, NEP5TransferSize, size)
|
|
|
|
}
|
|
|
|
|
2020-03-27 07:14:40 +00:00
|
|
|
func randomTransfer(r *rand.Rand) *NEP5Transfer {
|
|
|
|
return &NEP5Transfer{
|
2020-03-05 12:16:03 +00:00
|
|
|
Amount: int64(r.Uint64()),
|
|
|
|
Block: r.Uint32(),
|
2020-03-27 07:14:40 +00:00
|
|
|
Asset: random.Uint160(),
|
|
|
|
From: random.Uint160(),
|
|
|
|
To: random.Uint160(),
|
|
|
|
Tx: random.Uint256(),
|
2020-03-05 12:16:03 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-04 14:08:28 +00:00
|
|
|
|
|
|
|
func TestTransfer_Size(t *testing.T) {
|
|
|
|
require.Equal(t, TransferSize, io.GetVarSize(new(Transfer)))
|
|
|
|
}
|