Skip to content

Commit

Permalink
add coinbase tx to genesis block
Browse files Browse the repository at this point in the history
  • Loading branch information
liamzebedee committed Jan 1, 2025
1 parent 9c4c332 commit cea349f
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 27 deletions.
32 changes: 10 additions & 22 deletions core/nakamoto/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,16 @@ func GetRawGenesisBlockFromConfig(consensus ConsensusConfig) RawBlock {
TransactionsMerkleRoot: [32]byte{},
Nonce: [32]byte{},
Graffiti: [32]byte{0xca, 0xfe, 0xba, 0xbe, 0xde, 0xca, 0xfb, 0xad, 0xde, 0xad, 0xbe, 0xef}, // 0x cafebabe decafbad deadbeef
Transactions: []RawTransaction{},
Transactions: []RawTransaction{
{
FromPubkey: [65]byte{0x04, 0xd0, 0xe7, 0x0c, 0xe1, 0xed, 0x48, 0x3e, 0x2f, 0x32, 0xad, 0x55, 0x11, 0x53, 0xa5, 0x36, 0x3f, 0xa6, 0xc8, 0x2f, 0xd5, 0xea, 0x0c, 0xd9, 0x5b, 0xf8, 0x0e, 0xae, 0xb3, 0x44, 0x15, 0xcb, 0x8d, 0x45, 0xc3, 0x93, 0x1f, 0xcc, 0x49, 0xed, 0x44, 0x4b, 0xe5, 0x44, 0x0f, 0x1d, 0x52, 0x23, 0x4a, 0x7e, 0xb2, 0xbd, 0x9d, 0xc3, 0x7a, 0x40, 0x90, 0x69, 0x79, 0xa5, 0x2f, 0xa0, 0x72, 0xe5, 0xb4},
ToPubkey: [65]byte{0x04, 0xd0, 0xe7, 0x0c, 0xe1, 0xed, 0x48, 0x3e, 0x2f, 0x32, 0xad, 0x55, 0x11, 0x53, 0xa5, 0x36, 0x3f, 0xa6, 0xc8, 0x2f, 0xd5, 0xea, 0x0c, 0xd9, 0x5b, 0xf8, 0x0e, 0xae, 0xb3, 0x44, 0x15, 0xcb, 0x8d, 0x45, 0xc3, 0x93, 0x1f, 0xcc, 0x49, 0xed, 0x44, 0x4b, 0xe5, 0x44, 0x0f, 0x1d, 0x52, 0x23, 0x4a, 0x7e, 0xb2, 0xbd, 0x9d, 0xc3, 0x7a, 0x40, 0x90, 0x69, 0x79, 0xa5, 0x2f, 0xa0, 0x72, 0xe5, 0xb4},
Sig: [64]byte{0xb3, 0x4c, 0x36, 0x1d, 0xb2, 0xbe, 0x89, 0x48, 0xfb, 0xd6, 0x61, 0x9e, 0xa5, 0xeb, 0xe2, 0x7b, 0x90, 0xae, 0x8d, 0x5a, 0xca, 0xa6, 0x94, 0xc2, 0x1f, 0x11, 0x81, 0x7e, 0x16, 0x98, 0x17, 0x41, 0xeb, 0x6a, 0xe8, 0xc4, 0xbf, 0x48, 0xe3, 0x13, 0x99, 0x81, 0x0e, 0xec, 0xb9, 0x62, 0x69, 0x8d, 0x8d, 0xb1, 0x15, 0x3b, 0xfb, 0x0d, 0x67, 0x6c, 0xa8, 0x6e, 0x52, 0x55, 0x1e, 0xf4, 0x27, 0x8d},
Amount: 50,
Fee: 0,
Nonce: 0,
},
},
}

// Mine the block.
Expand All @@ -55,26 +64,5 @@ func GetRawGenesisBlockFromConfig(consensus ConsensusConfig) RawBlock {

fmt.Printf("Genesis block hash=%x work=%s\n", block.Hash(), work.String())

// to block header
// header := BlockHeader{
// ParentHash: block.ParentHash,
// ParentTotalWork: block.ParentTotalWork,
// Difficulty: block.Difficulty,
// Timestamp: block.Timestamp,
// NumTransactions: block.NumTransactions,
// TransactionsMerkleRoot: block.TransactionsMerkleRoot,
// Nonce: block.Nonce,
// Graffiti: block.Graffiti,
// }
// fmt.Printf("Genesis header block hash=%x\n", header.BlockHash())
// fmt.Printf("ParentHash: %x\n", header.ParentHash)
// fmt.Printf("ParentTotalWork: %x\n", header.ParentTotalWork)
// fmt.Printf("Difficulty: %x\n", header.Difficulty)
// fmt.Printf("Timestamp: %x\n", header.Timestamp)
// fmt.Printf("NumTransactions: %d\n", header.NumTransactions)
// fmt.Printf("TransactionsMerkleRoot: %x\n", header.TransactionsMerkleRoot)
// fmt.Printf("Nonce: %x\n", header.Nonce)
// fmt.Printf("Graffiti: %x\n", header.Graffiti)

return block
}
38 changes: 38 additions & 0 deletions core/nakamoto/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"math/big"
"testing"

"github.com/liamzebedee/tinychain-go/core"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -39,3 +40,40 @@ func TestGetRawGenesisBlockFromConfig(t *testing.T) {
assert.Equal([32]byte{}, genesisBlock.TransactionsMerkleRoot)
assert.Equal(big.NewInt(21).String(), genesisNonce.String())
}

func formatByteArrayDynamic(b []byte) string {
out := fmt.Sprintf("[%d]byte{", len(b))
for i, v := range b {
if i > 0 {
out += ", "
}
out += fmt.Sprintf("0x%02x", v)
}
out += "}"
return out
}

func TestWalletCreateSignTransferTx(t *testing.T) {
wallet, err := core.CreateRandomWallet()
if err != nil {
panic(err)
}
tx := MakeCoinbaseTx(wallet, uint64(GetBlockReward(0)))

// JSON dump.
// str, err := json.Marshal(tx)
// if err != nil {
// panic(err)
// }

// Print as a Go-formatted RawTransaction{} for usage in genesis.go.
fmt.Printf("Coinbase tx:\n")
fmt.Printf("RawTransaction {\n")
fmt.Printf("From: %v,\n", formatByteArrayDynamic(tx.FromPubkey[:]))
fmt.Printf("To: %v,\n", formatByteArrayDynamic(tx.ToPubkey[:]))
fmt.Printf("Sig: %v,\n", formatByteArrayDynamic(tx.Sig[:]))
fmt.Printf("Amount: %d,\n", tx.Amount)
fmt.Printf("Fee: %d,\n", tx.Fee)
fmt.Printf("Nonce: %d,\n", tx.Nonce)
fmt.Printf("}\n")
}
11 changes: 6 additions & 5 deletions core/nakamoto/miner.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ func NewMiner(dag BlockDAG, coinbaseWallet *core.Wallet) *Miner {
}
}

func MakeCoinbaseTx(wallet *core.Wallet) RawTransaction {
func MakeCoinbaseTx(wallet *core.Wallet, amount uint64) RawTransaction {
// Construct coinbase tx.
tx := RawTransaction{
Version: 1,
Sig: [64]byte{},
FromPubkey: wallet.PubkeyBytes(),
ToPubkey: wallet.PubkeyBytes(),
Amount: 50,
Amount: amount,
Fee: 0,
Nonce: 0,
}
Expand Down Expand Up @@ -148,9 +148,6 @@ func (miner *Miner) MineWithStatus(hashrateChannel chan float64, solutionChannel

// Creates a new block template for mining.
func (miner *Miner) MakeNewPuzzle() POWPuzzle {
// Construct coinbase tx.
coinbaseTx := MakeCoinbaseTx(miner.CoinbaseWallet)

// Get the current tip.
current_tip, err := miner.dag.GetLatestFullTip()
if err != nil {
Expand All @@ -161,6 +158,10 @@ func (miner *Miner) MakeNewPuzzle() POWPuzzle {
current_tip = miner.GetTipForMining()
}

// Construct coinbase tx.
blockReward := uint64(GetBlockReward(int(current_tip.Height)))
coinbaseTx := MakeCoinbaseTx(miner.CoinbaseWallet, blockReward)

// Get the block body.
blockBody := []RawTransaction{}
blockBody = append(blockBody, coinbaseTx)
Expand Down

0 comments on commit cea349f

Please sign in to comment.