Skip to content

Commit 767a492

Browse files
antouhouCofresi
authored andcommitted
Feature/v13 sml changes (#101)
* Add changes to SMLEntries from the latest core version * Fix SMLEntry.fromBuffer method * Fix ip serialization and tests * Add more tests * Reverse byte order in hash in tests * Update SMLDiff test with a new diff * Fix SMLDiff, coinbase payload and ip serializer * Bump module version * Update Fix typo in `deterministicmnlist` dir name
1 parent 87412e3 commit 767a492

File tree

13 files changed

+213
-137
lines changed

13 files changed

+213
-137
lines changed

index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ bitcore.errors = require('./lib/errors');
4848
bitcore.Address = require('./lib/address');
4949
bitcore.Block = require('./lib/block');
5050
bitcore.MerkleBlock = require('./lib/block/merkleblock');
51-
bitcore.SimplifiedMNListDiff = require('./lib/deterministcmnlist/SimplifiedMNListDiff');
52-
bitcore.SimplifiedMNListEntry = require('./lib/deterministcmnlist/SimplifiedMNListEntry');
51+
bitcore.SimplifiedMNListDiff = require('./lib/deterministicmnlist/SimplifiedMNListDiff');
52+
bitcore.SimplifiedMNListEntry = require('./lib/deterministicmnlist/SimplifiedMNListEntry');
5353
bitcore.BlockHeader = require('./lib/block/blockheader');
5454
bitcore.HDPrivateKey = require('./lib/hdprivatekey.js');
5555
bitcore.HDPublicKey = require('./lib/hdpublickey.js');

lib/constants/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,5 @@ module.exports = {
3535
EMPTY_IPV6_ADDRESS: '[0:0:0:0:0:0:0:0]:0',
3636
EMPTY_IPV4_ADDRESS: '0.0.0.0:0',
3737
CURRENT_PROTOCOL_VERSION: 70211,
38+
SML_ENTRY_SIZE: 151,
3839
};

lib/deterministcmnlist/SimplifiedMNListDiff.js renamed to lib/deterministicmnlist/SimplifiedMNListDiff.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ var constants = require('../constants');
2323
* @property {Transaction} cbTx;
2424
* @property {Array<string>} deletedMNs - sha256 hashes of deleted MNs
2525
* @property {Array<SimplifiedMNListEntry>} mnList
26+
* @property {string} merkleRootMNList - merkle root of the whole mn list
2627
*/
2728
function SimplifiedMNListDiff(arg) {
2829
if (arg) {
@@ -46,27 +47,28 @@ function SimplifiedMNListDiff(arg) {
4647
* @return {SimplifiedMNListDiff}
4748
*/
4849
SimplifiedMNListDiff.fromBuffer = function fromBuffer(buffer) {
49-
var bufferReader = new BufferReader(buffer);
50+
var bufferReader = new BufferReader(Buffer.from(buffer));
5051
var data = {};
5152

52-
data.baseBlockHash = bufferReader.read(constants.SHA256_HASH_SIZE).toString('hex');
53-
data.blockHash = bufferReader.read(constants.SHA256_HASH_SIZE).toString('hex');
53+
data.baseBlockHash = bufferReader.read(constants.SHA256_HASH_SIZE).reverse().toString('hex');
54+
data.blockHash = bufferReader.read(constants.SHA256_HASH_SIZE).reverse().toString('hex');
5455

5556
data.cbTxMerkleTree = PartialMerkleTree.fromBufferReader(bufferReader);
5657
data.cbTx = new Transaction().fromBufferReader(bufferReader);
5758

5859
var deletedMNsCount = bufferReader.readVarintNum();
5960
data.deletedMNs = [];
6061
for (var i = 0; i < deletedMNsCount; i++) {
61-
data.deletedMNs.push(bufferReader.read(constants.SHA256_HASH_SIZE).toString('hex'));
62+
data.deletedMNs.push(bufferReader.read(constants.SHA256_HASH_SIZE).reverse().toString('hex'));
6263
}
6364

6465
var mnListSize = bufferReader.readVarintNum();
6566
data.mnList = [];
6667
for (var i = 0; i < mnListSize; i++) {
67-
data.mnList.push(SimplifiedMNListEntry.fromBuffer(bufferReader.read(91)));
68+
data.mnList.push(SimplifiedMNListEntry.fromBuffer(bufferReader.read(constants.SML_ENTRY_SIZE)));
6869
}
6970

71+
data.merkleRootMNList = data.cbTx.extraPayload.merkleRootMNList;
7072
return this.fromObject(data);
7173
};
7274

@@ -85,15 +87,15 @@ SimplifiedMNListDiff.fromHexString = function fromHexString(hexString) {
8587
SimplifiedMNListDiff.prototype.toBuffer = function toBuffer() {
8688
var bufferWriter = new BufferWriter();
8789

88-
bufferWriter.write(Buffer.from(this.baseBlockHash, 'hex'));
89-
bufferWriter.write(Buffer.from(this.blockHash, 'hex'));
90+
bufferWriter.write(Buffer.from(this.baseBlockHash, 'hex').reverse());
91+
bufferWriter.write(Buffer.from(this.blockHash, 'hex').reverse());
9092

9193
bufferWriter.write(this.cbTxMerkleTree.toBuffer());
9294
bufferWriter.write(this.cbTx.toBuffer());
9395

9496
bufferWriter.writeVarintNum(this.deletedMNs.length);
9597
this.deletedMNs.forEach(function (deleteMNHash) {
96-
bufferWriter.write(Buffer.from(deleteMNHash, 'hex'));
98+
bufferWriter.write(Buffer.from(deleteMNHash, 'hex').reverse());
9799
});
98100

99101
bufferWriter.writeVarintNum(this.mnList.length);
@@ -125,6 +127,7 @@ SimplifiedMNListDiff.fromObject = function fromObject(obj) {
125127
simplifiedMNListDiff.mnList = obj.mnList.map(function (SMLEntry) {
126128
return new SimplifiedMNListEntry(SMLEntry);
127129
});
130+
simplifiedMNListDiff.merkleRootMNList = obj.merkleRootMNList;
128131

129132
return simplifiedMNListDiff;
130133
};
@@ -144,6 +147,7 @@ SimplifiedMNListDiff.prototype.toObject = function toObject() {
144147
obj.mnList = this.mnList.map(function (SMLEntry) {
145148
return SMLEntry.toObject();
146149
});
150+
obj.merkleRootMNList = this.merkleRootMNList;
147151

148152
return obj;
149153
};

lib/deterministcmnlist/SimplifiedMNListEntry.js renamed to lib/deterministicmnlist/SimplifiedMNListEntry.js

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@ var serializeIp = ipUtils.ipAndPortToBuffer;
2020

2121
var SHA256_HASH_SIZE = constants.SHA256_HASH_SIZE;
2222
var PUBKEY_ID_SIZE = constants.PUBKEY_ID_SIZE;
23+
var BLS_PUBLIC_KEY_SIZE = constants.BLS_PUBLIC_KEY_SIZE;
2324

2425
/**
2526
* @typedef {Object} SMLEntry
26-
* @property {string} proRegTxHash uint256
27+
* @property {string} proRegTxHash
28+
* @property {string} confirmedHash
2729
* @property {string} service - ip and port
28-
* @property {string} keyIDOperator - public key hash, 20 bytes
30+
* @property {string} pubKeyOperator - operator public key
2931
* @property {string} keyIDVoting - public key hash, 20 bytes
3032
* @property {boolean} isValid
3133
*/
@@ -34,9 +36,10 @@ var PUBKEY_ID_SIZE = constants.PUBKEY_ID_SIZE;
3436
* @class SimplifiedMNListEntry
3537
* @param {*} arg - A Buffer, JSON string, or Object representing a SmlEntry
3638
* @constructor
37-
* @property {string} proRegTxHash uint256
39+
* @property {string} proRegTxHash
40+
* @property {string} confirmedHash
3841
* @property {string} service - ip and port
39-
* @property {string} keyIDOperator - public key hash, 20 bytes
42+
* @property {string} pubKeyOperator - operator public key
4043
* @property {string} keyIDVoting - public key hash, 20 bytes
4144
* @property {boolean} isValid
4245
*/
@@ -67,10 +70,11 @@ SimplifiedMNListEntry.fromBuffer = function fromBuffer(buffer) {
6770
var bufferReader = new BufferReader(buffer);
6871

6972
return SimplifiedMNListEntry.fromObject({
70-
proRegTxHash: bufferReader.read(SHA256_HASH_SIZE).toString('hex'),
73+
proRegTxHash: bufferReader.read(SHA256_HASH_SIZE).reverse().toString('hex'),
74+
confirmedHash: bufferReader.read(SHA256_HASH_SIZE).reverse().toString('hex'),
7175
service: parseIp(bufferReader.read(ipUtils.IP_AND_PORT_SIZE)),
72-
keyIDOperator: bufferReader.read(PUBKEY_ID_SIZE).toString('hex'),
73-
keyIDVoting: bufferReader.read(PUBKEY_ID_SIZE).toString('hex'),
76+
pubKeyOperator: bufferReader.read(BLS_PUBLIC_KEY_SIZE).toString('hex'),
77+
keyIDVoting: bufferReader.read(PUBKEY_ID_SIZE).reverse().toString('hex'),
7478
isValid: Boolean(bufferReader.readUInt8())
7579
});
7680
};
@@ -91,10 +95,11 @@ SimplifiedMNListEntry.prototype.toBuffer = function toBuffer() {
9195
this.validate();
9296
var bufferWriter = new BufferWriter();
9397

94-
bufferWriter.write(Buffer.from(this.proRegTxHash, 'hex'));
98+
bufferWriter.write(Buffer.from(this.proRegTxHash, 'hex').reverse());
99+
bufferWriter.write(Buffer.from(this.confirmedHash, 'hex').reverse());
95100
bufferWriter.write(serializeIp(this.service));
96-
bufferWriter.write(Buffer.from(this.keyIDOperator, 'hex'));
97-
bufferWriter.write(Buffer.from(this.keyIDVoting, 'hex'));
101+
bufferWriter.write(Buffer.from(this.pubKeyOperator, 'hex'));
102+
bufferWriter.write(Buffer.from(this.keyIDVoting, 'hex').reverse());
98103
bufferWriter.writeUInt8(Number(this.isValid));
99104

100105
return bufferWriter.toBuffer();
@@ -108,8 +113,9 @@ SimplifiedMNListEntry.prototype.toBuffer = function toBuffer() {
108113
SimplifiedMNListEntry.fromObject = function fromObject(obj) {
109114
var SMLEntry = new SimplifiedMNListEntry();
110115
SMLEntry.proRegTxHash = obj.proRegTxHash;
116+
SMLEntry.confirmedHash = obj.confirmedHash;
111117
SMLEntry.service = obj.service;
112-
SMLEntry.keyIDOperator = obj.keyIDOperator;
118+
SMLEntry.pubKeyOperator = obj.pubKeyOperator;
113119
SMLEntry.keyIDVoting = obj.keyIDVoting;
114120
SMLEntry.isValid = obj.isValid;
115121

@@ -119,22 +125,24 @@ SimplifiedMNListEntry.fromObject = function fromObject(obj) {
119125

120126
SimplifiedMNListEntry.prototype.validate = function validate() {
121127
$.checkArgument(isSha256(this.proRegTxHash), 'Expected proRegTxHash to be a sha256 hex string');
128+
$.checkArgument(isSha256(this.confirmedHash), 'Expected confirmedHash to be a sha256 hex string');
122129
if (!ipUtils.isZeroAddress(this.service)) {
123130
$.checkArgument(ipUtils.isIPV4(this.service), 'Expected service to be a string with ip address and port');
124131
}
125132
if (!isHexStringOfSize(this.keyIDVoting, PUBKEY_ID_SIZE * 2)) {
126133
console.log(this.keyIDVoting);
127134
}
128-
$.checkArgument(isHexStringOfSize(this.keyIDOperator, PUBKEY_ID_SIZE * 2), 'Expected keyIDOperator to be a pubkey id');
135+
$.checkArgument(isHexStringOfSize(this.pubKeyOperator, BLS_PUBLIC_KEY_SIZE * 2), 'Expected pubKeyOperator to be a pubkey id');
129136
$.checkArgument(isHexStringOfSize(this.keyIDVoting, PUBKEY_ID_SIZE * 2), 'Expected keyIDVoting to be a pubkey id');
130137
$.checkArgument(typeof this.isValid === 'boolean', 'Expected isValid to be a boolean');
131138
};
132139

133140
SimplifiedMNListEntry.prototype.toObject = function toObject() {
134141
return {
135142
proRegTxHash: this.proRegTxHash,
143+
confirmedHash: this.confirmedHash,
136144
service: this.service,
137-
keyIDOperator: this.keyIDOperator,
145+
pubKeyOperator: this.pubKeyOperator,
138146
keyIDVoting: this.keyIDVoting,
139147
isValid: this.isValid
140148
};
@@ -143,7 +151,7 @@ SimplifiedMNListEntry.prototype.toObject = function toObject() {
143151
/**
144152
* @return {Buffer}
145153
*/
146-
SimplifiedMNListEntry.prototype.getHash = function hash() {
154+
SimplifiedMNListEntry.prototype.calculateHash = function calculateHash() {
147155
return Hash.sha256sha256(this.toBuffer());
148156
};
149157

lib/transaction/payload/coinbasepayload.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ CoinbasePayload.fromBuffer = function (rawPayload) {
4747
var payload = new CoinbasePayload();
4848
payload.version = payloadBufferReader.readUInt16LE();
4949
payload.height = payloadBufferReader.readUInt32LE();
50-
payload.merkleRootMNList = payloadBufferReader.read(HASH_SIZE).toString('hex');
50+
payload.merkleRootMNList = payloadBufferReader.read(HASH_SIZE).reverse().toString('hex');
5151

5252
if (!payloadBufferReader.finished()) {
5353
throw new Error('Failed to parse payload: raw payload is bigger than expected.');
@@ -110,7 +110,7 @@ CoinbasePayload.prototype.toBuffer = function toBuffer() {
110110
payloadBufferWriter
111111
.writeUInt16LE(this.version)
112112
.writeUInt32LE(this.height)
113-
.write(Buffer.from(this.merkleRootMNList, 'hex'));
113+
.write(Buffer.from(this.merkleRootMNList, 'hex').reverse());
114114

115115
return payloadBufferWriter.toBuffer();
116116
};

lib/util/ip.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ var BufferWriter = require('../encoding/bufferwriter');
55
var BufferReader = require('../encoding/bufferreader');
66
var constants = require('../constants');
77
var serviceRegex = /^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]):[0-9]+$/;
8-
var ipV6prefix = Buffer.alloc(12);
8+
var ipV6prefix = Buffer.from('00000000000000000000ffff', 'hex');
99
var emptyAddress = Buffer.alloc(18);
1010
var EMPTY_IPV6_ADDRESS = constants.EMPTY_IPV6_ADDRESS;
1111
var EMPTY_IPV4_ADDRESS = constants.EMPTY_IPV4_ADDRESS;

0 commit comments

Comments
 (0)