mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2024-11-26 19:42:23 +00:00
5bf00db2c9
The logic here is that we'll have all binary encoding/decoding done via our io package, which simplifies error handling. This functionality doesn't belong to util, so it's moved. This also expands BufBinWriter with Reset() method to fit the needs of core package.
46 lines
1.8 KiB
Go
46 lines
1.8 KiB
Go
package core
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/CityOfZion/neo-go/pkg/core/transaction"
|
|
"github.com/CityOfZion/neo-go/pkg/crypto/hash"
|
|
"github.com/CityOfZion/neo-go/pkg/io"
|
|
"github.com/CityOfZion/neo-go/pkg/util"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
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 := io.NewBufBinWriter()
|
|
if err := header.EncodeBinary(buf.BinWriter); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
headerDecode := &Header{}
|
|
if err := headerDecode.DecodeBinary(io.NewBinReaderFromBuf(buf.Bytes())); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
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")
|
|
}
|