2018-03-21 16:11:04 +00:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
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
|
|
|
|
balances = make(map[util.Uint256]util.Fixed8)
|
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-08-23 15:50:45 +00:00
|
|
|
balances[randomUint256()] = util.Fixed8(int64(randomInt(1, 10000)))
|
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
|
|
|
}
|
|
|
|
|
|
|
|
a := &AccountState{
|
2018-03-25 10:45:54 +00:00
|
|
|
Version: 0,
|
2019-08-23 15:50:45 +00:00
|
|
|
ScriptHash: 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()
|
|
|
|
if err := a.EncodeBinary(buf.BinWriter); err != nil {
|
2018-03-21 16:11:04 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
aDecode := &AccountState{}
|
2019-09-16 09:18:13 +00:00
|
|
|
if err := aDecode.DecodeBinary(io.NewBinReaderFromBuf(buf.Bytes())); err != nil {
|
2018-03-21 16:11:04 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|