diff --git a/core/blockchain.go b/core/blockchain.go index 937c952b4..e1da21ff3 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -48,8 +48,10 @@ import ( ) var ( + // blockInsertTimer blockInsertTimer = metrics.NewRegisteredTimer("chain/inserts", nil) + // ErrNoGenesis is the error when there is no genesis. ErrNoGenesis = errors.New("Genesis not found in chain") ) @@ -189,6 +191,7 @@ func NewBlockChain(db hdb.Database, cacheConfig *CacheConfig, chainConfig *param return bc, nil } +// ValidateNewBlock validates new block. func (bc *BlockChain) ValidateNewBlock(block *types.Block, address common.Address) error { state, err := state.New(bc.CurrentBlock().Root(), bc.stateCache) @@ -734,6 +737,7 @@ func (bc *BlockChain) procFutureBlocks() { // WriteStatus status of write type WriteStatus byte +// Constants for WriteStatus const ( NonStatTy WriteStatus = iota CanonStatTy diff --git a/core/events.go b/core/events.go index d95586182..9a61c6243 100644 --- a/core/events.go +++ b/core/events.go @@ -35,14 +35,17 @@ type NewMinedBlockEvent struct{ Block *types.Block } // RemovedLogsEvent is posted when a reorg happens type RemovedLogsEvent struct{ Logs []*types.Log } +// ChainEvent is the struct of chain event. type ChainEvent struct { Block *types.Block Hash common.Hash Logs []*types.Log } +// ChainSideEvent is chain side event. type ChainSideEvent struct { Block *types.Block } +// ChainHeadEvent is the struct of chain head event. type ChainHeadEvent struct{ Block *types.Block } diff --git a/core/genesis.go b/core/genesis.go index 994de6548..c1e8b0f6e 100644 --- a/core/genesis.go +++ b/core/genesis.go @@ -64,6 +64,7 @@ type Genesis struct { // GenesisAlloc specifies the initial state that is part of the genesis block. type GenesisAlloc map[common.Address]GenesisAccount +// UnmarshalJSON is to deserialize the data into GenesisAlloc. func (ga *GenesisAlloc) UnmarshalJSON(data []byte) error { m := make(map[common.UnprefixedAddress]GenesisAccount) if err := json.Unmarshal(data, &m); err != nil { diff --git a/core/headerchain.go b/core/headerchain.go index c2b2fd375..f9c5fcc7f 100644 --- a/core/headerchain.go +++ b/core/headerchain.go @@ -204,6 +204,7 @@ func (hc *HeaderChain) WriteHeader(header *types.Header) (status WriteStatus, er // header writes should be protected by the parent chain mutex individually. type WhCallback func(*types.Header) error +// ValidateHeaderChain validates header chain. func (hc *HeaderChain) ValidateHeaderChain(chain []*types.Header, checkFreq int) (int, error) { // Do a sanity check that the provided chain is actually ordered and linked for i := 1; i < len(chain); i++ { @@ -330,9 +331,8 @@ func (hc *HeaderChain) GetAncestor(hash common.Hash, number, ancestor uint64, ma // in this case it is cheaper to just read the header if header := hc.GetHeader(hash, number); header != nil { return header.ParentHash, number - 1 - } else { - return common.Hash{}, 0 } + return common.Hash{}, 0 } for ancestor != 0 { if rawdb.ReadCanonicalHash(hc.chainDb, number) == hash { diff --git a/core/state_transition.go b/core/state_transition.go index 866dbaf6d..c6a504396 100644 --- a/core/state_transition.go +++ b/core/state_transition.go @@ -33,7 +33,7 @@ var ( ) /* -The State Transitioning Model +StateTransition is the State Transitioning Model which is described as follows: A state transition is a change made when a transaction is applied to the current world state The state transitioning model does all the necessary work to work out a valid new state root. diff --git a/core/tx_pool.go b/core/tx_pool.go index 1f35875b0..3e3b045b8 100644 --- a/core/tx_pool.go +++ b/core/tx_pool.go @@ -104,6 +104,7 @@ var ( // TxStatus is the current status of a transaction as seen by the pool. type TxStatus uint +// Constants for TxStatus. const ( TxStatusUnknown TxStatus = iota TxStatusQueued diff --git a/core/tx_pool_test.go b/core/tx_pool_test.go index 40b79b711..b1ea3f29f 100644 --- a/core/tx_pool_test.go +++ b/core/tx_pool_test.go @@ -1053,13 +1053,13 @@ func TestTransactionPoolRepricingKeepsLocals(t *testing.T) { // Create transaction (both pending and queued) with a linearly growing gasprice for i := uint64(0); i < 500; i++ { // Add pending - p_tx := pricedTransaction(i, 100000, big.NewInt(int64(i)), keys[2]) - if err := pool.AddLocal(p_tx); err != nil { + pTx := pricedTransaction(i, 100000, big.NewInt(int64(i)), keys[2]) + if err := pool.AddLocal(pTx); err != nil { t.Fatal(err) } // Add queued - q_tx := pricedTransaction(i+501, 100000, big.NewInt(int64(i)), keys[2]) - if err := pool.AddLocal(q_tx); err != nil { + qTx := pricedTransaction(i+501, 100000, big.NewInt(int64(i)), keys[2]) + if err := pool.AddLocal(qTx); err != nil { t.Fatal(err) } } diff --git a/core/types/transaction.go b/core/types/transaction.go index eb9da3942..4c5316239 100644 --- a/core/types/transaction.go +++ b/core/types/transaction.go @@ -281,6 +281,7 @@ func (tx *Transaction) Cost() *big.Int { return total } +// RawSignatureValues return raw signature values. func (tx *Transaction) RawSignatureValues() (*big.Int, *big.Int, *big.Int) { return tx.data.V, tx.data.R, tx.data.S } @@ -335,10 +336,12 @@ func (s TxByPrice) Len() int { return len(s) } func (s TxByPrice) Less(i, j int) bool { return s[i].data.Price.Cmp(s[j].data.Price) > 0 } func (s TxByPrice) Swap(i, j int) { s[i], s[j] = s[j], s[i] } +// Push pushes a transaction. func (s *TxByPrice) Push(x interface{}) { *s = append(*s, x.(*Transaction)) } +// Pop pops a transaction. func (s *TxByPrice) Pop() interface{} { old := *s n := len(old) @@ -423,6 +426,7 @@ type Message struct { checkNonce bool } +// NewMessage returns new message. func NewMessage(from common.Address, to *common.Address, nonce uint64, amount *big.Int, gasLimit uint64, gasPrice *big.Int, data []byte, checkNonce bool) Message { return Message{ from: from, diff --git a/core/types/transaction_signing.go b/core/types/transaction_signing.go index 290a12887..20c613fc5 100644 --- a/core/types/transaction_signing.go +++ b/core/types/transaction_signing.go @@ -103,36 +103,39 @@ type Signer interface { Equal(Signer) bool } -// EIP155Transaction implements Signer using the EIP155 rules. +// EIP155Signer implements Signer using the EIP155 rules. type EIP155Signer struct { - chainId, chainIdMul *big.Int + chainID, chainIDMul *big.Int } -func NewEIP155Signer(chainId *big.Int) EIP155Signer { - if chainId == nil { - chainId = new(big.Int) +// NewEIP155Signer creates a EIP155Signer given chainID. +func NewEIP155Signer(chainID *big.Int) EIP155Signer { + if chainID == nil { + chainID = new(big.Int) } return EIP155Signer{ - chainId: chainId, - chainIdMul: new(big.Int).Mul(chainId, big.NewInt(2)), + chainID: chainID, + chainIDMul: new(big.Int).Mul(chainID, big.NewInt(2)), } } +// Equal checks if the given EIP155Signer is equal to another Signer. func (s EIP155Signer) Equal(s2 Signer) bool { eip155, ok := s2.(EIP155Signer) - return ok && eip155.chainId.Cmp(s.chainId) == 0 + return ok && eip155.chainID.Cmp(s.chainID) == 0 } var big8 = big.NewInt(8) +// Sender returns the sender address of the given signer. func (s EIP155Signer) Sender(tx *Transaction) (common.Address, error) { if !tx.Protected() { return HomesteadSigner{}.Sender(tx) } - if tx.ChainID().Cmp(s.chainId) != 0 { + if tx.ChainID().Cmp(s.chainID) != 0 { return common.Address{}, ErrInvalidChainID } - V := new(big.Int).Sub(tx.data.V, s.chainIdMul) + V := new(big.Int).Sub(tx.data.V, s.chainIDMul) V.Sub(V, big8) return recoverPlain(s.Hash(tx), tx.data.R, tx.data.S, V, true) } @@ -144,9 +147,9 @@ func (s EIP155Signer) SignatureValues(tx *Transaction, sig []byte) (R, S, V *big if err != nil { return nil, nil, nil, err } - if s.chainId.Sign() != 0 { + if s.chainID.Sign() != 0 { V = big.NewInt(int64(sig[64] + 35)) - V.Add(V, s.chainIdMul) + V.Add(V, s.chainIDMul) } return R, S, V, nil } @@ -161,15 +164,16 @@ func (s EIP155Signer) Hash(tx *Transaction) common.Hash { tx.data.Recipient, tx.data.Amount, tx.data.Payload, - s.chainId, uint(0), uint(0), + s.chainID, uint(0), uint(0), }) } -// HomesteadTransaction implements TransactionInterface using the +// HomesteadSigner implements TransactionInterface using the // homestead rules. type HomesteadSigner struct{ FrontierSigner } -func (s HomesteadSigner) Equal(s2 Signer) bool { +// Equal checks if it is equal to s2 signer. +func (hs HomesteadSigner) Equal(s2 Signer) bool { _, ok := s2.(HomesteadSigner) return ok } @@ -180,13 +184,16 @@ func (hs HomesteadSigner) SignatureValues(tx *Transaction, sig []byte) (r, s, v return hs.FrontierSigner.SignatureValues(tx, sig) } +// Sender returns the address of the sender. func (hs HomesteadSigner) Sender(tx *Transaction) (common.Address, error) { return recoverPlain(hs.Hash(tx), tx.data.R, tx.data.S, tx.data.V, true) } +// FrontierSigner ... type FrontierSigner struct{} -func (s FrontierSigner) Equal(s2 Signer) bool { +// Equal checks if the s2 signer is equal to the given signer. +func (fs FrontierSigner) Equal(s2 Signer) bool { _, ok := s2.(FrontierSigner) return ok } @@ -216,6 +223,7 @@ func (fs FrontierSigner) Hash(tx *Transaction) common.Hash { }) } +// Sender returns the sender address of the given transaction. func (fs FrontierSigner) Sender(tx *Transaction) (common.Address, error) { return recoverPlain(fs.Hash(tx), tx.data.R, tx.data.S, tx.data.V, false) } diff --git a/core/types/transaction_signing_test.go b/core/types/transaction_signing_test.go index e6ea1f9e7..61345ebef 100644 --- a/core/types/transaction_signing_test.go +++ b/core/types/transaction_signing_test.go @@ -56,8 +56,8 @@ func TestEIP155ChainID(t *testing.T) { t.Fatal("expected tx to be protected") } - if tx.ChainID().Cmp(signer.chainId) != 0 { - t.Error("expected chainId to be", signer.chainId, "got", tx.ChainID()) + if tx.ChainID().Cmp(signer.chainID) != 0 { + t.Error("expected chainID to be", signer.chainID, "got", tx.ChainID()) } tx = NewTransaction(0, addr, 0, new(big.Int), 0, new(big.Int), nil) diff --git a/core/vm/logger_test.go b/core/vm/logger_test.go index efc364d33..380072b6a 100644 --- a/core/vm/logger_test.go +++ b/core/vm/logger_test.go @@ -46,7 +46,10 @@ type dummyStatedb struct { state.StateDB } -func (dummyStatedb) GetRefund() uint64 { return 1337 } +// GetRefund ... +func (*dummyStatedb) GetRefund() uint64 { + return 1337 +} func TestStoreCapture(t *testing.T) { var ( diff --git a/core/vm/runtime/env.go b/core/vm/runtime/env.go index 904e73d65..f7c04b84a 100644 --- a/core/vm/runtime/env.go +++ b/core/vm/runtime/env.go @@ -22,6 +22,7 @@ import ( "github.com/harmony-one/harmony/core/vm" ) +// NewEnv returns new EVM. func NewEnv(cfg *Config) *vm.EVM { context := vm.Context{ CanTransfer: core.CanTransfer, diff --git a/core/vm/runtime/runtime_test.go b/core/vm/runtime/runtime_test.go index 99f6a0b72..b21a87659 100644 --- a/core/vm/runtime/runtime_test.go +++ b/core/vm/runtime/runtime_test.go @@ -149,7 +149,8 @@ func BenchmarkCall(b *testing.B) { } } } -func benchmarkEVM_Create(bench *testing.B, code string) { + +func benchmarkEVMCreate(bench *testing.B, code string) { var ( statedb, _ = state.New(common.Hash{}, state.NewDatabase(db.NewMemDatabase())) sender = common.BytesToAddress([]byte("sender")) @@ -189,17 +190,17 @@ func benchmarkEVM_Create(bench *testing.B, code string) { func BenchmarkEVM_CREATE_500(bench *testing.B) { // initcode size 500K, repeatedly calls CREATE and then modifies the mem contents - benchmarkEVM_Create(bench, "5b6207a120600080f0600152600056") + benchmarkEVMCreate(bench, "5b6207a120600080f0600152600056") } func BenchmarkEVM_CREATE2_500(bench *testing.B) { // initcode size 500K, repeatedly calls CREATE2 and then modifies the mem contents - benchmarkEVM_Create(bench, "5b586207a120600080f5600152600056") + benchmarkEVMCreate(bench, "5b586207a120600080f5600152600056") } func BenchmarkEVM_CREATE_1200(bench *testing.B) { // initcode size 1200K, repeatedly calls CREATE and then modifies the mem contents - benchmarkEVM_Create(bench, "5b62124f80600080f0600152600056") + benchmarkEVMCreate(bench, "5b62124f80600080f0600152600056") } func BenchmarkEVM_CREATE2_1200(bench *testing.B) { // initcode size 1200K, repeatedly calls CREATE2 and then modifies the mem contents - benchmarkEVM_Create(bench, "5b5862124f80600080f5600152600056") + benchmarkEVMCreate(bench, "5b5862124f80600080f5600152600056") }