-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathprogram_test.go
149 lines (114 loc) · 4.9 KB
/
program_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
package system
import (
"crypto/ed25519"
"encoding/binary"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/code-payments/code-server/pkg/solana"
)
func TestCreateAccount(t *testing.T) {
keys := generateKeys(t, 3)
instruction := CreateAccount(keys[0], keys[1], keys[2], 12345, 67890)
command := make([]byte, 4)
lamports := make([]byte, 8)
binary.LittleEndian.PutUint64(lamports, 12345)
size := make([]byte, 8)
binary.LittleEndian.PutUint64(size, 67890)
assert.Equal(t, command, instruction.Data[0:4])
assert.Equal(t, lamports, instruction.Data[4:12])
assert.Equal(t, size, instruction.Data[12:20])
assert.Equal(t, []byte(keys[2]), instruction.Data[20:52])
var tx solana.Transaction
require.NoError(t, tx.Unmarshal(solana.NewTransaction(keys[0], instruction).Marshal()))
decompiled, err := DecompileCreateAccount(tx.Message, 0)
require.NoError(t, err)
assert.Equal(t, decompiled.Funder, keys[0])
assert.Equal(t, decompiled.Address, keys[1])
assert.Equal(t, decompiled.Owner, keys[2])
assert.EqualValues(t, decompiled.Lamports, 12345)
assert.EqualValues(t, decompiled.Size, 67890)
}
func TestDecompileNonCreate(t *testing.T) {
keys := generateKeys(t, 4)
instruction := CreateAccount(keys[0], keys[1], keys[2], 12345, 67890)
instruction.Accounts = instruction.Accounts[:1]
_, err := DecompileCreateAccount(solana.NewTransaction(keys[0], instruction).Message, 0)
assert.NotNil(t, err)
assert.True(t, strings.HasPrefix(err.Error(), "invalid number of accounts"), err)
binary.BigEndian.PutUint32(instruction.Data, commandAllocate)
_, err = DecompileCreateAccount(solana.NewTransaction(keys[0], instruction).Message, 0)
assert.Equal(t, solana.ErrIncorrectInstruction, err)
instruction.Data = make([]byte, 3)
_, err = DecompileCreateAccount(solana.NewTransaction(keys[0], instruction).Message, 0)
assert.Equal(t, solana.ErrIncorrectInstruction, err)
instruction.Program = keys[3]
_, err = DecompileCreateAccount(solana.NewTransaction(keys[0], instruction).Message, 0)
assert.Equal(t, solana.ErrIncorrectProgram, err)
_, err = DecompileCreateAccount(solana.NewTransaction(keys[0], instruction).Message, 1)
assert.NotNil(t, err)
assert.True(t, strings.HasPrefix(err.Error(), "instruction doesn't exist"))
}
func TestAdvanceNonceAccount(t *testing.T) {
keys := generateKeys(t, 3)
instruction := AdvanceNonce(keys[0], keys[1])
command := make([]byte, 4)
binary.LittleEndian.PutUint32(command, commandAdvanceNonceAccount)
assert.EqualValues(t, command, instruction.Data)
assert.EqualValues(t, ProgramKey[:], instruction.Program)
require.Len(t, instruction.Accounts, 3)
assert.EqualValues(t, keys[0], instruction.Accounts[0].PublicKey)
assert.False(t, instruction.Accounts[0].IsSigner)
assert.True(t, instruction.Accounts[0].IsWritable)
assert.EqualValues(t, RecentBlockhashesSysVar, instruction.Accounts[1].PublicKey)
assert.False(t, instruction.Accounts[1].IsSigner)
assert.False(t, instruction.Accounts[1].IsWritable)
assert.EqualValues(t, keys[1], instruction.Accounts[2].PublicKey)
assert.True(t, instruction.Accounts[2].IsSigner)
decompiled, err := DecompileAdvanceNonce(solana.NewTransaction(keys[0], instruction).Message, 0)
assert.NoError(t, err)
assert.EqualValues(t, keys[0], decompiled.Nonce)
assert.EqualValues(t, keys[1], decompiled.Authority)
instruction.Accounts[1].PublicKey = keys[2]
_, err = DecompileAdvanceNonce(solana.NewTransaction(keys[0], instruction).Message, 0)
assert.NotNil(t, err)
assert.True(t, strings.Contains(err.Error(), "invalid RecentBlockhashesSysVar"))
instruction.Accounts = instruction.Accounts[:1]
_, err = DecompileAdvanceNonce(solana.NewTransaction(keys[0], instruction).Message, 0)
assert.NotNil(t, err)
assert.True(t, strings.Contains(err.Error(), "invalid number of accounts"))
binary.LittleEndian.PutUint32(instruction.Data, commandCreateAccount)
_, err = DecompileAdvanceNonce(solana.NewTransaction(keys[0], instruction).Message, 0)
assert.Equal(t, solana.ErrIncorrectInstruction, err)
instruction.Data = nil
_, err = DecompileAdvanceNonce(solana.NewTransaction(keys[0], instruction).Message, 0)
assert.Equal(t, solana.ErrIncorrectInstruction, err)
instruction.Program = keys[2]
_, err = DecompileAdvanceNonce(solana.NewTransaction(keys[0], instruction).Message, 0)
assert.Equal(t, solana.ErrIncorrectProgram, err)
}
func TestGetNonceValue(t *testing.T) {
// lay
info := solana.AccountInfo{
Data: make([]byte, 80),
Owner: ProgramKey[:],
}
var val solana.Blockhash
for i := 0; i < 32; i++ {
val[i] = byte(i)
}
copy(info.Data[4+4+32:], val[:])
actual, err := GetNonceValueFromAccount(info)
assert.NoError(t, err)
assert.EqualValues(t, val, actual)
}
func generateKeys(t *testing.T, amount int) []ed25519.PublicKey {
keys := make([]ed25519.PublicKey, amount)
for i := 0; i < amount; i++ {
pub, _, err := ed25519.GenerateKey(nil)
require.NoError(t, err)
keys[i] = pub
}
return keys
}