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"
|
|
|
|
|
2019-10-15 09:52:10 +00:00
|
|
|
"github.com/CityOfZion/neo-go/config"
|
|
|
|
"github.com/CityOfZion/neo-go/pkg/core/storage"
|
2018-03-09 15:55:25 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/core/transaction"
|
2019-10-15 09:52:10 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/crypto/keys"
|
2019-09-16 09:18:13 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/io"
|
2019-10-15 09:52:10 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/smartcontract"
|
2018-03-09 15:55:25 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/util"
|
2019-12-03 14:54:47 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/vm/opcode"
|
2019-10-15 09:52:10 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-12-30 07:43:05 +00:00
|
|
|
"go.uber.org/zap/zaptest"
|
2018-03-09 15:55:25 +00:00
|
|
|
)
|
|
|
|
|
2019-10-15 09:52:10 +00:00
|
|
|
var newBlockPrevHash util.Uint256
|
|
|
|
var unitTestNetCfg config.Config
|
|
|
|
|
|
|
|
var privNetKeys = []string{
|
|
|
|
"KxyjQ8eUa4FHt3Gvioyt1Wz29cTUrE4eTqX3yFSk1YFCsPL8uNsY",
|
|
|
|
"KzfPUYDC9n2yf4fK5ro4C8KMcdeXtFuEnStycbZgX3GomiUsvX6W",
|
|
|
|
"KzgWE3u3EDp13XPXXuTKZxeJ3Gi8Bsm8f9ijY3ZsCKKRvZUo1Cdn",
|
|
|
|
"L2oEXKRAAMiPEZukwR5ho2S6SMeQLhcK9mF71ZnF7GvT8dU4Kkgz",
|
|
|
|
}
|
|
|
|
|
|
|
|
// newTestChain should be called before newBlock invocation to properly setup
|
|
|
|
// global state.
|
|
|
|
func newTestChain(t *testing.T) *Blockchain {
|
|
|
|
var err error
|
|
|
|
unitTestNetCfg, err = config.Load("../../config", config.ModeUnitTestNet)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-12-30 07:43:05 +00:00
|
|
|
chain, err := NewBlockchain(storage.NewMemoryStore(), unitTestNetCfg.ProtocolConfiguration, zaptest.NewLogger(t))
|
2019-10-15 09:52:10 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-11-07 17:47:48 +00:00
|
|
|
go chain.Run()
|
2019-10-15 09:52:10 +00:00
|
|
|
zeroHash, err := chain.GetHeader(chain.GetHeaderHash(0))
|
|
|
|
require.Nil(t, err)
|
|
|
|
newBlockPrevHash = zeroHash.Hash()
|
|
|
|
return chain
|
|
|
|
}
|
|
|
|
|
2018-03-09 15:55:25 +00:00
|
|
|
func newBlock(index uint32, txs ...*transaction.Transaction) *Block {
|
2019-10-15 09:52:10 +00:00
|
|
|
validators, _ := getValidators(unitTestNetCfg.ProtocolConfiguration)
|
|
|
|
vlen := len(validators)
|
|
|
|
valScript, _ := smartcontract.CreateMultiSigRedeemScript(
|
|
|
|
vlen-(vlen-1)/3,
|
|
|
|
validators,
|
|
|
|
)
|
2019-12-09 14:14:10 +00:00
|
|
|
witness := transaction.Witness{
|
2019-10-15 09:52:10 +00:00
|
|
|
VerificationScript: valScript,
|
|
|
|
}
|
2018-03-09 15:55:25 +00:00
|
|
|
b := &Block{
|
|
|
|
BlockBase: BlockBase{
|
|
|
|
Version: 0,
|
2019-10-15 09:52:10 +00:00
|
|
|
PrevHash: newBlockPrevHash,
|
|
|
|
Timestamp: uint32(time.Now().UTC().Unix()) + index,
|
2018-03-09 15:55:25 +00:00
|
|
|
Index: index,
|
|
|
|
ConsensusData: 1111,
|
2019-10-15 09:52:10 +00:00
|
|
|
NextConsensus: witness.ScriptHash(),
|
|
|
|
Script: witness,
|
2018-03-09 15:55:25 +00:00
|
|
|
},
|
|
|
|
Transactions: txs,
|
|
|
|
}
|
2019-10-15 09:52:10 +00:00
|
|
|
_ = b.rebuildMerkleRoot()
|
2018-03-25 10:45:54 +00:00
|
|
|
b.createHash()
|
2019-10-15 09:52:10 +00:00
|
|
|
newBlockPrevHash = b.Hash()
|
2018-03-25 10:45:54 +00:00
|
|
|
|
2019-10-15 09:52:10 +00:00
|
|
|
invScript := make([]byte, 0)
|
|
|
|
for _, wif := range privNetKeys {
|
|
|
|
pKey, err := keys.NewPrivateKeyFromWIF(wif)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2019-11-15 10:32:40 +00:00
|
|
|
b := b.GetHashableData()
|
2019-10-15 09:52:10 +00:00
|
|
|
sig, err := pKey.Sign(b)
|
|
|
|
if err != nil || len(sig) != 64 {
|
|
|
|
panic(err)
|
|
|
|
}
|
2019-12-03 14:54:47 +00:00
|
|
|
invScript = append(invScript, byte(opcode.PUSHBYTES64))
|
2019-10-15 09:52:10 +00:00
|
|
|
invScript = append(invScript, sig...)
|
|
|
|
}
|
|
|
|
b.Script.InvocationScript = invScript
|
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++ {
|
2019-10-10 17:02:09 +00:00
|
|
|
blocks[i] = newBlock(uint32(i+1), newMinerTX())
|
2018-03-17 11:53:21 +00:00
|
|
|
}
|
|
|
|
return blocks
|
|
|
|
}
|
|
|
|
|
2019-10-10 17:02:09 +00:00
|
|
|
func newMinerTX() *transaction.Transaction {
|
2018-03-09 15:55:25 +00:00
|
|
|
return &transaction.Transaction{
|
2019-10-10 17:02:09 +00:00
|
|
|
Type: transaction.MinerType,
|
|
|
|
Data: &transaction.MinerTX{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|