core: refactor out Block, BlockBase and Header structs

See #597.
This commit is contained in:
Evgenii Stratonikov 2020-01-14 15:32:07 +03:00
parent 54d3880b93
commit 63c56cca5c
30 changed files with 190 additions and 106 deletions

View file

@ -8,6 +8,7 @@ import (
"github.com/CityOfZion/neo-go/config" "github.com/CityOfZion/neo-go/config"
"github.com/CityOfZion/neo-go/pkg/core" "github.com/CityOfZion/neo-go/pkg/core"
"github.com/CityOfZion/neo-go/pkg/core/block"
"github.com/CityOfZion/neo-go/pkg/core/storage" "github.com/CityOfZion/neo-go/pkg/core/storage"
"github.com/CityOfZion/neo-go/pkg/encoding/address" "github.com/CityOfZion/neo-go/pkg/encoding/address"
"github.com/CityOfZion/neo-go/pkg/io" "github.com/CityOfZion/neo-go/pkg/io"
@ -268,7 +269,7 @@ func restoreDB(ctx *cli.Context) error {
} }
for ; i < skip+count; i++ { for ; i < skip+count; i++ {
bytes, err := readBlock(reader) bytes, err := readBlock(reader)
block := &core.Block{} block := &block.Block{}
newReader := io.NewBinReaderFromBuf(bytes) newReader := io.NewBinReaderFromBuf(bytes)
block.DecodeBinary(newReader) block.DecodeBinary(newReader)
if err != nil { if err != nil {

View file

@ -1,7 +1,7 @@
package consensus package consensus
import ( import (
"github.com/CityOfZion/neo-go/pkg/core" coreb "github.com/CityOfZion/neo-go/pkg/core/block"
"github.com/CityOfZion/neo-go/pkg/core/transaction" "github.com/CityOfZion/neo-go/pkg/core/transaction"
"github.com/CityOfZion/neo-go/pkg/util" "github.com/CityOfZion/neo-go/pkg/util"
"github.com/nspcc-dev/dbft/block" "github.com/nspcc-dev/dbft/block"
@ -11,7 +11,7 @@ import (
// neoBlock is a wrapper of core.Block which implements // neoBlock is a wrapper of core.Block which implements
// methods necessary for dBFT library. // methods necessary for dBFT library.
type neoBlock struct { type neoBlock struct {
core.Block coreb.Block
signature []byte signature []byte
} }

View file

@ -8,6 +8,7 @@ import (
"github.com/CityOfZion/neo-go/config" "github.com/CityOfZion/neo-go/config"
"github.com/CityOfZion/neo-go/pkg/core" "github.com/CityOfZion/neo-go/pkg/core"
coreb "github.com/CityOfZion/neo-go/pkg/core/block"
"github.com/CityOfZion/neo-go/pkg/core/transaction" "github.com/CityOfZion/neo-go/pkg/core/transaction"
"github.com/CityOfZion/neo-go/pkg/crypto/hash" "github.com/CityOfZion/neo-go/pkg/crypto/hash"
"github.com/CityOfZion/neo-go/pkg/crypto/keys" "github.com/CityOfZion/neo-go/pkg/crypto/keys"
@ -67,7 +68,7 @@ type Config struct {
Broadcast func(p *Payload) Broadcast func(p *Payload)
// RelayBlock is a callback that is called to notify server // RelayBlock is a callback that is called to notify server
// about the new block that needs to be broadcasted. // about the new block that needs to be broadcasted.
RelayBlock func(b *core.Block) RelayBlock func(b *coreb.Block)
// Chain is a core.Blockchainer instance. // Chain is a core.Blockchainer instance.
Chain core.Blockchainer Chain core.Blockchainer
// RequestTx is a callback to which will be called // RequestTx is a callback to which will be called
@ -285,7 +286,7 @@ func (s *service) processBlock(b block.Block) {
} }
} }
func (s *service) getBlockWitness(b *core.Block) *transaction.Witness { func (s *service) getBlockWitness(b *coreb.Block) *transaction.Witness {
dctx := s.dbft.Context dctx := s.dbft.Context
pubs := convertKeys(dctx.Validators) pubs := convertKeys(dctx.Validators)
sigs := make(map[*keys.PublicKey][]byte) sigs := make(map[*keys.PublicKey][]byte)

View file

@ -1,4 +1,4 @@
package core package block
import ( import (
"errors" "errors"
@ -39,8 +39,8 @@ func merkleTreeFromTransactions(txes []*transaction.Transaction) (*hash.MerkleTr
return hash.NewMerkleTree(hashes) return hash.NewMerkleTree(hashes)
} }
// rebuildMerkleRoot rebuilds the merkleroot of the block. // RebuildMerkleRoot rebuilds the merkleroot of the block.
func (b *Block) rebuildMerkleRoot() error { func (b *Block) RebuildMerkleRoot() error {
merkle, err := merkleTreeFromTransactions(b.Transactions) merkle, err := merkleTreeFromTransactions(b.Transactions)
if err != nil { if err != nil {
return err return err

View file

@ -1,4 +1,4 @@
package core package block
import ( import (
"fmt" "fmt"

View file

@ -1,4 +1,4 @@
package core package block
import ( import (
"encoding/hex" "encoding/hex"
@ -108,21 +108,21 @@ func TestHashBlockEqualsHashHeader(t *testing.T) {
func TestBlockVerify(t *testing.T) { func TestBlockVerify(t *testing.T) {
block := newDumbBlock() block := newDumbBlock()
assert.NotNil(t, block.Verify()) assert.NotNil(t, block.Verify())
assert.Nil(t, block.rebuildMerkleRoot()) assert.Nil(t, block.RebuildMerkleRoot())
assert.Nil(t, block.Verify()) assert.Nil(t, block.Verify())
block.Transactions = []*transaction.Transaction{ block.Transactions = []*transaction.Transaction{
{Type: transaction.IssueType}, {Type: transaction.IssueType},
{Type: transaction.MinerType}, {Type: transaction.MinerType},
} }
assert.NoError(t, block.rebuildMerkleRoot()) assert.NoError(t, block.RebuildMerkleRoot())
assert.NotNil(t, block.Verify()) assert.NotNil(t, block.Verify())
block.Transactions = []*transaction.Transaction{ block.Transactions = []*transaction.Transaction{
{Type: transaction.MinerType}, {Type: transaction.MinerType},
{Type: transaction.MinerType}, {Type: transaction.MinerType},
} }
assert.NoError(t, block.rebuildMerkleRoot()) assert.NoError(t, block.RebuildMerkleRoot())
assert.NotNil(t, block.Verify()) assert.NotNil(t, block.Verify())
block.Transactions = []*transaction.Transaction{ block.Transactions = []*transaction.Transaction{
{Type: transaction.MinerType}, {Type: transaction.MinerType},

View file

@ -1,4 +1,4 @@
package core package block
import ( import (
"fmt" "fmt"

View file

@ -1,4 +1,4 @@
package core package block
import ( import (
"testing" "testing"

View file

@ -0,0 +1,44 @@
package block
import (
"encoding/hex"
"encoding/json"
"fmt"
"io/ioutil"
"testing"
"github.com/CityOfZion/neo-go/pkg/io"
)
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{}
r := io.NewBinReaderFromBuf(b)
block.DecodeBinary(r)
if r.Err != nil {
t.Fatal(r.Err)
}
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
}

View file

@ -11,6 +11,7 @@ import (
"time" "time"
"github.com/CityOfZion/neo-go/config" "github.com/CityOfZion/neo-go/config"
"github.com/CityOfZion/neo-go/pkg/core/block"
"github.com/CityOfZion/neo-go/pkg/core/state" "github.com/CityOfZion/neo-go/pkg/core/state"
"github.com/CityOfZion/neo-go/pkg/core/storage" "github.com/CityOfZion/neo-go/pkg/core/storage"
"github.com/CityOfZion/neo-go/pkg/core/transaction" "github.com/CityOfZion/neo-go/pkg/core/transaction"
@ -179,7 +180,7 @@ func (bc *Blockchain) init() error {
targetHash = genesisBlock.Hash() targetHash = genesisBlock.Hash()
bc.headerList.Add(targetHash) bc.headerList.Add(targetHash)
} }
headers := make([]*Header, 0) headers := make([]*block.Header, 0)
for hash != targetHash { for hash != targetHash {
header, err := bc.GetHeader(hash) header, err := bc.GetHeader(hash)
@ -242,7 +243,7 @@ func (bc *Blockchain) Close() {
// AddBlock accepts successive block for the Blockchain, verifies it and // AddBlock accepts successive block for the Blockchain, verifies it and
// stores internally. Eventually it will be persisted to the backing storage. // stores internally. Eventually it will be persisted to the backing storage.
func (bc *Blockchain) AddBlock(block *Block) error { func (bc *Blockchain) AddBlock(block *block.Block) error {
expectedHeight := bc.BlockHeight() + 1 expectedHeight := bc.BlockHeight() + 1
if expectedHeight != block.Index { if expectedHeight != block.Index {
return fmt.Errorf("expected block %d, but passed block %d", expectedHeight, block.Index) return fmt.Errorf("expected block %d, but passed block %d", expectedHeight, block.Index)
@ -276,7 +277,7 @@ func (bc *Blockchain) AddBlock(block *Block) error {
// AddHeaders processes the given headers and add them to the // AddHeaders processes the given headers and add them to the
// HeaderHashList. // HeaderHashList.
func (bc *Blockchain) AddHeaders(headers ...*Header) (err error) { func (bc *Blockchain) AddHeaders(headers ...*block.Header) (err error) {
var ( var (
start = time.Now() start = time.Now()
batch = bc.dao.store.Batch() batch = bc.dao.store.Batch()
@ -321,7 +322,7 @@ func (bc *Blockchain) AddHeaders(headers ...*Header) (err error) {
// processHeader processes the given header. Note that this is only thread safe // processHeader processes the given header. Note that this is only thread safe
// if executed in headers operation. // if executed in headers operation.
func (bc *Blockchain) processHeader(h *Header, batch storage.Batch, headerList *HeaderHashList) error { func (bc *Blockchain) processHeader(h *block.Header, batch storage.Batch, headerList *HeaderHashList) error {
headerList.Add(h.Hash()) headerList.Add(h.Hash())
buf := io.NewBufBinWriter() buf := io.NewBufBinWriter()
@ -352,7 +353,7 @@ func (bc *Blockchain) processHeader(h *Header, batch storage.Batch, headerList *
// project. This for the sake of development speed and understanding of what // project. This for the sake of development speed and understanding of what
// is happening here, quite allot as you can see :). If things are wired together // is happening here, quite allot as you can see :). If things are wired together
// and all tests are in place, we can make a more optimized and cleaner implementation. // and all tests are in place, we can make a more optimized and cleaner implementation.
func (bc *Blockchain) storeBlock(block *Block) error { func (bc *Blockchain) storeBlock(block *block.Block) error {
cache := newCachedDao(bc.dao.store) cache := newCachedDao(bc.dao.store)
if err := cache.StoreAsBlock(block, 0); err != nil { if err := cache.StoreAsBlock(block, 0); err != nil {
return err return err
@ -756,10 +757,10 @@ func (bc *Blockchain) GetStorageItems(hash util.Uint160) (map[string]*state.Stor
} }
// GetBlock returns a Block by the given hash. // GetBlock returns a Block by the given hash.
func (bc *Blockchain) GetBlock(hash util.Uint256) (*Block, error) { func (bc *Blockchain) GetBlock(hash util.Uint256) (*block.Block, error) {
topBlock := bc.topBlock.Load() topBlock := bc.topBlock.Load()
if topBlock != nil { if topBlock != nil {
if tb, ok := topBlock.(*Block); ok && tb.Hash().Equals(hash) { if tb, ok := topBlock.(*block.Block); ok && tb.Hash().Equals(hash) {
return tb, nil return tb, nil
} }
} }
@ -782,10 +783,10 @@ func (bc *Blockchain) GetBlock(hash util.Uint256) (*Block, error) {
} }
// GetHeader returns data block header identified with the given hash value. // GetHeader returns data block header identified with the given hash value.
func (bc *Blockchain) GetHeader(hash util.Uint256) (*Header, error) { func (bc *Blockchain) GetHeader(hash util.Uint256) (*block.Header, error) {
topBlock := bc.topBlock.Load() topBlock := bc.topBlock.Load()
if topBlock != nil { if topBlock != nil {
if tb, ok := topBlock.(*Block); ok && tb.Hash().Equals(hash) { if tb, ok := topBlock.(*block.Block); ok && tb.Hash().Equals(hash) {
return tb.Header(), nil return tb.Header(), nil
} }
} }
@ -955,7 +956,7 @@ func (bc *Blockchain) GetMemPool() MemPool {
} }
// VerifyBlock verifies block against its current state. // VerifyBlock verifies block against its current state.
func (bc *Blockchain) VerifyBlock(block *Block) error { func (bc *Blockchain) VerifyBlock(block *block.Block) error {
prevHeader, err := bc.GetHeader(block.PrevHash) prevHeader, err := bc.GetHeader(block.PrevHash)
if err != nil { if err != nil {
return errors.Wrap(err, "unable to get previous header") return errors.Wrap(err, "unable to get previous header")
@ -973,7 +974,7 @@ func (bc *Blockchain) VerifyBlock(block *Block) error {
// is used for easy interop access and can be omitted for transactions that are // is used for easy interop access and can be omitted for transactions that are
// not yet added into any block. // not yet added into any block.
// Golang implementation of Verify method in C# (https://github.com/neo-project/neo/blob/master/neo/Network/P2P/Payloads/Transaction.cs#L270). // Golang implementation of Verify method in C# (https://github.com/neo-project/neo/blob/master/neo/Network/P2P/Payloads/Transaction.cs#L270).
func (bc *Blockchain) VerifyTx(t *transaction.Transaction, block *Block) error { func (bc *Blockchain) VerifyTx(t *transaction.Transaction, block *block.Block) error {
if io.GetVarSize(t) > transaction.MaxTransactionSize { if io.GetVarSize(t) > transaction.MaxTransactionSize {
return errors.Errorf("invalid transaction size = %d. It shoud be less then MaxTransactionSize = %d", io.GetVarSize(t), transaction.MaxTransactionSize) return errors.Errorf("invalid transaction size = %d. It shoud be less then MaxTransactionSize = %d", io.GetVarSize(t), transaction.MaxTransactionSize)
} }
@ -1440,7 +1441,7 @@ func (bc *Blockchain) verifyHashAgainstScript(hash util.Uint160, witness *transa
// not yet added into any block. // not yet added into any block.
// Golang implementation of VerifyWitnesses method in C# (https://github.com/neo-project/neo/blob/master/neo/SmartContract/Helper.cs#L87). // Golang implementation of VerifyWitnesses method in C# (https://github.com/neo-project/neo/blob/master/neo/SmartContract/Helper.cs#L87).
// Unfortunately the IVerifiable interface could not be implemented because we can't move the References method in blockchain.go to the transaction.go file. // Unfortunately the IVerifiable interface could not be implemented because we can't move the References method in blockchain.go to the transaction.go file.
func (bc *Blockchain) verifyTxWitnesses(t *transaction.Transaction, block *Block) error { func (bc *Blockchain) verifyTxWitnesses(t *transaction.Transaction, block *block.Block) error {
hashes, err := bc.GetScriptHashesForVerifying(t) hashes, err := bc.GetScriptHashesForVerifying(t)
if err != nil { if err != nil {
return err return err
@ -1465,7 +1466,7 @@ func (bc *Blockchain) verifyTxWitnesses(t *transaction.Transaction, block *Block
} }
// verifyBlockWitnesses is a block-specific implementation of VerifyWitnesses logic. // verifyBlockWitnesses is a block-specific implementation of VerifyWitnesses logic.
func (bc *Blockchain) verifyBlockWitnesses(block *Block, prevHeader *Header) error { func (bc *Blockchain) verifyBlockWitnesses(block *block.Block, prevHeader *block.Header) error {
var hash util.Uint160 var hash util.Uint160
if prevHeader == nil && block.PrevHash.Equals(util.Uint256{}) { if prevHeader == nil && block.PrevHash.Equals(util.Uint256{}) {
hash = block.Script.ScriptHash() hash = block.Script.ScriptHash()
@ -1487,6 +1488,6 @@ func (bc *Blockchain) secondsPerBlock() int {
return bc.config.SecondsPerBlock return bc.config.SecondsPerBlock
} }
func (bc *Blockchain) newInteropContext(trigger byte, s storage.Store, block *Block, tx *transaction.Transaction) *interopContext { func (bc *Blockchain) newInteropContext(trigger byte, s storage.Store, block *block.Block, tx *transaction.Transaction) *interopContext {
return newInteropContext(trigger, bc, s, block, tx, bc.log) return newInteropContext(trigger, bc, s, block, tx, bc.log)
} }

View file

@ -3,6 +3,7 @@ package core
import ( import (
"testing" "testing"
"github.com/CityOfZion/neo-go/pkg/core/block"
"github.com/CityOfZion/neo-go/pkg/core/storage" "github.com/CityOfZion/neo-go/pkg/core/storage"
"github.com/CityOfZion/neo-go/pkg/core/transaction" "github.com/CityOfZion/neo-go/pkg/core/transaction"
"github.com/CityOfZion/neo-go/pkg/crypto/hash" "github.com/CityOfZion/neo-go/pkg/crypto/hash"
@ -38,7 +39,7 @@ func TestAddHeaders(t *testing.T) {
func TestAddBlock(t *testing.T) { func TestAddBlock(t *testing.T) {
bc := newTestChain(t) bc := newTestChain(t)
blocks := []*Block{ blocks := []*block.Block{
newBlock(1, newMinerTX()), newBlock(1, newMinerTX()),
newBlock(2, newMinerTX()), newBlock(2, newMinerTX()),
newBlock(3, newMinerTX()), newBlock(3, newMinerTX()),

View file

@ -2,6 +2,7 @@ package core
import ( import (
"github.com/CityOfZion/neo-go/config" "github.com/CityOfZion/neo-go/config"
"github.com/CityOfZion/neo-go/pkg/core/block"
"github.com/CityOfZion/neo-go/pkg/core/state" "github.com/CityOfZion/neo-go/pkg/core/state"
"github.com/CityOfZion/neo-go/pkg/core/storage" "github.com/CityOfZion/neo-go/pkg/core/storage"
"github.com/CityOfZion/neo-go/pkg/core/transaction" "github.com/CityOfZion/neo-go/pkg/core/transaction"
@ -14,15 +15,15 @@ import (
// of the blockchain. // of the blockchain.
type Blockchainer interface { type Blockchainer interface {
GetConfig() config.ProtocolConfiguration GetConfig() config.ProtocolConfiguration
AddHeaders(...*Header) error AddHeaders(...*block.Header) error
AddBlock(*Block) error AddBlock(*block.Block) error
BlockHeight() uint32 BlockHeight() uint32
Close() Close()
HeaderHeight() uint32 HeaderHeight() uint32
GetBlock(hash util.Uint256) (*Block, error) GetBlock(hash util.Uint256) (*block.Block, error)
GetContractState(hash util.Uint160) *state.Contract GetContractState(hash util.Uint160) *state.Contract
GetHeaderHash(int) util.Uint256 GetHeaderHash(int) util.Uint256
GetHeader(hash util.Uint256) (*Header, error) GetHeader(hash util.Uint256) (*block.Header, error)
CurrentHeaderHash() util.Uint256 CurrentHeaderHash() util.Uint256
CurrentBlockHash() util.Uint256 CurrentBlockHash() util.Uint256
HasBlock(util.Uint256) bool HasBlock(util.Uint256) bool
@ -38,6 +39,6 @@ type Blockchainer interface {
GetUnspentCoinState(util.Uint256) *UnspentCoinState GetUnspentCoinState(util.Uint256) *UnspentCoinState
References(t *transaction.Transaction) map[transaction.Input]*transaction.Output References(t *transaction.Transaction) map[transaction.Input]*transaction.Output
Feer // fee interface Feer // fee interface
VerifyTx(*transaction.Transaction, *Block) error VerifyTx(*transaction.Transaction, *block.Block) error
GetMemPool() MemPool GetMemPool() MemPool
} }

View file

@ -6,6 +6,7 @@ import (
"fmt" "fmt"
"sort" "sort"
"github.com/CityOfZion/neo-go/pkg/core/block"
"github.com/CityOfZion/neo-go/pkg/core/state" "github.com/CityOfZion/neo-go/pkg/core/state"
"github.com/CityOfZion/neo-go/pkg/core/storage" "github.com/CityOfZion/neo-go/pkg/core/storage"
"github.com/CityOfZion/neo-go/pkg/core/transaction" "github.com/CityOfZion/neo-go/pkg/core/transaction"
@ -358,13 +359,13 @@ func makeStorageItemKey(scripthash util.Uint160, key []byte) []byte {
// -- other. // -- other.
// GetBlock returns Block by the given hash if it exists in the store. // GetBlock returns Block by the given hash if it exists in the store.
func (dao *dao) GetBlock(hash util.Uint256) (*Block, error) { func (dao *dao) GetBlock(hash util.Uint256) (*block.Block, error) {
key := storage.AppendPrefix(storage.DataBlock, hash.BytesLE()) key := storage.AppendPrefix(storage.DataBlock, hash.BytesLE())
b, err := dao.store.Get(key) b, err := dao.store.Get(key)
if err != nil { if err != nil {
return nil, err return nil, err
} }
block, err := NewBlockFromTrimmedBytes(b) block, err := block.NewBlockFromTrimmedBytes(b)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -486,7 +487,7 @@ func (dao *dao) HasTransaction(hash util.Uint256) bool {
} }
// StoreAsBlock stores the given block as DataBlock. // StoreAsBlock stores the given block as DataBlock.
func (dao *dao) StoreAsBlock(block *Block, sysFee uint32) error { func (dao *dao) StoreAsBlock(block *block.Block, sysFee uint32) error {
var ( var (
key = storage.AppendPrefix(storage.DataBlock, block.Hash().BytesLE()) key = storage.AppendPrefix(storage.DataBlock, block.Hash().BytesLE())
buf = io.NewBufBinWriter() buf = io.NewBufBinWriter()
@ -505,7 +506,7 @@ func (dao *dao) StoreAsBlock(block *Block, sysFee uint32) error {
} }
// StoreAsCurrentBlock stores the given block witch prefix SYSCurrentBlock. // StoreAsCurrentBlock stores the given block witch prefix SYSCurrentBlock.
func (dao *dao) StoreAsCurrentBlock(block *Block) error { func (dao *dao) StoreAsCurrentBlock(block *block.Block) error {
buf := io.NewBufBinWriter() buf := io.NewBufBinWriter()
h := block.Hash() h := block.Hash()
h.EncodeBinary(buf.BinWriter) h.EncodeBinary(buf.BinWriter)

View file

@ -3,6 +3,7 @@ package core
import ( import (
"testing" "testing"
"github.com/CityOfZion/neo-go/pkg/core/block"
"github.com/CityOfZion/neo-go/pkg/core/state" "github.com/CityOfZion/neo-go/pkg/core/state"
"github.com/CityOfZion/neo-go/pkg/core/storage" "github.com/CityOfZion/neo-go/pkg/core/storage"
"github.com/CityOfZion/neo-go/pkg/core/transaction" "github.com/CityOfZion/neo-go/pkg/core/transaction"
@ -260,16 +261,16 @@ func TestGetBlock_NotExists(t *testing.T) {
func TestPutGetBlock(t *testing.T) { func TestPutGetBlock(t *testing.T) {
dao := newDao(storage.NewMemoryStore()) dao := newDao(storage.NewMemoryStore())
block := &Block{ b := &block.Block{
BlockBase: BlockBase{ BlockBase: block.BlockBase{
Script: transaction.Witness{ Script: transaction.Witness{
VerificationScript: []byte{byte(opcode.PUSH1)}, VerificationScript: []byte{byte(opcode.PUSH1)},
InvocationScript: []byte{byte(opcode.NOP)}, InvocationScript: []byte{byte(opcode.NOP)},
}, },
}, },
} }
hash := block.Hash() hash := b.Hash()
err := dao.StoreAsBlock(block, 0) err := dao.StoreAsBlock(b, 0)
require.NoError(t, err) require.NoError(t, err)
gotBlock, err := dao.GetBlock(hash) gotBlock, err := dao.GetBlock(hash)
require.NoError(t, err) require.NoError(t, err)
@ -301,15 +302,15 @@ func TestGetCurrentHeaderHeight_NoHeader(t *testing.T) {
func TestGetCurrentHeaderHeight_Store(t *testing.T) { func TestGetCurrentHeaderHeight_Store(t *testing.T) {
dao := newDao(storage.NewMemoryStore()) dao := newDao(storage.NewMemoryStore())
block := &Block{ b := &block.Block{
BlockBase: BlockBase{ BlockBase: block.BlockBase{
Script: transaction.Witness{ Script: transaction.Witness{
VerificationScript: []byte{byte(opcode.PUSH1)}, VerificationScript: []byte{byte(opcode.PUSH1)},
InvocationScript: []byte{byte(opcode.NOP)}, InvocationScript: []byte{byte(opcode.NOP)},
}, },
}, },
} }
err := dao.StoreAsCurrentBlock(block) err := dao.StoreAsCurrentBlock(b)
require.NoError(t, err) require.NoError(t, err)
height, err := dao.GetCurrentBlockHeight() height, err := dao.GetCurrentBlockHeight()
require.NoError(t, err) require.NoError(t, err)

View file

@ -9,8 +9,10 @@ import (
"time" "time"
"github.com/CityOfZion/neo-go/config" "github.com/CityOfZion/neo-go/config"
"github.com/CityOfZion/neo-go/pkg/core/block"
"github.com/CityOfZion/neo-go/pkg/core/storage" "github.com/CityOfZion/neo-go/pkg/core/storage"
"github.com/CityOfZion/neo-go/pkg/core/transaction" "github.com/CityOfZion/neo-go/pkg/core/transaction"
"github.com/CityOfZion/neo-go/pkg/crypto/hash"
"github.com/CityOfZion/neo-go/pkg/crypto/keys" "github.com/CityOfZion/neo-go/pkg/crypto/keys"
"github.com/CityOfZion/neo-go/pkg/io" "github.com/CityOfZion/neo-go/pkg/io"
"github.com/CityOfZion/neo-go/pkg/smartcontract" "github.com/CityOfZion/neo-go/pkg/smartcontract"
@ -49,7 +51,7 @@ func newTestChain(t *testing.T) *Blockchain {
return chain return chain
} }
func newBlock(index uint32, txs ...*transaction.Transaction) *Block { func newBlock(index uint32, txs ...*transaction.Transaction) *block.Block {
validators, _ := getValidators(unitTestNetCfg.ProtocolConfiguration) validators, _ := getValidators(unitTestNetCfg.ProtocolConfiguration)
vlen := len(validators) vlen := len(validators)
valScript, _ := smartcontract.CreateMultiSigRedeemScript( valScript, _ := smartcontract.CreateMultiSigRedeemScript(
@ -59,8 +61,8 @@ func newBlock(index uint32, txs ...*transaction.Transaction) *Block {
witness := transaction.Witness{ witness := transaction.Witness{
VerificationScript: valScript, VerificationScript: valScript,
} }
b := &Block{ b := &block.Block{
BlockBase: BlockBase{ BlockBase: block.BlockBase{
Version: 0, Version: 0,
PrevHash: newBlockPrevHash, PrevHash: newBlockPrevHash,
Timestamp: uint32(time.Now().UTC().Unix()) + index, Timestamp: uint32(time.Now().UTC().Unix()) + index,
@ -71,8 +73,7 @@ func newBlock(index uint32, txs ...*transaction.Transaction) *Block {
}, },
Transactions: txs, Transactions: txs,
} }
_ = b.rebuildMerkleRoot() _ = b.RebuildMerkleRoot()
b.createHash()
newBlockPrevHash = b.Hash() newBlockPrevHash = b.Hash()
invScript := make([]byte, 0) invScript := make([]byte, 0)
@ -93,8 +94,8 @@ func newBlock(index uint32, txs ...*transaction.Transaction) *Block {
return b return b
} }
func makeBlocks(n int) []*Block { func makeBlocks(n int) []*block.Block {
blocks := make([]*Block, n) blocks := make([]*block.Block, n)
for i := 0; i < n; i++ { for i := 0; i < n; i++ {
blocks[i] = newBlock(uint32(i+1), newMinerTX()) blocks[i] = newBlock(uint32(i+1), newMinerTX())
} }
@ -108,7 +109,7 @@ func newMinerTX() *transaction.Transaction {
} }
} }
func getDecodedBlock(t *testing.T, i int) *Block { func getDecodedBlock(t *testing.T, i int) *block.Block {
data, err := getBlockData(i) data, err := getBlockData(i)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
@ -119,7 +120,7 @@ func getDecodedBlock(t *testing.T, i int) *Block {
t.Fatal(err) t.Fatal(err)
} }
block := &Block{} block := &block.Block{}
r := io.NewBinReaderFromBuf(b) r := io.NewBinReaderFromBuf(b)
block.DecodeBinary(r) block.DecodeBinary(r)
if r.Err != nil { if r.Err != nil {
@ -140,3 +141,25 @@ func getBlockData(i int) (map[string]interface{}, error) {
} }
return data, err return data, err
} }
func newDumbBlock() *block.Block {
return &block.Block{
BlockBase: block.BlockBase{
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},
},
}
}

View file

@ -4,6 +4,7 @@ import (
"math/big" "math/big"
"testing" "testing"
"github.com/CityOfZion/neo-go/pkg/core/block"
"github.com/CityOfZion/neo-go/pkg/core/state" "github.com/CityOfZion/neo-go/pkg/core/state"
"github.com/CityOfZion/neo-go/pkg/core/storage" "github.com/CityOfZion/neo-go/pkg/core/storage"
"github.com/CityOfZion/neo-go/pkg/core/transaction" "github.com/CityOfZion/neo-go/pkg/core/transaction"
@ -451,7 +452,7 @@ func TestAssetGetPrecision(t *testing.T) {
// Helper functions to create VM, InteropContext, TX, Account, Contract, Asset. // Helper functions to create VM, InteropContext, TX, Account, Contract, Asset.
func createVMAndPushBlock(t *testing.T) (*vm.VM, *Block, *interopContext, *Blockchain) { func createVMAndPushBlock(t *testing.T) (*vm.VM, *block.Block, *interopContext, *Blockchain) {
v := vm.New() v := vm.New()
block := newDumbBlock() block := newDumbBlock()
chain := newTestChain(t) chain := newTestChain(t)

View file

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"math" "math"
"github.com/CityOfZion/neo-go/pkg/core/block"
"github.com/CityOfZion/neo-go/pkg/core/state" "github.com/CityOfZion/neo-go/pkg/core/state"
"github.com/CityOfZion/neo-go/pkg/core/transaction" "github.com/CityOfZion/neo-go/pkg/core/transaction"
"github.com/CityOfZion/neo-go/pkg/crypto/hash" "github.com/CityOfZion/neo-go/pkg/crypto/hash"
@ -132,11 +133,11 @@ func (ic *interopContext) bcGetTransactionHeight(v *vm.VM) error {
// popHeaderFromVM returns pointer to Header or error. It's main feature is // popHeaderFromVM returns pointer to Header or error. It's main feature is
// proper treatment of Block structure, because C# code implicitly assumes // proper treatment of Block structure, because C# code implicitly assumes
// that header APIs can also operate on blocks. // that header APIs can also operate on blocks.
func popHeaderFromVM(v *vm.VM) (*Header, error) { func popHeaderFromVM(v *vm.VM) (*block.Header, error) {
iface := v.Estack().Pop().Value() iface := v.Estack().Pop().Value()
header, ok := iface.(*Header) header, ok := iface.(*block.Header)
if !ok { if !ok {
block, ok := iface.(*Block) block, ok := iface.(*block.Block)
if !ok { if !ok {
return nil, errors.New("value is not a header or block") return nil, errors.New("value is not a header or block")
} }
@ -188,7 +189,7 @@ func (ic *interopContext) headerGetTimestamp(v *vm.VM) error {
// blockGetTransactionCount returns transactions count in the given block. // blockGetTransactionCount returns transactions count in the given block.
func (ic *interopContext) blockGetTransactionCount(v *vm.VM) error { func (ic *interopContext) blockGetTransactionCount(v *vm.VM) error {
blockInterface := v.Estack().Pop().Value() blockInterface := v.Estack().Pop().Value()
block, ok := blockInterface.(*Block) block, ok := blockInterface.(*block.Block)
if !ok { if !ok {
return errors.New("value is not a block") return errors.New("value is not a block")
} }
@ -199,7 +200,7 @@ func (ic *interopContext) blockGetTransactionCount(v *vm.VM) error {
// blockGetTransactions returns transactions from the given block. // blockGetTransactions returns transactions from the given block.
func (ic *interopContext) blockGetTransactions(v *vm.VM) error { func (ic *interopContext) blockGetTransactions(v *vm.VM) error {
blockInterface := v.Estack().Pop().Value() blockInterface := v.Estack().Pop().Value()
block, ok := blockInterface.(*Block) block, ok := blockInterface.(*block.Block)
if !ok { if !ok {
return errors.New("value is not a block") return errors.New("value is not a block")
} }
@ -218,7 +219,7 @@ func (ic *interopContext) blockGetTransactions(v *vm.VM) error {
// block. // block.
func (ic *interopContext) blockGetTransaction(v *vm.VM) error { func (ic *interopContext) blockGetTransaction(v *vm.VM) error {
blockInterface := v.Estack().Pop().Value() blockInterface := v.Estack().Pop().Value()
block, ok := blockInterface.(*Block) block, ok := blockInterface.(*block.Block)
if !ok { if !ok {
return errors.New("value is not a block") return errors.New("value is not a block")
} }
@ -359,7 +360,7 @@ func (ic *interopContext) runtimeLog(v *vm.VM) error {
// runtimeGetTime returns timestamp of the block being verified, or the latest // runtimeGetTime returns timestamp of the block being verified, or the latest
// one in the blockchain if no block is given to interopContext. // one in the blockchain if no block is given to interopContext.
func (ic *interopContext) runtimeGetTime(v *vm.VM) error { func (ic *interopContext) runtimeGetTime(v *vm.VM) error {
var header *Header var header *block.Header
if ic.block == nil { if ic.block == nil {
var err error var err error
header, err = ic.bc.GetHeader(ic.bc.CurrentBlockHash()) header, err = ic.bc.GetHeader(ic.bc.CurrentBlockHash())

View file

@ -10,6 +10,7 @@ package core
import ( import (
"sort" "sort"
"github.com/CityOfZion/neo-go/pkg/core/block"
"github.com/CityOfZion/neo-go/pkg/core/state" "github.com/CityOfZion/neo-go/pkg/core/state"
"github.com/CityOfZion/neo-go/pkg/core/storage" "github.com/CityOfZion/neo-go/pkg/core/storage"
"github.com/CityOfZion/neo-go/pkg/core/transaction" "github.com/CityOfZion/neo-go/pkg/core/transaction"
@ -20,14 +21,14 @@ import (
type interopContext struct { type interopContext struct {
bc Blockchainer bc Blockchainer
trigger byte trigger byte
block *Block block *block.Block
tx *transaction.Transaction tx *transaction.Transaction
dao *cachedDao dao *cachedDao
notifications []state.NotificationEvent notifications []state.NotificationEvent
log *zap.Logger log *zap.Logger
} }
func newInteropContext(trigger byte, bc Blockchainer, s storage.Store, block *Block, tx *transaction.Transaction, log *zap.Logger) *interopContext { func newInteropContext(trigger byte, bc Blockchainer, s storage.Store, block *block.Block, tx *transaction.Transaction, log *zap.Logger) *interopContext {
dao := newCachedDao(s) dao := newCachedDao(s)
nes := make([]state.NotificationEvent, 0) nes := make([]state.NotificationEvent, 0)
return &interopContext{bc, trigger, block, tx, dao, nes, log} return &interopContext{bc, trigger, block, tx, dao, nes, log}

View file

@ -4,6 +4,7 @@ import (
"time" "time"
"github.com/CityOfZion/neo-go/config" "github.com/CityOfZion/neo-go/config"
"github.com/CityOfZion/neo-go/pkg/core/block"
"github.com/CityOfZion/neo-go/pkg/core/transaction" "github.com/CityOfZion/neo-go/pkg/core/transaction"
"github.com/CityOfZion/neo-go/pkg/crypto/hash" "github.com/CityOfZion/neo-go/pkg/crypto/hash"
"github.com/CityOfZion/neo-go/pkg/crypto/keys" "github.com/CityOfZion/neo-go/pkg/crypto/keys"
@ -13,7 +14,7 @@ import (
) )
// createGenesisBlock creates a genesis block based on the given configuration. // createGenesisBlock creates a genesis block based on the given configuration.
func createGenesisBlock(cfg config.ProtocolConfiguration) (*Block, error) { func createGenesisBlock(cfg config.ProtocolConfiguration) (*block.Block, error) {
validators, err := getValidators(cfg) validators, err := getValidators(cfg)
if err != nil { if err != nil {
return nil, err return nil, err
@ -24,7 +25,7 @@ func createGenesisBlock(cfg config.ProtocolConfiguration) (*Block, error) {
return nil, err return nil, err
} }
base := BlockBase{ base := block.BlockBase{
Version: 0, Version: 0,
PrevHash: util.Uint256{}, PrevHash: util.Uint256{},
Timestamp: uint32(time.Date(2016, 7, 15, 15, 8, 21, 0, time.UTC).Unix()), Timestamp: uint32(time.Date(2016, 7, 15, 15, 8, 21, 0, time.UTC).Unix()),
@ -48,7 +49,7 @@ func createGenesisBlock(cfg config.ProtocolConfiguration) (*Block, error) {
} }
scriptOut := hash.Hash160(rawScript) scriptOut := hash.Hash160(rawScript)
block := &Block{ b := &block.Block{
BlockBase: base, BlockBase: base,
Transactions: []*transaction.Transaction{ Transactions: []*transaction.Transaction{
{ {
@ -84,11 +85,11 @@ func createGenesisBlock(cfg config.ProtocolConfiguration) (*Block, error) {
}, },
} }
if err = block.rebuildMerkleRoot(); err != nil { if err = b.RebuildMerkleRoot(); err != nil {
return nil, err return nil, err
} }
return block, nil return b, nil
} }
func governingTokenTX() *transaction.Transaction { func governingTokenTX() *transaction.Transaction {
@ -167,7 +168,7 @@ func calculateUtilityAmount() util.Fixed8 {
} }
// headerSliceReverse reverses the given slice of *Header. // headerSliceReverse reverses the given slice of *Header.
func headerSliceReverse(dest []*Header) { func headerSliceReverse(dest []*block.Header) {
for i, j := 0, len(dest)-1; i < j; i, j = i+1, j-1 { for i, j := 0, len(dest)-1; i < j; i, j = i+1, j-1 {
dest[i], dest[j] = dest[j], dest[i] dest[i], dest[j] = dest[j], dest[i]
} }

View file

@ -2,6 +2,7 @@ package network
import ( import (
"github.com/CityOfZion/neo-go/pkg/core" "github.com/CityOfZion/neo-go/pkg/core"
"github.com/CityOfZion/neo-go/pkg/core/block"
"github.com/Workiva/go-datastructures/queue" "github.com/Workiva/go-datastructures/queue"
"go.uber.org/zap" "go.uber.org/zap"
) )
@ -37,7 +38,7 @@ func (bq *blockQueue) run() {
if item == nil { if item == nil {
break break
} }
minblock := item.(*core.Block) minblock := item.(*block.Block)
if minblock.Index <= bq.chain.BlockHeight()+1 { if minblock.Index <= bq.chain.BlockHeight()+1 {
_, _ = bq.queue.Get(1) _, _ = bq.queue.Get(1)
updateBlockQueueLenMetric(bq.length()) updateBlockQueueLenMetric(bq.length())
@ -57,7 +58,7 @@ func (bq *blockQueue) run() {
} }
} }
func (bq *blockQueue) putBlock(block *core.Block) error { func (bq *blockQueue) putBlock(block *block.Block) error {
if bq.chain.BlockHeight() >= block.Index { if bq.chain.BlockHeight() >= block.Index {
// can easily happen when fetching the same blocks from // can easily happen when fetching the same blocks from
// different peers, thus not considered as error // different peers, thus not considered as error

View file

@ -4,7 +4,7 @@ import (
"testing" "testing"
"time" "time"
"github.com/CityOfZion/neo-go/pkg/core" "github.com/CityOfZion/neo-go/pkg/core/block"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"go.uber.org/zap/zaptest" "go.uber.org/zap/zaptest"
) )
@ -13,9 +13,9 @@ func TestBlockQueue(t *testing.T) {
chain := &testChain{} chain := &testChain{}
// notice, it's not yet running // notice, it's not yet running
bq := newBlockQueue(0, chain, zaptest.NewLogger(t)) bq := newBlockQueue(0, chain, zaptest.NewLogger(t))
blocks := make([]*core.Block, 11) blocks := make([]*block.Block, 11)
for i := 1; i < 11; i++ { for i := 1; i < 11; i++ {
blocks[i] = &core.Block{BlockBase: core.BlockBase{Index: uint32(i)}} blocks[i] = &block.Block{BlockBase: block.BlockBase{Index: uint32(i)}}
} }
// not the ones expected currently // not the ones expected currently
for i := 3; i < 5; i++ { for i := 3; i < 5; i++ {

View file

@ -9,6 +9,7 @@ import (
"github.com/CityOfZion/neo-go/config" "github.com/CityOfZion/neo-go/config"
"github.com/CityOfZion/neo-go/pkg/core" "github.com/CityOfZion/neo-go/pkg/core"
"github.com/CityOfZion/neo-go/pkg/core/block"
"github.com/CityOfZion/neo-go/pkg/core/state" "github.com/CityOfZion/neo-go/pkg/core/state"
"github.com/CityOfZion/neo-go/pkg/core/storage" "github.com/CityOfZion/neo-go/pkg/core/storage"
"github.com/CityOfZion/neo-go/pkg/core/transaction" "github.com/CityOfZion/neo-go/pkg/core/transaction"
@ -43,10 +44,10 @@ func (chain testChain) NetworkFee(t *transaction.Transaction) util.Fixed8 {
panic("TODO") panic("TODO")
} }
func (chain testChain) AddHeaders(...*core.Header) error { func (chain testChain) AddHeaders(...*block.Header) error {
panic("TODO") panic("TODO")
} }
func (chain *testChain) AddBlock(block *core.Block) error { func (chain *testChain) AddBlock(block *block.Block) error {
if block.Index == chain.blockheight+1 { if block.Index == chain.blockheight+1 {
atomic.StoreUint32(&chain.blockheight, block.Index) atomic.StoreUint32(&chain.blockheight, block.Index)
} }
@ -61,7 +62,7 @@ func (chain *testChain) Close() {
func (chain testChain) HeaderHeight() uint32 { func (chain testChain) HeaderHeight() uint32 {
return 0 return 0
} }
func (chain testChain) GetBlock(hash util.Uint256) (*core.Block, error) { func (chain testChain) GetBlock(hash util.Uint256) (*block.Block, error) {
panic("TODO") panic("TODO")
} }
func (chain testChain) GetContractState(hash util.Uint160) *state.Contract { func (chain testChain) GetContractState(hash util.Uint160) *state.Contract {
@ -70,7 +71,7 @@ func (chain testChain) GetContractState(hash util.Uint160) *state.Contract {
func (chain testChain) GetHeaderHash(int) util.Uint256 { func (chain testChain) GetHeaderHash(int) util.Uint256 {
return util.Uint256{} return util.Uint256{}
} }
func (chain testChain) GetHeader(hash util.Uint256) (*core.Header, error) { func (chain testChain) GetHeader(hash util.Uint256) (*block.Header, error) {
panic("TODO") panic("TODO")
} }
@ -123,7 +124,7 @@ func (chain testChain) IsLowPriority(*transaction.Transaction) bool {
panic("TODO") panic("TODO")
} }
func (chain testChain) VerifyTx(*transaction.Transaction, *core.Block) error { func (chain testChain) VerifyTx(*transaction.Transaction, *block.Block) error {
panic("TODO") panic("TODO")
} }

View file

@ -7,7 +7,7 @@ import (
"github.com/CityOfZion/neo-go/config" "github.com/CityOfZion/neo-go/config"
"github.com/CityOfZion/neo-go/pkg/consensus" "github.com/CityOfZion/neo-go/pkg/consensus"
"github.com/CityOfZion/neo-go/pkg/core" "github.com/CityOfZion/neo-go/pkg/core/block"
"github.com/CityOfZion/neo-go/pkg/core/transaction" "github.com/CityOfZion/neo-go/pkg/core/transaction"
"github.com/CityOfZion/neo-go/pkg/crypto/hash" "github.com/CityOfZion/neo-go/pkg/crypto/hash"
"github.com/CityOfZion/neo-go/pkg/io" "github.com/CityOfZion/neo-go/pkg/io"
@ -184,7 +184,7 @@ func (m *Message) decodePayload(br *io.BinReader) error {
case CMDAddr: case CMDAddr:
p = &payload.AddressList{} p = &payload.AddressList{}
case CMDBlock: case CMDBlock:
p = &core.Block{} p = &block.Block{}
case CMDConsensus: case CMDConsensus:
p = &consensus.Payload{} p = &consensus.Payload{}
case CMDGetBlocks: case CMDGetBlocks:

View file

@ -1,14 +1,14 @@
package payload package payload
import ( import (
"github.com/CityOfZion/neo-go/pkg/core" "github.com/CityOfZion/neo-go/pkg/core/block"
"github.com/CityOfZion/neo-go/pkg/io" "github.com/CityOfZion/neo-go/pkg/io"
"github.com/pkg/errors" "github.com/pkg/errors"
) )
// Headers payload. // Headers payload.
type Headers struct { type Headers struct {
Hdrs []*core.Header Hdrs []*block.Header
} }
// Users can at most request 2k header. // Users can at most request 2k header.
@ -30,10 +30,10 @@ func (p *Headers) DecodeBinary(br *io.BinReader) {
lenHeaders = MaxHeadersAllowed lenHeaders = MaxHeadersAllowed
} }
p.Hdrs = make([]*core.Header, lenHeaders) p.Hdrs = make([]*block.Header, lenHeaders)
for i := 0; i < int(lenHeaders); i++ { for i := 0; i < int(lenHeaders); i++ {
header := &core.Header{} header := &block.Header{}
header.DecodeBinary(br) header.DecodeBinary(br)
p.Hdrs[i] = header p.Hdrs[i] = header
} }

View file

@ -4,7 +4,7 @@ import (
"encoding/hex" "encoding/hex"
"testing" "testing"
"github.com/CityOfZion/neo-go/pkg/core" "github.com/CityOfZion/neo-go/pkg/core/block"
"github.com/CityOfZion/neo-go/pkg/core/transaction" "github.com/CityOfZion/neo-go/pkg/core/transaction"
"github.com/CityOfZion/neo-go/pkg/io" "github.com/CityOfZion/neo-go/pkg/io"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@ -26,11 +26,11 @@ func TestHeadersEncodeDecode(t *testing.T) {
} }
func newTestHeaders(n int) *Headers { func newTestHeaders(n int) *Headers {
headers := &Headers{Hdrs: make([]*core.Header, n)} headers := &Headers{Hdrs: make([]*block.Header, n)}
for i := range headers.Hdrs { for i := range headers.Hdrs {
headers.Hdrs[i] = &core.Header{ headers.Hdrs[i] = &block.Header{
BlockBase: core.BlockBase{ BlockBase: block.BlockBase{
Index: uint32(i + 1), Index: uint32(i + 1),
Script: transaction.Witness{ Script: transaction.Witness{
InvocationScript: []byte{0x0}, InvocationScript: []byte{0x0},

View file

@ -1,14 +1,14 @@
package payload package payload
import ( import (
"github.com/CityOfZion/neo-go/pkg/core" "github.com/CityOfZion/neo-go/pkg/core/block"
"github.com/CityOfZion/neo-go/pkg/io" "github.com/CityOfZion/neo-go/pkg/io"
"github.com/CityOfZion/neo-go/pkg/util" "github.com/CityOfZion/neo-go/pkg/util"
) )
// MerkleBlock represents a merkle block packet payload. // MerkleBlock represents a merkle block packet payload.
type MerkleBlock struct { type MerkleBlock struct {
*core.BlockBase *block.BlockBase
TxCount int TxCount int
Hashes []util.Uint256 Hashes []util.Uint256
Flags []byte Flags []byte
@ -16,7 +16,7 @@ type MerkleBlock struct {
// DecodeBinary implements Serializable interface. // DecodeBinary implements Serializable interface.
func (m *MerkleBlock) DecodeBinary(br *io.BinReader) { func (m *MerkleBlock) DecodeBinary(br *io.BinReader) {
m.BlockBase = &core.BlockBase{} m.BlockBase = &block.BlockBase{}
m.BlockBase.DecodeBinary(br) m.BlockBase.DecodeBinary(br)
m.TxCount = int(br.ReadVarUint()) m.TxCount = int(br.ReadVarUint())
@ -26,7 +26,7 @@ func (m *MerkleBlock) DecodeBinary(br *io.BinReader) {
// EncodeBinary implements Serializable interface. // EncodeBinary implements Serializable interface.
func (m *MerkleBlock) EncodeBinary(bw *io.BinWriter) { func (m *MerkleBlock) EncodeBinary(bw *io.BinWriter) {
m.BlockBase = &core.BlockBase{} m.BlockBase = &block.BlockBase{}
m.BlockBase.EncodeBinary(bw) m.BlockBase.EncodeBinary(bw)
bw.WriteVarUint(uint64(m.TxCount)) bw.WriteVarUint(uint64(m.TxCount))

View file

@ -12,6 +12,7 @@ import (
"github.com/CityOfZion/neo-go/pkg/consensus" "github.com/CityOfZion/neo-go/pkg/consensus"
"github.com/CityOfZion/neo-go/pkg/core" "github.com/CityOfZion/neo-go/pkg/core"
"github.com/CityOfZion/neo-go/pkg/core/block"
"github.com/CityOfZion/neo-go/pkg/core/transaction" "github.com/CityOfZion/neo-go/pkg/core/transaction"
"github.com/CityOfZion/neo-go/pkg/network/payload" "github.com/CityOfZion/neo-go/pkg/network/payload"
"github.com/CityOfZion/neo-go/pkg/util" "github.com/CityOfZion/neo-go/pkg/util"
@ -400,7 +401,7 @@ func (s *Server) handleHeadersCmd(p Peer, headers *payload.Headers) {
} }
// handleBlockCmd processes the received block received from its peer. // handleBlockCmd processes the received block received from its peer.
func (s *Server) handleBlockCmd(p Peer, block *core.Block) error { func (s *Server) handleBlockCmd(p Peer, block *block.Block) error {
return s.bQueue.putBlock(block) return s.bQueue.putBlock(block)
} }
@ -505,7 +506,7 @@ func (s *Server) handleGetHeadersCmd(p Peer, gh *payload.GetBlocks) error {
return err return err
} }
resp := payload.Headers{} resp := payload.Headers{}
resp.Hdrs = make([]*core.Header, 0, payload.MaxHeadersAllowed) resp.Hdrs = make([]*block.Header, 0, payload.MaxHeadersAllowed)
for i := start.Index + 1; i < start.Index+1+payload.MaxHeadersAllowed; i++ { for i := start.Index + 1; i < start.Index+1+payload.MaxHeadersAllowed; i++ {
hash := s.chain.GetHeaderHash(int(i)) hash := s.chain.GetHeaderHash(int(i))
if hash.Equals(util.Uint256{}) || hash.Equals(gh.HashStop) { if hash.Equals(util.Uint256{}) || hash.Equals(gh.HashStop) {
@ -632,7 +633,7 @@ func (s *Server) handleMessage(peer Peer, msg *Message) error {
inventory := msg.Payload.(*payload.Inventory) inventory := msg.Payload.(*payload.Inventory)
return s.handleInvCmd(peer, inventory) return s.handleInvCmd(peer, inventory)
case CMDBlock: case CMDBlock:
block := msg.Payload.(*core.Block) block := msg.Payload.(*block.Block)
return s.handleBlockCmd(peer, block) return s.handleBlockCmd(peer, block)
case CMDConsensus: case CMDConsensus:
cp := msg.Payload.(*consensus.Payload) cp := msg.Payload.(*consensus.Payload)
@ -689,7 +690,7 @@ func (s *Server) relayInventoryCmd(cmd CommandType, t payload.InventoryType, has
} }
// relayBlock tells all the other connected nodes about the given block. // relayBlock tells all the other connected nodes about the given block.
func (s *Server) relayBlock(b *core.Block) { func (s *Server) relayBlock(b *block.Block) {
s.relayInventoryCmd(CMDInv, payload.BlockType, b.Hash()) s.relayInventoryCmd(CMDInv, payload.BlockType, b.Hash())
} }

View file

@ -7,6 +7,7 @@ import (
"github.com/CityOfZion/neo-go/config" "github.com/CityOfZion/neo-go/config"
"github.com/CityOfZion/neo-go/pkg/core" "github.com/CityOfZion/neo-go/pkg/core"
"github.com/CityOfZion/neo-go/pkg/core/block"
"github.com/CityOfZion/neo-go/pkg/core/storage" "github.com/CityOfZion/neo-go/pkg/core/storage"
"github.com/CityOfZion/neo-go/pkg/io" "github.com/CityOfZion/neo-go/pkg/io"
"github.com/CityOfZion/neo-go/pkg/network" "github.com/CityOfZion/neo-go/pkg/network"
@ -193,10 +194,10 @@ func initServerWithInMemoryChain(t *testing.T) (*core.Blockchain, http.HandlerFu
nBlocks = br.ReadU32LE() nBlocks = br.ReadU32LE()
require.Nil(t, br.Err) require.Nil(t, br.Err)
for i := 0; i < int(nBlocks); i++ { for i := 0; i < int(nBlocks); i++ {
block := &core.Block{} b := &block.Block{}
block.DecodeBinary(br) b.DecodeBinary(br)
require.Nil(t, br.Err) require.Nil(t, br.Err)
require.NoError(t, chain.AddBlock(block)) require.NoError(t, chain.AddBlock(b))
} }
serverConfig := network.NewServerConfig(cfg) serverConfig := network.NewServerConfig(cfg)

View file

@ -2,14 +2,15 @@ package wrappers
import ( import (
"github.com/CityOfZion/neo-go/pkg/core" "github.com/CityOfZion/neo-go/pkg/core"
"github.com/CityOfZion/neo-go/pkg/core/block"
"github.com/CityOfZion/neo-go/pkg/util" "github.com/CityOfZion/neo-go/pkg/util"
) )
type ( type (
// Block wrapper used for the representation of // Block wrapper used for the representation of
// core.Block / core.BlockBase on the RPC Server. // block.Block / block.BlockBase on the RPC Server.
Block struct { Block struct {
*core.Block *block.Block
Confirmations uint32 `json:"confirmations"` Confirmations uint32 `json:"confirmations"`
NextBlockHash util.Uint256 `json:"nextblockhash,omitempty"` NextBlockHash util.Uint256 `json:"nextblockhash,omitempty"`
Hash util.Uint256 `json:"hash"` Hash util.Uint256 `json:"hash"`
@ -17,7 +18,7 @@ type (
) )
// NewBlock creates a new Block wrapper. // NewBlock creates a new Block wrapper.
func NewBlock(block *core.Block, chain core.Blockchainer) Block { func NewBlock(block *block.Block, chain core.Blockchainer) Block {
blockWrapper := Block{ blockWrapper := Block{
Block: block, Block: block,
Hash: block.Hash(), Hash: block.Hash(),

View file

@ -2,6 +2,7 @@ package wrappers
import ( import (
"github.com/CityOfZion/neo-go/pkg/core" "github.com/CityOfZion/neo-go/pkg/core"
"github.com/CityOfZion/neo-go/pkg/core/block"
"github.com/CityOfZion/neo-go/pkg/core/transaction" "github.com/CityOfZion/neo-go/pkg/core/transaction"
"github.com/CityOfZion/neo-go/pkg/io" "github.com/CityOfZion/neo-go/pkg/io"
"github.com/CityOfZion/neo-go/pkg/util" "github.com/CityOfZion/neo-go/pkg/util"
@ -21,7 +22,7 @@ type TransactionOutputRaw struct {
} }
// NewTransactionOutputRaw returns a new ransactionOutputRaw object. // NewTransactionOutputRaw returns a new ransactionOutputRaw object.
func NewTransactionOutputRaw(tx *transaction.Transaction, header *core.Header, chain core.Blockchainer) TransactionOutputRaw { func NewTransactionOutputRaw(tx *transaction.Transaction, header *block.Header, chain core.Blockchainer) TransactionOutputRaw {
// confirmations formula // confirmations formula
confirmations := int(chain.BlockHeight() - header.BlockBase.Index + 1) confirmations := int(chain.BlockHeight() - header.BlockBase.Index + 1)
// set index position // set index position