gecko/vms/avm/state_test.go

379 lines
7.3 KiB
Go
Raw Normal View History

2020-03-10 12:20:34 -07:00
// (c) 2019-2020, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package avm
import (
"testing"
"github.com/ava-labs/gecko/ids"
"github.com/ava-labs/gecko/snow/choices"
"github.com/ava-labs/gecko/utils/crypto"
"github.com/ava-labs/gecko/utils/units"
2020-03-23 13:40:37 -07:00
"github.com/ava-labs/gecko/vms/components/ava"
2020-03-10 12:20:34 -07:00
"github.com/ava-labs/gecko/vms/secp256k1fx"
)
func TestStateIDs(t *testing.T) {
_, _, vm := GenesisVM(t)
defer func() {
vm.Shutdown()
ctx.Lock.Unlock()
}()
2020-03-10 12:20:34 -07:00
state := vm.state.state
2020-05-28 20:50:33 -07:00
id0 := ids.NewID([32]byte{0x00, 0})
id1 := ids.NewID([32]byte{0x01, 0})
id2 := ids.NewID([32]byte{0x02, 0})
2020-03-10 12:20:34 -07:00
2020-05-28 07:36:00 -07:00
if _, err := state.IDs(ids.Empty); err != nil {
t.Fatal(err)
2020-03-10 12:20:34 -07:00
}
expected := []ids.ID{id0, id1}
2020-05-28 07:36:00 -07:00
for _, id := range expected {
if err := state.AddID(ids.Empty, id); err != nil {
t.Fatal(err)
}
2020-03-10 12:20:34 -07:00
}
result, err := state.IDs(ids.Empty)
if err != nil {
t.Fatal(err)
}
if len(result) != len(expected) {
t.Fatalf("Returned the wrong number of ids")
}
2020-05-28 20:50:33 -07:00
ids.SortIDs(result)
2020-03-10 12:20:34 -07:00
for i, resultID := range result {
expectedID := expected[i]
if !expectedID.Equals(resultID) {
t.Fatalf("Wrong ID returned")
}
}
2020-05-28 20:50:33 -07:00
for _, id := range expected {
if err := state.RemoveID(ids.Empty, id); err != nil {
t.Fatal(err)
}
}
result, err = state.IDs(ids.Empty)
if err != nil {
t.Fatal(err)
}
if len(result) != 0 {
t.Fatalf("Should have returned 0 IDs")
}
2020-03-10 12:20:34 -07:00
expected = []ids.ID{id1, id2}
2020-05-28 07:36:00 -07:00
for _, id := range expected {
if err := state.AddID(ids.Empty, id); err != nil {
t.Fatal(err)
}
2020-03-10 12:20:34 -07:00
}
result, err = state.IDs(ids.Empty)
if err != nil {
t.Fatal(err)
}
if len(result) != len(expected) {
t.Fatalf("Returned the wrong number of ids")
}
2020-05-28 20:50:33 -07:00
ids.SortIDs(result)
2020-03-10 12:20:34 -07:00
for i, resultID := range result {
expectedID := expected[i]
if !expectedID.Equals(resultID) {
t.Fatalf("Wrong ID returned")
}
}
2020-03-23 13:40:37 -07:00
state.Cache.Flush()
2020-03-10 12:20:34 -07:00
result, err = state.IDs(ids.Empty)
if err != nil {
t.Fatal(err)
}
if len(result) != len(expected) {
t.Fatalf("Returned the wrong number of ids")
}
2020-05-28 20:50:33 -07:00
ids.SortIDs(result)
2020-03-10 12:20:34 -07:00
for i, resultID := range result {
expectedID := expected[i]
if !expectedID.Equals(resultID) {
t.Fatalf("Wrong ID returned")
}
}
if err := state.SetStatus(ids.Empty, choices.Accepted); err != nil {
t.Fatal(err)
}
2020-05-28 20:50:33 -07:00
statusResult, err := state.Status(ids.Empty)
if err != nil {
t.Fatal(err)
}
if statusResult != choices.Accepted {
t.Fatalf("Should have returned the %s status", choices.Accepted)
2020-03-10 12:20:34 -07:00
}
2020-05-28 20:50:33 -07:00
for _, id := range expected {
if err := state.RemoveID(ids.Empty, id); err != nil {
t.Fatal(err)
}
2020-03-10 12:20:34 -07:00
}
2020-05-28 20:50:33 -07:00
result, err = state.IDs(ids.Empty)
2020-03-10 12:20:34 -07:00
if err != nil {
t.Fatal(err)
}
2020-05-28 20:50:33 -07:00
if len(result) != 0 {
t.Fatalf("Should have returned 0 IDs")
2020-03-10 12:20:34 -07:00
}
2020-05-28 07:36:00 -07:00
if err := state.AddID(ids.Empty, ids.ID{}); err == nil {
2020-03-10 12:20:34 -07:00
t.Fatalf("Should have errored during serialization")
}
2020-05-28 20:50:33 -07:00
if err := state.RemoveID(ids.Empty, ids.ID{}); err == nil {
t.Fatalf("Should have errored during serialization")
2020-03-10 12:20:34 -07:00
}
}
func TestStateStatuses(t *testing.T) {
_, _, vm := GenesisVM(t)
defer func() {
vm.Shutdown()
ctx.Lock.Unlock()
}()
2020-03-10 12:20:34 -07:00
state := vm.state.state
if _, err := state.Status(ids.Empty); err == nil {
t.Fatalf("Should have errored when reading ids")
}
if err := state.SetStatus(ids.Empty, choices.Accepted); err != nil {
t.Fatal(err)
}
status, err := state.Status(ids.Empty)
if err != nil {
t.Fatal(err)
}
if status != choices.Accepted {
t.Fatalf("Should have returned the %s status", choices.Accepted)
}
2020-05-28 07:36:00 -07:00
if err := state.AddID(ids.Empty, ids.Empty); err != nil {
2020-03-10 12:20:34 -07:00
t.Fatal(err)
}
status, err = state.Status(ids.Empty)
if err != nil {
t.Fatal(err)
}
if status != choices.Accepted {
t.Fatalf("Should have returned the %s status", choices.Accepted)
}
if err := state.SetStatus(ids.Empty, choices.Unknown); err != nil {
t.Fatal(err)
}
if _, err := state.Status(ids.Empty); err == nil {
t.Fatalf("Should have errored when reading ids")
}
}
func TestStateUTXOs(t *testing.T) {
_, _, vm := GenesisVM(t)
defer func() {
vm.Shutdown()
ctx.Lock.Unlock()
}()
2020-03-10 12:20:34 -07:00
state := vm.state.state
2020-03-25 20:48:21 -07:00
vm.codec.RegisterType(&ava.TestVerifiable{})
2020-03-10 12:20:34 -07:00
if _, err := state.UTXO(ids.Empty); err == nil {
t.Fatalf("Should have errored when reading utxo")
}
2020-03-23 13:40:37 -07:00
utxo := &ava.UTXO{
UTXOID: ava.UTXOID{
2020-03-10 12:20:34 -07:00
TxID: ids.Empty,
OutputIndex: 1,
},
2020-03-23 13:40:37 -07:00
Asset: ava.Asset{ID: ids.Empty},
2020-03-25 20:48:21 -07:00
Out: &ava.TestVerifiable{},
2020-03-10 12:20:34 -07:00
}
if err := state.SetUTXO(ids.Empty, utxo); err != nil {
t.Fatal(err)
}
result, err := state.UTXO(ids.Empty)
if err != nil {
t.Fatal(err)
}
if result.OutputIndex != 1 {
t.Fatalf("Wrong UTXO returned")
}
2020-03-23 13:40:37 -07:00
state.Cache.Flush()
2020-03-10 12:20:34 -07:00
result, err = state.UTXO(ids.Empty)
if err != nil {
t.Fatal(err)
}
if result.OutputIndex != 1 {
t.Fatalf("Wrong UTXO returned")
}
if err := state.SetUTXO(ids.Empty, nil); err != nil {
t.Fatal(err)
}
if _, err := state.UTXO(ids.Empty); err == nil {
t.Fatalf("Should have errored when reading utxo")
}
2020-03-23 13:40:37 -07:00
if err := state.SetUTXO(ids.Empty, &ava.UTXO{}); err == nil {
2020-03-10 12:20:34 -07:00
t.Fatalf("Should have errored packing the utxo")
}
if err := state.SetStatus(ids.Empty, choices.Accepted); err != nil {
t.Fatal(err)
}
if _, err := state.UTXO(ids.Empty); err == nil {
t.Fatalf("Should have errored when reading utxo")
}
2020-03-23 13:40:37 -07:00
state.Cache.Flush()
2020-03-10 12:20:34 -07:00
if _, err := state.UTXO(ids.Empty); err == nil {
t.Fatalf("Should have errored when reading utxo")
}
}
func TestStateTXs(t *testing.T) {
_, _, vm := GenesisVM(t)
defer func() {
vm.Shutdown()
ctx.Lock.Unlock()
}()
2020-03-10 12:20:34 -07:00
state := vm.state.state
2020-03-25 20:48:21 -07:00
vm.codec.RegisterType(&ava.TestTransferable{})
2020-03-10 12:20:34 -07:00
if _, err := state.Tx(ids.Empty); err == nil {
t.Fatalf("Should have errored when reading tx")
}
2020-03-19 15:36:10 -07:00
tx := &Tx{UnsignedTx: &BaseTx{
2020-03-10 12:20:34 -07:00
NetID: networkID,
BCID: chainID,
Ins: []*ava.TransferableInput{{
2020-03-25 20:48:21 -07:00
UTXOID: ava.UTXOID{
TxID: ids.Empty,
OutputIndex: 0,
},
Asset: ava.Asset{ID: asset},
In: &secp256k1fx.TransferInput{
Amt: 20 * units.KiloAva,
Input: secp256k1fx.Input{
SigIndices: []uint32{
0,
2020-03-10 12:20:34 -07:00
},
},
},
2020-03-25 20:48:21 -07:00
}},
2020-03-19 15:36:10 -07:00
}}
2020-03-10 12:20:34 -07:00
unsignedBytes, err := vm.codec.Marshal(tx.UnsignedTx)
if err != nil {
t.Fatal(err)
}
key := keys[0]
sig, err := key.Sign(unsignedBytes)
if err != nil {
t.Fatal(err)
}
fixedSig := [crypto.SECP256K1RSigLen]byte{}
copy(fixedSig[:], sig)
2020-03-19 15:36:10 -07:00
tx.Creds = append(tx.Creds, &secp256k1fx.Credential{
Sigs: [][crypto.SECP256K1RSigLen]byte{
fixedSig,
2020-03-10 12:20:34 -07:00
},
})
b, err := vm.codec.Marshal(tx)
if err != nil {
t.Fatal(err)
}
tx.Initialize(b)
if err := state.SetTx(ids.Empty, tx); err != nil {
t.Fatal(err)
}
result, err := state.Tx(ids.Empty)
if err != nil {
t.Fatal(err)
}
if !result.ID().Equals(tx.ID()) {
t.Fatalf("Wrong Tx returned")
}
2020-03-23 13:40:37 -07:00
state.Cache.Flush()
2020-03-10 12:20:34 -07:00
result, err = state.Tx(ids.Empty)
if err != nil {
t.Fatal(err)
}
if !result.ID().Equals(tx.ID()) {
t.Fatalf("Wrong Tx returned")
}
if err := state.SetTx(ids.Empty, nil); err != nil {
t.Fatal(err)
}
if _, err := state.Tx(ids.Empty); err == nil {
t.Fatalf("Should have errored when reading tx")
}
if err := state.SetStatus(ids.Empty, choices.Accepted); err != nil {
t.Fatal(err)
}
if _, err := state.Tx(ids.Empty); err == nil {
t.Fatalf("Should have errored when reading tx")
}
2020-03-23 13:40:37 -07:00
state.Cache.Flush()
2020-03-10 12:20:34 -07:00
if _, err := state.Tx(ids.Empty); err == nil {
t.Fatalf("Should have errored when reading tx")
}
}