gecko/vms/avm/operation_test.go

154 lines
3.2 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"
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/components/codec"
2020-03-19 15:36:10 -07:00
"github.com/ava-labs/gecko/vms/components/verify"
2020-03-10 12:20:34 -07:00
)
func TestOperationVerifyNil(t *testing.T) {
c := codec.NewDefault()
op := (*Operation)(nil)
if err := op.Verify(c); err == nil {
t.Fatalf("Should have errored due to nil operation")
}
}
func TestOperationVerifyEmpty(t *testing.T) {
c := codec.NewDefault()
op := &Operation{
2020-03-23 13:40:37 -07:00
Asset: ava.Asset{ID: ids.Empty},
2020-03-10 12:20:34 -07:00
}
if err := op.Verify(c); err == nil {
t.Fatalf("Should have errored due to empty operation")
}
}
func TestOperationVerifyInvalidInput(t *testing.T) {
c := codec.NewDefault()
op := &Operation{
2020-03-23 13:40:37 -07:00
Asset: ava.Asset{ID: ids.Empty},
2020-03-10 12:20:34 -07:00
Ins: []*OperableInput{
&OperableInput{},
},
}
if err := op.Verify(c); err == nil {
t.Fatalf("Should have errored due to an invalid input")
}
}
func TestOperationVerifyInputsNotSorted(t *testing.T) {
c := codec.NewDefault()
op := &Operation{
2020-03-23 13:40:37 -07:00
Asset: ava.Asset{ID: ids.Empty},
2020-03-10 12:20:34 -07:00
Ins: []*OperableInput{
&OperableInput{
2020-03-23 13:40:37 -07:00
UTXOID: ava.UTXOID{
2020-03-10 12:20:34 -07:00
TxID: ids.Empty,
OutputIndex: 1,
},
2020-03-25 20:48:21 -07:00
In: &ava.TestVerifiable{},
2020-03-10 12:20:34 -07:00
},
&OperableInput{
2020-03-23 13:40:37 -07:00
UTXOID: ava.UTXOID{
2020-03-10 12:20:34 -07:00
TxID: ids.Empty,
OutputIndex: 0,
},
2020-03-25 20:48:21 -07:00
In: &ava.TestVerifiable{},
2020-03-10 12:20:34 -07:00
},
},
}
if err := op.Verify(c); err == nil {
t.Fatalf("Should have errored due to unsorted inputs")
}
}
func TestOperationVerifyOutputsNotSorted(t *testing.T) {
c := codec.NewDefault()
2020-03-25 20:48:21 -07:00
c.RegisterType(&ava.TestTransferable{})
2020-03-10 12:20:34 -07:00
op := &Operation{
2020-03-23 13:40:37 -07:00
Asset: ava.Asset{ID: ids.Empty},
2020-03-19 15:36:10 -07:00
Outs: []verify.Verifiable{
2020-03-25 20:48:21 -07:00
&ava.TestTransferable{Val: 1},
&ava.TestTransferable{Val: 0},
2020-03-10 12:20:34 -07:00
},
}
if err := op.Verify(c); err == nil {
t.Fatalf("Should have errored due to unsorted outputs")
}
}
func TestOperationVerify(t *testing.T) {
c := codec.NewDefault()
op := &Operation{
2020-03-23 13:40:37 -07:00
Asset: ava.Asset{ID: ids.Empty},
2020-03-19 15:36:10 -07:00
Outs: []verify.Verifiable{
2020-03-25 20:48:21 -07:00
&ava.TestVerifiable{},
2020-03-10 12:20:34 -07:00
},
}
if err := op.Verify(c); err != nil {
t.Fatal(err)
}
}
func TestOperationSorting(t *testing.T) {
c := codec.NewDefault()
2020-03-25 20:48:21 -07:00
c.RegisterType(&ava.TestVerifiable{})
2020-03-10 12:20:34 -07:00
ops := []*Operation{
&Operation{
2020-03-23 13:40:37 -07:00
Asset: ava.Asset{ID: ids.Empty},
2020-03-10 12:20:34 -07:00
Ins: []*OperableInput{
&OperableInput{
2020-03-23 13:40:37 -07:00
UTXOID: ava.UTXOID{
2020-03-10 12:20:34 -07:00
TxID: ids.Empty,
OutputIndex: 1,
},
2020-03-25 20:48:21 -07:00
In: &ava.TestVerifiable{},
2020-03-10 12:20:34 -07:00
},
},
},
&Operation{
2020-03-23 13:40:37 -07:00
Asset: ava.Asset{ID: ids.Empty},
2020-03-10 12:20:34 -07:00
Ins: []*OperableInput{
&OperableInput{
2020-03-23 13:40:37 -07:00
UTXOID: ava.UTXOID{
2020-03-10 12:20:34 -07:00
TxID: ids.Empty,
OutputIndex: 0,
},
2020-03-25 20:48:21 -07:00
In: &ava.TestVerifiable{},
2020-03-10 12:20:34 -07:00
},
},
},
}
if isSortedAndUniqueOperations(ops, c) {
t.Fatalf("Shouldn't be sorted")
}
sortOperations(ops, c)
if !isSortedAndUniqueOperations(ops, c) {
t.Fatalf("Should be sorted")
}
ops = append(ops, &Operation{
2020-03-23 13:40:37 -07:00
Asset: ava.Asset{ID: ids.Empty},
2020-03-10 12:20:34 -07:00
Ins: []*OperableInput{
&OperableInput{
2020-03-23 13:40:37 -07:00
UTXOID: ava.UTXOID{
2020-03-10 12:20:34 -07:00
TxID: ids.Empty,
OutputIndex: 1,
},
2020-03-25 20:48:21 -07:00
In: &ava.TestVerifiable{},
2020-03-10 12:20:34 -07:00
},
},
})
if isSortedAndUniqueOperations(ops, c) {
t.Fatalf("Shouldn't be unique")
}
}