gecko/vms/spdagvm/utxo_test.go

135 lines
3.9 KiB
Go

// (c) 2019-2020, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package spdagvm
import (
"bytes"
"testing"
"github.com/ava-labs/gecko/ids"
"github.com/ava-labs/gecko/utils/crypto"
"github.com/ava-labs/gecko/utils/wrappers"
)
func TestUTXO(t *testing.T) {
skBytes := []byte{
0x98, 0xcb, 0x07, 0x7f, 0x97, 0x2f, 0xeb, 0x04,
0x81, 0xf1, 0xd8, 0x94, 0xf2, 0x72, 0xc6, 0xa1,
0xe3, 0xc1, 0x5e, 0x27, 0x2a, 0x16, 0x58, 0xff,
0x71, 0x64, 0x44, 0xf4, 0x65, 0x20, 0x00, 0x70,
}
outputPaymentBytes := []byte{
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x30, 0x39, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0xd4, 0x31, 0x00, 0x00, 0x00, 0x01,
0x00, 0x00, 0x00, 0x02, 0x51, 0x02, 0x5c, 0x61,
0xfb, 0xcf, 0xc0, 0x78, 0xf6, 0x93, 0x34, 0xf8,
0x34, 0xbe, 0x6d, 0xd2, 0x6d, 0x55, 0xa9, 0x55,
0xc3, 0x34, 0x41, 0x28, 0xe0, 0x60, 0x12, 0x8e,
0xde, 0x35, 0x23, 0xa2, 0x4a, 0x46, 0x1c, 0x89,
0x43, 0xab, 0x08, 0x59,
}
outputTakeOrLeaveBytes := []byte{
0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x30, 0x39, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0xd4, 0x31, 0x00, 0x00, 0x00, 0x01,
0x00, 0x00, 0x00, 0x01, 0x51, 0x02, 0x5c, 0x61,
0xfb, 0xcf, 0xc0, 0x78, 0xf6, 0x93, 0x34, 0xf8,
0x34, 0xbe, 0x6d, 0xd2, 0x6d, 0x55, 0xa9, 0x55,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xdd, 0xd5,
0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
0xc3, 0x34, 0x41, 0x28, 0xe0, 0x60, 0x12, 0x8e,
0xde, 0x35, 0x23, 0xa2, 0x4a, 0x46, 0x1c, 0x89,
0x43, 0xab, 0x08, 0x59,
}
inputPaymentBytes := []byte{
0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03,
0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13,
0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b,
0x1c, 0x1d, 0x1e, 0x1f, 0x00, 0x00, 0x00, 0x09,
0x00, 0x00, 0x00, 0x00, 0x07, 0x5b, 0xcd, 0x15,
0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x07,
}
f := crypto.FactorySECP256K1R{}
sk, err := f.ToPrivateKey(skBytes)
if err != nil {
t.Fatal(err)
}
c := Codec{}
p := wrappers.Packer{Bytes: outputPaymentBytes}
outputPayment := c.unmarshalOutput(&p)
if p.Errored() {
t.Fatal(p.Err)
}
p = wrappers.Packer{Bytes: outputTakeOrLeaveBytes}
outputTakeOrLeave := c.unmarshalOutput(&p)
if p.Errored() {
t.Fatal(p.Err)
}
p = wrappers.Packer{Bytes: inputPaymentBytes}
inputPayment := c.unmarshalInput(&p)
if p.Errored() {
t.Fatal(p.Err)
}
inputPaymentSigner := &InputSigner{
Keys: []*crypto.PrivateKeySECP256K1R{
sk.(*crypto.PrivateKeySECP256K1R),
},
}
b := Builder{
NetworkID: 0,
ChainID: ids.Empty,
}
tx, err := b.NewTx(
/*inputs=*/ []Input{inputPayment},
/*outputs=*/ []Output{outputPayment, outputTakeOrLeave},
/*signers=*/ []*InputSigner{inputPaymentSigner},
)
if err != nil {
t.Fatal(err)
}
utxos := tx.UTXOs()
if len(utxos) != 2 {
t.Fatalf("Produced %d UTXOs", len(utxos))
}
utxo := utxos[1]
utxoBytes, err := c.MarshalUTXO(utxo)
if err != nil {
t.Fatal(err)
}
expected := []byte{
// txID:
0xc7, 0xe6, 0xe2, 0xd3, 0x83, 0xa6, 0xd8, 0xac,
0x54, 0xdc, 0xc8, 0x4e, 0x32, 0x7c, 0x22, 0xa5,
0x6a, 0xf1, 0x25, 0x88, 0x33, 0x75, 0x0f, 0x94,
0x54, 0x1b, 0xdb, 0xa2, 0xc0, 0xac, 0x67, 0x38,
// output index:
0x00, 0x00, 0x00, 0x01,
// output:
0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x30, 0x39, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0xd4, 0x31, 0x00, 0x00, 0x00, 0x01,
0x00, 0x00, 0x00, 0x01, 0x51, 0x02, 0x5c, 0x61,
0xfb, 0xcf, 0xc0, 0x78, 0xf6, 0x93, 0x34, 0xf8,
0x34, 0xbe, 0x6d, 0xd2, 0x6d, 0x55, 0xa9, 0x55,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xdd, 0xd5,
0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
0xc3, 0x34, 0x41, 0x28, 0xe0, 0x60, 0x12, 0x8e,
0xde, 0x35, 0x23, 0xa2, 0x4a, 0x46, 0x1c, 0x89,
0x43, 0xab, 0x08, 0x59,
}
if !bytes.Equal(utxoBytes, expected) {
t.Fatalf("Codec.MarshalUTXO returned:\n0x%x\nExpected:\n0x%x", utxoBytes, expected)
}
}