2019-11-28 16:06:09 +00:00
|
|
|
package state
|
2018-03-21 16:11:04 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2019-11-25 17:39:11 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/core/testutil"
|
2019-08-27 13:29:42 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/crypto/keys"
|
2019-09-16 09:18:13 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/io"
|
2018-03-21 16:11:04 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/util"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestDecodeEncodeAccountState(t *testing.T) {
|
|
|
|
var (
|
|
|
|
n = 10
|
2019-11-15 17:35:09 +00:00
|
|
|
balances = make(map[util.Uint256][]UnspentBalance)
|
2019-08-27 13:29:42 +00:00
|
|
|
votes = make([]*keys.PublicKey, n)
|
2018-03-21 16:11:04 +00:00
|
|
|
)
|
|
|
|
for i := 0; i < n; i++ {
|
2019-11-25 17:39:11 +00:00
|
|
|
asset := testutil.RandomUint256()
|
2019-11-15 17:35:09 +00:00
|
|
|
for j := 0; j < i+1; j++ {
|
|
|
|
balances[asset] = append(balances[asset], UnspentBalance{
|
2019-11-25 17:39:11 +00:00
|
|
|
Tx: testutil.RandomUint256(),
|
|
|
|
Index: uint16(testutil.RandomInt(0, 65535)),
|
|
|
|
Value: util.Fixed8(int64(testutil.RandomInt(1, 10000))),
|
2019-11-15 17:35:09 +00:00
|
|
|
})
|
|
|
|
}
|
2019-09-04 20:03:25 +00:00
|
|
|
k, err := keys.NewPrivateKey()
|
|
|
|
assert.Nil(t, err)
|
2019-09-05 06:35:02 +00:00
|
|
|
votes[i] = k.PublicKey()
|
2018-03-21 16:11:04 +00:00
|
|
|
}
|
|
|
|
|
2019-11-28 16:06:09 +00:00
|
|
|
a := &Account{
|
2018-03-25 10:45:54 +00:00
|
|
|
Version: 0,
|
2019-11-25 17:39:11 +00:00
|
|
|
ScriptHash: testutil.RandomUint160(),
|
2018-03-21 16:11:04 +00:00
|
|
|
IsFrozen: true,
|
|
|
|
Votes: votes,
|
|
|
|
Balances: balances,
|
|
|
|
}
|
|
|
|
|
2019-09-16 09:18:13 +00:00
|
|
|
buf := io.NewBufBinWriter()
|
2019-09-16 16:31:49 +00:00
|
|
|
a.EncodeBinary(buf.BinWriter)
|
|
|
|
assert.Nil(t, buf.Err)
|
2018-03-21 16:11:04 +00:00
|
|
|
|
2019-11-28 16:06:09 +00:00
|
|
|
aDecode := &Account{}
|
2019-09-16 16:31:49 +00:00
|
|
|
r := io.NewBinReaderFromBuf(buf.Bytes())
|
|
|
|
aDecode.DecodeBinary(r)
|
|
|
|
assert.Nil(t, r.Err)
|
2018-03-21 16:11:04 +00:00
|
|
|
|
2018-03-25 10:45:54 +00:00
|
|
|
assert.Equal(t, a.Version, aDecode.Version)
|
2018-03-21 16:11:04 +00:00
|
|
|
assert.Equal(t, a.ScriptHash, aDecode.ScriptHash)
|
|
|
|
assert.Equal(t, a.IsFrozen, aDecode.IsFrozen)
|
|
|
|
|
|
|
|
for i, vote := range a.Votes {
|
|
|
|
assert.Equal(t, vote.X, aDecode.Votes[i].X)
|
|
|
|
}
|
|
|
|
assert.Equal(t, a.Balances, aDecode.Balances)
|
|
|
|
}
|
2019-11-15 17:35:09 +00:00
|
|
|
|
|
|
|
func TestAccountStateBalanceValues(t *testing.T) {
|
2019-11-25 17:39:11 +00:00
|
|
|
asset1 := testutil.RandomUint256()
|
|
|
|
asset2 := testutil.RandomUint256()
|
2019-11-28 16:06:09 +00:00
|
|
|
as := Account{Balances: make(map[util.Uint256][]UnspentBalance)}
|
2019-11-15 17:35:09 +00:00
|
|
|
ref := 0
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
ref += i
|
|
|
|
as.Balances[asset1] = append(as.Balances[asset1], UnspentBalance{Value: util.Fixed8(i)})
|
|
|
|
as.Balances[asset2] = append(as.Balances[asset2], UnspentBalance{Value: util.Fixed8(i * 10)})
|
|
|
|
}
|
|
|
|
bVals := as.GetBalanceValues()
|
|
|
|
assert.Equal(t, util.Fixed8(ref), bVals[asset1])
|
|
|
|
assert.Equal(t, util.Fixed8(ref*10), bVals[asset2])
|
|
|
|
}
|