3

I'm having trouble decoding a democracy.Voted event on Polkadot - for example: https://polkadot.js.org/apps/?rpc=wss%3A%2F%2Frpc.polkadot.io#/explorer/query/0x960b817cc1d137151ec6069753f7458fbdd6fb26dbff3888137ae8b09b4fc060

I'm working in Golang and I suspect that I am unpacking into a wrongly-defined data stucture.

As I understand it, democracy.Voted is comprised of:

  • AccountId32 - underlying data type is a 32 byte array
  • ReferendumIndex - underlying data type is an unsigned 32 bit integer
  • PalletDemocracyVoteAccountVote - described here

In Rust, the Vote data structure is:

pub struct Vote {
    pub aye: bool,
    pub conviction: Conviction,
}

I think I am having trouble with the Conviction enum.

I notice in block explorers (e.g. here) that the Vote is reported as what looks like a U8 integer - in the case of the example above, 128.

  1. What is the underlying type for Conviction?

  2. Are the variants for Conviction determined by a bitfield on a single byte?

Thanks in advance.

1 Answer 1

4

The aye (bool) & conviction (enum) is encoded together as a single byte. The top-most bit indicated the true/false, the remaining bits indicates the conviction.

You can look here for the specific Vote decoding code -

impl Decode for Vote {
    fn decode<I: Input>(input: &mut I) -> Result<Self, codec::Error> {
        let b = input.read_byte()?;
        Ok(Vote {
            aye: (b & 0b1000_0000) == 0b1000_0000,
            conviction: Conviction::try_from(b & 0b0111_1111)
                .map_err(|_| codec::Error::from("Invalid conviction"))?,
        })
    }
}

So in your example, 128 would be an aye with no conviction.

1
  • In case someone is not familiar with the binary system/bitwise operations/bit masking, maybe this hackmd Decoding vote lock up periods will be helpful. It explains in more detail the above code.
    – dominique
    Apr 14, 2023 at 12:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.