Skip to content

Commit

Permalink
Remove warning when setting instructions
Browse files Browse the repository at this point in the history
There is an error message from godot Object. This commit changes the way
account metas are created to avoid the warning. It adds a new static
creater method and utilizes it internally.
  • Loading branch information
Virus-Axel committed Dec 18, 2023
1 parent 7183a75 commit 42776f7
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 21 deletions.
2 changes: 2 additions & 0 deletions include/account_meta.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class AccountMeta : public Resource {
AccountMeta(const Variant& pid, bool signer, bool writeable);
AccountMeta(const Variant& other);

static Variant new_from_fields(const Variant& pid, bool signer, bool writeable);

void set_pubkey(const Variant &p_value);
Variant get_pubkey() const;

Expand Down
8 changes: 5 additions & 3 deletions include/message.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ namespace godot{
MAKE_TYPED_ARRAY(CompiledInstruction, Variant::OBJECT)
MAKE_TYPED_ARRAY(Instruction, Variant::OBJECT)

class Message: public Resource{ // Message
GDCLASS(Message, Resource)
class Message{ // Message
private:
uint8_t payer_index = 0;
uint8_t num_required_signatures = 0;
Expand All @@ -37,12 +36,15 @@ class Message: public Resource{ // Message

public:
Message();
Message(TypedArray<Instruction> instructions, Variant &payer);
Message(TypedArray<Instruction> instructions, const Variant &payer);

void set_latest_blockhash(const String& blockhash);
PackedByteArray serialize();
PackedByteArray serialize_blockhash();
int get_amount_signers();
Array &get_signers();

Message& operator=(const Message& other);
~Message();
};
}
Expand Down
3 changes: 2 additions & 1 deletion include/transaction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "pubkey.hpp"
#include "account.hpp"
#include "instruction.hpp"
#include "message.hpp"

#include <godot_cpp/classes/node.hpp>

Expand All @@ -16,7 +17,7 @@ class Transaction : public Node {
private:
uint32_t ready_signature_amount = 0;

Variant message;
Message message;

Array instructions;
Variant payer;
Expand Down
12 changes: 12 additions & 0 deletions src/account_meta.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ namespace godot{
using internal::gdextension_interface_print_warning;

void AccountMeta::_bind_methods() {
ClassDB::bind_static_method("AccountMeta", D_METHOD("new_from_fields", "key", "is_signer", "writable"), &AccountMeta::new_from_fields);

ClassDB::bind_method(D_METHOD("get_is_signer"), &AccountMeta::get_is_signer);
ClassDB::bind_method(D_METHOD("set_is_signer", "p_value"), &AccountMeta::set_is_signer);
ClassDB::bind_method(D_METHOD("get_writeable"), &AccountMeta::get_writeable);
Expand Down Expand Up @@ -53,6 +55,16 @@ bool AccountMeta::_get(const StringName &p_name, Variant &r_ret) const{
return true;
}

Variant AccountMeta::new_from_fields(const Variant& pid, bool signer, bool writeable){
AccountMeta *res = memnew(AccountMeta);

res->key = pid;
res->is_signer = signer;
res->writeable = writeable;

return res;
}

void AccountMeta::set_pubkey(const Variant &p_value) {
key = p_value;
}
Expand Down
23 changes: 18 additions & 5 deletions src/message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ TypedArray<AccountMeta> sort_metas(TypedArray<AccountMeta> input){
}

Message::Message(){
latest_blockhash = "";
}

void Message::_bind_methods(){
Expand Down Expand Up @@ -92,10 +93,9 @@ void Message::merge_account_meta(const AccountMeta &account_meta){
meta_1->set_writeable(meta_1->get_writeable() || account_meta.get_writeable());
}
else{
const AccountMeta *new_element = memnew(AccountMeta(account_meta));
Variant new_element = AccountMeta::new_from_fields(account_meta.get_pubkey(), account_meta.get_is_signer(), account_meta.get_writeable());
merged_metas.append(new_element);
}
}
}}

void Message::merge_signer(const Variant& signer){
for(unsigned int i = 0; i < signers.size(); i++){
Expand All @@ -107,7 +107,7 @@ void Message::merge_signer(const Variant& signer){
signers.append(signer);
}

Message::Message(TypedArray<Instruction> instructions, Variant &payer){
Message::Message(TypedArray<Instruction> instructions, const Variant &payer){
// Payer is signer.
signers.append(payer);

Expand All @@ -119,7 +119,6 @@ Message::Message(TypedArray<Instruction> instructions, Variant &payer){
// Prepend ComputeBudget instructions.
instructions.insert(0, ComputeBudget::set_compute_unit_limit(200000));
instructions.insert(1, ComputeBudget::set_compute_unit_price(8000));

for(unsigned int i = 0; i < instructions.size(); i++){
Instruction *element = Object::cast_to<Instruction>(instructions[i]);
const TypedArray<AccountMeta> &account_metas = element->get_accounts();
Expand Down Expand Up @@ -183,6 +182,20 @@ Array &Message::get_signers(){
return signers;
}

Message& Message::operator=(const Message& other){
payer_index = other.payer_index;
num_required_signatures = other.num_required_signatures;
num_readonly_signed_accounts = other.num_readonly_signed_accounts;
num_readonly_unsigned_accounts = other.num_readonly_unsigned_accounts;
account_keys = other.account_keys;
latest_blockhash = other.latest_blockhash;
compiled_instructions = other.compiled_instructions;
signers = other.signers;

merged_metas = other.merged_metas;
return *this;
}

int Message::locate_account_meta(const TypedArray<AccountMeta>& arr, const AccountMeta &input){
for(unsigned int i = 0; i < arr.size(); i++){
if (Pubkey(merged_metas[i]) == input.get_pubkey()){
Expand Down
2 changes: 1 addition & 1 deletion src/register_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ void initialize_solana_sdk_module(ModuleInitializationLevel p_level) {
ClassDB::register_class<SolanaClient>();
ClassDB::register_class<Pubkey>();
ClassDB::register_class<CompiledInstruction>();
ClassDB::register_class<Message>();
//ClassDB::register_class<Message>();
ClassDB::register_class<Hash>();
ClassDB::register_class<Account>();
ClassDB::register_class<AccountMeta>();
Expand Down
25 changes: 14 additions & 11 deletions src/transaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,11 @@ bool Transaction::is_phantom_payer() const{

void Transaction::create_message(){
// Free existing memory.
message.clear();
if(instructions.is_empty() || (payer.get_type() == Variant::NIL)){
signatures.clear();
return;
}

for(unsigned int i = 0; i < instructions.size(); i++){
if(instructions[i].get_type() != Variant::OBJECT){
signatures.clear();
Expand All @@ -95,10 +95,11 @@ void Transaction::create_message(){
return;
}
}
message = memnew(Message(instructions, payer));
Object::cast_to<Message>(message)->set_latest_blockhash(latest_blockhash_string);

const int amount_of_signers = Object::cast_to<Message>(message)->get_amount_signers();
message = Message(instructions, payer);
message.set_latest_blockhash(latest_blockhash_string);

const int amount_of_signers = message.get_amount_signers();

if(signers.size() == amount_of_signers){
return;
Expand All @@ -115,6 +116,7 @@ void Transaction::create_message(){

void Transaction::check_fully_signed(){
if(ready_signature_amount == signers.size()){
std::cout << "WOAH" << std::endl;
emit_signal("fully_signed");
}
}
Expand All @@ -140,6 +142,9 @@ void Transaction::sign_at_index(const uint32_t index){
controller->connect("signing_error", Callable(this, "_signer_failed"));
controller->sign_message(serialize(), index);
}
else{
gdextension_interface_print_warning("Unknown signer type.", "sign_at_index", __FILE__, __LINE__, false);
}
}

bool Transaction::_set(const StringName &p_name, const Variant &p_value){
Expand Down Expand Up @@ -277,11 +282,11 @@ void Transaction::update_latest_blockhash(const String &custom_hash){
const Dictionary blockhash_result = latest_blockhash["result"];
const Dictionary blockhash_value = blockhash_result["value"];
latest_blockhash_string = blockhash_value["blockhash"];
Object::cast_to<Message>(message)->set_latest_blockhash(latest_blockhash_string);
message.set_latest_blockhash(latest_blockhash_string);
}
else{
latest_blockhash_string = custom_hash;
Object::cast_to<Message>(message)->set_latest_blockhash(custom_hash);
message.set_latest_blockhash(custom_hash);
}
}

Expand All @@ -308,7 +313,7 @@ PackedByteArray Transaction::serialize(){
}

PackedByteArray Transaction::serialize_message(){
return Object::cast_to<Message>(message)->serialize();
return message.serialize();
}

PackedByteArray Transaction::serialize_signers(){
Expand All @@ -323,9 +328,7 @@ PackedByteArray Transaction::serialize_signers(){
Variant Transaction::sign_and_send(){
connect("fully_signed", Callable(this, "send_and_disconnect"));

sign();

return OK;
return sign();
}

Dictionary Transaction::send(){
Expand All @@ -349,7 +352,7 @@ void Transaction::send_and_disconnect(){
Error Transaction::sign(){
PackedByteArray msg = serialize();

signers = Object::cast_to<Message>(message)->get_signers();
signers = message.get_signers();

for (unsigned int i = 0; i < signers.size(); i++){
sign_at_index(i);
Expand Down

0 comments on commit 42776f7

Please sign in to comment.