Skip to content

Commit

Permalink
fix: ensure go-interop on the message level
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire committed Jun 8, 2016
1 parent 22d6f25 commit 6fecc48
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 10 deletions.
20 changes: 13 additions & 7 deletions src/message/index.js
Expand Up @@ -45,19 +45,25 @@ class BitswapMessage {
}

toProto () {
return pbm.Message.encode({
const msg = {
wantlist: {
entries: Array.from(this.wantlist.values()).map((e) => {
return {
block: mh.toB58String(e.key),
block: e.key,
priority: Number(e.priority),
cancel: Boolean(e.cancel)
}
}),
full: this.full
})
},
blocks: Array.from(this.blocks.values()).map((b) => b.data)
})
blocks: Array.from(this.blocks.values())
.map((b) => b.data)
}

if (this.full) {
msg.wantlist.full = true
}

return pbm.Message.encode(msg)
}

equals (other) {
Expand Down Expand Up @@ -89,7 +95,7 @@ BitswapMessage.fromProto = (raw) => {
const m = new BitswapMessage(dec.wantlist.full)

dec.wantlist.entries.forEach((e) => {
m.addEntry(mh.fromB58String(e.block), e.priority, e.cancel)
m.addEntry(e.block, e.priority, e.cancel)
})
dec.blocks.forEach((b) => m.addBlock(new Block(b)))

Expand Down
2 changes: 1 addition & 1 deletion src/message/message.proto
Expand Up @@ -5,7 +5,7 @@ message Message {
message Wantlist {

message Entry {
optional string block = 1; // the block key
optional bytes block = 1; // the block key
optional int32 priority = 2; // the priority (normalized). default to 1
optional bool cancel = 3; // whether this revokes an entry
}
Expand Down
23 changes: 21 additions & 2 deletions test/message.spec.js
Expand Up @@ -12,6 +12,25 @@ const pbm = protobuf(fs.readFileSync(path.join(__dirname, '../src/message/messag
const BitswapMessage = require('../src/message')

describe('BitswapMessage', () => {
it('go interop', () => {
const goEncoded = new Buffer('CioKKAoiEiAs8k26X7CjDiboOyrFueKeGxYeXB+nQl5zBDNik4uYJBAKGAA=', 'base64')

const m = new BitswapMessage(false)
m.addEntry(mh.fromB58String('QmRN6wdp1S2A5EtjW9A3M1vKSBuQQGcgvuhoMUoEz4iiT5'), 10)

expect(
BitswapMessage.fromProto(goEncoded)
).to.be.eql(
m
)

expect(
m.toProto()
).to.be.eql(
goEncoded
)
})

it('append wanted', () => {
const str = 'foo'
const block = new Block(str)
Expand All @@ -21,7 +40,7 @@ describe('BitswapMessage', () => {
expect(
pbm.Message.decode(m.toProto()).wantlist.entries[0]
).to.be.eql({
block: mh.toB58String(block.key),
block: block.key,
priority: 1,
cancel: false
})
Expand All @@ -43,7 +62,7 @@ describe('BitswapMessage', () => {
const raw = pbm.Message.encode({
wantlist: {
entries: [{
block: mh.toB58String(new Buffer('hello')),
block: new Buffer('hello'),
cancel: false
}],
full: true
Expand Down

0 comments on commit 6fecc48

Please sign in to comment.