mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-06-05 13:46:42 +00:00
Replace Double-SHA256 with blake2b and implement domain seperation (#1245)
* Replace default hasher (Double-SHA256) with domain seperated blake2b * Replace all hashes with domain seperated blake2b * Update the genesis blocks * Replace OP_HASH256 with OP_BLAKE2B * Fix the merkle tree by appending zeros instead of duplicating the hash when there is 1 branch left * Update tests * Add a payloadHash function * Update gitignore to ignore binaries * Fix a bug in the blake2b opcode
This commit is contained in:
parent
9f8f0fd747
commit
45edacfbfa
18
.gitignore
vendored
18
.gitignore
vendored
@ -13,6 +13,21 @@ kaspad.db
|
||||
*.o
|
||||
*.a
|
||||
*.so
|
||||
*.dylib
|
||||
|
||||
# Test binary, built with `go test -c`
|
||||
*.test
|
||||
|
||||
# Real binaries, build with `go build .`
|
||||
kaspad
|
||||
cmd/gencerts/gencerts
|
||||
cmd/kaspactl/kaspactl
|
||||
cmd/kasminer/kaspaminer
|
||||
*.exe
|
||||
*.exe~
|
||||
|
||||
# Output of the go coverage tool
|
||||
*.out
|
||||
|
||||
# Folders
|
||||
_obj
|
||||
@ -31,8 +46,7 @@ _cgo_export.*
|
||||
|
||||
_testmain.go
|
||||
|
||||
*.exe
|
||||
|
||||
|
||||
# IDE
|
||||
.idea
|
||||
.vscode
|
||||
|
@ -6,14 +6,13 @@ package appmessage
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"github.com/kaspanet/kaspad/domain/consensus/utils/hashes"
|
||||
"strconv"
|
||||
|
||||
"github.com/kaspanet/kaspad/domain/consensus/utils/constants"
|
||||
|
||||
"github.com/kaspanet/kaspad/domain/consensus/utils/consensushashing"
|
||||
|
||||
"github.com/kaspanet/kaspad/domain/consensus/utils/hashes"
|
||||
|
||||
"github.com/kaspanet/kaspad/domain/consensus/utils/subnetworks"
|
||||
|
||||
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
|
||||
@ -285,7 +284,7 @@ func newMsgTx(version int32, txIn []*TxIn, txOut []*TxOut, subnetworkID *externa
|
||||
|
||||
var payloadHash externalapi.DomainHash
|
||||
if *subnetworkID != subnetworks.SubnetworkIDNative {
|
||||
payloadHash = *hashes.HashData(payload)
|
||||
payloadHash = *hashes.PayloadHash(payload)
|
||||
}
|
||||
|
||||
return &MsgTx{
|
||||
|
@ -131,11 +131,15 @@ func TestTx(t *testing.T) {
|
||||
|
||||
// TestTxHash tests the ability to generate the hash of a transaction accurately.
|
||||
func TestTxHashAndID(t *testing.T) {
|
||||
txID1Str := "a3d29c39bfb578235e4813cc8138a9ba10def63acad193a7a880159624840d7f"
|
||||
txHash1Str := "c2ac1e792c5c49260103ad9f86caf749d431958b7c7e5e5129346ceab8b709cf"
|
||||
txID1Str := "47ce12a5ee5727cf97c0481eebedad0d80646b743305b0921a2403f1836f8b37"
|
||||
wantTxID1, err := transactionid.FromString(txID1Str)
|
||||
if err != nil {
|
||||
t.Errorf("NewTxIDFromStr: %v", err)
|
||||
return
|
||||
t.Fatalf("NewTxIDFromStr: %v", err)
|
||||
}
|
||||
wantTxHash1, err := transactionid.FromString(txHash1Str)
|
||||
if err != nil {
|
||||
t.Fatalf("NewTxIDFromStr: %v", err)
|
||||
}
|
||||
|
||||
// A coinbase transaction
|
||||
@ -167,7 +171,7 @@ func TestTxHashAndID(t *testing.T) {
|
||||
|
||||
// Ensure the hash produced is expected.
|
||||
tx1Hash := tx1.TxHash()
|
||||
if *tx1Hash != (externalapi.DomainHash)(*wantTxID1) {
|
||||
if *tx1Hash != (externalapi.DomainHash)(*wantTxHash1) {
|
||||
t.Errorf("TxHash: wrong hash - got %v, want %v",
|
||||
spew.Sprint(tx1Hash), spew.Sprint(wantTxID1))
|
||||
}
|
||||
@ -179,14 +183,14 @@ func TestTxHashAndID(t *testing.T) {
|
||||
spew.Sprint(tx1ID), spew.Sprint(wantTxID1))
|
||||
}
|
||||
|
||||
hash2Str := "c84f3009b337aaa3adeb2ffd41010d5f62dd773ca25b39c908a77da91f87b729"
|
||||
hash2Str := "6b769655a1420022e4690a4f7bb9b1c381185ebbefe3070351f06fb573a0600c"
|
||||
wantHash2, err := hashes.FromString(hash2Str)
|
||||
if err != nil {
|
||||
t.Errorf("NewTxIDFromStr: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
id2Str := "7c919f676109743a1271a88beeb43849a6f9cc653f6082e59a7266f3df4802b9"
|
||||
id2Str := "af916032e271adaaa21f02bee4b44db2cca4dad9149dcaebc188009c7313ec68"
|
||||
wantID2, err := transactionid.FromString(id2Str)
|
||||
if err != nil {
|
||||
t.Errorf("NewTxIDFromStr: %v", err)
|
||||
@ -249,7 +253,7 @@ func TestTxHashAndID(t *testing.T) {
|
||||
|
||||
tx2.TxIn[0].SignatureScript = []byte{}
|
||||
newTx2Hash := tx2.TxHash()
|
||||
if *tx2ID != (externalapi.DomainTransactionID)(*newTx2Hash) {
|
||||
t.Errorf("tx2ID and newTx2Hash should be the same for transaction with an empty signature")
|
||||
if *tx2ID == (externalapi.DomainTransactionID)(*newTx2Hash) {
|
||||
t.Errorf("tx2ID and newTx2Hash should not be the same even for transaction with an empty signature")
|
||||
}
|
||||
}
|
||||
|
@ -13,7 +13,6 @@ import (
|
||||
|
||||
func TestConsensus_GetBlockInfo(t *testing.T) {
|
||||
testutils.ForAllNets(t, true, func(t *testing.T, params *dagconfig.Params) {
|
||||
|
||||
factory := NewFactory()
|
||||
consensus, teardown, err := factory.NewTestConsensus(params, "TestConsensus_GetBlockInfo")
|
||||
if err != nil {
|
||||
|
@ -35,23 +35,17 @@ func calcPowValue(header *externalapi.DomainBlockHeader) *big.Int {
|
||||
header.TimeInMilliseconds, header.Nonce = timestamp, nonce
|
||||
|
||||
// PRE_POW_HASH || TIME || 32 zero byte padding || NONCE
|
||||
writer := hashes.NewHashWriter()
|
||||
_, err := writer.Write(prePowHash[:])
|
||||
writer := hashes.NewPoWHashWriter()
|
||||
writer.InfallibleWrite(prePowHash[:])
|
||||
err := serialization.WriteElement(writer, timestamp)
|
||||
if err != nil {
|
||||
panic(errors.Wrap(err, "this should never happen. SHA256's digest should never return an error"))
|
||||
}
|
||||
err = serialization.WriteElement(writer, timestamp)
|
||||
if err != nil {
|
||||
panic(errors.Wrap(err, "this should never happen. SHA256's digest should never return an error"))
|
||||
panic(errors.Wrap(err, "this should never happen. Hash digest should never return an error"))
|
||||
}
|
||||
zeroes := [32]byte{}
|
||||
_, err = writer.Write(zeroes[:])
|
||||
if err != nil {
|
||||
panic(errors.Wrap(err, "this should never happen. SHA256's digest should never return an error"))
|
||||
}
|
||||
writer.InfallibleWrite(zeroes[:])
|
||||
err = serialization.WriteElement(writer, nonce)
|
||||
if err != nil {
|
||||
panic(errors.Wrap(err, "this should never happen. SHA256's digest should never return an error"))
|
||||
panic(errors.Wrap(err, "this should never happen. Hash digest should never return an error"))
|
||||
}
|
||||
return hashes.ToBig(writer.Finalize())
|
||||
}
|
||||
|
@ -136,10 +136,10 @@ var unOrderedParentsBlock = externalapi.DomainBlock{
|
||||
},
|
||||
},
|
||||
HashMerkleRoot: externalapi.DomainHash{
|
||||
0xa2, 0x60, 0x5a, 0x45, 0xfe, 0x01, 0x41, 0xc9,
|
||||
0xc2, 0x8d, 0xe2, 0xc3, 0x2d, 0x00, 0xa4, 0x29,
|
||||
0xd4, 0x01, 0x57, 0x2d, 0x2f, 0xcd, 0x49, 0xd4,
|
||||
0xff, 0x6f, 0xab, 0xd2, 0xd1, 0x96, 0x38, 0xb9,
|
||||
0xf8, 0x55, 0x7b, 0xd0, 0xda, 0xf2, 0x06, 0x8b,
|
||||
0x3b, 0xb1, 0x93, 0x5a, 0x2c, 0x52, 0x43, 0xf0,
|
||||
0x02, 0xf2, 0xb1, 0x40, 0x81, 0x2c, 0x0c, 0x15,
|
||||
0x8d, 0x04, 0x3d, 0xe2, 0x23, 0x54, 0x98, 0x88,
|
||||
},
|
||||
AcceptedIDMerkleRoot: externalapi.DomainHash{
|
||||
0x80, 0xf7, 0x00, 0xe3, 0x16, 0x3d, 0x04, 0x95,
|
||||
@ -186,10 +186,10 @@ var unOrderedParentsBlock = externalapi.DomainBlock{
|
||||
SubnetworkID: subnetworks.SubnetworkIDCoinbase,
|
||||
Payload: []byte{9, 0, 0, 0, 0, 0, 0, 0, 0},
|
||||
PayloadHash: externalapi.DomainHash{
|
||||
0x95, 0x7b, 0xde, 0x03, 0xa6, 0x26, 0x1f, 0xf0,
|
||||
0x95, 0x5d, 0x2c, 0x92, 0x07, 0x4b, 0x5c, 0xdc,
|
||||
0xd5, 0xbb, 0x9f, 0x7d, 0x8f, 0xeb, 0x61, 0x16,
|
||||
0xe3, 0xe5, 0x77, 0x16, 0x5e, 0x98, 0x82, 0xa7,
|
||||
0x31, 0x3d, 0xd5, 0x20, 0x4c, 0xc9, 0x89, 0x20,
|
||||
0x46, 0x22, 0x59, 0xe0, 0x0d, 0x33, 0x27, 0xe6,
|
||||
0x04, 0x20, 0x5f, 0x4e, 0xd5, 0xf4, 0xf9, 0x2f,
|
||||
0x1a, 0xf0, 0x13, 0x0b, 0xe3, 0x92, 0xd8, 0xff,
|
||||
},
|
||||
},
|
||||
{
|
||||
@ -410,10 +410,10 @@ var exampleValidBlock = externalapi.DomainBlock{
|
||||
},
|
||||
},
|
||||
HashMerkleRoot: externalapi.DomainHash{
|
||||
0xce, 0xea, 0x78, 0x53, 0x7e, 0x89, 0x67, 0xaf,
|
||||
0xdc, 0x4a, 0xd1, 0x67, 0xb0, 0xc4, 0xfc, 0x6e,
|
||||
0xe5, 0x4b, 0x87, 0xb0, 0x55, 0x8f, 0xf4, 0x6b,
|
||||
0x05, 0x4d, 0x43, 0x0a, 0xb6, 0xbb, 0xe8, 0xdf,
|
||||
0x33, 0x70, 0xa7, 0x40, 0x9f, 0x2d, 0x87, 0xe1,
|
||||
0x26, 0xaf, 0x0f, 0x5c, 0x7e, 0xc3, 0x84, 0x5e,
|
||||
0x4f, 0x68, 0x42, 0x0a, 0xbf, 0x90, 0xcd, 0xef,
|
||||
0x94, 0x9b, 0xe1, 0x9a, 0xf7, 0xdd, 0xb0, 0xb5,
|
||||
},
|
||||
AcceptedIDMerkleRoot: externalapi.DomainHash{
|
||||
0x8a, 0xb7, 0xd6, 0x73, 0x1b, 0xe6, 0xc5, 0xd3,
|
||||
@ -458,10 +458,10 @@ var exampleValidBlock = externalapi.DomainBlock{
|
||||
SubnetworkID: subnetworks.SubnetworkIDCoinbase,
|
||||
Payload: []byte{9, 0, 0, 0, 0, 0, 0, 0, 0},
|
||||
PayloadHash: externalapi.DomainHash{
|
||||
0x95, 0x7b, 0xde, 0x03, 0xa6, 0x26, 0x1f, 0xf0,
|
||||
0x95, 0x5d, 0x2c, 0x92, 0x07, 0x4b, 0x5c, 0xdc,
|
||||
0xd5, 0xbb, 0x9f, 0x7d, 0x8f, 0xeb, 0x61, 0x16,
|
||||
0xe3, 0xe5, 0x77, 0x16, 0x5e, 0x98, 0x82, 0xa7,
|
||||
0x31, 0x3d, 0xd5, 0x20, 0x4c, 0xc9, 0x89, 0x20,
|
||||
0x46, 0x22, 0x59, 0xe0, 0x0d, 0x33, 0x27, 0xe6,
|
||||
0x04, 0x20, 0x5f, 0x4e, 0xd5, 0xf4, 0xf9, 0x2f,
|
||||
0x1a, 0xf0, 0x13, 0x0b, 0xe3, 0x92, 0xd8, 0xff,
|
||||
},
|
||||
},
|
||||
{
|
||||
@ -765,10 +765,10 @@ var blockWithWrongTxOrder = externalapi.DomainBlock{
|
||||
SubnetworkID: subnetworks.SubnetworkIDCoinbase,
|
||||
Payload: []byte{9, 0, 0, 0, 0, 0, 0, 0, 0},
|
||||
PayloadHash: externalapi.DomainHash{
|
||||
0x95, 0x7b, 0xde, 0x03, 0xa6, 0x26, 0x1f, 0xf0,
|
||||
0x95, 0x5d, 0x2c, 0x92, 0x07, 0x4b, 0x5c, 0xdc,
|
||||
0xd5, 0xbb, 0x9f, 0x7d, 0x8f, 0xeb, 0x61, 0x16,
|
||||
0xe3, 0xe5, 0x77, 0x16, 0x5e, 0x98, 0x82, 0xa7,
|
||||
0x31, 0x3d, 0xd5, 0x20, 0x4c, 0xc9, 0x89, 0x20,
|
||||
0x46, 0x22, 0x59, 0xe0, 0x0d, 0x33, 0x27, 0xe6,
|
||||
0x04, 0x20, 0x5f, 0x4e, 0xd5, 0xf4, 0xf9, 0x2f,
|
||||
0x1a, 0xf0, 0x13, 0x0b, 0xe3, 0x92, 0xd8, 0xff,
|
||||
},
|
||||
},
|
||||
{
|
||||
|
@ -48,7 +48,7 @@ func (c *coinbaseManager) ExpectedCoinbaseTransaction(blockHash *externalapi.Dom
|
||||
return nil, err
|
||||
}
|
||||
|
||||
payloadHash := hashes.HashData(payload)
|
||||
payloadHash := hashes.PayloadHash(payload)
|
||||
|
||||
return &externalapi.DomainTransaction{
|
||||
Version: constants.TransactionVersion,
|
||||
|
@ -129,37 +129,37 @@ func TestBlueBlockWindow(t *testing.T) {
|
||||
{
|
||||
parents: []string{"H", "F"},
|
||||
id: "I",
|
||||
expectedWindowWithGenesisPadding: []string{"F", "D", "C", "H", "G", "B", "A", "A", "A", "A"},
|
||||
expectedWindowWithGenesisPadding: []string{"F", "D", "H", "C", "B", "G", "A", "A", "A", "A"},
|
||||
},
|
||||
{
|
||||
parents: []string{"I"},
|
||||
id: "J",
|
||||
expectedWindowWithGenesisPadding: []string{"I", "F", "D", "C", "H", "G", "B", "A", "A", "A"},
|
||||
expectedWindowWithGenesisPadding: []string{"I", "F", "D", "H", "C", "B", "G", "A", "A", "A"},
|
||||
},
|
||||
{
|
||||
parents: []string{"J"},
|
||||
id: "K",
|
||||
expectedWindowWithGenesisPadding: []string{"J", "I", "F", "D", "C", "H", "G", "B", "A", "A"},
|
||||
expectedWindowWithGenesisPadding: []string{"J", "I", "F", "D", "H", "C", "B", "G", "A", "A"},
|
||||
},
|
||||
{
|
||||
parents: []string{"K"},
|
||||
id: "L",
|
||||
expectedWindowWithGenesisPadding: []string{"K", "J", "I", "F", "D", "C", "H", "G", "B", "A"},
|
||||
expectedWindowWithGenesisPadding: []string{"K", "J", "I", "F", "D", "H", "C", "B", "G", "A"},
|
||||
},
|
||||
{
|
||||
parents: []string{"L"},
|
||||
id: "M",
|
||||
expectedWindowWithGenesisPadding: []string{"L", "K", "J", "I", "F", "D", "C", "H", "G", "B"},
|
||||
expectedWindowWithGenesisPadding: []string{"L", "K", "J", "I", "F", "D", "H", "C", "B", "G"},
|
||||
},
|
||||
{
|
||||
parents: []string{"M"},
|
||||
id: "N",
|
||||
expectedWindowWithGenesisPadding: []string{"M", "L", "K", "J", "I", "F", "D", "C", "H", "G"},
|
||||
expectedWindowWithGenesisPadding: []string{"M", "L", "K", "J", "I", "F", "D", "H", "C", "B"},
|
||||
},
|
||||
{
|
||||
parents: []string{"N"},
|
||||
id: "O",
|
||||
expectedWindowWithGenesisPadding: []string{"N", "M", "L", "K", "J", "I", "F", "D", "C", "H"},
|
||||
expectedWindowWithGenesisPadding: []string{"N", "M", "L", "K", "J", "I", "F", "D", "H", "C"},
|
||||
},
|
||||
},
|
||||
"kaspa-devnet": {
|
||||
@ -181,12 +181,12 @@ func TestBlueBlockWindow(t *testing.T) {
|
||||
{
|
||||
parents: []string{"C", "D"},
|
||||
id: "E",
|
||||
expectedWindowWithGenesisPadding: []string{"D", "C", "B", "A", "A", "A", "A", "A", "A", "A"},
|
||||
expectedWindowWithGenesisPadding: []string{"C", "D", "B", "A", "A", "A", "A", "A", "A", "A"},
|
||||
},
|
||||
{
|
||||
parents: []string{"C", "D"},
|
||||
id: "F",
|
||||
expectedWindowWithGenesisPadding: []string{"D", "C", "B", "A", "A", "A", "A", "A", "A", "A"},
|
||||
expectedWindowWithGenesisPadding: []string{"C", "D", "B", "A", "A", "A", "A", "A", "A", "A"},
|
||||
},
|
||||
{
|
||||
parents: []string{"A"},
|
||||
@ -201,37 +201,37 @@ func TestBlueBlockWindow(t *testing.T) {
|
||||
{
|
||||
parents: []string{"H", "F"},
|
||||
id: "I",
|
||||
expectedWindowWithGenesisPadding: []string{"F", "D", "H", "C", "G", "B", "A", "A", "A", "A"},
|
||||
expectedWindowWithGenesisPadding: []string{"F", "C", "D", "H", "G", "B", "A", "A", "A", "A"},
|
||||
},
|
||||
{
|
||||
parents: []string{"I"},
|
||||
id: "J",
|
||||
expectedWindowWithGenesisPadding: []string{"I", "F", "D", "H", "C", "G", "B", "A", "A", "A"},
|
||||
expectedWindowWithGenesisPadding: []string{"I", "F", "C", "D", "H", "G", "B", "A", "A", "A"},
|
||||
},
|
||||
{
|
||||
parents: []string{"J"},
|
||||
id: "K",
|
||||
expectedWindowWithGenesisPadding: []string{"J", "I", "F", "D", "H", "C", "G", "B", "A", "A"},
|
||||
expectedWindowWithGenesisPadding: []string{"J", "I", "F", "C", "D", "H", "G", "B", "A", "A"},
|
||||
},
|
||||
{
|
||||
parents: []string{"K"},
|
||||
id: "L",
|
||||
expectedWindowWithGenesisPadding: []string{"K", "J", "I", "F", "D", "H", "C", "G", "B", "A"},
|
||||
expectedWindowWithGenesisPadding: []string{"K", "J", "I", "F", "C", "D", "H", "G", "B", "A"},
|
||||
},
|
||||
{
|
||||
parents: []string{"L"},
|
||||
id: "M",
|
||||
expectedWindowWithGenesisPadding: []string{"L", "K", "J", "I", "F", "D", "H", "C", "G", "B"},
|
||||
expectedWindowWithGenesisPadding: []string{"L", "K", "J", "I", "F", "C", "D", "H", "G", "B"},
|
||||
},
|
||||
{
|
||||
parents: []string{"M"},
|
||||
id: "N",
|
||||
expectedWindowWithGenesisPadding: []string{"M", "L", "K", "J", "I", "F", "D", "H", "C", "G"},
|
||||
expectedWindowWithGenesisPadding: []string{"M", "L", "K", "J", "I", "F", "C", "D", "H", "G"},
|
||||
},
|
||||
{
|
||||
parents: []string{"N"},
|
||||
id: "O",
|
||||
expectedWindowWithGenesisPadding: []string{"N", "M", "L", "K", "J", "I", "F", "D", "H", "C"},
|
||||
expectedWindowWithGenesisPadding: []string{"N", "M", "L", "K", "J", "I", "F", "C", "D", "H"},
|
||||
},
|
||||
},
|
||||
"kaspa-simnet": {
|
||||
@ -251,14 +251,14 @@ func TestBlueBlockWindow(t *testing.T) {
|
||||
expectedWindowWithGenesisPadding: []string{"B", "A", "A", "A", "A", "A", "A", "A", "A", "A"},
|
||||
},
|
||||
{
|
||||
parents: []string{"C", "D"},
|
||||
parents: []string{"D", "C"},
|
||||
id: "E",
|
||||
expectedWindowWithGenesisPadding: []string{"C", "D", "B", "A", "A", "A", "A", "A", "A", "A"},
|
||||
expectedWindowWithGenesisPadding: []string{"D", "C", "B", "A", "A", "A", "A", "A", "A", "A"},
|
||||
},
|
||||
{
|
||||
parents: []string{"C", "D"},
|
||||
parents: []string{"D", "C"},
|
||||
id: "F",
|
||||
expectedWindowWithGenesisPadding: []string{"C", "D", "B", "A", "A", "A", "A", "A", "A", "A"},
|
||||
expectedWindowWithGenesisPadding: []string{"D", "C", "B", "A", "A", "A", "A", "A", "A", "A"},
|
||||
},
|
||||
{
|
||||
parents: []string{"A"},
|
||||
@ -273,37 +273,37 @@ func TestBlueBlockWindow(t *testing.T) {
|
||||
{
|
||||
parents: []string{"H", "F"},
|
||||
id: "I",
|
||||
expectedWindowWithGenesisPadding: []string{"F", "C", "D", "H", "B", "G", "A", "A", "A", "A"},
|
||||
expectedWindowWithGenesisPadding: []string{"F", "D", "C", "H", "B", "G", "A", "A", "A", "A"},
|
||||
},
|
||||
{
|
||||
parents: []string{"I"},
|
||||
id: "J",
|
||||
expectedWindowWithGenesisPadding: []string{"I", "F", "C", "D", "H", "B", "G", "A", "A", "A"},
|
||||
expectedWindowWithGenesisPadding: []string{"I", "F", "D", "C", "H", "B", "G", "A", "A", "A"},
|
||||
},
|
||||
{
|
||||
parents: []string{"J"},
|
||||
id: "K",
|
||||
expectedWindowWithGenesisPadding: []string{"J", "I", "F", "C", "D", "H", "B", "G", "A", "A"},
|
||||
expectedWindowWithGenesisPadding: []string{"J", "I", "F", "D", "C", "H", "B", "G", "A", "A"},
|
||||
},
|
||||
{
|
||||
parents: []string{"K"},
|
||||
id: "L",
|
||||
expectedWindowWithGenesisPadding: []string{"K", "J", "I", "F", "C", "D", "H", "B", "G", "A"},
|
||||
expectedWindowWithGenesisPadding: []string{"K", "J", "I", "F", "D", "C", "H", "B", "G", "A"},
|
||||
},
|
||||
{
|
||||
parents: []string{"L"},
|
||||
id: "M",
|
||||
expectedWindowWithGenesisPadding: []string{"L", "K", "J", "I", "F", "C", "D", "H", "B", "G"},
|
||||
expectedWindowWithGenesisPadding: []string{"L", "K", "J", "I", "F", "D", "C", "H", "B", "G"},
|
||||
},
|
||||
{
|
||||
parents: []string{"M"},
|
||||
id: "N",
|
||||
expectedWindowWithGenesisPadding: []string{"M", "L", "K", "J", "I", "F", "C", "D", "H", "B"},
|
||||
expectedWindowWithGenesisPadding: []string{"M", "L", "K", "J", "I", "F", "D", "C", "H", "B"},
|
||||
},
|
||||
{
|
||||
parents: []string{"N"},
|
||||
id: "O",
|
||||
expectedWindowWithGenesisPadding: []string{"N", "M", "L", "K", "J", "I", "F", "C", "D", "H"},
|
||||
expectedWindowWithGenesisPadding: []string{"N", "M", "L", "K", "J", "I", "F", "D", "C", "H"},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
@ -34,8 +34,8 @@ func TestPruning(t *testing.T) {
|
||||
"dag-for-test-pruning.json": {
|
||||
"kaspa-mainnet": "503",
|
||||
"kaspa-simnet": "502",
|
||||
"kaspa-devnet": "502",
|
||||
"kaspa-testnet": "502",
|
||||
"kaspa-devnet": "503",
|
||||
"kaspa-testnet": "503",
|
||||
},
|
||||
}
|
||||
|
||||
|
@ -127,7 +127,7 @@ func (v *transactionValidator) checkCoinbaseLength(tx *externalapi.DomainTransac
|
||||
|
||||
func (v *transactionValidator) checkTransactionPayloadHash(tx *externalapi.DomainTransaction) error {
|
||||
if tx.SubnetworkID != subnetworks.SubnetworkIDNative {
|
||||
payloadHash := hashes.HashData(tx.Payload)
|
||||
payloadHash := hashes.PayloadHash(tx.Payload)
|
||||
if tx.PayloadHash != *payloadHash {
|
||||
return errors.Wrapf(ruleerrors.ErrInvalidPayloadHash, "invalid payload hash")
|
||||
}
|
||||
|
@ -1,13 +1,13 @@
|
||||
package transactionvalidator_test
|
||||
|
||||
import (
|
||||
"github.com/kaspanet/kaspad/domain/consensus/utils/hashes"
|
||||
"testing"
|
||||
|
||||
"github.com/kaspanet/kaspad/domain/consensus"
|
||||
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
|
||||
"github.com/kaspanet/kaspad/domain/consensus/ruleerrors"
|
||||
"github.com/kaspanet/kaspad/domain/consensus/utils/constants"
|
||||
"github.com/kaspanet/kaspad/domain/consensus/utils/hashes"
|
||||
"github.com/kaspanet/kaspad/domain/consensus/utils/subnetworks"
|
||||
"github.com/kaspanet/kaspad/domain/consensus/utils/testutils"
|
||||
"github.com/kaspanet/kaspad/domain/consensus/utils/transactionhelper"
|
||||
@ -113,7 +113,7 @@ func TestValidateTransactionInIsolation(t *testing.T) {
|
||||
subnetworks.SubnetworkIDNative,
|
||||
nil,
|
||||
func(tx *externalapi.DomainTransaction) {
|
||||
tx.PayloadHash = *hashes.HashData(tx.Payload)
|
||||
tx.PayloadHash = *hashes.PayloadHash(tx.Payload)
|
||||
},
|
||||
ruleerrors.ErrInvalidPayloadHash},
|
||||
}
|
||||
|
@ -41,7 +41,7 @@ func TestNewErrMissingTxOut(t *testing.T) {
|
||||
func TestNewErrInvalidTransactionsInNewBlock(t *testing.T) {
|
||||
outer := NewErrInvalidTransactionsInNewBlock([]InvalidTransaction{{&externalapi.DomainTransaction{Fee: 1337}, ErrNoTxInputs}})
|
||||
//TODO: Implement Stringer for `DomainTransaction`
|
||||
expectedOuterErr := "ErrInvalidTransactionsInNewBlock: [(3a464e1e43410c7add1dd81c3f10486f41eb473bb43e8d64feca3c7f0c8028d3: ErrNoTxInputs)]"
|
||||
expectedOuterErr := "ErrInvalidTransactionsInNewBlock: [(4ea5363088df94b7b52afa9df9db7b44561e0a2219f2bf85b2f1d699cade933e: ErrNoTxInputs)]"
|
||||
inner := &ErrInvalidTransactionsInNewBlock{}
|
||||
if !errors.As(outer, inner) {
|
||||
t.Fatal("TestNewErrInvalidTransactionsInNewBlock: Outer should contain ErrInvalidTransactionsInNewBlock in it")
|
||||
|
@ -17,15 +17,15 @@ func BlockHash(block *externalapi.DomainBlock) *externalapi.DomainHash {
|
||||
|
||||
// HeaderHash returns the given header's hash
|
||||
func HeaderHash(header *externalapi.DomainBlockHeader) *externalapi.DomainHash {
|
||||
// Encode the header and double sha256 everything prior to the number of
|
||||
// Encode the header and hash everything prior to the number of
|
||||
// transactions.
|
||||
writer := hashes.NewHashWriter()
|
||||
writer := hashes.NewBlockHashWriter()
|
||||
err := serializeHeader(writer, header)
|
||||
if err != nil {
|
||||
// It seems like this could only happen if the writer returned an error.
|
||||
// and this writer should never return an error (no allocations or possible failures)
|
||||
// the only non-writer error path here is unknown types in `WriteElement`
|
||||
panic(errors.Wrap(err, "this should never happen. SHA256's digest should never return an error"))
|
||||
panic(errors.Wrap(err, "this should never happen. Hash digest should never return an error"))
|
||||
}
|
||||
|
||||
return writer.Finalize()
|
||||
|
@ -30,9 +30,9 @@ const (
|
||||
// TransactionHashForSigning hashes the transaction and the given hash type in a way that is intended for
|
||||
// signatures.
|
||||
func TransactionHashForSigning(tx *externalapi.DomainTransaction, hashType uint32) *externalapi.DomainHash {
|
||||
// Encode the header and double sha256 everything prior to the number of
|
||||
// Encode the header and hash everything prior to the number of
|
||||
// transactions.
|
||||
writer := hashes.NewHashWriter()
|
||||
writer := hashes.NewTransactionSigningHashWriter()
|
||||
err := serializeTransaction(writer, tx, txEncodingExcludePayload)
|
||||
if err != nil {
|
||||
// It seems like this could only happen if the writer returned an error.
|
||||
@ -43,7 +43,7 @@ func TransactionHashForSigning(tx *externalapi.DomainTransaction, hashType uint3
|
||||
|
||||
err = serialization.WriteElement(writer, hashType)
|
||||
if err != nil {
|
||||
panic(errors.Wrap(err, "this should never happen. SHA256's digest should never return an error"))
|
||||
panic(errors.Wrap(err, "this should never happen. Hash digest should never return an error"))
|
||||
}
|
||||
|
||||
return writer.Finalize()
|
||||
@ -51,9 +51,9 @@ func TransactionHashForSigning(tx *externalapi.DomainTransaction, hashType uint3
|
||||
|
||||
// TransactionHash returns the transaction hash.
|
||||
func TransactionHash(tx *externalapi.DomainTransaction) *externalapi.DomainHash {
|
||||
// Encode the header and double sha256 everything prior to the number of
|
||||
// Encode the header and hash everything prior to the number of
|
||||
// transactions.
|
||||
writer := hashes.NewHashWriter()
|
||||
writer := hashes.NewTransactionHashWriter()
|
||||
err := serializeTransaction(writer, tx, txEncodingExcludePayload)
|
||||
if err != nil {
|
||||
// It seems like this could only happen if the writer returned an error.
|
||||
@ -73,12 +73,12 @@ func TransactionID(tx *externalapi.DomainTransaction) *externalapi.DomainTransac
|
||||
}
|
||||
|
||||
// Encode the transaction, replace signature script with zeroes, cut off
|
||||
// payload and calculate double sha256 on the result.
|
||||
// payload and hash the result.
|
||||
var encodingFlags txEncoding
|
||||
if !transactionhelper.IsCoinBase(tx) {
|
||||
encodingFlags = txEncodingExcludeSignatureScript | txEncodingExcludePayload
|
||||
}
|
||||
writer := hashes.NewHashWriter()
|
||||
writer := hashes.NewTransactionIDWriter()
|
||||
err := serializeTransaction(writer, tx, encodingFlags)
|
||||
if err != nil {
|
||||
// this writer never return errors (no allocations or possible failures) so errors can only come from validity checks,
|
||||
|
79
domain/consensus/utils/hashes/domains.go
Normal file
79
domain/consensus/utils/hashes/domains.go
Normal file
@ -0,0 +1,79 @@
|
||||
package hashes
|
||||
|
||||
import (
|
||||
"github.com/pkg/errors"
|
||||
"golang.org/x/crypto/blake2b"
|
||||
)
|
||||
|
||||
const (
|
||||
transcationHashDomain = "TransactionHash"
|
||||
transcationIDDomain = "TransactionID"
|
||||
transcationSigningDomain = "TransactionSigningHash"
|
||||
payloadDomain = "PayloadHash"
|
||||
blockDomain = "BlockHash"
|
||||
proofOfWorkDomain = "ProofOfWorkHash"
|
||||
merkleBranchDomain = "MerkleBranchHash"
|
||||
)
|
||||
|
||||
// NewTransactionHashWriter Returns a new HashWriter used for transaction hashes
|
||||
func NewTransactionHashWriter() HashWriter {
|
||||
blake, err := blake2b.New256([]byte(transcationHashDomain))
|
||||
if err != nil {
|
||||
panic(errors.Wrapf(err, "this should never happen. %s is less than 64 bytes", transcationHashDomain))
|
||||
}
|
||||
return HashWriter{blake}
|
||||
}
|
||||
|
||||
// NewTransactionIDWriter Returns a new HashWriter used for transaction IDs
|
||||
func NewTransactionIDWriter() HashWriter {
|
||||
blake, err := blake2b.New256([]byte(transcationIDDomain))
|
||||
if err != nil {
|
||||
panic(errors.Wrapf(err, "this should never happen. %s is less than 64 bytes", transcationIDDomain))
|
||||
}
|
||||
return HashWriter{blake}
|
||||
}
|
||||
|
||||
// NewTransactionSigningHashWriter Returns a new HashWriter used for signing on a transaction
|
||||
func NewTransactionSigningHashWriter() HashWriter {
|
||||
blake, err := blake2b.New256([]byte(transcationSigningDomain))
|
||||
if err != nil {
|
||||
panic(errors.Wrapf(err, "this should never happen. %s is less than 64 bytes", transcationSigningDomain))
|
||||
}
|
||||
return HashWriter{blake}
|
||||
}
|
||||
|
||||
// NewPayloadHashWriter Returns a new HashWriter used for hashing a transaction payload
|
||||
func NewPayloadHashWriter() HashWriter {
|
||||
blake, err := blake2b.New256([]byte(payloadDomain))
|
||||
if err != nil {
|
||||
panic(errors.Wrapf(err, "this should never happen. %s is less than 64 bytes", payloadDomain))
|
||||
}
|
||||
return HashWriter{blake}
|
||||
}
|
||||
|
||||
// NewBlockHashWriter Returns a new HashWriter used for hashing blocks
|
||||
func NewBlockHashWriter() HashWriter {
|
||||
blake, err := blake2b.New256([]byte(blockDomain))
|
||||
if err != nil {
|
||||
panic(errors.Wrapf(err, "this should never happen. %s is less than 64 bytes", blockDomain))
|
||||
}
|
||||
return HashWriter{blake}
|
||||
}
|
||||
|
||||
// NewPoWHashWriter Returns a new HashWriter used for the PoW function
|
||||
func NewPoWHashWriter() HashWriter {
|
||||
blake, err := blake2b.New256([]byte(proofOfWorkDomain))
|
||||
if err != nil {
|
||||
panic(errors.Wrapf(err, "this should never happen. %s is less than 64 bytes", proofOfWorkDomain))
|
||||
}
|
||||
return HashWriter{blake}
|
||||
}
|
||||
|
||||
// NewMerkleBranchHashWriter Returns a new HashWriter used for a merkle tree branch
|
||||
func NewMerkleBranchHashWriter() HashWriter {
|
||||
blake, err := blake2b.New256([]byte(merkleBranchDomain))
|
||||
if err != nil {
|
||||
panic(errors.Wrapf(err, "this should never happen. %s is less than 64 bytes", merkleBranchDomain))
|
||||
}
|
||||
return HashWriter{blake}
|
||||
}
|
10
domain/consensus/utils/hashes/helpers.go
Normal file
10
domain/consensus/utils/hashes/helpers.go
Normal file
@ -0,0 +1,10 @@
|
||||
package hashes
|
||||
|
||||
import "github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
|
||||
|
||||
// PayloadHash returns the payload hash.
|
||||
func PayloadHash(payload []byte) *externalapi.DomainHash {
|
||||
writer := NewPayloadHashWriter()
|
||||
writer.InfallibleWrite(payload)
|
||||
return writer.Finalize()
|
||||
}
|
@ -1,45 +1,32 @@
|
||||
package hashes
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"hash"
|
||||
|
||||
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
|
||||
"github.com/pkg/errors"
|
||||
"hash"
|
||||
)
|
||||
|
||||
// HashWriter is used to incrementally hash data without concatenating all of the data to a single buffer
|
||||
// it exposes an io.Writer api and a Finalize function to get the resulting hash.
|
||||
// The used hash function is double-sha256.
|
||||
// The used hash function is blake2b.
|
||||
// This can only be created via one of the domain separated constructors
|
||||
type HashWriter struct {
|
||||
inner hash.Hash
|
||||
hash.Hash
|
||||
}
|
||||
|
||||
// NewHashWriter Returns a new HashWriter
|
||||
func NewHashWriter() *HashWriter {
|
||||
return &HashWriter{sha256.New()}
|
||||
// InfallibleWrite is just like write but doesn't return anything
|
||||
func (h HashWriter) InfallibleWrite(p []byte) {
|
||||
// This write can never return an error, this is part of the hash.Hash interface contract.
|
||||
_, err := h.Write(p)
|
||||
if err != nil {
|
||||
panic(errors.Wrap(err, "this should never happen. hash.Hash interface promises to not return errors."))
|
||||
}
|
||||
}
|
||||
|
||||
// Write will always return (len(p), nil)
|
||||
func (h *HashWriter) Write(p []byte) (n int, err error) {
|
||||
return h.inner.Write(p)
|
||||
}
|
||||
|
||||
// Finalize returns the resulting double hash
|
||||
func (h *HashWriter) Finalize() *externalapi.DomainHash {
|
||||
firstHashInTheSum := h.inner.Sum(nil)
|
||||
sum := externalapi.DomainHash(sha256.Sum256(firstHashInTheSum))
|
||||
|
||||
// Finalize returns the resulting hash
|
||||
func (h HashWriter) Finalize() *externalapi.DomainHash {
|
||||
var sum externalapi.DomainHash
|
||||
// This should prevent `Sum` for allocating an output buffer, by using the DomainHash buffer. we still copy because we don't want to rely on that.
|
||||
copy(sum[:], h.Sum(sum[:0]))
|
||||
return &sum
|
||||
}
|
||||
|
||||
// HashData hashes the given byte slice
|
||||
func HashData(data []byte) *externalapi.DomainHash {
|
||||
w := NewHashWriter()
|
||||
_, err := w.Write(data)
|
||||
if err != nil {
|
||||
panic(errors.Wrap(err, "this should never happen. SHA256's digest should never return an error"))
|
||||
}
|
||||
|
||||
return w.Finalize()
|
||||
}
|
||||
|
@ -6,7 +6,6 @@ import (
|
||||
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
|
||||
"github.com/kaspanet/kaspad/domain/consensus/utils/consensushashing"
|
||||
"github.com/kaspanet/kaspad/domain/consensus/utils/hashes"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// nextPowerOfTwo returns the next highest power of two from a given number if
|
||||
@ -28,17 +27,10 @@ func nextPowerOfTwo(n int) int {
|
||||
// function used to aid in the generation of a merkle tree.
|
||||
func hashMerkleBranches(left, right *externalapi.DomainHash) *externalapi.DomainHash {
|
||||
// Concatenate the left and right nodes.
|
||||
w := hashes.NewHashWriter()
|
||||
w := hashes.NewMerkleBranchHashWriter()
|
||||
|
||||
_, err := w.Write(left[:])
|
||||
if err != nil {
|
||||
panic(errors.Wrap(err, "this should never happen. SHA256's digest should never return an error"))
|
||||
}
|
||||
|
||||
_, err = w.Write(right[:])
|
||||
if err != nil {
|
||||
panic(errors.Wrap(err, "this should never happen. SHA256's digest should never return an error"))
|
||||
}
|
||||
w.InfallibleWrite(left[:])
|
||||
w.InfallibleWrite(right[:])
|
||||
|
||||
return w.Finalize()
|
||||
}
|
||||
@ -90,12 +82,12 @@ func merkleRoot(hashes []*externalapi.DomainHash) *externalapi.DomainHash {
|
||||
merkles[offset] = nil
|
||||
|
||||
// When there is no right child, the parent is generated by
|
||||
// hashing the concatenation of the left child with itself.
|
||||
// hashing the concatenation of the left child with zeros.
|
||||
case merkles[i+1] == nil:
|
||||
newHash := hashMerkleBranches(merkles[i], merkles[i])
|
||||
newHash := hashMerkleBranches(merkles[i], &externalapi.DomainHash{})
|
||||
merkles[offset] = newHash
|
||||
|
||||
// The normal case sets the parent node to the double sha256
|
||||
// The normal case sets the parent node to the hash
|
||||
// of the concatentation of the left and right children.
|
||||
default:
|
||||
newHash := hashMerkleBranches(merkles[i], merkles[i+1])
|
||||
|
@ -11,7 +11,7 @@ func NewSubnetworkTransaction(version int32, inputs []*externalapi.DomainTransac
|
||||
outputs []*externalapi.DomainTransactionOutput, subnetworkID *externalapi.DomainSubnetworkID,
|
||||
gas uint64, payload []byte) *externalapi.DomainTransaction {
|
||||
|
||||
payloadHash := hashes.HashData(payload)
|
||||
payloadHash := hashes.PayloadHash(payload)
|
||||
return &externalapi.DomainTransaction{
|
||||
Version: version,
|
||||
Inputs: inputs,
|
||||
|
@ -1197,12 +1197,6 @@
|
||||
"",
|
||||
"OK"
|
||||
],
|
||||
[
|
||||
"''",
|
||||
"DUP HASH256 SWAP SHA256 SHA256 EQUAL",
|
||||
"",
|
||||
"OK"
|
||||
],
|
||||
[
|
||||
"''",
|
||||
"NOP HASH160 0x14 0xb472a266d0bd89c13706a4132ccfb16f7c3b9fcb EQUAL",
|
||||
@ -1224,19 +1218,19 @@
|
||||
],
|
||||
[
|
||||
"''",
|
||||
"HASH256 0x20 0x5df6e0e2761359d30a8275058e299fcc0381534545f55cf43e41983f5d4c9456 EQUAL",
|
||||
"BLAKE2B 0x20 0x0e5751c026e543b2e8ab2eb06099daa1d1e5df47778f7787faab45cdf12fe3a8 EQUAL",
|
||||
"",
|
||||
"OK"
|
||||
],
|
||||
[
|
||||
"'a'",
|
||||
"HASH256 0x20 0xbf5d3affb73efd2ec6c36ad3112dd933efed63c4e1cbffcfa88e2759c144f2d8 EQUAL",
|
||||
"BLAKE2B 0x20 0x8928aae63c84d87ea098564d1e03ad813f107add474e56aedd286349c0c03ea4 EQUAL",
|
||||
"",
|
||||
"OK"
|
||||
],
|
||||
[
|
||||
"'abcdefghijklmnopqrstuvwxyz'",
|
||||
"HASH256 0x20 0xca139bc10c2f660da42666f72e89a225936fc60f193c161124a672050c434671 EQUAL",
|
||||
"BLAKE2B 0x20 0x117ad6b940f5e8292c007d9c7e7350cd33cf85b5887e8da71c7957830f536e7c EQUAL",
|
||||
"",
|
||||
"OK"
|
||||
],
|
||||
@ -2225,7 +2219,7 @@
|
||||
],
|
||||
[
|
||||
"0",
|
||||
"HASH256",
|
||||
"BLAKE2B",
|
||||
"",
|
||||
"OK"
|
||||
],
|
||||
@ -4326,7 +4320,7 @@
|
||||
],
|
||||
[
|
||||
"",
|
||||
"HASH256",
|
||||
"BLAKE2B",
|
||||
"",
|
||||
"INVALID_STACK_OPERATION"
|
||||
],
|
||||
@ -4803,7 +4797,7 @@
|
||||
],
|
||||
[
|
||||
"",
|
||||
"HASH256 1",
|
||||
"BLAKE2B 1",
|
||||
"",
|
||||
"INVALID_STACK_OPERATION"
|
||||
],
|
||||
|
@ -10,13 +10,12 @@ import (
|
||||
"crypto/sha256"
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"golang.org/x/crypto/blake2b"
|
||||
"hash"
|
||||
|
||||
"github.com/kaspanet/kaspad/domain/consensus/utils/constants"
|
||||
|
||||
"github.com/kaspanet/go-secp256k1"
|
||||
"github.com/kaspanet/kaspad/domain/consensus/utils/hashes"
|
||||
|
||||
"golang.org/x/crypto/ripemd160"
|
||||
)
|
||||
|
||||
@ -205,7 +204,7 @@ const (
|
||||
OpSHA1 = 0xa7 // 167
|
||||
OpSHA256 = 0xa8 // 168
|
||||
OpHash160 = 0xa9 // 169
|
||||
OpHash256 = 0xaa // 170
|
||||
OpBlake2b = 0xaa // 170
|
||||
OpUnknown171 = 0xab // 171
|
||||
OpCheckSig = 0xac // 172
|
||||
OpCheckSigVerify = 0xad // 173
|
||||
@ -489,7 +488,7 @@ var opcodeArray = [256]opcode{
|
||||
OpSHA1: {OpSHA1, "OP_SHA1", 1, opcodeSha1},
|
||||
OpSHA256: {OpSHA256, "OP_SHA256", 1, opcodeSha256},
|
||||
OpHash160: {OpHash160, "OP_HASH160", 1, opcodeHash160},
|
||||
OpHash256: {OpHash256, "OP_HASH256", 1, opcodeHash256},
|
||||
OpBlake2b: {OpBlake2b, "OP_BLAKE2B", 1, opcodeBlake2b},
|
||||
OpCheckSig: {OpCheckSig, "OP_CHECKSIG", 1, opcodeCheckSig},
|
||||
OpCheckSigVerify: {OpCheckSigVerify, "OP_CHECKSIGVERIFY", 1, opcodeCheckSigVerify},
|
||||
OpCheckMultiSig: {OpCheckMultiSig, "OP_CHECKMULTISIG", 1, opcodeCheckMultiSig},
|
||||
@ -1962,17 +1961,17 @@ func opcodeHash160(op *parsedOpcode, vm *Engine) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// opcodeHash256 treats the top item of the data stack as raw bytes and replaces
|
||||
// it with sha256(sha256(data)).
|
||||
// opcodeBlake2b treats the top item of the data stack as raw bytes and replaces
|
||||
// it with blake2b(data).
|
||||
//
|
||||
// Stack transformation: [... x1] -> [... sha256(sha256(x1))]
|
||||
func opcodeHash256(op *parsedOpcode, vm *Engine) error {
|
||||
// Stack transformation: [... x1] -> [... blake2b(x1)]
|
||||
func opcodeBlake2b(op *parsedOpcode, vm *Engine) error {
|
||||
buf, err := vm.dstack.PopByteArray()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
vm.dstack.PushByteArray(hashes.HashData(buf)[:])
|
||||
hash := blake2b.Sum256(buf)
|
||||
vm.dstack.PushByteArray(hash[:])
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -72,7 +72,7 @@ func TestOpcodeDisasm(t *testing.T) {
|
||||
0xa1: "OP_LESSTHANOREQUAL", 0xa2: "OP_GREATERTHANOREQUAL",
|
||||
0xa3: "OP_MIN", 0xa4: "OP_MAX", 0xa5: "OP_WITHIN",
|
||||
0xa6: "OP_RIPEMD160", 0xa7: "OP_SHA1", 0xa8: "OP_SHA256",
|
||||
0xa9: "OP_HASH160", 0xaa: "OP_HASH256",
|
||||
0xa9: "OP_HASH160", 0xaa: "OP_BLAKE2B",
|
||||
0xac: "OP_CHECKSIG", 0xad: "OP_CHECKSIGVERIFY",
|
||||
0xae: "OP_CHECKMULTISIG", 0xaf: "OP_CHECKMULTISIGVERIFY",
|
||||
0xb0: "OP_CHECKLOCKTIMEVERIFY", 0xb1: "OP_CHECKSEQUENCEVERIFY",
|
||||
|
@ -358,7 +358,7 @@ func calcSignatureHash(script []parsedOpcode, hashType SigHashType, tx *external
|
||||
txCopy.Inputs = txCopy.Inputs[idx : idx+1]
|
||||
}
|
||||
|
||||
// The final hash is the double sha256 of both the serialized modified
|
||||
// The final hash is the hash of both the serialized modified
|
||||
// transaction and the hash type (encoded as a 4-byte little-endian
|
||||
// value) appended.
|
||||
return consensushashing.TransactionHashForSigning(&txCopy, uint32(hashType)), nil
|
||||
|
@ -3363,17 +3363,17 @@ func TestUnparsingInvalidOpcodes(t *testing.T) {
|
||||
expectedErr: scriptError(ErrInternal, ""),
|
||||
},
|
||||
{
|
||||
name: "OP_HASH256",
|
||||
name: "OP_BLAKE2B",
|
||||
pop: &parsedOpcode{
|
||||
opcode: &opcodeArray[OpHash256],
|
||||
opcode: &opcodeArray[OpBlake2b],
|
||||
data: nil,
|
||||
},
|
||||
expectedErr: nil,
|
||||
},
|
||||
{
|
||||
name: "OP_HASH256 long",
|
||||
name: "OP_BLAKE2B long",
|
||||
pop: &parsedOpcode{
|
||||
opcode: &opcodeArray[OpHash256],
|
||||
opcode: &opcodeArray[OpBlake2b],
|
||||
data: make([]byte, 1),
|
||||
},
|
||||
expectedErr: scriptError(ErrInternal, ""),
|
||||
|
@ -28,19 +28,19 @@ var genesisCoinbaseTx = transactionhelper.NewSubnetworkTransaction(1, []*externa
|
||||
// genesisHash is the hash of the first block in the block DAG for the main
|
||||
// network (genesis block).
|
||||
var genesisHash = externalapi.DomainHash{
|
||||
0xfe, 0x18, 0xeb, 0xc3, 0x20, 0xe2, 0xdf, 0x7b,
|
||||
0xf5, 0x62, 0x27, 0xe4, 0x8c, 0x32, 0xdd, 0x06,
|
||||
0x19, 0xe2, 0xf9, 0xb1, 0xef, 0xf4, 0x96, 0x38,
|
||||
0x6b, 0x1f, 0x11, 0xba, 0x0b, 0x9f, 0x92, 0xa8,
|
||||
0x8c, 0x74, 0x62, 0xc9, 0xb6, 0xa8, 0xb2, 0x7c,
|
||||
0x8d, 0x03, 0xa3, 0x7e, 0x45, 0x73, 0x31, 0x77,
|
||||
0xc7, 0xe1, 0x00, 0xa8, 0xc7, 0x75, 0xe9, 0xaa,
|
||||
0x31, 0x02, 0xa9, 0x82, 0x9f, 0xad, 0x34, 0xc8,
|
||||
}
|
||||
|
||||
// genesisMerkleRoot is the hash of the first transaction in the genesis block
|
||||
// for the main network.
|
||||
var genesisMerkleRoot = externalapi.DomainHash{
|
||||
0x2f, 0x55, 0x37, 0x11, 0x8a, 0x30, 0xcd, 0xcd,
|
||||
0xa7, 0xdb, 0xe5, 0xe6, 0x42, 0xf9, 0x1b, 0xf3,
|
||||
0xb4, 0x62, 0xd6, 0xb6, 0xed, 0xc0, 0x5c, 0xe1,
|
||||
0x6e, 0xee, 0x0f, 0x3c, 0xdc, 0xf6, 0x01, 0x15,
|
||||
0x32, 0xea, 0x93, 0x9a, 0x1f, 0x00, 0x50, 0xc3,
|
||||
0x97, 0x2c, 0x3d, 0xdf, 0x28, 0xb4, 0x8f, 0x1d,
|
||||
0x75, 0x9f, 0xb1, 0x82, 0x99, 0x79, 0x7a, 0x48,
|
||||
0xc9, 0xf6, 0x05, 0xc6, 0xae, 0x30, 0x49, 0xf7,
|
||||
}
|
||||
|
||||
// genesisBlock defines the genesis block of the block DAG which serves as the
|
||||
@ -54,7 +54,7 @@ var genesisBlock = externalapi.DomainBlock{
|
||||
UTXOCommitment: externalapi.DomainHash{},
|
||||
TimeInMilliseconds: 0x1763db5c4a9,
|
||||
Bits: 0x207fffff,
|
||||
Nonce: 0x0,
|
||||
Nonce: 0x1,
|
||||
},
|
||||
Transactions: []*externalapi.DomainTransaction{genesisCoinbaseTx},
|
||||
}
|
||||
@ -79,19 +79,19 @@ var devnetGenesisCoinbaseTx = transactionhelper.NewSubnetworkTransaction(1,
|
||||
// devGenesisHash is the hash of the first block in the block DAG for the development
|
||||
// network (genesis block).
|
||||
var devnetGenesisHash = externalapi.DomainHash{
|
||||
0x13, 0x2d, 0xfc, 0xf3, 0xcf, 0x03, 0xfb, 0x21,
|
||||
0x30, 0xb8, 0x67, 0x79, 0xbc, 0x2e, 0x18, 0x4e,
|
||||
0x08, 0xd7, 0xeb, 0xf7, 0xb6, 0x86, 0x3c, 0x3e,
|
||||
0xe0, 0x8c, 0x8d, 0x02, 0x3c, 0xfd, 0xfe, 0x66,
|
||||
0xee, 0xce, 0x68, 0x63, 0x61, 0xb4, 0xa8, 0x09,
|
||||
0x5d, 0xa3, 0x91, 0x6c, 0x12, 0x20, 0x27, 0xdd,
|
||||
0xf8, 0x16, 0x74, 0x8e, 0xd8, 0x7a, 0xfe, 0x2c,
|
||||
0xb7, 0x98, 0xe6, 0x9d, 0x47, 0x07, 0x02, 0xc5,
|
||||
}
|
||||
|
||||
// devnetGenesisMerkleRoot is the hash of the first transaction in the genesis block
|
||||
// for the devopment network.
|
||||
var devnetGenesisMerkleRoot = externalapi.DomainHash{
|
||||
0x00, 0x94, 0xfd, 0xff, 0x4d, 0xb2, 0x4d, 0x18,
|
||||
0x95, 0x21, 0x36, 0x2a, 0x14, 0xfb, 0x19, 0x7a,
|
||||
0x99, 0x51, 0x7e, 0x3f, 0x44, 0xf6, 0x2e, 0x0b,
|
||||
0xe7, 0xb3, 0xc0, 0xbb, 0x00, 0x3b, 0x0b, 0xbd,
|
||||
0xdf, 0x52, 0x65, 0x3a, 0x5a, 0xd4, 0x07, 0x4e,
|
||||
0xad, 0xac, 0xb3, 0xd7, 0xd6, 0x9a, 0xf5, 0xd3,
|
||||
0x68, 0x05, 0x4d, 0xef, 0xd9, 0x41, 0x28, 0x84,
|
||||
0xa9, 0x56, 0xdd, 0x68, 0x60, 0x1b, 0x8d, 0x2c,
|
||||
}
|
||||
|
||||
// devnetGenesisBlock defines the genesis block of the block DAG which serves as the
|
||||
@ -105,7 +105,7 @@ var devnetGenesisBlock = externalapi.DomainBlock{
|
||||
UTXOCommitment: externalapi.DomainHash{},
|
||||
TimeInMilliseconds: 0x1763db5c4a9,
|
||||
Bits: 0x1e7fffff,
|
||||
Nonce: 0x281ad,
|
||||
Nonce: 0xb6c8,
|
||||
},
|
||||
Transactions: []*externalapi.DomainTransaction{devnetGenesisCoinbaseTx},
|
||||
}
|
||||
@ -129,19 +129,19 @@ var simnetGenesisCoinbaseTx = transactionhelper.NewSubnetworkTransaction(1,
|
||||
// simnetGenesisHash is the hash of the first block in the block DAG for
|
||||
// the simnet (genesis block).
|
||||
var simnetGenesisHash = externalapi.DomainHash{
|
||||
0xda, 0xcc, 0x82, 0xd2, 0xf2, 0x53, 0x49, 0x48,
|
||||
0x18, 0x9e, 0x08, 0x8f, 0xd3, 0xe1, 0xbc, 0xce,
|
||||
0xb6, 0x0d, 0x48, 0xa6, 0x51, 0x53, 0xe1, 0xc1,
|
||||
0xa7, 0xa7, 0x10, 0x8a, 0xde, 0x96, 0xa3, 0x4d,
|
||||
0xe3, 0xa4, 0x4a, 0xe5, 0xdc, 0x3d, 0x39, 0x6a,
|
||||
0xc8, 0x5b, 0x1b, 0x95, 0x30, 0x05, 0x7d, 0xb9,
|
||||
0xd4, 0xfa, 0x30, 0x9a, 0x20, 0x7a, 0x42, 0x54,
|
||||
0xf8, 0x10, 0x73, 0xc0, 0x15, 0x31, 0xf5, 0x1a,
|
||||
}
|
||||
|
||||
// simnetGenesisMerkleRoot is the hash of the first transaction in the genesis block
|
||||
// for the devopment network.
|
||||
var simnetGenesisMerkleRoot = externalapi.DomainHash{
|
||||
0x79, 0x77, 0x9c, 0xad, 0x8d, 0x5a, 0x37, 0x57,
|
||||
0x75, 0x8b, 0x2f, 0xa5, 0x82, 0x47, 0x2f, 0xb6,
|
||||
0xbe, 0x24, 0x5f, 0xcb, 0x21, 0x68, 0x21, 0x44,
|
||||
0x45, 0x39, 0x44, 0xaf, 0xab, 0x9f, 0x0f, 0xc1,
|
||||
0x16, 0x07, 0x15, 0x0f, 0x1b, 0xc0, 0x26, 0x27,
|
||||
0x42, 0xc5, 0x84, 0x77, 0xdb, 0x58, 0xf7, 0x87,
|
||||
0xa8, 0xe9, 0x9f, 0x21, 0x73, 0xa0, 0x9d, 0x96,
|
||||
0x6a, 0x99, 0x55, 0x46, 0x7b, 0xb2, 0x1b, 0x99,
|
||||
}
|
||||
|
||||
// simnetGenesisBlock defines the genesis block of the block DAG which serves as the
|
||||
@ -153,7 +153,7 @@ var simnetGenesisBlock = externalapi.DomainBlock{
|
||||
HashMerkleRoot: simnetGenesisMerkleRoot,
|
||||
AcceptedIDMerkleRoot: externalapi.DomainHash{},
|
||||
UTXOCommitment: externalapi.DomainHash{},
|
||||
TimeInMilliseconds: 0x1763db5c5de,
|
||||
TimeInMilliseconds: 0x1763db5c4a9,
|
||||
Bits: 0x207fffff,
|
||||
Nonce: 0x0,
|
||||
},
|
||||
@ -177,19 +177,19 @@ var testnetGenesisCoinbaseTx = transactionhelper.NewSubnetworkTransaction(1,
|
||||
// testnetGenesisHash is the hash of the first block in the block DAG for the test
|
||||
// network (genesis block).
|
||||
var testnetGenesisHash = externalapi.DomainHash{
|
||||
0x97, 0xb5, 0xd5, 0xf6, 0x0d, 0xfe, 0x77, 0x83,
|
||||
0x87, 0xe8, 0x06, 0x52, 0xd5, 0xbe, 0xd2, 0x5e,
|
||||
0x92, 0x5a, 0x70, 0x03, 0x2b, 0x7a, 0x75, 0x5c,
|
||||
0xc5, 0xe4, 0x73, 0xa0, 0x23, 0x47, 0x25, 0x2f,
|
||||
0x17, 0xb3, 0x16, 0xd3, 0x4f, 0xb5, 0x2c, 0xc1,
|
||||
0x22, 0x53, 0x1a, 0xc9, 0xde, 0x79, 0xc3, 0x03,
|
||||
0x53, 0xa2, 0x1a, 0x0d, 0x00, 0x40, 0x7d, 0x49,
|
||||
0x66, 0x0c, 0x76, 0xf2, 0x61, 0xe4, 0x9a, 0x23,
|
||||
}
|
||||
|
||||
// testnetGenesisMerkleRoot is the hash of the first transaction in the genesis block
|
||||
// for testnet.
|
||||
var testnetGenesisMerkleRoot = externalapi.DomainHash{
|
||||
0x7c, 0x5a, 0x9a, 0xb4, 0xa6, 0xd5, 0x03, 0xf3,
|
||||
0x19, 0x3c, 0x26, 0x82, 0xf5, 0x45, 0xdf, 0xe3,
|
||||
0x08, 0x6d, 0x94, 0xfc, 0x7d, 0xb9, 0x42, 0x4b,
|
||||
0x2c, 0x38, 0xf2, 0x5c, 0x64, 0xbc, 0xb4, 0x98,
|
||||
0xd7, 0x16, 0x4a, 0x38, 0x3b, 0x8a, 0x67, 0xc2,
|
||||
0x3b, 0x89, 0x12, 0x1c, 0xcb, 0x97, 0x89, 0xe1,
|
||||
0x12, 0x82, 0x12, 0xc2, 0x69, 0x95, 0x7f, 0x03,
|
||||
0x29, 0xd1, 0x4f, 0xdd, 0xf1, 0x93, 0xd8, 0x47,
|
||||
}
|
||||
|
||||
// testnetGenesisBlock defines the genesis block of the block DAG which serves as the
|
||||
@ -201,9 +201,9 @@ var testnetGenesisBlock = externalapi.DomainBlock{
|
||||
HashMerkleRoot: testnetGenesisMerkleRoot,
|
||||
AcceptedIDMerkleRoot: externalapi.DomainHash{},
|
||||
UTXOCommitment: externalapi.DomainHash{},
|
||||
TimeInMilliseconds: 0x1763db5c5de,
|
||||
TimeInMilliseconds: 0x1763db5c4a9,
|
||||
Bits: 0x1e7fffff,
|
||||
Nonce: 0xec6f,
|
||||
Nonce: 0x493d,
|
||||
},
|
||||
Transactions: []*externalapi.DomainTransaction{testnetGenesisCoinbaseTx},
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user