Skip to content

Commit 7926abb

Browse files
Merge pull request #310 from 0xvbetsun/v/set-loaded-accounts-data-size-limit
feat: add SetLoadedAccountsDataSizeLimitInstruction
2 parents bdd6a7d + 3433ac4 commit 7926abb

4 files changed

Lines changed: 168 additions & 0 deletions

File tree

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
// Copyright 2021 github.com/gagliardetto
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package computebudget
16+
17+
import (
18+
"errors"
19+
20+
ag_binary "github.com/gagliardetto/binary"
21+
ag_solanago "github.com/gagliardetto/solana-go"
22+
ag_format "github.com/gagliardetto/solana-go/text/format"
23+
ag_treeout "github.com/gagliardetto/treeout"
24+
)
25+
26+
type SetLoadedAccountsDataSizeLimit struct {
27+
Bytes uint32
28+
}
29+
30+
func (obj *SetLoadedAccountsDataSizeLimit) SetAccounts(accounts []*ag_solanago.AccountMeta) error {
31+
return nil
32+
}
33+
34+
func (slice SetLoadedAccountsDataSizeLimit) GetAccounts() (accounts []*ag_solanago.AccountMeta) {
35+
return
36+
}
37+
38+
// NewSetLoadedAccountsDataSizeLimitInstructionBuilder creates a new `SetLoadedAccountsDataSizeLimit` instruction builder.
39+
func NewSetLoadedAccountsDataSizeLimitInstructionBuilder() *SetLoadedAccountsDataSizeLimit {
40+
nd := &SetLoadedAccountsDataSizeLimit{}
41+
return nd
42+
}
43+
44+
// Byytes limit
45+
func (inst *SetLoadedAccountsDataSizeLimit) SetBytes(bytes uint32) *SetLoadedAccountsDataSizeLimit {
46+
inst.Bytes = bytes
47+
return inst
48+
}
49+
50+
func (inst SetLoadedAccountsDataSizeLimit) Build() *Instruction {
51+
return &Instruction{BaseVariant: ag_binary.BaseVariant{
52+
Impl: inst,
53+
TypeID: ag_binary.TypeIDFromUint8(Instruction_SetLoadedAccountsDataSizeLimit),
54+
}}
55+
}
56+
57+
// ValidateAndBuild validates the instruction parameters and accounts;
58+
// if there is a validation error, it returns the error.
59+
// Otherwise, it builds and returns the instruction.
60+
func (inst SetLoadedAccountsDataSizeLimit) ValidateAndBuild() (*Instruction, error) {
61+
if err := inst.Validate(); err != nil {
62+
return nil, err
63+
}
64+
return inst.Build(), nil
65+
}
66+
67+
func (inst *SetLoadedAccountsDataSizeLimit) Validate() error {
68+
// Check whether all (required) parameters are set:
69+
if inst.Bytes == 0 {
70+
return errors.New("Bytes parameter is not set")
71+
}
72+
73+
return nil
74+
}
75+
76+
func (inst *SetLoadedAccountsDataSizeLimit) EncodeToTree(parent ag_treeout.Branches) {
77+
parent.Child(ag_format.Program(ProgramName, ProgramID)).
78+
//
79+
ParentFunc(func(programBranch ag_treeout.Branches) {
80+
programBranch.Child(ag_format.Instruction("SetLoadedAccountsDataSizeLimit")).
81+
//
82+
ParentFunc(func(instructionBranch ag_treeout.Branches) {
83+
84+
// Parameters of the instruction:
85+
instructionBranch.Child("Params").ParentFunc(func(paramsBranch ag_treeout.Branches) {
86+
paramsBranch.Child(ag_format.Param("Bytes", inst.Bytes))
87+
})
88+
})
89+
})
90+
}
91+
92+
func (obj SetLoadedAccountsDataSizeLimit) MarshalWithEncoder(encoder *ag_binary.Encoder) (err error) {
93+
// Serialize `Bytes` param:
94+
err = encoder.Encode(obj.Bytes)
95+
if err != nil {
96+
return err
97+
}
98+
return nil
99+
}
100+
func (obj *SetLoadedAccountsDataSizeLimit) UnmarshalWithDecoder(decoder *ag_binary.Decoder) (err error) {
101+
// Deserialize `Bytes`:
102+
err = decoder.Decode(&obj.Bytes)
103+
if err != nil {
104+
return err
105+
}
106+
return nil
107+
}
108+
109+
// NewSetLoadedAccountsDataSizeLimitInstruction declares a new SetLoadedAccountsDataSizeLimit instruction with the provided parameters and accounts.
110+
func NewSetLoadedAccountsDataSizeLimitInstruction(
111+
// Parameters:
112+
bytes uint32,
113+
) *SetLoadedAccountsDataSizeLimit {
114+
return NewSetLoadedAccountsDataSizeLimitInstructionBuilder().SetBytes(bytes)
115+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright 2021 github.com/gagliardetto
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package computebudget
16+
17+
import (
18+
"testing"
19+
20+
"github.com/stretchr/testify/require"
21+
)
22+
23+
func TestSetLoadedAccountsDataSizeLimitInstruction(t *testing.T) {
24+
ix, err := NewSetLoadedAccountsDataSizeLimitInstruction(32 * 1024).ValidateAndBuild()
25+
require.Nil(t, err)
26+
27+
require.Equal(t, ProgramID, ix.ProgramID())
28+
require.Equal(t, 0, len(ix.Accounts()))
29+
30+
data, err := ix.Data()
31+
require.Nil(t, err)
32+
require.Equal(t, []byte{0x4, 0x0, 0x80, 0x0, 0x0}, data)
33+
}

programs/compute-budget/instruction.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ const (
5757
// Set a compute unit price in "micro-lamports" to pay a higher transaction
5858
// fee for higher transaction prioritization.
5959
Instruction_SetComputeUnitPrice
60+
61+
// Set a specific transaction-wide account data size limit, in bytes, is allowed to load.
62+
Instruction_SetLoadedAccountsDataSizeLimit
6063
)
6164

6265
// InstructionIDToName returns the name of the instruction given its ID.
@@ -70,6 +73,8 @@ func InstructionIDToName(id uint8) string {
7073
return "SetComputeUnitLimit"
7174
case Instruction_SetComputeUnitPrice:
7275
return "SetComputeUnitPrice"
76+
case Instruction_SetLoadedAccountsDataSizeLimit:
77+
return "SetLoadedAccountsDataSizeLimit"
7378
default:
7479
return ""
7580
}
@@ -102,6 +107,9 @@ var InstructionImplDef = ag_binary.NewVariantDefinition(
102107
{
103108
"SetComputeUnitPrice", (*SetComputeUnitPrice)(nil),
104109
},
110+
{
111+
"SetLoadedAccountsDataSizeLimit", (*SetLoadedAccountsDataSizeLimit)(nil),
112+
},
105113
},
106114
)
107115

programs/compute-budget/instruction_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,18 @@ func TestEncodingInstruction(t *testing.T) {
7979
},
8080
},
8181
},
82+
{
83+
name: "SetLoadedAccountsDataSizeLimit",
84+
hexData: "0400800000",
85+
expectInstruction: &Instruction{
86+
BaseVariant: bin.BaseVariant{
87+
TypeID: bin.TypeIDFromUint8(4),
88+
Impl: &SetLoadedAccountsDataSizeLimit{
89+
Bytes: 32 * 1024,
90+
},
91+
},
92+
},
93+
},
8294
}
8395

8496
t.Run("should encode", func(t *testing.T) {

0 commit comments

Comments
 (0)