OSDN Git Service

modify sort for coinbase reward (#261)
authoroysheng <33340252+oysheng@users.noreply.github.com>
Thu, 11 Jul 2019 09:03:17 +0000 (17:03 +0800)
committerPaladz <yzhu101@uottawa.ca>
Thu, 11 Jul 2019 09:03:16 +0000 (17:03 +0800)
proposal/proposal_test.go
protocol/state/consensus_result.go
protocol/validation/block.go

index cf6c82a..d8f7abf 100644 (file)
@@ -176,13 +176,13 @@ func TestCountCoinbaseTxRewards(t *testing.T) {
                        },
                        wantRewards: []state.CoinbaseReward{
                                state.CoinbaseReward{
                        },
                        wantRewards: []state.CoinbaseReward{
                                state.CoinbaseReward{
-                                       Amount:         20,
-                                       ControlProgram: []byte{0x52},
-                               },
-                               state.CoinbaseReward{
                                        Amount:         34,
                                        ControlProgram: []byte{0x51},
                                },
                                        Amount:         34,
                                        ControlProgram: []byte{0x51},
                                },
+                               state.CoinbaseReward{
+                                       Amount:         20,
+                                       ControlProgram: []byte{0x52},
+                               },
                        },
                },
                {
                        },
                },
                {
@@ -210,6 +210,7 @@ func TestCountCoinbaseTxRewards(t *testing.T) {
                        },
                        consensusResult: &state.ConsensusResult{
                                CoinbaseReward: map[string]uint64{
                        },
                        consensusResult: &state.ConsensusResult{
                                CoinbaseReward: map[string]uint64{
+                                       "50": 20,
                                        "51": 10,
                                        "52": 20,
                                        "53": 30,
                                        "51": 10,
                                        "52": 20,
                                        "53": 30,
@@ -217,16 +218,20 @@ func TestCountCoinbaseTxRewards(t *testing.T) {
                        },
                        wantRewards: []state.CoinbaseReward{
                                state.CoinbaseReward{
                        },
                        wantRewards: []state.CoinbaseReward{
                                state.CoinbaseReward{
-                                       Amount:         20,
-                                       ControlProgram: []byte{0x52},
+                                       Amount:         34,
+                                       ControlProgram: []byte{0x51},
                                },
                                state.CoinbaseReward{
                                        Amount:         30,
                                        ControlProgram: []byte{0x53},
                                },
                                state.CoinbaseReward{
                                },
                                state.CoinbaseReward{
                                        Amount:         30,
                                        ControlProgram: []byte{0x53},
                                },
                                state.CoinbaseReward{
-                                       Amount:         34,
-                                       ControlProgram: []byte{0x51},
+                                       Amount:         20,
+                                       ControlProgram: []byte{0x52},
+                               },
+                               state.CoinbaseReward{
+                                       Amount:         20,
+                                       ControlProgram: []byte{0x50},
                                },
                        },
                },
                                },
                        },
                },
index ff08429..9313c4b 100644 (file)
@@ -38,9 +38,11 @@ type CoinbaseReward struct {
 // SortByAmount implements sort.Interface for CoinbaseReward slices
 type SortByAmount []CoinbaseReward
 
 // SortByAmount implements sort.Interface for CoinbaseReward slices
 type SortByAmount []CoinbaseReward
 
-func (a SortByAmount) Len() int           { return len(a) }
-func (a SortByAmount) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
-func (a SortByAmount) Less(i, j int) bool { return a[i].Amount < a[j].Amount }
+func (a SortByAmount) Len() int      { return len(a) }
+func (a SortByAmount) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
+func (a SortByAmount) Less(i, j int) bool {
+       return a[i].Amount > a[j].Amount || (a[i].Amount == a[j].Amount && hex.EncodeToString(a[i].ControlProgram) > hex.EncodeToString(a[j].ControlProgram))
+}
 
 // CalCoinbaseReward calculate the coinbase reward for block
 func CalCoinbaseReward(block *types.Block) (*CoinbaseReward, error) {
 
 // CalCoinbaseReward calculate the coinbase reward for block
 func CalCoinbaseReward(block *types.Block) (*CoinbaseReward, error) {
index 5e12cbd..9ed82be 100644 (file)
@@ -2,6 +2,7 @@ package validation
 
 import (
        "bytes"
 
 import (
        "bytes"
+       "encoding/hex"
        "time"
 
        log "github.com/sirupsen/logrus"
        "time"
 
        log "github.com/sirupsen/logrus"
@@ -64,7 +65,7 @@ func checkCoinbaseTx(b *bc.Block, rewards []state.CoinbaseReward) error {
                }
 
                if res := bytes.Compare(rewards[i].ControlProgram, out.ControlProgram.Code); res != 0 {
                }
 
                if res := bytes.Compare(rewards[i].ControlProgram, out.ControlProgram.Code); res != 0 {
-                       return errors.Wrapf(ErrWrongCoinbaseTransaction, "dismatch output control_program, got:%d, want:%d", out.ControlProgram.Code, rewards[i].ControlProgram)
+                       return errors.Wrapf(ErrWrongCoinbaseTransaction, "dismatch output control_program, got:%s, want:%s", hex.EncodeToString(out.ControlProgram.Code), hex.EncodeToString(rewards[i].ControlProgram))
                }
        }
        return nil
                }
        }
        return nil