// ParseContract parses and performs sanity checks on the payload of a contract transaction.
ParseContract parses and performs sanity checks on the payload of a contract transaction.
// ParseContract parses and performs sanity checks on the payload of a contract transaction .
func ParseContract(payload []byte) (Contract, error) {
	r := bytes.NewReader(payload)
	b := make([]byte, 8)
	var contract Contract
	if _, err := io.ReadFull(r, b[:8]); err != nil {
		return contract, errors.Wrap(err, "contract: failed to decode gas limit")
	}
	contract.GasLimit = binary.LittleEndian.Uint64(b)
	if _, err := io.ReadFull(r, b[:8]); err != nil {
		return contract, errors.Wrap(err, "contract: failed to decode gas deposit")
	}
	if contract.GasLimit == 0 {
		return contract, errors.New(
			"contract: gas limit for invoking smart contract function must be greater than zero",
		)
	}
	contract.GasDeposit = binary.LittleEndian.Uint64(b)
	if _, err := io.ReadFull(r, b[:4]); err != nil {
		return contract, errors.Wrap(err, "contract: failed to decode number of smart contract init parameters")
	}
	size := binary.LittleEndian.Uint32(b[:4])
	if size > 1024*1024 {
		return contract, errors.New("contract: smart contract payload exceeds 1MB")
	}
	contract.Params = make([]byte, size)
	if _, err := io.ReadFull(r, contract.Params); err != nil {
		return contract, errors.Wrap(err, "contract: failed to decode smart contract init parameters")
	}
	var err error
	if contract.Code, err = ioutil.ReadAll(r); err != nil {
		return contract, errors.Wrap(err, "contract: failed to decode smart contract code")
	}
	if len(contract.Code) == 0 {
		return contract, errors.New("contract: smart contract must have code of length greater than zero")
	}
	return contract, nil
}
func ParseContract ( payload [ ] byte ) ( Contract , error ) { r := bytes . NewReader ( payload ) b := make ( [ ] byte , 8 ) var contract Contract if _ , err := io . ReadFull ( r , b [ : 8 ] ) ; err != nil { return contract , errors . Wrap ( err , " " ) } contract . GasLimit = binary . LittleEndian . Uint64 ( b ) if _ , err := io . ReadFull ( r , b [ : 8 ] ) ; err != nil { return contract , errors . Wrap ( err , " " ) } if contract . GasLimit == 0 { return contract , errors . New ( " " , ) } contract . GasDeposit = binary . LittleEndian . Uint64 ( b ) if _ , err := io . ReadFull ( r , b [ : 4 ] ) ; err != nil { return contract , errors . Wrap ( err , " " ) } size := binary . LittleEndian . Uint32 ( b [ : 4 ] ) if size > 1024 * 1024 { return contract , errors . New ( " " ) } contract . Params = make ( [ ] byte , size ) if _ , err := io . ReadFull ( r , contract . Params ) ; err != nil { return contract , errors . Wrap ( err , " " ) } var err error if contract . Code , err = ioutil . ReadAll ( r ) ; err != nil { return contract , errors . Wrap ( err , " " ) } if len ( contract . Code ) == 0 { return contract , errors . New ( " " ) } return contract , nil }
ParseContract parses and performs sanity checks on the payload of a contract transaction.
ParseContract parses and performs sanity checks on the payload of a contract transaction .