web3j icon indicating copy to clipboard operation
web3j copied to clipboard

Call the contract method error

Open zunpan opened this issue 3 years ago • 0 comments

_issue_title

Call the contract method error

Issue_description

I am using zksnark in my contract.

This is my contract.

library Pairing {
    struct G1Point {
        uint X;
        uint Y;
    }
    // Encoding of field elements is: X[0] * z + X[1]
    struct G2Point {
        uint[2] X;
        uint[2] Y;
    }
}

function set_verififying_key(
        uint vid,
        Pairing.G2Point memory a,
        Pairing.G1Point memory b,
        Pairing.G2Point memory c,
        Pairing.G2Point memory gamma,
        Pairing.G1Point memory gamma_beta_1,
        Pairing.G2Point memory gamma_beta_2,
        Pairing.G2Point memory z,
        Pairing.G1Point[] memory _ic
    ) public onlyHasVoting(vid) onlyVotingNotStarted(vid) {

This is the Contract class compiled by web3j-cli

public static class G2Point extends StaticStruct {
        public List<BigInteger> X;

        public List<BigInteger> Y;

        public G2Point(List<BigInteger> X, List<BigInteger> Y) {
            super(new org.web3j.abi.datatypes.generated.StaticArray2<org.web3j.abi.datatypes.generated.Uint256>(X),new org.web3j.abi.datatypes.generated.StaticArray2<org.web3j.abi.datatypes.generated.Uint256>(Y));
            this.X = X;
            this.Y = Y;
        }

        public G2Point(StaticArray2<Uint256> X, StaticArray2<Uint256> Y) {
            super(X,Y);
            this.X = X.getValue();
            this.Y = Y.getValue();
        }
    }

    public static class G1Point extends StaticStruct {
        public BigInteger X;

        public BigInteger Y;

        public G1Point(BigInteger X, BigInteger Y) {
            super(new org.web3j.abi.datatypes.generated.Uint256(X),new org.web3j.abi.datatypes.generated.Uint256(Y));
            this.X = X;
            this.Y = Y;
        }

        public G1Point(Uint256 X, Uint256 Y) {
            super(X,Y);
            this.X = X.getValue();
            this.Y = Y.getValue();
        }
    }

There is a bug in G2Point class. So I modified as follows

public static class G2Point extends StaticStruct {
        public List<BigInteger> X;

        public List<BigInteger> Y;

        public G2Point(List<Uint256> X, List<Uint256> Y) {
            super(new org.web3j.abi.datatypes.generated.StaticArray2<Uint256>(X),new org.web3j.abi.datatypes.generated.StaticArray2<Uint256>(Y));
            this.X = X.stream().map(x -> x.getValue()).collect(Collectors.toList());
            this.Y = Y.stream().map(x -> x.getValue()).collect(Collectors.toList());
        }

        public G2Point(StaticArray2<Uint256> X, StaticArray2<Uint256> Y) {
            super(X,Y);
            this.X = X.getValue().stream().map(x -> x.getValue()).collect(Collectors.toList());
            this.Y = Y.getValue().stream().map(x -> x.getValue()).collect(Collectors.toList());
        }
    }

  public static class G1Point extends StaticStruct {
      public BigInteger X;

      public BigInteger Y;

      public G1Point(BigInteger X, BigInteger Y) {
          super(new org.web3j.abi.datatypes.generated.Uint256(X),new org.web3j.abi.datatypes.generated.Uint256(Y));
          this.X = X;
          this.Y = Y;
      }

      public G1Point(Uint256 X, Uint256 Y) {
          super(X,Y);
          this.X = X.getValue();
          this.Y = Y.getValue();
      }
  }

This is my setVerifyingKeyTest function

    @Test
    public void setVerifyingKeyTest(){
        Contracts_Voting_sol_VotingContract contract = Contracts_Voting_sol_VotingContract.load(votingContractAddress, web3j, credentials, contractGasProvider);

        BigInteger[] ids = new BigInteger[2];
        ids[0] = new BigInteger("1");
        ids[1] = new BigInteger("2");
        Key key = ZKVotingJNI.GenerateVoterKeys(ids);
        VerifyingKey verifyingKey = key.verifyingKey;

        VerifyingKeySolidity verifyingKeySolidity = VerifyingKey.toVerifyingKeySolidity(verifyingKey);
        
        // exception
        try {
            contract.set_verififying_key(new BigInteger("0"), verifyingKeySolidity.a, verifyingKeySolidity.b, verifyingKeySolidity.c,
                    verifyingKeySolidity.gamma, verifyingKeySolidity.gamma_beta_1, verifyingKeySolidity.gamma_beta_2, verifyingKeySolidity.z, verifyingKeySolidity._ic).send();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

Error happens when I use web3j call set_verififying_key method

Issue_context

I had debugger the test method. I deployed the contract in remix and I copyed all value in to remix like follows. It works image

image

zunpan avatar Apr 12 '22 02:04 zunpan