-
Notifications
You must be signed in to change notification settings - Fork 1
/
send_block.go
62 lines (48 loc) · 1.44 KB
/
send_block.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package transaction
import (
"errors"
"math/big"
"go.uber.org/zap"
"github.com/ariden83/blockchain/internal/blockchain"
"github.com/ariden83/blockchain/internal/p2p/address"
"github.com/ariden83/blockchain/internal/p2p/validator"
"github.com/ariden83/blockchain/internal/transaction"
pkgError "github.com/ariden83/blockchain/pkg/errors"
)
type SendBlockInput struct {
From []byte
To []byte
Amount *big.Int
}
func (t *Transactions) SendBlock(input transaction.SendBlockInput) error {
lastHash, index, err := t.GetLastBlock()
if err != nil {
return err
}
tx, err := t.New(input.From, input.To, input.Amount)
if err == pkgError.ErrNotEnoughFunds {
t.log.Info("Transaction failed, not enough funds",
zap.Any("param", input),
zap.String("input", "sendBlock"))
return pkgError.ErrNotEnoughFunds
} else if err != nil {
return err
}
newBlock := blockchain.AddBlock(lastHash, index, tx)
if !blockchain.IsBlockValid(newBlock, blockchain.BlockChain[len(blockchain.BlockChain)-1]) {
return errors.New("new block is invalid")
}
mutex.Lock()
t.event.PushBlock(validator.New(newBlock, address.IAM.CurrentAddress()))
mutex.Unlock()
/*mutex.Lock()
blockchain.BlockChain = append(blockchain.BlockChain, newBlock)
mutex.Unlock()
ser, err := utils.Serialize(&newBlock)
e.Handle(err)
e.event.Push(event.BlockChain)
err = e.persistence.Update(newBlock.Hash, ser)
e.Handle(err)
spew.Dump(blockchain.BlockChain)*/
return nil
}