3
0
mirror of https://github.com/Qortal/altcoinj.git synced 2025-02-15 03:35:52 +00:00

ScriptBuilder: Fix minimal number encoding for -1.

This commit is contained in:
Nicola Atzei 2017-10-26 15:20:06 +02:00 committed by Andreas Schildbach
parent 383801bbb9
commit 9f5f973d8a
2 changed files with 38 additions and 6 deletions

View File

@ -112,11 +112,7 @@ public class ScriptBuilder {
* shortest encoding possible.
*/
public ScriptBuilder number(long num) {
if (num >= 0 && num <= 16) {
return smallNum((int) num);
} else {
return bigNum(num);
}
return number(chunks.size(), num);
}
/**
@ -124,7 +120,9 @@ public class ScriptBuilder {
* uses shortest encoding possible.
*/
public ScriptBuilder number(int index, long num) {
if (num >= 0 && num <= 16) {
if (num == -1) {
return op(index, OP_1NEGATE);
} else if (num >= 0 && num <= 16) {
return smallNum(index, (int) num);
} else {
return bigNum(index, num);

View File

@ -0,0 +1,34 @@
/*
* Copyright 2017 Nicola Atzei
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.bitcoinj.script;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
public class ScriptBuilderTest {
@Test
public void testNumber() {
for (int i = -100; i <= 100; i++) {
Script s = new ScriptBuilder().number(i).build();
for (ScriptChunk ch : s.chunks) {
assertTrue(Integer.toString(i), ch.isShortestPossiblePushData());
}
}
}
}