copy codes from raftd.