gecko/vms/avm/operation_test.go

126 lines
2.6 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
)
2020-03-29 22:08:45 -07:00
type testOperable struct {
ava.TestTransferable `serialize:"true"`
Outputs []verify.Verifiable `serialize:"true"`
}
func (o *testOperable) Outs() []verify.Verifiable { return o.Outputs }
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")
}
}
2020-03-29 22:08:45 -07:00
func TestOperationVerifyUTXOIDsNotSorted(t *testing.T) {
2020-03-10 12:20:34 -07:00
c := codec.NewDefault()
op := &Operation{
2020-03-23 13:40:37 -07:00
Asset: ava.Asset{ID: ids.Empty},
2020-03-29 22:08:45 -07:00
UTXOIDs: []*ava.UTXOID{
&ava.UTXOID{
TxID: ids.Empty,
OutputIndex: 1,
2020-03-10 12:20:34 -07:00
},
2020-03-29 22:08:45 -07:00
&ava.UTXOID{
TxID: ids.Empty,
OutputIndex: 0,
2020-03-10 12:20:34 -07:00
},
},
2020-03-29 22:08:45 -07:00
Op: &testOperable{},
2020-03-10 12:20:34 -07:00
}
if err := op.Verify(c); err == nil {
2020-03-29 22:08:45 -07:00
t.Fatalf("Should have errored due to unsorted utxoIDs")
2020-03-10 12:20:34 -07:00
}
}
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-29 22:08:45 -07:00
UTXOIDs: []*ava.UTXOID{
&ava.UTXOID{
TxID: ids.Empty,
OutputIndex: 1,
},
2020-03-10 12:20:34 -07:00
},
2020-03-29 22:08:45 -07:00
Op: &testOperable{},
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-29 22:08:45 -07:00
c.RegisterType(&testOperable{})
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-29 22:08:45 -07:00
UTXOIDs: []*ava.UTXOID{
&ava.UTXOID{
TxID: ids.Empty,
OutputIndex: 1,
2020-03-10 12:20:34 -07:00
},
},
2020-03-29 22:08:45 -07:00
Op: &testOperable{},
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-29 22:08:45 -07:00
UTXOIDs: []*ava.UTXOID{
&ava.UTXOID{
TxID: ids.Empty,
OutputIndex: 0,
2020-03-10 12:20:34 -07:00
},
},
2020-03-29 22:08:45 -07:00
Op: &testOperable{},
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-29 22:08:45 -07:00
UTXOIDs: []*ava.UTXOID{
&ava.UTXOID{
TxID: ids.Empty,
OutputIndex: 1,
2020-03-10 12:20:34 -07:00
},
},
2020-03-29 22:08:45 -07:00
Op: &testOperable{},
2020-03-10 12:20:34 -07:00
})
if isSortedAndUniqueOperations(ops, c) {
t.Fatalf("Shouldn't be unique")
}
}