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"
|
|
|
|
|
2020-03-25 15:30:21 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/config"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/block"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/storage"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
|
2020-04-22 10:15:31 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/internal/testchain"
|
2020-03-26 14:43:24 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/internal/testserdes"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/emit"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/opcode"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/wallet"
|
2020-04-16 14:10:42 +00:00
|
|
|
"github.com/pkg/errors"
|
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
|
|
|
)
|
|
|
|
|
2020-04-16 14:10:42 +00:00
|
|
|
// multisig address which possess all NEO
|
2020-04-21 15:09:34 +00:00
|
|
|
var neoOwner = testchain.MultisigScriptHash()
|
2020-04-16 14:10:42 +00:00
|
|
|
|
2019-10-15 09:52:10 +00:00
|
|
|
// newTestChain should be called before newBlock invocation to properly setup
|
|
|
|
// global state.
|
|
|
|
func newTestChain(t *testing.T) *Blockchain {
|
2020-02-29 14:24:37 +00:00
|
|
|
unitTestNetCfg, err := config.Load("../../config", config.ModeUnitTestNet)
|
2020-02-29 15:55:16 +00:00
|
|
|
require.NoError(t, err)
|
2019-12-30 07:43:05 +00:00
|
|
|
chain, err := NewBlockchain(storage.NewMemoryStore(), unitTestNetCfg.ProtocolConfiguration, zaptest.NewLogger(t))
|
2020-02-29 15:55:16 +00:00
|
|
|
require.NoError(t, err)
|
2019-11-07 17:47:48 +00:00
|
|
|
go chain.Run()
|
2019-10-15 09:52:10 +00:00
|
|
|
return chain
|
|
|
|
}
|
|
|
|
|
2020-02-29 14:24:37 +00:00
|
|
|
func (bc *Blockchain) newBlock(txs ...*transaction.Transaction) *block.Block {
|
|
|
|
lastBlock := bc.topBlock.Load().(*block.Block)
|
|
|
|
return newBlock(bc.config, lastBlock.Index+1, lastBlock.Hash(), txs...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func newBlock(cfg config.ProtocolConfiguration, index uint32, prev util.Uint256, txs ...*transaction.Transaction) *block.Block {
|
|
|
|
validators, _ := getValidators(cfg)
|
2019-10-15 09:52:10 +00:00
|
|
|
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,
|
2020-02-29 14:24:37 +00:00
|
|
|
PrevHash: prev,
|
2020-04-21 11:26:57 +00:00
|
|
|
Timestamp: uint64(time.Now().UTC().Unix()) + uint64(index),
|
2018-03-09 15:55:25 +00:00
|
|
|
Index: index,
|
2019-10-15 09:52:10 +00:00
|
|
|
NextConsensus: witness.ScriptHash(),
|
|
|
|
Script: witness,
|
2018-03-09 15:55:25 +00:00
|
|
|
},
|
2020-04-22 05:57:55 +00:00
|
|
|
ConsensusData: block.ConsensusData{
|
|
|
|
PrimaryIndex: 0,
|
|
|
|
Nonce: 1111,
|
|
|
|
},
|
2018-03-09 15:55:25 +00:00
|
|
|
Transactions: txs,
|
|
|
|
}
|
2020-05-07 18:46:28 +00:00
|
|
|
err := b.RebuildMerkleRoot()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-03-25 10:45:54 +00:00
|
|
|
|
2020-04-23 13:11:40 +00:00
|
|
|
b.Script.InvocationScript = testchain.Sign(b.GetSignedPart())
|
2018-03-09 15:55:25 +00:00
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2020-02-29 14:16:13 +00:00
|
|
|
func (bc *Blockchain) genBlocks(n int) ([]*block.Block, error) {
|
2020-01-14 12:32:07 +00:00
|
|
|
blocks := make([]*block.Block, n)
|
2020-02-29 14:24:37 +00:00
|
|
|
lastHash := bc.topBlock.Load().(*block.Block).Hash()
|
2020-03-04 14:45:29 +00:00
|
|
|
lastIndex := bc.topBlock.Load().(*block.Block).Index
|
2018-03-17 11:53:21 +00:00
|
|
|
for i := 0; i < n; i++ {
|
2020-04-22 17:42:38 +00:00
|
|
|
blocks[i] = newBlock(bc.config, uint32(i)+lastIndex+1, lastHash)
|
2020-02-29 14:16:13 +00:00
|
|
|
if err := bc.AddBlock(blocks[i]); err != nil {
|
|
|
|
return blocks, err
|
|
|
|
}
|
2020-02-29 14:24:37 +00:00
|
|
|
lastHash = blocks[i].Hash()
|
2018-03-17 11:53:21 +00:00
|
|
|
}
|
2020-02-29 14:16:13 +00:00
|
|
|
return blocks, nil
|
2018-03-17 11:53:21 +00:00
|
|
|
}
|
|
|
|
|
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)
|
2020-02-29 15:55:16 +00:00
|
|
|
require.NoError(t, err)
|
2018-03-17 11:53:21 +00:00
|
|
|
|
|
|
|
b, err := hex.DecodeString(data["raw"].(string))
|
2020-02-29 15:55:16 +00:00
|
|
|
require.NoError(t, err)
|
2018-03-17 11:53:21 +00:00
|
|
|
|
2020-01-14 12:32:07 +00:00
|
|
|
block := &block.Block{}
|
2020-03-26 14:43:24 +00:00
|
|
|
require.NoError(t, testserdes.DecodeBinary(b, block))
|
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")),
|
2020-04-21 11:26:57 +00:00
|
|
|
Timestamp: 100500,
|
2020-01-14 12:32:07 +00:00
|
|
|
Index: 1,
|
|
|
|
NextConsensus: hash.Hash160([]byte("a")),
|
|
|
|
Script: transaction.Witness{
|
|
|
|
VerificationScript: []byte{0x51}, // PUSH1
|
|
|
|
InvocationScript: []byte{0x61}, // NOP
|
|
|
|
},
|
|
|
|
},
|
2020-04-22 05:57:55 +00:00
|
|
|
ConsensusData: block.ConsensusData{
|
|
|
|
PrimaryIndex: 0,
|
|
|
|
Nonce: 1111,
|
|
|
|
},
|
2020-01-14 12:32:07 +00:00
|
|
|
Transactions: []*transaction.Transaction{
|
|
|
|
{Type: transaction.IssueType},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2020-02-17 12:04:25 +00:00
|
|
|
|
|
|
|
// This function generates "../rpc/testdata/testblocks.acc" file which contains data
|
2020-03-04 14:45:29 +00:00
|
|
|
// for RPC unit tests. It also is a nice integration test.
|
2020-02-17 12:04:25 +00:00
|
|
|
// To generate new "../rpc/testdata/testblocks.acc", follow the steps:
|
2020-03-04 14:45:29 +00:00
|
|
|
// 1. Set saveChain down below to true
|
|
|
|
// 2. Run tests with `$ make test`
|
|
|
|
func TestCreateBasicChain(t *testing.T) {
|
|
|
|
const saveChain = false
|
2020-02-26 12:37:53 +00:00
|
|
|
const prefix = "../rpc/server/testdata/"
|
2020-03-04 14:45:29 +00:00
|
|
|
// To make enough GAS.
|
|
|
|
const numOfEmptyBlocks = 200
|
2020-04-15 06:50:13 +00:00
|
|
|
// Increase in case if you need more blocks
|
|
|
|
const validUntilBlock = numOfEmptyBlocks + 1000
|
2020-02-26 12:37:53 +00:00
|
|
|
|
2020-04-10 10:41:49 +00:00
|
|
|
// To be incremented after each created transaction to keep chain constant.
|
|
|
|
var testNonce uint32 = 1
|
|
|
|
|
|
|
|
// Use as nonce when new transaction is created to avoid random data in tests.
|
|
|
|
getNextNonce := func() uint32 {
|
|
|
|
testNonce++
|
|
|
|
return testNonce
|
|
|
|
}
|
|
|
|
|
2020-03-04 14:45:29 +00:00
|
|
|
var neoAmount = util.Fixed8FromInt64(99999000)
|
|
|
|
var neoRemainder = util.Fixed8FromInt64(100000000) - neoAmount
|
2020-02-17 12:04:25 +00:00
|
|
|
bc := newTestChain(t)
|
2020-04-22 14:43:50 +00:00
|
|
|
defer bc.Close()
|
2020-02-17 12:04:25 +00:00
|
|
|
|
2020-05-08 17:54:24 +00:00
|
|
|
gasHash := bc.contracts.GAS.Hash
|
|
|
|
t.Logf("native GAS hash: %v", gasHash)
|
|
|
|
|
|
|
|
priv0 := testchain.PrivateKeyByID(0)
|
|
|
|
priv0ScriptHash := priv0.GetScriptHash()
|
|
|
|
|
2020-05-27 20:29:33 +00:00
|
|
|
require.Equal(t, util.Fixed8FromInt64(0), bc.GetUtilityTokenBalance(priv0ScriptHash))
|
2020-05-08 17:54:24 +00:00
|
|
|
// Move almost all NEO and some nep5 GAS to one simple account.
|
|
|
|
txMoveNeo := newNEP5Transfer(gasHash, neoOwner, priv0ScriptHash, 1000000000)
|
2020-04-15 06:50:13 +00:00
|
|
|
txMoveNeo.ValidUntilBlock = validUntilBlock
|
2020-04-10 10:41:49 +00:00
|
|
|
txMoveNeo.Nonce = getNextNonce()
|
2020-05-08 17:54:24 +00:00
|
|
|
txMoveNeo.Sender = neoOwner
|
2020-05-19 07:19:05 +00:00
|
|
|
txMoveNeo.Cosigners = []transaction.Cosigner{{
|
|
|
|
Account: neoOwner,
|
|
|
|
Scopes: transaction.CalledByEntry,
|
|
|
|
AllowedContracts: nil,
|
|
|
|
AllowedGroups: nil,
|
|
|
|
}}
|
2020-04-10 10:41:49 +00:00
|
|
|
|
|
|
|
// use output of issue tx from genesis block as an input
|
|
|
|
genesisBlock, err := bc.GetBlock(bc.GetHeaderHash(0))
|
2020-02-26 12:37:53 +00:00
|
|
|
require.NoError(t, err)
|
2020-04-22 17:42:38 +00:00
|
|
|
require.Equal(t, 4, len(genesisBlock.Transactions))
|
|
|
|
h := genesisBlock.Transactions[2].Hash()
|
2020-03-04 14:45:29 +00:00
|
|
|
txMoveNeo.AddInput(&transaction.Input{
|
2020-02-26 12:37:53 +00:00
|
|
|
PrevHash: h,
|
|
|
|
PrevIndex: 0,
|
|
|
|
})
|
2020-03-04 14:45:29 +00:00
|
|
|
txMoveNeo.AddOutput(&transaction.Output{
|
2020-02-26 12:37:53 +00:00
|
|
|
AssetID: GoverningTokenID(),
|
2020-03-04 14:45:29 +00:00
|
|
|
Amount: neoAmount,
|
2020-05-08 17:54:24 +00:00
|
|
|
ScriptHash: priv0ScriptHash,
|
2020-02-26 12:37:53 +00:00
|
|
|
Position: 0,
|
|
|
|
})
|
2020-03-04 14:45:29 +00:00
|
|
|
txMoveNeo.AddOutput(&transaction.Output{
|
2020-02-26 12:37:53 +00:00
|
|
|
AssetID: GoverningTokenID(),
|
2020-03-04 14:45:29 +00:00
|
|
|
Amount: neoRemainder,
|
2020-04-21 15:09:34 +00:00
|
|
|
ScriptHash: neoOwner,
|
2020-02-26 12:37:53 +00:00
|
|
|
Position: 1,
|
|
|
|
})
|
2020-04-22 17:42:38 +00:00
|
|
|
require.NoError(t, signTx(bc, txMoveNeo))
|
|
|
|
b := bc.newBlock(txMoveNeo)
|
2020-02-18 08:09:07 +00:00
|
|
|
require.NoError(t, bc.AddBlock(b))
|
2020-03-04 14:45:29 +00:00
|
|
|
t.Logf("txMoveNeo: %s", txMoveNeo.Hash().StringLE())
|
2020-02-18 08:09:07 +00:00
|
|
|
|
2020-05-27 20:29:33 +00:00
|
|
|
require.Equal(t, util.Fixed8FromInt64(10), bc.GetUtilityTokenBalance(priv0ScriptHash))
|
2020-04-06 06:27:15 +00:00
|
|
|
// info for getblockheader rpc tests
|
|
|
|
t.Logf("header hash: %s", b.Hash().StringLE())
|
|
|
|
buf := io.NewBufBinWriter()
|
|
|
|
b.Header().EncodeBinary(buf.BinWriter)
|
|
|
|
t.Logf("header: %s", hex.EncodeToString(buf.Bytes()))
|
|
|
|
|
2020-03-04 14:45:29 +00:00
|
|
|
// Generate some blocks to be able to claim GAS for them.
|
|
|
|
_, err = bc.genBlocks(numOfEmptyBlocks)
|
2020-02-27 12:59:17 +00:00
|
|
|
require.NoError(t, err)
|
2020-03-04 14:45:29 +00:00
|
|
|
|
|
|
|
acc0, err := wallet.NewAccountFromWIF(priv0.WIF())
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Make a NEO roundtrip (send to myself) and claim GAS.
|
|
|
|
txNeoRound := transaction.NewContractTX()
|
2020-04-10 10:41:49 +00:00
|
|
|
txNeoRound.Nonce = getNextNonce()
|
2020-04-16 14:10:42 +00:00
|
|
|
txNeoRound.Sender = priv0ScriptHash
|
2020-04-15 06:50:13 +00:00
|
|
|
txNeoRound.ValidUntilBlock = validUntilBlock
|
2020-03-04 14:45:29 +00:00
|
|
|
txNeoRound.AddInput(&transaction.Input{
|
|
|
|
PrevHash: txMoveNeo.Hash(),
|
2020-02-27 12:59:17 +00:00
|
|
|
PrevIndex: 0,
|
|
|
|
})
|
2020-03-04 14:45:29 +00:00
|
|
|
txNeoRound.AddOutput(&transaction.Output{
|
2020-02-27 12:59:17 +00:00
|
|
|
AssetID: GoverningTokenID(),
|
2020-03-04 14:45:29 +00:00
|
|
|
Amount: neoAmount,
|
|
|
|
ScriptHash: priv0.GetScriptHash(),
|
|
|
|
Position: 0,
|
2020-02-27 12:59:17 +00:00
|
|
|
})
|
2020-03-04 14:45:29 +00:00
|
|
|
txNeoRound.Data = new(transaction.ContractTX)
|
2020-05-08 17:54:24 +00:00
|
|
|
require.NoError(t, addNetworkFee(bc, txNeoRound, acc0))
|
2020-03-04 14:45:29 +00:00
|
|
|
require.NoError(t, acc0.SignTx(txNeoRound))
|
2020-04-22 17:42:38 +00:00
|
|
|
b = bc.newBlock(txNeoRound)
|
2020-03-04 14:45:29 +00:00
|
|
|
require.NoError(t, bc.AddBlock(b))
|
|
|
|
t.Logf("txNeoRound: %s", txNeoRound.Hash().StringLE())
|
2020-02-27 12:59:17 +00:00
|
|
|
|
2020-03-04 14:45:29 +00:00
|
|
|
claim := new(transaction.ClaimTX)
|
|
|
|
claim.Claims = append(claim.Claims, transaction.Input{
|
|
|
|
PrevHash: txMoveNeo.Hash(),
|
|
|
|
PrevIndex: 0,
|
|
|
|
})
|
2020-04-10 10:41:49 +00:00
|
|
|
txClaim := transaction.NewClaimTX(claim)
|
|
|
|
txClaim.Nonce = getNextNonce()
|
2020-04-15 06:50:13 +00:00
|
|
|
txClaim.ValidUntilBlock = validUntilBlock
|
2020-04-16 14:10:42 +00:00
|
|
|
txClaim.Sender = priv0ScriptHash
|
2020-03-04 14:45:29 +00:00
|
|
|
txClaim.Data = claim
|
|
|
|
neoGas, sysGas, err := bc.CalculateClaimable(neoAmount, 1, bc.BlockHeight())
|
2020-02-28 16:01:07 +00:00
|
|
|
require.NoError(t, err)
|
2020-03-04 14:45:29 +00:00
|
|
|
gasOwned := neoGas + sysGas
|
|
|
|
txClaim.AddOutput(&transaction.Output{
|
|
|
|
AssetID: UtilityTokenID(),
|
|
|
|
Amount: gasOwned,
|
|
|
|
ScriptHash: priv0.GetScriptHash(),
|
|
|
|
Position: 0,
|
|
|
|
})
|
2020-05-08 17:54:24 +00:00
|
|
|
require.NoError(t, addNetworkFee(bc, txClaim, acc0))
|
2020-03-04 14:45:29 +00:00
|
|
|
require.NoError(t, acc0.SignTx(txClaim))
|
2020-04-22 17:42:38 +00:00
|
|
|
b = bc.newBlock(txClaim)
|
2020-02-27 12:59:17 +00:00
|
|
|
require.NoError(t, bc.AddBlock(b))
|
2020-03-04 14:45:29 +00:00
|
|
|
t.Logf("txClaim: %s", txClaim.Hash().StringLE())
|
2020-02-27 12:59:17 +00:00
|
|
|
|
2020-03-04 14:45:29 +00:00
|
|
|
// Push some contract into the chain.
|
|
|
|
avm, err := ioutil.ReadFile(prefix + "test_contract.avm")
|
2020-02-29 15:55:16 +00:00
|
|
|
require.NoError(t, err)
|
2020-03-05 09:28:46 +00:00
|
|
|
t.Logf("contractHash: %s", hash.Hash160(avm).StringLE())
|
2020-02-17 12:04:25 +00:00
|
|
|
|
2020-03-04 14:45:29 +00:00
|
|
|
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()
|
|
|
|
|
|
|
|
invFee := util.Fixed8FromFloat(100)
|
|
|
|
txDeploy := transaction.NewInvocationTX(txScript, invFee)
|
2020-04-10 10:41:49 +00:00
|
|
|
txDeploy.Nonce = getNextNonce()
|
2020-04-15 06:50:13 +00:00
|
|
|
txDeploy.ValidUntilBlock = validUntilBlock
|
2020-04-16 14:10:42 +00:00
|
|
|
txDeploy.Sender = priv0ScriptHash
|
2020-03-04 14:45:29 +00:00
|
|
|
txDeploy.AddInput(&transaction.Input{
|
|
|
|
PrevHash: txClaim.Hash(),
|
|
|
|
PrevIndex: 0,
|
|
|
|
})
|
|
|
|
txDeploy.AddOutput(&transaction.Output{
|
|
|
|
AssetID: UtilityTokenID(),
|
|
|
|
Amount: gasOwned - invFee,
|
|
|
|
ScriptHash: priv0.GetScriptHash(),
|
|
|
|
Position: 0,
|
|
|
|
})
|
|
|
|
gasOwned -= invFee
|
2020-05-08 17:54:24 +00:00
|
|
|
require.NoError(t, addNetworkFee(bc, txDeploy, acc0))
|
2020-03-04 14:45:29 +00:00
|
|
|
require.NoError(t, acc0.SignTx(txDeploy))
|
2020-04-22 17:42:38 +00:00
|
|
|
b = bc.newBlock(txDeploy)
|
2020-03-04 14:45:29 +00:00
|
|
|
require.NoError(t, bc.AddBlock(b))
|
|
|
|
t.Logf("txDeploy: %s", txDeploy.Hash().StringLE())
|
|
|
|
|
|
|
|
// Now invoke this contract.
|
|
|
|
script = io.NewBufBinWriter()
|
2020-03-27 08:00:06 +00:00
|
|
|
emit.AppCallWithOperationAndArgs(script.BinWriter, hash.Hash160(avm), "Put", "testkey", "testvalue")
|
2020-02-17 12:04:25 +00:00
|
|
|
|
2020-03-04 14:45:29 +00:00
|
|
|
txInv := transaction.NewInvocationTX(script.Bytes(), 0)
|
2020-04-10 10:41:49 +00:00
|
|
|
txInv.Nonce = getNextNonce()
|
2020-04-15 06:50:13 +00:00
|
|
|
txInv.ValidUntilBlock = validUntilBlock
|
2020-04-16 14:10:42 +00:00
|
|
|
txInv.Sender = priv0ScriptHash
|
2020-05-08 17:54:24 +00:00
|
|
|
require.NoError(t, addNetworkFee(bc, txInv, acc0))
|
2020-04-16 14:10:42 +00:00
|
|
|
require.NoError(t, acc0.SignTx(txInv))
|
2020-04-22 17:42:38 +00:00
|
|
|
b = bc.newBlock(txInv)
|
2020-03-04 14:45:29 +00:00
|
|
|
require.NoError(t, bc.AddBlock(b))
|
|
|
|
t.Logf("txInv: %s", txInv.Hash().StringLE())
|
2020-02-17 12:04:25 +00:00
|
|
|
|
2020-04-22 10:15:31 +00:00
|
|
|
priv1 := testchain.PrivateKeyByID(1)
|
2020-03-04 14:45:29 +00:00
|
|
|
txNeo0to1 := transaction.NewContractTX()
|
2020-04-10 10:41:49 +00:00
|
|
|
txNeo0to1.Nonce = getNextNonce()
|
2020-04-15 06:50:13 +00:00
|
|
|
txNeo0to1.ValidUntilBlock = validUntilBlock
|
2020-04-16 14:10:42 +00:00
|
|
|
txNeo0to1.Sender = priv0ScriptHash
|
2020-03-04 14:45:29 +00:00
|
|
|
txNeo0to1.Data = new(transaction.ContractTX)
|
|
|
|
txNeo0to1.AddInput(&transaction.Input{
|
|
|
|
PrevHash: txNeoRound.Hash(),
|
|
|
|
PrevIndex: 0,
|
|
|
|
})
|
|
|
|
txNeo0to1.AddOutput(&transaction.Output{
|
|
|
|
AssetID: GoverningTokenID(),
|
|
|
|
Amount: util.Fixed8FromInt64(1000),
|
|
|
|
ScriptHash: priv1.GetScriptHash(),
|
|
|
|
})
|
|
|
|
txNeo0to1.AddOutput(&transaction.Output{
|
|
|
|
AssetID: GoverningTokenID(),
|
|
|
|
Amount: neoAmount - util.Fixed8FromInt64(1000),
|
|
|
|
ScriptHash: priv0.GetScriptHash(),
|
|
|
|
})
|
|
|
|
|
2020-05-08 17:54:24 +00:00
|
|
|
require.NoError(t, addNetworkFee(bc, txNeo0to1, acc0))
|
2020-03-04 14:45:29 +00:00
|
|
|
require.NoError(t, acc0.SignTx(txNeo0to1))
|
2020-04-22 17:42:38 +00:00
|
|
|
b = bc.newBlock(txNeo0to1)
|
2020-03-04 14:45:29 +00:00
|
|
|
require.NoError(t, bc.AddBlock(b))
|
|
|
|
|
2020-03-05 09:28:46 +00:00
|
|
|
sh := hash.Hash160(avm)
|
|
|
|
w := io.NewBufBinWriter()
|
2020-03-27 08:00:06 +00:00
|
|
|
emit.AppCallWithOperationAndArgs(w.BinWriter, sh, "init")
|
2020-03-05 09:28:46 +00:00
|
|
|
initTx := transaction.NewInvocationTX(w.Bytes(), 0)
|
2020-04-10 10:41:49 +00:00
|
|
|
initTx.Nonce = getNextNonce()
|
2020-04-15 06:50:13 +00:00
|
|
|
initTx.ValidUntilBlock = validUntilBlock
|
2020-04-16 14:10:42 +00:00
|
|
|
initTx.Sender = priv0ScriptHash
|
2020-05-08 17:54:24 +00:00
|
|
|
require.NoError(t, addNetworkFee(bc, initTx, acc0))
|
2020-04-16 14:10:42 +00:00
|
|
|
require.NoError(t, acc0.SignTx(initTx))
|
2020-03-05 09:28:46 +00:00
|
|
|
transferTx := newNEP5Transfer(sh, sh, priv0.GetScriptHash(), 1000)
|
2020-04-10 10:41:49 +00:00
|
|
|
transferTx.Nonce = getNextNonce()
|
2020-04-15 06:50:13 +00:00
|
|
|
transferTx.ValidUntilBlock = validUntilBlock
|
2020-04-16 14:10:42 +00:00
|
|
|
transferTx.Sender = priv0ScriptHash
|
2020-05-19 07:19:05 +00:00
|
|
|
transferTx.Cosigners = []transaction.Cosigner{
|
|
|
|
{
|
|
|
|
Account: priv0ScriptHash,
|
|
|
|
Scopes: transaction.CalledByEntry,
|
|
|
|
AllowedContracts: nil,
|
|
|
|
AllowedGroups: nil,
|
|
|
|
},
|
|
|
|
}
|
2020-05-08 17:54:24 +00:00
|
|
|
require.NoError(t, addNetworkFee(bc, transferTx, acc0))
|
2020-04-16 14:10:42 +00:00
|
|
|
require.NoError(t, acc0.SignTx(transferTx))
|
2020-03-05 09:28:46 +00:00
|
|
|
|
2020-04-22 17:42:38 +00:00
|
|
|
b = bc.newBlock(initTx, transferTx)
|
2020-03-05 09:28:46 +00:00
|
|
|
require.NoError(t, bc.AddBlock(b))
|
2020-05-27 20:28:20 +00:00
|
|
|
t.Logf("recieveRublesTx: %v", transferTx.Hash().StringLE())
|
2020-03-05 09:28:46 +00:00
|
|
|
|
|
|
|
transferTx = newNEP5Transfer(sh, priv0.GetScriptHash(), priv1.GetScriptHash(), 123)
|
2020-04-10 10:41:49 +00:00
|
|
|
transferTx.Nonce = getNextNonce()
|
2020-04-15 06:50:13 +00:00
|
|
|
transferTx.ValidUntilBlock = validUntilBlock
|
2020-04-16 14:10:42 +00:00
|
|
|
transferTx.Sender = priv0ScriptHash
|
2020-05-19 07:19:05 +00:00
|
|
|
transferTx.Cosigners = []transaction.Cosigner{
|
|
|
|
{
|
|
|
|
Account: priv0ScriptHash,
|
|
|
|
Scopes: transaction.CalledByEntry,
|
|
|
|
AllowedContracts: nil,
|
|
|
|
AllowedGroups: nil,
|
|
|
|
},
|
|
|
|
}
|
2020-05-08 17:54:24 +00:00
|
|
|
require.NoError(t, addNetworkFee(bc, transferTx, acc0))
|
2020-04-16 14:10:42 +00:00
|
|
|
require.NoError(t, acc0.SignTx(transferTx))
|
|
|
|
|
2020-04-22 17:42:38 +00:00
|
|
|
b = bc.newBlock(transferTx)
|
2020-03-05 09:28:46 +00:00
|
|
|
require.NoError(t, bc.AddBlock(b))
|
2020-05-27 20:28:20 +00:00
|
|
|
t.Logf("sendRublesTx: %v", transferTx.Hash().StringLE())
|
2020-03-05 09:28:46 +00:00
|
|
|
|
2020-03-04 14:45:29 +00:00
|
|
|
if saveChain {
|
|
|
|
outStream, err := os.Create(prefix + "testblocks.acc")
|
2020-02-29 15:55:16 +00:00
|
|
|
require.NoError(t, err)
|
2020-03-04 14:45:29 +00:00
|
|
|
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)
|
|
|
|
require.NoError(t, err)
|
2020-03-26 14:43:24 +00:00
|
|
|
bytes, err := testserdes.EncodeBinary(b)
|
|
|
|
require.NoError(t, err)
|
2020-03-04 14:45:29 +00:00
|
|
|
writer.WriteU32LE(uint32(len(bytes)))
|
|
|
|
writer.WriteBytes(bytes)
|
|
|
|
require.NoError(t, writer.Err)
|
|
|
|
}
|
2020-02-17 12:04:25 +00:00
|
|
|
}
|
2020-03-02 17:01:32 +00:00
|
|
|
|
2020-04-10 10:41:49 +00:00
|
|
|
// Make a NEO roundtrip (send to myself) and claim GAS.
|
|
|
|
txNeoRound = transaction.NewContractTX()
|
|
|
|
txNeoRound.Nonce = getNextNonce()
|
2020-04-15 06:50:13 +00:00
|
|
|
txNeoRound.ValidUntilBlock = validUntilBlock
|
2020-04-16 14:10:42 +00:00
|
|
|
txNeoRound.Sender = priv0ScriptHash
|
2020-04-10 10:41:49 +00:00
|
|
|
txNeoRound.AddInput(&transaction.Input{
|
|
|
|
PrevHash: txNeo0to1.Hash(),
|
|
|
|
PrevIndex: 1,
|
|
|
|
})
|
|
|
|
txNeoRound.AddOutput(&transaction.Output{
|
|
|
|
AssetID: GoverningTokenID(),
|
|
|
|
Amount: neoAmount - util.Fixed8FromInt64(1000),
|
|
|
|
ScriptHash: priv0.GetScriptHash(),
|
|
|
|
Position: 0,
|
|
|
|
})
|
|
|
|
txNeoRound.Data = new(transaction.ContractTX)
|
2020-05-08 17:54:24 +00:00
|
|
|
require.NoError(t, addNetworkFee(bc, txNeoRound, acc0))
|
2020-04-10 10:41:49 +00:00
|
|
|
require.NoError(t, acc0.SignTx(txNeoRound))
|
|
|
|
bw := io.NewBufBinWriter()
|
|
|
|
txNeoRound.EncodeBinary(bw.BinWriter)
|
|
|
|
t.Logf("sendrawtransaction: %s", hex.EncodeToString(bw.Bytes()))
|
2020-02-17 12:04:25 +00:00
|
|
|
}
|
2020-03-05 09:28:46 +00:00
|
|
|
|
|
|
|
func newNEP5Transfer(sc, from, to util.Uint160, amount int64) *transaction.Transaction {
|
|
|
|
w := io.NewBufBinWriter()
|
2020-03-27 08:00:06 +00:00
|
|
|
emit.AppCallWithOperationAndArgs(w.BinWriter, sc, "transfer", from, to, amount)
|
2020-05-06 12:54:18 +00:00
|
|
|
emit.Opcode(w.BinWriter, opcode.ASSERT)
|
2020-03-05 09:28:46 +00:00
|
|
|
|
|
|
|
script := w.Bytes()
|
|
|
|
return transaction.NewInvocationTX(script, 0)
|
|
|
|
}
|
2020-04-16 14:10:42 +00:00
|
|
|
|
|
|
|
func addSender(txs ...*transaction.Transaction) error {
|
|
|
|
for _, tx := range txs {
|
2020-04-21 15:09:34 +00:00
|
|
|
tx.Sender = neoOwner
|
2020-04-16 14:10:42 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func signTx(bc *Blockchain, txs ...*transaction.Transaction) error {
|
|
|
|
validators, err := getValidators(bc.config)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "fail to sign tx")
|
|
|
|
}
|
|
|
|
rawScript, err := smartcontract.CreateMultiSigRedeemScript(len(bc.config.StandbyValidators)/2+1, validators)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "fail to sign tx")
|
|
|
|
}
|
|
|
|
for _, tx := range txs {
|
2020-05-08 17:54:24 +00:00
|
|
|
size := io.GetVarSize(tx)
|
|
|
|
netFee, sizeDelta := CalculateNetworkFee(rawScript)
|
|
|
|
tx.NetworkFee = tx.NetworkFee.Add(netFee)
|
|
|
|
size += sizeDelta
|
|
|
|
tx.NetworkFee = tx.NetworkFee.Add(util.Fixed8(int64(size) * int64(bc.FeePerByte())))
|
2020-04-16 14:10:42 +00:00
|
|
|
data := tx.GetSignedPart()
|
|
|
|
tx.Scripts = []transaction.Witness{{
|
2020-04-23 13:11:40 +00:00
|
|
|
InvocationScript: testchain.Sign(data),
|
2020-04-16 14:10:42 +00:00
|
|
|
VerificationScript: rawScript,
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2020-05-08 17:54:24 +00:00
|
|
|
|
|
|
|
func addNetworkFee(bc *Blockchain, tx *transaction.Transaction, sender *wallet.Account) error {
|
|
|
|
size := io.GetVarSize(tx)
|
|
|
|
netFee, sizeDelta := CalculateNetworkFee(sender.Contract.Script)
|
|
|
|
tx.NetworkFee += netFee
|
|
|
|
size += sizeDelta
|
|
|
|
for _, cosigner := range tx.Cosigners {
|
|
|
|
contract := bc.GetContractState(cosigner.Account)
|
|
|
|
if contract != nil {
|
|
|
|
netFee, sizeDelta = CalculateNetworkFee(contract.Script)
|
|
|
|
tx.NetworkFee += netFee
|
|
|
|
size += sizeDelta
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tx.NetworkFee += util.Fixed8(int64(size) * int64(bc.FeePerByte()))
|
|
|
|
return nil
|
|
|
|
}
|