forked from gagliardetto/solana-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transaction_test.go
170 lines (147 loc) · 5.35 KB
/
transaction_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Copyright 2021 github.com/gagliardetto
// This file has been modified by github.com/gagliardetto
//
// Copyright 2020 dfuse Platform Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package solana
import (
"encoding/base64"
"testing"
bin "github.com/gagliardetto/binary"
"github.com/magiconair/properties/assert"
"github.com/mr-tron/base58"
"github.com/stretchr/testify/require"
)
type testTransactionInstructions struct {
accounts []*AccountMeta
data []byte
programID PublicKey
}
func (t *testTransactionInstructions) Accounts() []*AccountMeta {
return t.accounts
}
func (t *testTransactionInstructions) ProgramID() PublicKey {
return t.programID
}
func (t *testTransactionInstructions) Data() ([]byte, error) {
return t.data, nil
}
func TestNewTransaction(t *testing.T) {
debugNewTransaction = true
instructions := []Instruction{
&testTransactionInstructions{
accounts: []*AccountMeta{
{PublicKey: MustPublicKeyFromBase58("A9QnpgfhCkmiBSjgBuWk76Wo3HxzxvDopUq9x6UUMmjn"), IsSigner: true, IsWritable: false},
{PublicKey: MustPublicKeyFromBase58("9hFtYBYmBJCVguRYs9pBTWKYAFoKfjYR7zBPpEkVsmD"), IsSigner: true, IsWritable: true},
},
data: []byte{0xaa, 0xbb},
programID: MustPublicKeyFromBase58("11111111111111111111111111111111"),
},
&testTransactionInstructions{
accounts: []*AccountMeta{
{PublicKey: MustPublicKeyFromBase58("SysvarC1ock11111111111111111111111111111111"), IsSigner: false, IsWritable: false},
{PublicKey: MustPublicKeyFromBase58("SysvarS1otHashes111111111111111111111111111"), IsSigner: false, IsWritable: true},
{PublicKey: MustPublicKeyFromBase58("9hFtYBYmBJCVguRYs9pBTWKYAFoKfjYR7zBPpEkVsmD"), IsSigner: false, IsWritable: true},
{PublicKey: MustPublicKeyFromBase58("6FzXPEhCJoBx7Zw3SN9qhekHemd6E2b8kVguitmVAngW"), IsSigner: true, IsWritable: false},
},
data: []byte{0xcc, 0xdd},
programID: MustPublicKeyFromBase58("Vote111111111111111111111111111111111111111"),
},
}
blockhash, err := HashFromBase58("A9QnpgfhCkmiBSjgBuWk76Wo3HxzxvDopUq9x6UUMmjn")
require.NoError(t, err)
trx, err := NewTransaction(instructions, blockhash)
require.NoError(t, err)
assert.Equal(t, trx.Message.Header, MessageHeader{
NumRequiredSignatures: 3,
NumReadonlySignedAccounts: 1,
NumReadonlyUnsignedAccounts: 3,
})
assert.Equal(t, trx.Message.RecentBlockhash, blockhash)
assert.Equal(t, trx.Message.AccountKeys, []PublicKey{
MustPublicKeyFromBase58("A9QnpgfhCkmiBSjgBuWk76Wo3HxzxvDopUq9x6UUMmjn"),
MustPublicKeyFromBase58("9hFtYBYmBJCVguRYs9pBTWKYAFoKfjYR7zBPpEkVsmD"),
MustPublicKeyFromBase58("6FzXPEhCJoBx7Zw3SN9qhekHemd6E2b8kVguitmVAngW"),
MustPublicKeyFromBase58("SysvarS1otHashes111111111111111111111111111"),
MustPublicKeyFromBase58("SysvarC1ock11111111111111111111111111111111"),
MustPublicKeyFromBase58("11111111111111111111111111111111"),
MustPublicKeyFromBase58("Vote111111111111111111111111111111111111111"),
})
assert.Equal(t, trx.Message.Instructions, []CompiledInstruction{
{
ProgramIDIndex: 5,
AccountCount: 2,
Accounts: []uint16{0, 01},
DataLength: 2,
Data: []byte{0xaa, 0xbb},
},
{
ProgramIDIndex: 6,
AccountCount: 4,
Accounts: []uint16{4, 3, 1, 2},
DataLength: 2,
Data: []byte{0xcc, 0xdd},
},
})
}
func TestTransactionDecode(t *testing.T) {
encoded := "AfjEs3XhTc3hrxEvlnMPkm/cocvAUbFNbCl00qKnrFue6J53AhEqIFmcJJlJW3EDP5RmcMz+cNTTcZHW/WJYwAcBAAEDO8hh4VddzfcO5jbCt95jryl6y8ff65UcgukHNLWH+UQGgxCGGpgyfQVQV02EQYqm4QwzUt2qf9f1gVLM7rI4hwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA6ANIF55zOZWROWRkeh+lExxZBnKFqbvIxZDLE7EijjoBAgIAAQwCAAAAOTAAAAAAAAA="
data, err := base64.StdEncoding.DecodeString(encoded)
require.NoError(t, err)
tx, err := TransactionFromDecoder(bin.NewBinDecoder(data))
require.NoError(t, err)
require.NotNil(t, tx)
require.Len(t, tx.Signatures, 1)
require.Equal(t,
MustSignatureFromBase58("5yUSwqQqeZLEEYKxnG4JC4XhaaBpV3RS4nQbK8bQTyjLX5btVq9A1Ja5nuJzV7Z3Zq8G6EVKFvN4DKUL6PSAxmTk"),
tx.Signatures[0],
)
require.Equal(t,
[]PublicKey{
MustPublicKeyFromBase58("52NGrUqh6tSGhr59ajGxsH3VnAaoRdSdTbAaV9G3UW35"),
MustPublicKeyFromBase58("SRMuApVNdxXokk5GT7XD5cUUgXMBCoAz2LHeuAoKWRt"),
MustPublicKeyFromBase58("11111111111111111111111111111111"),
},
tx.Message.AccountKeys,
)
require.Equal(t,
MessageHeader{
NumRequiredSignatures: 1,
NumReadonlySignedAccounts: 0,
NumReadonlyUnsignedAccounts: 1,
},
tx.Message.Header,
)
require.Equal(t,
MustHashFromBase58("GcgVK9buRA7YepZh3zXuS399GJAESCisLnLDBCmR5Aoj"),
tx.Message.RecentBlockhash,
)
decodedData, err := base58.Decode("3Bxs4ART6LMJ13T5")
require.NoError(t, err)
require.Equal(t,
[]CompiledInstruction{
{
ProgramIDIndex: 2,
AccountCount: 2,
DataLength: 12,
Accounts: []uint16{
0,
1,
},
Data: Base58(decodedData),
},
},
tx.Message.Instructions,
)
}