11 "github.com/davecgh/go-spew/spew"
13 "github.com/vapor/common"
14 "github.com/vapor/consensus"
15 "github.com/vapor/crypto"
16 "github.com/vapor/crypto/ed25519/chainkd"
17 chainjson "github.com/vapor/encoding/json"
18 "github.com/vapor/errors"
19 "github.com/vapor/protocol/bc"
20 "github.com/vapor/protocol/bc/types"
21 "github.com/vapor/protocol/vm/vmutil"
22 "github.com/vapor/testutil"
25 type testAction bc.AssetAmount
27 func (t testAction) Build(ctx context.Context, b *TemplateBuilder) error {
28 in := types.NewSpendInput(nil, bc.NewHash([32]byte{0xff}), *t.AssetId, t.Amount, 0, nil)
29 tplIn := &SigningInstruction{}
31 err := b.AddInput(in, tplIn)
35 return b.AddOutput(types.NewIntraChainOutput(*t.AssetId, t.Amount, []byte("change")))
38 func (t testAction) ActionType() string {
42 func newControlProgramAction(assetAmt bc.AssetAmount, script []byte) *controlProgramAction {
43 return &controlProgramAction{
44 AssetAmount: assetAmt,
49 func TestBuild(t *testing.T) {
50 ctx := context.Background()
52 assetID1 := bc.NewAssetID([32]byte{1})
53 assetID2 := bc.NewAssetID([32]byte{2})
56 newControlProgramAction(bc.AssetAmount{AssetId: &assetID2, Amount: 6}, []byte("dest")),
57 testAction(bc.AssetAmount{AssetId: &assetID1, Amount: 5}),
59 expiryTime := time.Now().Add(time.Minute)
60 got, err := Build(ctx, nil, actions, expiryTime, 0)
62 testutil.FatalErr(t, err)
66 Transaction: types.NewTx(types.TxData{
68 Inputs: []*types.TxInput{
69 types.NewSpendInput(nil, bc.NewHash([32]byte{0xff}), assetID1, 5, 0, nil),
71 Outputs: []*types.TxOutput{
72 types.NewIntraChainOutput(assetID2, 6, []byte("dest")),
73 types.NewIntraChainOutput(assetID1, 5, []byte("change")),
76 SigningInstructions: []*SigningInstruction{{
77 WitnessComponents: []witnessComponent{},
81 if !testutil.DeepEqual(got.Transaction.TxData, want.Transaction.TxData) {
82 t.Errorf("got tx:\n%s\nwant tx:\n%s", spew.Sdump(got.Transaction.TxData), spew.Sdump(want.Transaction.TxData))
85 if !testutil.DeepEqual(got.SigningInstructions, want.SigningInstructions) {
86 t.Errorf("got signing instructions:\n\t%#v\nwant signing instructions:\n\t%#v", got.SigningInstructions, want.SigningInstructions)
90 func mustDecodeHex(str string) []byte {
91 data, err := hex.DecodeString(str)
98 func TestCheckBlankCheck(t *testing.T) {
104 Inputs: []*types.TxInput{types.NewSpendInput(nil, bc.NewHash([32]byte{0xff}), bc.AssetID{}, 5, 0, nil)},
109 Inputs: []*types.TxInput{types.NewSpendInput(nil, bc.NewHash([32]byte{0xff}), bc.AssetID{}, 5, 0, nil)},
110 Outputs: []*types.TxOutput{types.NewIntraChainOutput(bc.AssetID{}, 3, nil)},
115 Inputs: []*types.TxInput{
116 types.NewSpendInput(nil, bc.NewHash([32]byte{0xff}), bc.AssetID{}, 5, 0, nil),
117 types.NewSpendInput(nil, bc.NewHash([32]byte{0xff}), bc.NewAssetID([32]byte{1}), 5, 0, nil),
119 Outputs: []*types.TxOutput{types.NewIntraChainOutput(bc.AssetID{}, 5, nil)},
124 Inputs: []*types.TxInput{types.NewSpendInput(nil, bc.NewHash([32]byte{0xff}), bc.AssetID{}, 5, 0, nil)},
125 Outputs: []*types.TxOutput{
126 types.NewIntraChainOutput(bc.AssetID{}, math.MaxInt64, nil),
127 types.NewIntraChainOutput(bc.AssetID{}, 7, nil),
133 Inputs: []*types.TxInput{
134 types.NewSpendInput(nil, bc.NewHash([32]byte{0xff}), bc.AssetID{}, 5, 0, nil),
135 types.NewSpendInput(nil, bc.NewHash([32]byte{0xff}), bc.AssetID{}, math.MaxInt64, 0, nil),
141 Inputs: []*types.TxInput{types.NewSpendInput(nil, bc.NewHash([32]byte{0xff}), bc.AssetID{}, 5, 0, nil)},
142 Outputs: []*types.TxOutput{types.NewIntraChainOutput(bc.AssetID{}, 5, nil)},
147 Outputs: []*types.TxOutput{types.NewIntraChainOutput(bc.AssetID{}, 5, nil)},
152 Inputs: []*types.TxInput{types.NewSpendInput(nil, bc.NewHash([32]byte{0xff}), bc.AssetID{}, 5, 0, nil)},
153 Outputs: []*types.TxOutput{types.NewIntraChainOutput(bc.NewAssetID([32]byte{1}), 5, nil)},
158 for _, c := range cases {
159 got := checkBlankCheck(c.tx)
160 if errors.Root(got) != c.want {
161 t.Errorf("checkUnsafe(%+v) err = %v want %v", c.tx, errors.Root(got), c.want)
166 func TestCreateTxByUtxo(t *testing.T) {
167 xprv, xpub, err := chainkd.NewXKeys(nil)
172 pub := xpub.PublicKey()
173 pubHash := crypto.Ripemd160(pub)
174 program, err := vmutil.P2WPKHProgram([]byte(pubHash))
179 address, err := common.NewAddressWitnessPubKeyHash(pubHash, &consensus.ActiveNetParams)
184 muxID := testutil.MustDecodeHash("1e673900965623ec3305cead5a78dfb68a34599f8bc078460f3f202256c3dfa6")
190 ControlProgram []byte
194 AssetID: *consensus.BTMAssetID,
197 ControlProgram: program,
198 Address: address.EncodeAddress(),
201 recvProg := mustDecodeHex("00145056532ecd3621c9ce8adde5505c058610b287cf")
202 tx := types.NewTx(types.TxData{
204 Inputs: []*types.TxInput{
205 types.NewSpendInput(nil, utxo.SourceID, utxo.AssetID, utxo.Amount, utxo.SourcePos, utxo.ControlProgram),
207 Outputs: []*types.TxOutput{
208 types.NewIntraChainOutput(*consensus.BTMAssetID, 10000000000, recvProg),
214 AllowAdditional: false,
217 h := tpl.Hash(0).Byte32()
218 sig := xprv.Sign(h[:])
221 // Test with more signatures than required, in correct order
222 tpl.SigningInstructions = []*SigningInstruction{{
223 WitnessComponents: []witnessComponent{
226 Sigs: []chainjson.HexBytes{sig},
232 if err = materializeWitnesses(tpl); err != nil {
236 if !testutil.DeepEqual(tx, tpl.Transaction) {
237 t.Errorf("tx:%v result is equal to want:%v", tx, tpl.Transaction)
241 func TestAddContractArgs(t *testing.T) {
242 hexXpub, err := hex.DecodeString("ba76bb52574b3f40315f2c01f1818a9072ced56e9d4b68acbef56a4d0077d08e5e34837963e4cdc54eb251aa34aad01e6ae48b140f6a2743fbb0a0abd9cf8aac")
247 var xpub chainkd.XPub
248 copy(xpub[:], hexXpub)
250 rawTxSig := RawTxSigArgument{RootXPub: xpub, Path: []chainjson.HexBytes{{1, 1, 0, 0, 0, 0, 0, 0, 0}, {1, 0, 0, 0, 0, 0, 0, 0}}}
251 rawTxSigMsg, err := json.Marshal(rawTxSig)
256 value, err := hex.DecodeString("7468697320697320612074657374")
260 data := DataArgument{value}
261 dataMsg, err := json.Marshal(data)
266 strMsg, err := json.Marshal(StrArgument{"this is a test string"})
271 integerMsg, err := json.Marshal(IntegerArgument{100})
276 boolMsg, err := json.Marshal(BoolArgument{true})
282 arguments []ContractArgument
286 arguments: []ContractArgument{
288 Type: "raw_tx_signature",
289 RawData: rawTxSigMsg,
295 arguments: []ContractArgument{
304 arguments: []ContractArgument{
313 arguments: []ContractArgument{
322 arguments: []ContractArgument{
331 arguments: []ContractArgument{
333 Type: "raw_tx_signature",
334 RawData: rawTxSigMsg,
344 arguments: []ContractArgument{
350 Type: "raw_tx_signature",
351 RawData: rawTxSigMsg,
357 arguments: []ContractArgument{
359 Type: "raw_tx_signature",
360 RawData: rawTxSigMsg,
382 arguments: []ContractArgument{
389 RawData: rawTxSigMsg,
392 wantResult: ErrBadContractArgType,
396 sigInst := &SigningInstruction{}
397 for _, c := range cases {
398 err := AddContractArgs(sigInst, c.arguments)
399 if err != c.wantResult {
400 t.Fatalf("got result=%v, want result=%v", err, c.wantResult)