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"
|
2020-02-17 12:04:25 +00:00
|
|
|
"os"
|
2018-03-17 11:53:21 +00:00
|
|
|
"testing"
|
2018-03-09 15:55:25 +00:00
|
|
|
"time"
|
|
|
|
|
2019-10-15 09:52:10 +00:00
|
|
|
"github.com/CityOfZion/neo-go/config"
|
2020-01-14 12:32:07 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/core/block"
|
2019-10-15 09:52:10 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/core/storage"
|
2018-03-09 15:55:25 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/core/transaction"
|
2020-01-14 12:32:07 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/crypto/hash"
|
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"
|
2020-02-17 12:04:25 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/vm/emit"
|
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
|
|
|
|
}
|
|
|
|
|
2020-01-14 12:32:07 +00:00
|
|
|
func newBlock(index uint32, txs ...*transaction.Transaction) *block.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,
|
|
|
|
}
|
2020-01-14 12:32:07 +00:00
|
|
|
b := &block.Block{
|
2020-01-15 08:29:50 +00:00
|
|
|
Base: block.Base{
|
2018-03-09 15:55:25 +00:00
|
|
|
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,
|
|
|
|
}
|
2020-01-14 12:32:07 +00:00
|
|
|
_ = b.RebuildMerkleRoot()
|
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()
|
2020-01-16 15:30:16 +00:00
|
|
|
sig := pKey.Sign(b)
|
|
|
|
if len(sig) != 64 {
|
|
|
|
panic("wrong signature length")
|
2019-10-15 09:52:10 +00:00
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
2020-01-14 12:32:07 +00:00
|
|
|
func makeBlocks(n int) []*block.Block {
|
|
|
|
blocks := make([]*block.Block, n)
|
2018-03-17 11:53:21 +00:00
|
|
|
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{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-14 12:32:07 +00:00
|
|
|
func getDecodedBlock(t *testing.T, i int) *block.Block {
|
2018-03-17 11:53:21 +00:00
|
|
|
data, err := getBlockData(i)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
b, err := hex.DecodeString(data["raw"].(string))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-01-14 12:32:07 +00:00
|
|
|
block := &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
|
|
|
|
}
|
2020-01-14 12:32:07 +00:00
|
|
|
|
|
|
|
func newDumbBlock() *block.Block {
|
|
|
|
return &block.Block{
|
2020-01-15 08:29:50 +00:00
|
|
|
Base: block.Base{
|
2020-01-14 12:32:07 +00:00
|
|
|
Version: 0,
|
|
|
|
PrevHash: hash.Sha256([]byte("a")),
|
|
|
|
MerkleRoot: hash.Sha256([]byte("b")),
|
|
|
|
Timestamp: uint32(100500),
|
|
|
|
Index: 1,
|
|
|
|
ConsensusData: 1111,
|
|
|
|
NextConsensus: hash.Hash160([]byte("a")),
|
|
|
|
Script: transaction.Witness{
|
|
|
|
VerificationScript: []byte{0x51}, // PUSH1
|
|
|
|
InvocationScript: []byte{0x61}, // NOP
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Transactions: []*transaction.Transaction{
|
|
|
|
{Type: transaction.MinerType},
|
|
|
|
{Type: transaction.IssueType},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2020-02-17 12:04:25 +00:00
|
|
|
|
|
|
|
// This function generates "../rpc/testdata/testblocks.acc" file which contains data
|
|
|
|
// for RPC unit tests.
|
|
|
|
// To generate new "../rpc/testdata/testblocks.acc", follow the steps:
|
|
|
|
// 1. Rename the function
|
|
|
|
// 2. Add specific test-case into "neo-go/pkg/core/blockchain_test.go"
|
|
|
|
// 3. Run tests with `$ make test`
|
|
|
|
func _(t *testing.T) {
|
|
|
|
bc := newTestChain(t)
|
|
|
|
n := 50
|
|
|
|
blocks := makeBlocks(n)
|
|
|
|
|
|
|
|
for i := 0; i < len(blocks); i++ {
|
|
|
|
if err := bc.AddBlock(blocks[i]); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tx1 := newMinerTX()
|
|
|
|
|
|
|
|
avm, err := ioutil.ReadFile("../rpc/testdata/test_contract.avm")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var props smartcontract.PropertyState
|
|
|
|
script := io.NewBufBinWriter()
|
|
|
|
emit.Bytes(script.BinWriter, []byte("Da contract dat hallos u"))
|
|
|
|
emit.Bytes(script.BinWriter, []byte("joe@example.com"))
|
|
|
|
emit.Bytes(script.BinWriter, []byte("Random Guy"))
|
|
|
|
emit.Bytes(script.BinWriter, []byte("0.99"))
|
|
|
|
emit.Bytes(script.BinWriter, []byte("Helloer"))
|
|
|
|
props |= smartcontract.HasStorage
|
|
|
|
emit.Int(script.BinWriter, int64(props))
|
|
|
|
emit.Int(script.BinWriter, int64(5))
|
|
|
|
params := make([]byte, 1)
|
|
|
|
params[0] = byte(7)
|
|
|
|
emit.Bytes(script.BinWriter, params)
|
|
|
|
emit.Bytes(script.BinWriter, avm)
|
|
|
|
emit.Syscall(script.BinWriter, "Neo.Contract.Create")
|
|
|
|
txScript := script.Bytes()
|
|
|
|
|
|
|
|
tx2 := transaction.NewInvocationTX(txScript, util.Fixed8FromFloat(100))
|
|
|
|
|
|
|
|
block := newBlock(uint32(n+1), tx1, tx2)
|
|
|
|
if err := bc.AddBlock(block); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
outStream, err := os.Create("../rpc/testdata/testblocks.acc")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer outStream.Close()
|
|
|
|
|
|
|
|
writer := io.NewBinWriterFromIO(outStream)
|
|
|
|
|
|
|
|
count := bc.BlockHeight() + 1
|
|
|
|
writer.WriteU32LE(count - 1)
|
|
|
|
|
|
|
|
for i := 1; i < int(count); i++ {
|
|
|
|
bh := bc.GetHeaderHash(i)
|
|
|
|
b, err := bc.GetBlock(bh)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
buf := io.NewBufBinWriter()
|
|
|
|
b.EncodeBinary(buf.BinWriter)
|
|
|
|
bytes := buf.Bytes()
|
|
|
|
writer.WriteBytes(bytes)
|
|
|
|
if writer.Err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|