本文整理匯總了Golang中github.com/gitchain/gitchain/types.EmptyHash函數的典型用法代碼示例。如果您正苦於以下問題:Golang EmptyHash函數的具體用法?Golang EmptyHash怎麽用?Golang EmptyHash使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了EmptyHash函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: TestEncodeDecode
func TestEncodeDecode(t *testing.T) {
privateKey := generateKey(t)
txn1, rand := trans.NewNameReservation("my-new-repository")
txn1e := trans.NewEnvelope(types.EmptyHash(), txn1)
txn2, _ := trans.NewNameAllocation("my-new-repository", rand)
txn2e := trans.NewEnvelope(types.EmptyHash(), txn2)
txn3, _ := trans.NewNameDeallocation("my-new-repository")
txn3e := trans.NewEnvelope(types.EmptyHash(), txn3)
txn1e.Sign(privateKey)
txn2e.Sign(privateKey)
txn3e.Sign(privateKey)
transactions := []*trans.Envelope{txn1e, txn2e, txn3e}
block, err := NewBlock(types.EmptyHash(), HIGHEST_TARGET, transactions)
if err != nil {
t.Errorf("can't create a block because of %v", err)
}
enc, err := block.Encode()
if err != nil {
t.Errorf("error while encoding block: %v", err)
}
block1, err := Decode(enc)
if err != nil {
t.Errorf("error while encoding block: %v", err)
}
assert.Equal(t, block, block1, "encoded and decoded block should be identical to the original one")
}
示例2: TestListRepository
func TestListRepository(t *testing.T) {
db, err := NewDB("test.db")
defer os.Remove("test.db")
if err != nil {
t.Errorf("error opening database: %v", err)
}
repo := repository.NewRepository("test", repository.PENDING, types.EmptyHash())
err = db.PutRepository(repo)
if err != nil {
t.Errorf("error putting repository: %v", err)
}
repo1 := repository.NewRepository("hello_world", repository.ACTIVE, types.EmptyHash())
err = db.PutRepository(repo1)
if err != nil {
t.Errorf("error putting repository: %v", err)
}
actualRepositories := db.ListRepositories()
sort.Strings(actualRepositories)
expectedRepositories := []string{"test", "hello_world"}
sort.Strings(expectedRepositories)
assert.Equal(t, actualRepositories, expectedRepositories)
}
示例3: TestNewBlockNoTx
func TestNewBlockNoTx(t *testing.T) {
transactions := []*trans.Envelope{}
block1, err := NewBlock(types.EmptyHash(), HIGHEST_TARGET, transactions)
if err != nil {
t.Errorf("can't create a block because of %v", err)
}
assert.Equal(t, transactions, block1.Transactions)
assert.Equal(t, block1.MerkleRootHash, types.EmptyHash())
}
示例4: TestGetNextBlock
func TestGetNextBlock(t *testing.T) {
transactions, _ := fixtureSampleTransactions(t)
blk, err := block.NewBlock(types.EmptyHash(), block.HIGHEST_TARGET, transactions)
if err != nil {
t.Errorf("can't create a block because of %v", err)
}
blk1, err := block.NewBlock(blk.Hash(), block.HIGHEST_TARGET, []*transaction.Envelope{})
if err != nil {
t.Errorf("can't create a block because of %v", err)
}
db, err := NewDB("test.db")
defer os.Remove("test.db")
if err != nil {
t.Errorf("error opening database: %v", err)
}
err = db.PutBlock(blk, false)
if err != nil {
t.Errorf("error putting block: %v", err)
}
blk_, err := db.GetNextBlock(types.EmptyHash())
if err != nil {
t.Errorf("error getting next block: %v", err)
}
assert.Equal(t, blk, blk_)
err = db.PutBlock(blk1, false)
if err != nil {
t.Errorf("error putting block: %v", err)
}
blk1_, err := db.GetNextBlock(blk.Hash())
if err != nil {
t.Errorf("error getting next block: %v", err)
}
assert.Equal(t, blk1_, blk1)
block0, err := db.GetBlock(types.EmptyHash())
if err == nil {
t.Errorf("error getting zero block (no error thrown)")
}
if block0 != nil {
t.Errorf("error getting zero block")
}
assert.True(t, nil == block0)
}
示例5: TestNewBlockSingleTx
func TestNewBlockSingleTx(t *testing.T) {
privateKey := generateKey(t)
txn1, _ := trans.NewNameReservation("my-new-repository", &privateKey.PublicKey)
transactions := []trans.T{txn1}
block1, err := NewBlock(types.EmptyHash(), HIGHEST_TARGET, transactions)
if err != nil {
t.Errorf("can't create a block because of %v", err)
return
}
assert.Equal(t, transactions, block1.Transactions)
assert.NotEqual(t, block1.MerkleRootHash, types.EmptyHash())
}
示例6: TestListRefs
func TestListRefs(t *testing.T) {
db, err := NewDB("test.db")
defer os.Remove("test.db")
if err != nil {
t.Errorf("error opening database: %v", err)
}
repo := repository.NewRepository("myrepo", repository.ACTIVE, types.EmptyHash())
err = db.PutRepository(repo)
if err != nil {
t.Errorf("error putting repository: %v", err)
}
ref := util.SHA160([]byte("random"))
err = db.PutRef("myrepo", "refs/heads/master", ref)
if err != nil {
t.Errorf("error putting repository ref: %v", err)
}
err = db.PutRef("myrepo", "refs/heads/next", ref)
if err != nil {
t.Errorf("error putting repository ref: %v", err)
}
refs, err := db.ListRefs("myrepo")
if err != nil {
t.Errorf("error listing repository refs: %v", err)
}
assert.Equal(t, refs, []string{"refs/heads/master", "refs/heads/next"})
}
示例7: NewBlock
func NewBlock(previousBlockHash types.Hash, bits uint32, transactions []*trans.Envelope) (*Block, error) {
encodedTransactions := make([][]byte, len(transactions))
for i := range transactions {
t, _ := transactions[i].Encode()
encodedTransactions[i] = make([]byte, len(t))
copy(encodedTransactions[i], t)
}
var merkleRootHash types.Hash
var err error
if len(encodedTransactions) > 0 {
merkleRootHash, err = merkleRoot(encodedTransactions)
} else {
merkleRootHash = types.EmptyHash()
}
if err != nil {
return nil, err
}
return &Block{
Version: BLOCK_VERSION,
PreviousBlockHash: previousBlockHash,
MerkleRootHash: merkleRootHash,
Timestamp: time.Now().UTC().Unix(),
Bits: bits,
Nonce: 0,
Transactions: transactions}, nil
}
示例8: MiningFactory
func MiningFactory(srv *context.T) {
log := srv.Log.New("cmp", "mining")
var status MiningStatus
n := 1 // TODO: in the future: srv.Config.Mining.Processes
minedCh := make(chan *block.Block)
for i := 0; i < n; i++ {
ch := make(chan *block.Block)
status.Miners = append(status.Miners, Miner{signallingChannel: ch})
go block.Miner(ch, minedCh)
}
ch := srv.Router.Sub("/transaction")
transactionsPool := make([]*transaction.Envelope, 0)
var previousBlockHash types.Hash
// Setup previous block hash
if blk, _ := srv.DB.GetLastBlock(); blk == nil {
previousBlockHash = types.EmptyHash()
} else {
previousBlockHash = blk.Hash()
}
loop:
select {
case txni := <-ch:
if txn, ok := txni.(*transaction.Envelope); ok {
transactionsPool = append(transactionsPool, txn)
stopMiners(status)
mineBlock(status, srv, log, previousBlockHash, transactionsPool)
}
case blk := <-minedCh:
if bytes.Compare(blk.PreviousBlockHash, previousBlockHash) == 0 {
log.Debug("mined", "block", blk)
for i := range blk.Transactions {
for j := range transactionsPool {
if bytes.Compare(transactionsPool[j].Hash(), blk.Transactions[i].Hash()) == 0 {
transactionsPool = append(transactionsPool[0:j], transactionsPool[j+1:]...)
}
}
}
previousBlockHash = blk.Hash()
stopMiners(status)
err := srv.DB.PutBlock(blk, true)
if err != nil {
log.Error("error while serializing block", "block", blk.Hash(), "err", err)
} else {
srv.Router.Pub(blk, "/block", "/block/last")
mineBlock(status, srv, log, previousBlockHash, transactionsPool)
}
}
case reqi := <-miningFactoryRequests:
if req, ok := reqi.(*MiningFactoryStatusRequest); ok {
req.ResponseChannel <- status
}
case <-time.After(time.Second * 1):
if key, _ := srv.DB.GetMainKey(); key != nil && len(transactionsPool) == 0 && status.AvailableMiners() == n {
mineBlock(status, srv, log, previousBlockHash, make([]*transaction.Envelope, 0))
}
}
goto loop
}
示例9: TestGetTransactionBlock
func TestGetTransactionBlock(t *testing.T) {
transactions := fixtureSampleTransactions(t)
block, err := block.NewBlock(types.EmptyHash(), block.HIGHEST_TARGET, transactions)
if err != nil {
t.Errorf("can't create a block because of %v", err)
}
db, err := NewDB("test.db")
defer os.Remove("test.db")
if err != nil {
t.Errorf("error opening database: %v", err)
}
err = db.PutBlock(block, false)
if err != nil {
t.Errorf("error putting block: %v", err)
}
// Block<->transaction indexing
for i := range transactions {
block1, err := db.GetTransactionBlock(transactions[i].Hash())
if err != nil {
t.Errorf("error getting transaction's block: %v", err)
}
assert.Equal(t, block, block1)
}
}
示例10: merkleRoot
func merkleRoot(data [][]byte) (types.Hash, error) {
tree := merkle.NewTree()
err := tree.Generate(data, fastsha256.New())
if err != nil {
return types.EmptyHash(), err
}
return tree.Root().Hash, err
}
示例11: merkleRoot
func merkleRoot(data [][]byte) (types.Hash, error) {
if len(data) == 1 { // FIXME: a workaround for trees with one element
data = append(data, []byte{})
}
tree := merkle.NewTree()
err := tree.Generate(data, fastsha256.New())
if err != nil {
return types.EmptyHash(), err
}
return types.NewHash(tree.Root().Hash), err
}
示例12: TestPutGetBlock
func TestPutGetBlock(t *testing.T) {
privateKey := generateECDSAKey(t)
txn1, rand := transaction.NewNameReservation("my-new-repository", &privateKey.PublicKey)
txn2, _ := transaction.NewNameAllocation("my-new-repository", rand, privateKey)
txn3, _ := transaction.NewNameDeallocation("my-new-repository", privateKey)
transactions := []transaction.T{txn1, txn2, txn3}
block, err := block.NewBlock(types.EmptyHash(), block.HIGHEST_TARGET, transactions)
if err != nil {
t.Errorf("can't create a block because of %v", err)
}
db, err := NewDB("test.db")
defer os.Remove("test.db")
if err != nil {
t.Errorf("error opening database: %v", err)
}
err = db.PutBlock(block, false)
if err != nil {
t.Errorf("error putting block: %v", err)
}
block1, err := db.GetBlock(block.Hash())
if err != nil {
t.Errorf("error getting block: %v", err)
}
if block1 == nil {
t.Errorf("error getting block %v", block.Hash())
}
assert.Equal(t, block, block1)
// Attempt fetching the last one
block1, err = db.GetLastBlock()
if err != nil {
t.Errorf("error getting block: %v", err)
}
if block1 != nil {
t.Errorf("error getting block, there should be no last block")
}
// Set the last one
err = db.PutBlock(block, true)
if err != nil {
t.Errorf("error putting block: %v", err)
}
block1, err = db.GetLastBlock()
if err != nil {
t.Errorf("error getting last block: %v", err)
}
if block1 == nil {
t.Errorf("error getting block, there should be a last block")
}
assert.Equal(t, block, block1)
}
示例13: GetNextTransactionHash
func (db *T) GetNextTransactionHash(hash []byte) (h types.Hash, e error) {
readable(&e, db, func(dbtx *bolt.Tx) {
bucket := dbtx.Bucket([]byte("blocks"))
if bucket != nil {
h = bucket.Get(append([]byte(">"), hash...))
if h == nil {
h = types.EmptyHash()
}
}
})
return
}
示例14: TestGetNextTransactionHash
func TestGetNextTransactionHash(t *testing.T) {
transactions, _ := fixtureSampleTransactions(t)
block, err := block.NewBlock(types.EmptyHash(), block.HIGHEST_TARGET, transactions)
if err != nil {
t.Errorf("can't create a block because of %v", err)
}
db, err := NewDB("test.db")
defer os.Remove("test.db")
if err != nil {
t.Errorf("error opening database: %v", err)
}
err = db.PutBlock(block, false)
if err != nil {
t.Errorf("error putting block: %v", err)
}
tx, err := db.GetNextTransactionHash(transactions[0].Hash())
if err != nil {
t.Errorf("error getting next transaction: %v", err)
}
assert.True(t, bytes.Compare(tx, transactions[1].Hash()) == 0)
tx, err = db.GetNextTransactionHash(transactions[1].Hash())
if err != nil {
t.Errorf("error getting next transaction: %v", err)
}
assert.True(t, bytes.Compare(tx, transactions[2].Hash()) == 0)
tx, err = db.GetNextTransactionHash(transactions[2].Hash())
if err != nil {
t.Errorf("error getting next transaction: %v", err)
}
assert.True(t, bytes.Compare(tx, types.EmptyHash()) == 0)
}
示例15: TestNewBlock
func TestNewBlock(t *testing.T) {
privateKey := generateKey(t)
txn1, rand := trans.NewNameReservation("my-new-repository")
txn1e := trans.NewEnvelope(types.EmptyHash(), txn1)
txn2, _ := trans.NewNameAllocation("my-new-repository", rand)
txn2e := trans.NewEnvelope(types.EmptyHash(), txn2)
txn3, _ := trans.NewNameDeallocation("my-new-repository")
txn3e := trans.NewEnvelope(types.EmptyHash(), txn3)
txn1e.Sign(privateKey)
txn2e.Sign(privateKey)
txn3e.Sign(privateKey)
transactions := []*trans.Envelope{txn1e, txn2e, txn3e}
block1, err := NewBlock(types.EmptyHash(), HIGHEST_TARGET, transactions)
if err != nil {
t.Errorf("can't create a block because of %v", err)
}
assert.Equal(t, transactions, block1.Transactions)
assert.NotEqual(t, block1.MerkleRootHash, types.EmptyHash())
}