The Replica returned an error: code 5, message: "Canister trapped explicitly: RTS error: blob_of_principal: principal too short"

Hello!

I got an error when I redeployed after editing the backend canister.
I have tried to search for the reason for the error, but I am not sure why.
The Replica returned an error: code 5, message: "Canister trapped explicitly: RTS error: blob_of_principal: principal too short"

Under what circumstances would such an error appear?
Also, what should I check?

[ Error ]

$ dfx deploy app_backend

Deploying: app_backend
All canisters have already been created.
Building canisters...
Installing canisters...
Upgrading code for canister app_backend, with canister ID rno2w-sqaaa-aaaaa-aaacq-cai
Error: Failed while trying to deploy canisters.
Caused by: Failed while trying to deploy canisters.
  Failed while trying to install all canisters.
    Failed to install wasm module to canister 'app_backend'.
      Failed to install wasm in canister 'rno2w-sqaaa-aaaaa-aaacq-cai'.
        Failed to install wasm.
          The Replica returned an error: code 5, message: "Canister rno2w-sqaaa-aaaaa-aaacq-cai trapped explicitly: RTS error: blob_of_principal: principal too short"

[ What I did when I redeployed ]
I wanted to check the functionality of the stable variable and preupgrade / postupgrade methods, so I defined a dummy variable and tried to redeploy.

Thank you in advance for your answer.

Can you show the code you use for preupgrade and postupgrade? Otherwise this will be very hard to debug.

Our apologies.

Here is the actual method (preupgrade / postupgrade).

I had tried to create a simpler DEX myself, using Definity/example/motoko/defi as a reference.

actor class SimpleDex() = this {
  stable var orders_stable : [T.Order] = [];
  private stable var book_stable : [var (Principal, [(T.Token, Nat)])] = [var];

  // ===== UPGRADE METHODS =====

  system func preupgrade() {
    book_stable := Array.init(book.size(), (Principal.fromText(""), []));
    var i = 0;
    for ((x, y) in book.entries()) {
      book_stable[i] := (x, Iter.toArray(y.entries()));
      i += 1;
    };

    // Save order
    orders_stable := exchange.getOrders();
  };

  system func postupgrade() {
    // Reload book.
    for ((key : Principal, value : [(T.Token, Nat)]) in book_stable.vals()) {
      let tmp : HashMap.HashMap<T.Token, Nat>
        = HashMap.fromIter<T.Token, Nat>(
            Iter.fromArray<(T.Token, Nat)>(value), 
            10, 
            Principal.equal, 
            Principal.hash);
      book.put(key, tmp);
    };

    // Reload order
    for (order in orders_stable.vals()) {
      exchange.addOrder(order);
    };

    // Clean stable memory.
    book_stable := [var];
    orders_stable := [];
  };
};

Empty String is not a valid principal and this will panic, as you observed in the error message. Please use a valid one instead. The two most common ones are the management canister’s Principal "aaaaa-aa" and the anonymous Principal "2vxsx-fae", but you can use any valid principal you like.

1 Like

Thank you very much.
I set the initial values as follows, and the redeployment worked fine, and I even checked stable!

system func preupgrade() {
    book_stable := Array.init(book.size(), (Principal.fromText("aaaaa-aa"), []));

I was thinking something was failing in the loop, but the initial value was the error…
Thanks again!