[英]Borsh JS and Borsh Rust slightly different serialized output
我正在尝试将 borsh 序列化数据从 JS 发送到 rust 应用程序。 但是,当序列化 javascript 和 rust 中的数据(以比较输出)时,我在 rust 序列化 output 中得到 4 个额外字节。这是代码:
博尔什 JS 代码
// class
class Poll {
id: string = '';
question: string = '';
options: string[] = [];
votes: number[] = [];
constructor(fields?: {
id: string;
question: string;
options: string[];
votes: number[];
}) {
if (fields) {
this.id = fields.id;
this.question = fields.question;
this.options = fields.options;
this.votes = fields.votes;
}
}
}
// Schema
const schema: Schema = new Map([
[
Poll,
{
kind: 'struct',
fields: [
['id', 'string'],
['question', 'string'],
['options', ['string']],
['votes', ['u32', 1]],
],
},
],
]);
// class object
const testPoll = new Poll({
id: '1',
question: 'What is your favorite color?',
options: ['a', 'b', 'c'],
votes: [100],
});
//object serialization
let serializedPoll: Uint8Array = new Uint8Array();
serializedPoll = serialize(schema, testPoll); // this succeeds
// output
[1, 0, 0, 0, 49, 28, 0, 0, 0, 87, 104, 97, 116, 32, 105, 115, 32, 121, 111, 117, 114, 32, 102, 97, 118, 111, 114, 105, 116, 101, 32, 99, 111, 108, 111, 114, 63, 3, 0, 0, 0, 1, 0, 0, 0, 97, 1, 0, 0, 0, 98, 1, 0, 0, 0, 99, 100, 0, 0, 0]
博尔什 Rust 代码
#[derive(BorshDeserialize, BorshSerialize, Debug)]
pub struct Poll {
pub id: String,
pub question: String,
pub options: Vec<String>,
pub votes: Vec<u32>,
}
// poll object - with same values as that in JS code above
let p = Poll {
id: "1".to_string(),
question: "What is your favorite color?".to_string(),
options: vec!["a".to_string(), "b".to_string(), "c".to_string()],
votes: vec![100],
};
// serialization
let serialized_data = p.try_to_vec().unwrap(); // this succeeds
//output
[1, 0, 0, 0, 49, 28, 0, 0, 0, 87, 104, 97, 116, 32, 105, 115, 32, 121, 111, 117, 114, 32, 102, 97, 118, 111, 114, 105, 116, 101, 32, 99, 111, 108, 111, 114, 63, 3, 0, 0, 0, 1, 0, 0, 0, 97, 1, 0, 0, 0, 98, 1, 0, 0, 0, 99, 1, 0, 0, 0, 100, 0, 0, 0]
比较两者的输出
[1, 0, 0, 0, 49, 28, 0, 0, 0, 87, 104, 97, 116, 32, 105, 115, 32, 121, 111, 117, 114, 32, 102, 97, 118, 111, 114, 105, 116, 101, 32, 99, 111, 108, 111, 114, 63, 3, 0, 0, 0, 1, 0, 0, 0, 97, 1, 0, 0, 0, 98, 1, 0, 0, 0, 99, 100, 0, 0, 0]
[1, 0, 0, 0, 49, 28, 0, 0, 0, 87, 104, 97, 116, 32, 105, 115, 32, 121, 111, 117, 114, 32, 102, 97, 118, 111, 114, 105, 116, 101, 32, 99, 111, 108, 111, 114, 63, 3, 0, 0, 0, 1, 0, 0, 0, 97, 1, 0, 0, 0, 98, 1, 0, 0, 0, 99, 1, 0, 0, 0, 100, 0, 0, 0]
序列化为 output 的 rust 中有额外的 4 个字节(1、0、0、0) 。我相信这是因为Vec<u32>
用于votes
字段(它适用于u32
)。 但是我无法理解为什么会这样。
感谢任何形式的帮助/见解。
谢谢!
Vec<u32>
必须对数据的长度进行编码,因为Vec
表示可变大小。 相反,您在 JS 中将模式设计为['u32', 1]
,这是一个长度为 1 的数组,因此它不需要编码长度,因为它是固定大小。
要解决差异,请将架构设置为可变大小数组: ['u32']
。 或者将 Rust 中的类型更改为固定大小的数组: votes: [u32; 1]
votes: [u32; 1]
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.