txscript: Convert to new scriptnum type.
This commit implements a new type, named scriptNum, for handling all numeric values used in scripts and converts the code over to make use of it. This is being done for a few of reasons. First, the consensus rules for handling numeric values in the scripts require special handling with subtle semantics. By encapsulating those details into a type specifically dedicated to that purpose, it simplifies the code and generally helps prevent improper usage. Second, the new type is quite a bit more efficient than big.Ints which are designed to be arbitrarily large and thus involve a lot of heap allocations and additional multi-precision bookkeeping. Because this new type is based on an int64, it allows the numbers to be stack allocated thereby eliminating a lot of GC and also eliminates the extra multi-precision arithmetic bookkeeping. The use of an int64 is possible because the consensus rules dictate that when data is interpreted as a number, it is limited to an int32 even though results outside of this range are allowed so long as they are not interpreted as integers again themselves. Thus, the maximum possible result comes from multiplying a max int32 by itself which safely fits into an int64 and can then still appropriately provide the serialization of the larger number as required by consensus. Finally, it more closely resembles the implementation used by Bitcoin Core and thus makes is easier to compare the behavior between the two implementations. This commit also includes a full suite of tests with 100% coverage of the semantics of the new type.
This commit is contained in:
parent
6e402deb35
commit
b6e52fbd93
9 changed files with 582 additions and 340 deletions
txscript
|
@ -7,7 +7,6 @@ package txscript
|
|||
import (
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"math/big"
|
||||
)
|
||||
|
||||
const (
|
||||
|
@ -228,38 +227,7 @@ func (b *ScriptBuilder) AddInt64(val int64) *ScriptBuilder {
|
|||
return b
|
||||
}
|
||||
|
||||
return b.AddData(fromInt(new(big.Int).SetInt64(val)))
|
||||
}
|
||||
|
||||
// AddUint64 pushes the passed integer to the end of the script. The script
|
||||
// will not be modified if pushing the data would cause the script to
|
||||
// exceed the maximum allowed script engine size.
|
||||
func (b *ScriptBuilder) AddUint64(val uint64) *ScriptBuilder {
|
||||
if b.err != nil {
|
||||
return b
|
||||
}
|
||||
|
||||
// Pushes that would cause the script to exceed the largest allowed
|
||||
// script size would result in a non-canonical script.
|
||||
if len(b.script)+1 > maxScriptSize {
|
||||
str := fmt.Sprintf("adding an unsigned integer would exceed "+
|
||||
"the maximum allow canonical script length of %d",
|
||||
maxScriptSize)
|
||||
b.err = ErrScriptNotCanonical(str)
|
||||
return b
|
||||
}
|
||||
|
||||
// Fast path for small integers.
|
||||
if val == 0 {
|
||||
b.script = append(b.script, OP_0)
|
||||
return b
|
||||
}
|
||||
if val >= 1 && val <= 16 {
|
||||
b.script = append(b.script, byte((OP_1-1)+val))
|
||||
return b
|
||||
}
|
||||
|
||||
return b.AddData(fromInt(new(big.Int).SetUint64(val)))
|
||||
return b.AddData(scriptNum(val).Bytes())
|
||||
}
|
||||
|
||||
// Reset resets the script so it has no content.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue