@@ -22,37 +22,23 @@ def json(self) -> typing.Dict:
2222 'pubkey' : self .pubkey .base58 (),
2323 }
2424
25- def program_buffer_closed (self , program_buffer : pxsol .core .PubKey ) -> None :
26- # Close a buffer account. This method is used to withdraw all lamports when the buffer account is no longer in
27- # use due to unexpected errors.
28- rq = pxsol .core .Requisition (pxsol .program .LoaderUpgradeable .pubkey , [], bytearray ())
29- rq .account .append (pxsol .core .AccountMeta (program_buffer , 1 ))
30- rq .account .append (pxsol .core .AccountMeta (self .pubkey , 1 ))
31- rq .account .append (pxsol .core .AccountMeta (self .pubkey , 2 ))
32- rq .data = pxsol .program .LoaderUpgradeable .close ()
33- tx = pxsol .core .Transaction .requisition_decode (self .pubkey , [rq ])
34- tx .message .recent_blockhash = pxsol .base58 .decode (pxsol .rpc .get_latest_blockhash ({})['blockhash' ])
35- tx .sign ([self .prikey ])
36- txid = pxsol .rpc .send_transaction (base64 .b64encode (tx .serialize ()).decode (), {})
37- pxsol .rpc .wait ([txid ])
38-
39- def program_buffer_create (self , bincode : bytearray ) -> pxsol .core .PubKey :
25+ def program_buffer (self , bincode : bytearray ) -> pxsol .core .PubKey :
4026 # Writes a program into a buffer account. The buffer account is randomly generated, and its public key serves
4127 # as the function's return value.
4228 tempory_prikey = pxsol .core .PriKey .random ()
4329 program_buffer = tempory_prikey .pubkey ()
30+ pxsol .log .debugln (f'pxsol: program buffer prikey={ tempory_prikey } ' )
31+ pxsol .log .debugln (f'pxsol: program buffer pubkey={ program_buffer } ' )
32+ account_length = pxsol .program .LoaderUpgradeable .size_program_buffer + len (bincode )
4433 # Sends a transaction which creates a buffer account large enough for the byte-code being deployed. It also
4534 # invokes the initialize buffer instruction to set the buffer authority to restrict writes to the deployer's
4635 # chosen address.
4736 r0 = pxsol .core .Requisition (pxsol .program .System .pubkey , [], bytearray ())
4837 r0 .account .append (pxsol .core .AccountMeta (self .pubkey , 3 ))
4938 r0 .account .append (pxsol .core .AccountMeta (program_buffer , 3 ))
5039 r0 .data = pxsol .program .System .create_account (
51- pxsol .rpc .get_minimum_balance_for_rent_exemption (
52- pxsol .program .LoaderUpgradeable .size_program_data + len (bincode ),
53- {},
54- ),
55- pxsol .program .LoaderUpgradeable .size_program_buffer + len (bincode ),
40+ pxsol .rpc .get_minimum_balance_for_rent_exemption (account_length , {}),
41+ account_length ,
5642 pxsol .program .LoaderUpgradeable .pubkey ,
5743 )
5844 r1 = pxsol .core .Requisition (pxsol .program .LoaderUpgradeable .pubkey , [], bytearray ())
@@ -100,10 +86,12 @@ def program_closed(self, program: pxsol.core.PubKey) -> None:
10086
10187 def program_deploy (self , bincode : bytearray ) -> pxsol .core .PubKey :
10288 # Deploying a program on solana, returns the program's public key.
89+ program_buffer = self .program_buffer (bincode )
10390 tempory_prikey = pxsol .core .PriKey .random ()
104- program_buffer = self .program_buffer_create (bincode )
10591 program = tempory_prikey .pubkey ()
10692 program_data = pxsol .program .LoaderUpgradeable .pubkey .derive_pda (program .p )
93+ pxsol .log .debugln (f'pxsol: program prikey={ tempory_prikey } ' )
94+ pxsol .log .debugln (f'pxsol: program pubkey={ program } ' )
10795 # Deploy with max data len.
10896 r0 = pxsol .core .Requisition (pxsol .program .System .pubkey , [], bytearray ())
10997 r0 .account .append (pxsol .core .AccountMeta (self .pubkey , 3 ))
@@ -122,19 +110,37 @@ def program_deploy(self, bincode: bytearray) -> pxsol.core.PubKey:
122110 r1 .account .append (pxsol .core .AccountMeta (pxsol .program .SysvarClock .pubkey , 0 ))
123111 r1 .account .append (pxsol .core .AccountMeta (pxsol .program .System .pubkey , 0 ))
124112 r1 .account .append (pxsol .core .AccountMeta (self .pubkey , 2 ))
125- r1 .data = pxsol .program .LoaderUpgradeable .deploy_with_max_data_len (len (bincode ) * 2 )
113+ r1 .data = pxsol .program .LoaderUpgradeable .deploy_with_max_data_len (len (bincode ))
126114 tx = pxsol .core .Transaction .requisition_decode (self .pubkey , [r0 , r1 ])
127115 tx .message .recent_blockhash = pxsol .base58 .decode (pxsol .rpc .get_latest_blockhash ({})['blockhash' ])
128116 tx .sign ([self .prikey , tempory_prikey ])
129117 txid = pxsol .rpc .send_transaction (base64 .b64encode (tx .serialize ()).decode (), {})
130118 pxsol .rpc .wait ([txid ])
131- pxsol .rpc .step ()
132119 return program
133120
134121 def program_update (self , program : pxsol .core .PubKey , bincode : bytearray ) -> None :
135122 # Updating an existing solana program by new program data and the same program id.
136- program_buffer = self .program_buffer_create (bincode )
123+ program_buffer = self .program_buffer (bincode )
137124 program_data = pxsol .program .LoaderUpgradeable .pubkey .derive_pda (program .p )
125+ # Check the existing program data account size. If the new program data is larger than the existing one,
126+ # extend the program data account first.
127+ program_data_info = pxsol .rpc .get_account_info (program_data .base58 (), {})
128+ assert len (base64 .b64decode (program_data_info ['data' ][0 ])) == program_data_info ['space' ]
129+ addi = pxsol .program .LoaderUpgradeable .size_program_data + len (bincode ) - program_data_info ['space' ]
130+ if addi > 0 :
131+ pxsol .log .debugln (f'pxsol: extend program data addi={ addi } ' )
132+ rq = pxsol .core .Requisition (pxsol .program .LoaderUpgradeable .pubkey , [], bytearray ())
133+ rq .account .append (pxsol .core .AccountMeta (program_data , 1 ))
134+ rq .account .append (pxsol .core .AccountMeta (program , 1 ))
135+ rq .account .append (pxsol .core .AccountMeta (self .pubkey , 2 ))
136+ rq .account .append (pxsol .core .AccountMeta (pxsol .program .System .pubkey , 0 ))
137+ rq .account .append (pxsol .core .AccountMeta (self .pubkey , 3 ))
138+ rq .data = pxsol .program .LoaderUpgradeable .extend_program_checked (addi )
139+ tx = pxsol .core .Transaction .requisition_decode (self .pubkey , [rq ])
140+ tx .message .recent_blockhash = pxsol .base58 .decode (pxsol .rpc .get_latest_blockhash ({})['blockhash' ])
141+ tx .sign ([self .prikey ])
142+ txid = pxsol .rpc .send_transaction (base64 .b64encode (tx .serialize ()).decode (), {})
143+ pxsol .rpc .wait ([txid ])
138144 rq = pxsol .core .Requisition (pxsol .program .LoaderUpgradeable .pubkey , [], bytearray ())
139145 rq .account .append (pxsol .core .AccountMeta (program_data , 1 ))
140146 rq .account .append (pxsol .core .AccountMeta (program , 1 ))
@@ -149,7 +155,6 @@ def program_update(self, program: pxsol.core.PubKey, bincode: bytearray) -> None
149155 tx .sign ([self .prikey ])
150156 txid = pxsol .rpc .send_transaction (base64 .b64encode (tx .serialize ()).decode (), {})
151157 pxsol .rpc .wait ([txid ])
152- pxsol .rpc .step ()
153158
154159 def sol_balance (self ) -> int :
155160 # Returns the lamport balance of the account.
0 commit comments