2020-01-14 12:32:07 +00:00
|
|
|
package block
|
2018-02-07 14:16:50 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2020-11-23 11:09:00 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/internal/random"
|
|
|
|
"github.com/nspcc-dev/neo-go/internal/testserdes"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2019-09-16 09:07:06 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2018-02-07 14:16:50 +00:00
|
|
|
)
|
|
|
|
|
2020-11-17 12:57:50 +00:00
|
|
|
func testHeaderEncodeDecode(t *testing.T, stateRootEnabled bool) {
|
2021-03-01 13:44:47 +00:00
|
|
|
header := Header{
|
2018-02-07 14:16:50 +00:00
|
|
|
Version: 0,
|
2019-08-23 15:50:45 +00:00
|
|
|
PrevHash: hash.Sha256([]byte("prevhash")),
|
|
|
|
MerkleRoot: hash.Sha256([]byte("merkleroot")),
|
2020-04-24 09:49:17 +00:00
|
|
|
Timestamp: uint64(time.Now().UTC().Unix() * 1000),
|
2018-02-07 14:16:50 +00:00
|
|
|
Index: 3445,
|
|
|
|
NextConsensus: util.Uint160{},
|
2019-12-09 14:14:10 +00:00
|
|
|
Script: transaction.Witness{
|
2018-02-07 14:16:50 +00:00
|
|
|
InvocationScript: []byte{0x10},
|
|
|
|
VerificationScript: []byte{0x11},
|
|
|
|
},
|
2021-03-01 13:44:47 +00:00
|
|
|
}
|
2020-11-17 12:57:50 +00:00
|
|
|
if stateRootEnabled {
|
|
|
|
header.StateRootEnabled = stateRootEnabled
|
|
|
|
header.PrevStateRoot = random.Uint256()
|
|
|
|
}
|
2018-02-07 14:16:50 +00:00
|
|
|
|
2020-03-26 14:43:24 +00:00
|
|
|
_ = header.Hash()
|
2021-03-01 13:44:47 +00:00
|
|
|
headerDecode := &Header{StateRootEnabled: stateRootEnabled}
|
2020-03-26 14:43:24 +00:00
|
|
|
testserdes.EncodeDecodeBinary(t, &header, headerDecode)
|
|
|
|
|
2019-09-16 09:07:06 +00:00
|
|
|
assert.Equal(t, header.Version, headerDecode.Version, "expected both versions to be equal")
|
|
|
|
assert.Equal(t, header.PrevHash, headerDecode.PrevHash, "expected both prev hashes to be equal")
|
|
|
|
assert.Equal(t, header.MerkleRoot, headerDecode.MerkleRoot, "expected both merkle roots to be equal")
|
|
|
|
assert.Equal(t, header.Index, headerDecode.Index, "expected both indexes to be equal")
|
|
|
|
assert.Equal(t, header.NextConsensus, headerDecode.NextConsensus, "expected both next consensus fields to be equal")
|
|
|
|
assert.Equal(t, header.Script.InvocationScript, headerDecode.Script.InvocationScript, "expected equal invocation scripts")
|
|
|
|
assert.Equal(t, header.Script.VerificationScript, headerDecode.Script.VerificationScript, "expected equal verification scripts")
|
2020-11-17 12:57:50 +00:00
|
|
|
assert.Equal(t, header.PrevStateRoot, headerDecode.PrevStateRoot, "expected equal state roots")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHeaderEncodeDecode(t *testing.T) {
|
|
|
|
t.Run("NoStateRoot", func(t *testing.T) {
|
|
|
|
testHeaderEncodeDecode(t, false)
|
|
|
|
})
|
|
|
|
t.Run("WithStateRoot", func(t *testing.T) {
|
|
|
|
testHeaderEncodeDecode(t, true)
|
|
|
|
})
|
2018-02-07 14:16:50 +00:00
|
|
|
}
|