goat-systems / go-tezos

Go Tezos Is a Go library that exposes and builds upon the Tezos RPC.
MIT License
71 stars 45 forks source link

Forging endorsement and seed nonce revelation contain null metadata #168

Closed utdrmac closed 3 years ago

utdrmac commented 3 years ago

When forging the two noted operations, the Marshal function creates "metadata:null" in the resulting JSON. This produces a Unexpected object field metadata error when preapplying to the RPC node.

nonceRevelation := rpc.Content{
    Kind:  rpc.SEEDNONCEREVELATION,
    Level: nonce.Level,
    Nonce: nonce.SeedHashHex,
}

nonceRevelationBytes, err := forge.Encode(block.Hash, nonceRevelation)
if err != nil {
    log.WithError(err).Error("Error Forging Nonce Reveal")
    return
}

preapplyNonceRevealOp := rpc.PreapplyOperationsInput{
    Blockhash: block.Hash,
    Operations: []rpc.Operations{
        {
            Branch: block.Hash,
            Contents:  rpc.Contents{
                nonceRevelation,
            },
            Protocol:  block.Protocol,
            Signature: signedNonceReveal.EDSig,
        },
    },
}

// Validate the operation against the node for any errors
preApplyResp, err := gt.PreapplyOperations(preapplyNonceRevealOp)
if err != nil {
    log.WithError(err).Error("Could not preapply nonce reveal operation")
    return
}

Output:

ERRO[2020-10-09T22:01:22Z] Could not preapply nonce reveal operation     error="failed to preapply operation: response returned code 400 with body Failed to parse the request body: No case matched:\n  At /kind, unexpected string instead of endorsement\n  Unexpected object field metadata

Solution that worked for me, set metadata to omitempty:

// https://github.com/goat-systems/go-tezos/blob/6fecc940c49c88c9f7e7f600d3417f74e31b2252/rpc/block.go#L847
type SeedNonceRevelation struct {
    Kind     Kind                         `json:"kind"`
    Level    int                          `json:"level"`
    Nonce    string                       `json:"nonce"`
    Metadata *SeedNonceRevelationMetadata `json:"metadata,omitempty"`
}
DefinitelyNotAGoat commented 3 years ago

Thanks for reporting!

DefinitelyNotAGoat commented 3 years ago

Fixed in V4.