2018-03-09 15:55:25 +00:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
2018-03-17 11:53:21 +00:00
|
|
|
"encoding/hex"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"testing"
|
2018-03-09 15:55:25 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"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-03-09 15:55:25 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
func newBlock(index uint32, txs ...*transaction.Transaction) *Block {
|
|
|
|
b := &Block{
|
|
|
|
BlockBase: BlockBase{
|
|
|
|
Version: 0,
|
2019-08-23 15:50:45 +00:00
|
|
|
PrevHash: hash.Sha256([]byte("a")),
|
|
|
|
MerkleRoot: hash.Sha256([]byte("b")),
|
2018-03-09 15:55:25 +00:00
|
|
|
Timestamp: uint32(time.Now().UTC().Unix()),
|
|
|
|
Index: index,
|
|
|
|
ConsensusData: 1111,
|
|
|
|
NextConsensus: util.Uint160{},
|
|
|
|
Script: &transaction.Witness{
|
|
|
|
VerificationScript: []byte{0x0},
|
|
|
|
InvocationScript: []byte{0x1},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Transactions: txs,
|
|
|
|
}
|
2018-03-25 10:45:54 +00:00
|
|
|
|
|
|
|
b.createHash()
|
|
|
|
|
2018-03-09 15:55:25 +00:00
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2018-03-17 11:53:21 +00:00
|
|
|
func makeBlocks(n int) []*Block {
|
|
|
|
blocks := make([]*Block, n)
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
blocks[i] = newBlock(uint32(i+1), newTX(transaction.MinerType))
|
|
|
|
}
|
|
|
|
return blocks
|
|
|
|
}
|
|
|
|
|
2018-03-09 15:55:25 +00:00
|
|
|
func newTX(t transaction.TXType) *transaction.Transaction {
|
|
|
|
return &transaction.Transaction{
|
|
|
|
Type: t,
|
|
|
|
}
|
|
|
|
}
|
2018-03-17 11:53:21 +00:00
|
|
|
|
|
|
|
func getDecodedBlock(t *testing.T, i int) *Block {
|
|
|
|
data, err := getBlockData(i)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
b, err := hex.DecodeString(data["raw"].(string))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
block := &Block{}
|
2019-09-16 16:31:49 +00:00
|
|
|
r := io.NewBinReaderFromBuf(b)
|
|
|
|
block.DecodeBinary(r)
|
|
|
|
if r.Err != nil {
|
|
|
|
t.Fatal(r.Err)
|
2018-03-17 11:53:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return block
|
|
|
|
}
|
|
|
|
|
|
|
|
func getBlockData(i int) (map[string]interface{}, error) {
|
|
|
|
b, err := ioutil.ReadFile(fmt.Sprintf("test_data/block_%d.json", i))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var data map[string]interface{}
|
|
|
|
if err := json.Unmarshal(b, &data); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return data, err
|
|
|
|
}
|