Skip to content

Commit

Permalink
feat: add initial implementation of status and transaction pool
Browse files Browse the repository at this point in the history
  • Loading branch information
stinkymonkeyph committed Jul 24, 2024
1 parent 3e12bc6 commit ef000a3
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 8 deletions.
13 changes: 12 additions & 1 deletion blockchain/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ func (b *Block) ToJson() string {
}

func (b *Block) Hash() string {

bs, err := json.Marshal(b)

if err != nil {
Expand All @@ -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)
}
11 changes: 10 additions & 1 deletion blockchain/blockchain.go
Original file line number Diff line number Diff line change
@@ -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"`
Expand All @@ -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)
}
28 changes: 23 additions & 5 deletions blockchain/transaction.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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
}
3 changes: 3 additions & 0 deletions constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@ package constants
const (
BLOCKCHAIN_NAME = "GopherBlocks"
HEX_PREFIX = "0x"
STATUS_SUCCESS = "success"
STATUS_FAILED = "failed"
STATUS_PENDING = "pending"
)
1 change: 0 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,4 @@ func main() {
block := blockchain.NewBlock("0x0", 0)
bc := blockchain.NewBlockchain(block)
log.Print(bc.ToJSON())

}

0 comments on commit ef000a3

Please sign in to comment.