2018-02-07 14:16:50 +00:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"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"
|
2018-02-07 14:16:50 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
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},
|
|
|
|
},
|
|
|
|
}}
|
|
|
|
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
if err := header.EncodeBinary(buf); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
headerDecode := &Header{}
|
|
|
|
if err := headerDecode.DecodeBinary(buf); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if header.Version != headerDecode.Version {
|
|
|
|
t.Fatal("expected both versions to be equal")
|
|
|
|
}
|
|
|
|
if !header.PrevHash.Equals(headerDecode.PrevHash) {
|
|
|
|
t.Fatal("expected both prev hashes to be equal")
|
|
|
|
}
|
|
|
|
if !header.MerkleRoot.Equals(headerDecode.MerkleRoot) {
|
|
|
|
t.Fatal("expected both merkle roots to be equal")
|
|
|
|
}
|
|
|
|
if header.Index != headerDecode.Index {
|
|
|
|
t.Fatal("expected both indexes to be equal")
|
|
|
|
}
|
|
|
|
if header.ConsensusData != headerDecode.ConsensusData {
|
|
|
|
t.Fatal("expected both consensus data fields to be equal")
|
|
|
|
}
|
|
|
|
if !header.NextConsensus.Equals(headerDecode.NextConsensus) {
|
|
|
|
t.Fatalf("expected both next consensus fields to be equal")
|
|
|
|
}
|
2019-02-09 15:53:58 +00:00
|
|
|
if !bytes.Equal(header.Script.InvocationScript, headerDecode.Script.InvocationScript) {
|
2018-02-07 14:16:50 +00:00
|
|
|
t.Fatalf("expected equal invocation scripts %v and %v", header.Script.InvocationScript, headerDecode.Script.InvocationScript)
|
|
|
|
}
|
2019-02-09 15:53:58 +00:00
|
|
|
if !bytes.Equal(header.Script.VerificationScript, headerDecode.Script.VerificationScript) {
|
2018-02-07 14:16:50 +00:00
|
|
|
t.Fatalf("expected equal verification scripts %v and %v", header.Script.VerificationScript, headerDecode.Script.VerificationScript)
|
|
|
|
}
|
|
|
|
}
|