New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace Buffer.from(bn.toArray()) with bn.toArrayLike(Buffer) #180

Merged
merged 1 commit into from Aug 17, 2017

Conversation

Projects
None yet
3 participants
@axic
Member

axic commented Aug 15, 2017

The point of this is to avoid double serialisation. Currently bn.js is serialised to a Javascript Array which is then ported into a Javascript Buffer. After this change it is directly serialised into a Buffer.

@axic axic added the in progress label Aug 15, 2017

@axic

This comment has been minimized.

Show comment
Hide comment
@axic

axic Aug 16, 2017

Member

@holgerd77 @jwasinger please review this too

Member

axic commented Aug 16, 2017

@holgerd77 @jwasinger please review this too

@axic axic requested review from wanderer and cdetrio Aug 16, 2017

@holgerd77

Everything look good to me.

@@ -49,7 +49,7 @@ function VM (opts = {}) {
if (this.opts.activatePrecompiles) {
for (var i = 1; i <= 4; i++) {
this.trie.put(Buffer.from(new BN(i).toArray('be', 20)), new Account().serialize())
this.trie.put(new BN(i).toArrayLike(Buffer, 'be', 20), new Account().serialize())

This comment has been minimized.

@holgerd77

holgerd77 Aug 16, 2017

Member

Ok, same resulting buffer objects.

@holgerd77

holgerd77 Aug 16, 2017

Member

Ok, same resulting buffer objects.

.imul(new BN(b))
.mod(utils.TWO_POW256)
.toArray('be', 32)
)
.toArrayLike(Buffer, 'be', 32)

This comment has been minimized.

@holgerd77

holgerd77 Aug 16, 2017

Member

Both ok.

@holgerd77

holgerd77 Aug 16, 2017

Member

Both ok.

} else {
a = utils.fromSigned(a)
r = utils.toUnsigned(a.div(b))
return utils.toUnsigned(a.div(b))

This comment has been minimized.

@holgerd77

holgerd77 Aug 16, 2017

Member

Both ok.

@holgerd77

holgerd77 Aug 16, 2017

Member

Both ok.

} else {
a = new BN(a).imul(new BN(b))
r = a.mod(c).toArray('be', 32)
return a.mod(c).toArrayLike(Buffer, 'be', 32)

This comment has been minimized.

@holgerd77

holgerd77 Aug 16, 2017

Member

All 3 ok.

@holgerd77

holgerd77 Aug 16, 2017

Member

All 3 ok.

} else {
result = Buffer.from([1])
return Buffer.from([1])

This comment has been minimized.

@holgerd77

holgerd77 Aug 16, 2017

Member

Ok.

@holgerd77
.inotn(256)
.toArray('be', 32))
.toArrayLike(Buffer, 'be', 32)

This comment has been minimized.

@holgerd77

holgerd77 Aug 16, 2017

Member

All 4 ok.

@holgerd77

holgerd77 Aug 16, 2017

Member

All 4 ok.

@@ -456,7 +433,7 @@ module.exports = {
return utils.intToBuffer(runState.memoryWordCount * 32)
},
GAS: function (runState) {
return Buffer.from(runState.gasLeft.toArray('be', 32))
return runState.gasLeft.toArrayLike(Buffer, 'be', 32)

This comment has been minimized.

@holgerd77

holgerd77 Aug 16, 2017

Member

Ok.

@holgerd77
@@ -733,7 +710,7 @@ module.exports = {
runState.selfdestruct[contractAddress.toString('hex')] = selfdestructToAddress
runState.stopped = true
var newBalance = Buffer.from(new BN(contract.balance).add(new BN(toAccount.balance)).toArray())
var newBalance = new BN(contract.balance).add(new BN(toAccount.balance)).toArrayLike(Buffer)

This comment has been minimized.

@holgerd77

holgerd77 Aug 16, 2017

Member

Ok, tested one example for equivalency.

@holgerd77

holgerd77 Aug 16, 2017

Member

Ok, tested one example for equivalency.

@@ -310,7 +310,7 @@ proto.generateGenesis = function (initState, cb) {
var addresses = Object.keys(initState)
async.eachSeries(addresses, function (address, done) {
var account = new Account()
account.balance = Buffer.from((new BN(initState[address])).toArray())
account.balance = new BN(initState[address]).toArrayLike(Buffer)

This comment has been minimized.

@holgerd77

holgerd77 Aug 16, 2017

Member

All 3 ok.

@holgerd77

holgerd77 Aug 16, 2017

Member

All 3 ok.

@cdetrio

This comment has been minimized.

Show comment
Hide comment
@cdetrio

cdetrio Aug 17, 2017

Contributor

just for reference, this is using indutny/bn.js:

  • a.toBuffer(endian, length) - convert to Node.js Buffer (if available). For compatibility with browserify and similar tools, use this instead: a.toArrayLike(Buffer, endian, length)
Contributor

cdetrio commented Aug 17, 2017

just for reference, this is using indutny/bn.js:

  • a.toBuffer(endian, length) - convert to Node.js Buffer (if available). For compatibility with browserify and similar tools, use this instead: a.toArrayLike(Buffer, endian, length)

@axic axic merged commit 8a606d1 into master Aug 17, 2017

2 checks passed

ci/circleci Your tests passed on CircleCI!
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details

@axic axic removed the in progress label Aug 17, 2017

@axic axic deleted the toarraylike branch Aug 17, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment