From ef000a3d6922cd4f5808ac542ce859ccd4278dee Mon Sep 17 00:00:00 2001 From: stinkymonkeyph Date: Thu, 25 Jul 2024 00:34:12 +0800 Subject: [PATCH] feat: add initial implementation of status and transaction pool --- blockchain/block.go | 13 ++++++++++++- blockchain/blockchain.go | 11 ++++++++++- blockchain/transaction.go | 28 +++++++++++++++++++++++----- constants/constants.go | 3 +++ main.go | 1 - 5 files changed, 48 insertions(+), 8 deletions(-) diff --git a/blockchain/block.go b/blockchain/block.go index 7b43dd4..969d25f 100644 --- a/blockchain/block.go +++ b/blockchain/block.go @@ -36,7 +36,6 @@ func (b *Block) ToJson() string { } func (b *Block) Hash() string { - bs, err := json.Marshal(b) if err != nil { @@ -49,3 +48,15 @@ func (b *Block) Hash() string { return hex } + +func (b *Block) AddTransactionToTheBlock(txn *Transaction) { + isValid := txn.VerifyTransaction() + + if isValid { + txn.Status = constants.STATUS_SUCCESS + } else { + txn.Status = constants.STATUS_FAILED + } + + b.Transactions = append(b.Transactions, txn) +} diff --git a/blockchain/blockchain.go b/blockchain/blockchain.go index 3bd9450..1d37f64 100644 --- a/blockchain/blockchain.go +++ b/blockchain/blockchain.go @@ -1,6 +1,10 @@ package blockchain -import "encoding/json" +import ( + "encoding/json" + + "github.com/stinkymonkeyph/gopher-blocks/constants" +) type Blockchain struct { TransactionPool []*Transaction `json:"transaction_pool"` @@ -24,3 +28,8 @@ func (bc *Blockchain) ToJSON() string { return string(bbc) } + +func (bc *Blockchain) AddTransactionToTransactionPool(txn *Transaction) { + txn.Status = constants.STATUS_PENDING + bc.TransactionPool = append(bc.TransactionPool, txn) +} diff --git a/blockchain/transaction.go b/blockchain/transaction.go index f4a2faa..b633443 100644 --- a/blockchain/transaction.go +++ b/blockchain/transaction.go @@ -1,12 +1,16 @@ package blockchain -import "encoding/json" +import ( + "encoding/json" + "math" +) type Transaction struct { - From string `json:"from"` - To string `json:"to"` - Value uint64 `json:"value"` - Data []byte `json:"data"` + From string `json:"from"` + To string `json:"to"` + Value uint64 `json:"value"` + Data []byte `json:"data"` + Status string `json:"status"` } func NewTransaction(from string, to string, value uint64, data []byte) *Transaction { @@ -28,3 +32,17 @@ func (t *Transaction) ToJSON() string { return string(tb) } + +func (t *Transaction) VerifyTransaction() bool { + if t.Value == 0 { + return false + } + + if t.Value > math.MaxInt64 { + return false + } + + // TODO: check signature of transaction here + + return true +} diff --git a/constants/constants.go b/constants/constants.go index f53fcb9..c0f6236 100644 --- a/constants/constants.go +++ b/constants/constants.go @@ -3,4 +3,7 @@ package constants const ( BLOCKCHAIN_NAME = "GopherBlocks" HEX_PREFIX = "0x" + STATUS_SUCCESS = "success" + STATUS_FAILED = "failed" + STATUS_PENDING = "pending" ) diff --git a/main.go b/main.go index 71d2e73..bca4a7b 100644 --- a/main.go +++ b/main.go @@ -15,5 +15,4 @@ func main() { block := blockchain.NewBlock("0x0", 0) bc := blockchain.NewBlockchain(block) log.Print(bc.ToJSON()) - }