2018-02-07 14:16:50 +00:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2018-03-04 13:56:49 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/core/transaction"
|
2019-08-23 15:50:45 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/crypto/hash"
|
2019-09-16 09:18:13 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/io"
|
2018-02-07 14:16:50 +00:00
|
|
|
"github.com/CityOfZion/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
|
|
|
)
|
|
|
|
|
|
|
|
func TestHeaderEncodeDecode(t *testing.T) {
|
|
|
|
header := Header{BlockBase: BlockBase{
|
|
|
|
Version: 0,
|
2019-08-23 15:50:45 +00:00
|
|
|
PrevHash: hash.Sha256([]byte("prevhash")),
|
|
|
|
MerkleRoot: hash.Sha256([]byte("merkleroot")),
|
2018-02-07 14:16:50 +00:00
|
|
|
Timestamp: uint32(time.Now().UTC().Unix()),
|
|
|
|
Index: 3445,
|
|
|
|
ConsensusData: 394949,
|
|
|
|
NextConsensus: util.Uint160{},
|
2018-03-04 13:56:49 +00:00
|
|
|
Script: &transaction.Witness{
|
2018-02-07 14:16:50 +00:00
|
|
|
InvocationScript: []byte{0x10},
|
|
|
|
VerificationScript: []byte{0x11},
|
|
|
|
},
|
|
|
|
}}
|
|
|
|
|
2019-09-16 09:18:13 +00:00
|
|
|
buf := io.NewBufBinWriter()
|
|
|
|
if err := header.EncodeBinary(buf.BinWriter); err != nil {
|
2018-02-07 14:16:50 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
headerDecode := &Header{}
|
2019-09-16 09:18:13 +00:00
|
|
|
if err := headerDecode.DecodeBinary(io.NewBinReaderFromBuf(buf.Bytes())); err != nil {
|
2018-02-07 14:16:50 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
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.ConsensusData, headerDecode.ConsensusData, "expected both consensus data fields 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")
|
2018-02-07 14:16:50 +00:00
|
|
|
}
|