mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2024-11-29 23:33:37 +00:00
ec7e17ffa6
Simplifies a lot of code and removes some duplication. Unfortunately I had to move test_util random functions in same commit to avoid cycle dependencies. One of these random functions was also used in core/transaction testing, to simplify things I've just dropped it there and used a static string (which is nice to have for a test anyway). There is still sha256 left in wallet (but it needs to pass Hash structure into the signing function).
61 lines
1.9 KiB
Go
61 lines
1.9 KiB
Go
package core
|
|
|
|
import (
|
|
"bytes"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/CityOfZion/neo-go/pkg/core/transaction"
|
|
"github.com/CityOfZion/neo-go/pkg/crypto/hash"
|
|
"github.com/CityOfZion/neo-go/pkg/util"
|
|
)
|
|
|
|
func TestHeaderEncodeDecode(t *testing.T) {
|
|
header := Header{BlockBase: BlockBase{
|
|
Version: 0,
|
|
PrevHash: hash.Sha256([]byte("prevhash")),
|
|
MerkleRoot: hash.Sha256([]byte("merkleroot")),
|
|
Timestamp: uint32(time.Now().UTC().Unix()),
|
|
Index: 3445,
|
|
ConsensusData: 394949,
|
|
NextConsensus: util.Uint160{},
|
|
Script: &transaction.Witness{
|
|
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")
|
|
}
|
|
if !bytes.Equal(header.Script.InvocationScript, headerDecode.Script.InvocationScript) {
|
|
t.Fatalf("expected equal invocation scripts %v and %v", header.Script.InvocationScript, headerDecode.Script.InvocationScript)
|
|
}
|
|
if !bytes.Equal(header.Script.VerificationScript, headerDecode.Script.VerificationScript) {
|
|
t.Fatalf("expected equal verification scripts %v and %v", header.Script.VerificationScript, headerDecode.Script.VerificationScript)
|
|
}
|
|
}
|